1# Copyright (c) 2006-2012 Mitch Garnaat http://garnaat.org/
2# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.  All Rights Reserved
3#
4# Permission is hereby granted, free of charge, to any person obtaining a
5# copy of this software and associated documentation files (the
6# "Software"), to deal in the Software without restriction, including
7# without limitation the rights to use, copy, modify, merge, publish, dis-
8# tribute, sublicense, and/or sell copies of the Software, and to permit
9# persons to whom the Software is furnished to do so, subject to the fol-
10# lowing conditions:
11#
12# The above copyright notice and this permission notice shall be included
13# in all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
17# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
18# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21# IN THE SOFTWARE.
22
23"""
24Represents a launch specification for Spot instances.
25"""
26
27from boto.ec2.ec2object import EC2Object
28from boto.resultset import ResultSet
29from boto.ec2.blockdevicemapping import BlockDeviceMapping
30from boto.ec2.group import Group
31from boto.ec2.instance import SubParse
32
33
34class GroupList(list):
35
36    def startElement(self, name, attrs, connection):
37        pass
38
39    def endElement(self, name, value, connection):
40        if name == 'groupId':
41            self.append(value)
42
43
44class LaunchSpecification(EC2Object):
45
46    def __init__(self, connection=None):
47        super(LaunchSpecification, self).__init__(connection)
48        self.key_name = None
49        self.instance_type = None
50        self.image_id = None
51        self.groups = []
52        self.placement = None
53        self.kernel = None
54        self.ramdisk = None
55        self.monitored = False
56        self.subnet_id = None
57        self._in_monitoring_element = False
58        self.block_device_mapping = None
59        self.instance_profile = None
60        self.ebs_optimized = False
61
62    def __repr__(self):
63        return 'LaunchSpecification(%s)' % self.image_id
64
65    def startElement(self, name, attrs, connection):
66        if name == 'groupSet':
67            self.groups = ResultSet([('item', Group)])
68            return self.groups
69        elif name == 'monitoring':
70            self._in_monitoring_element = True
71        elif name == 'blockDeviceMapping':
72            self.block_device_mapping = BlockDeviceMapping()
73            return self.block_device_mapping
74        elif name == 'iamInstanceProfile':
75            self.instance_profile = SubParse('iamInstanceProfile')
76            return self.instance_profile
77        else:
78            return None
79
80    def endElement(self, name, value, connection):
81        if name == 'imageId':
82            self.image_id = value
83        elif name == 'keyName':
84            self.key_name = value
85        elif name == 'instanceType':
86            self.instance_type = value
87        elif name == 'availabilityZone':
88            self.placement = value
89        elif name == 'placement':
90            pass
91        elif name == 'kernelId':
92            self.kernel = value
93        elif name == 'ramdiskId':
94            self.ramdisk = value
95        elif name == 'subnetId':
96            self.subnet_id = value
97        elif name == 'state':
98            if self._in_monitoring_element:
99                if value == 'enabled':
100                    self.monitored = True
101                self._in_monitoring_element = False
102        elif name == 'ebsOptimized':
103            self.ebs_optimized = (value == 'true')
104        else:
105            setattr(self, name, value)
106