1
# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
3
# Permission is hereby granted, free of charge, to any person obtaining a
4
# copy of this software and associated documentation files (the
5
# "Software"), to deal in the Software without restriction, including
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
8
# persons to whom the Software is furnished to do so, subject to the fol-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
from boto.sdb.db.property import StringProperty, IntegerProperty
24
from boto.manage import propget
26
InstanceTypes = ['m1.small', 'm1.large', 'm1.xlarge', 'c1.medium', 'c1.xlarge']
28
class BuyReservation(object):
30
def get_region(self, params):
31
if not params.get('region', None):
32
prop = StringProperty(name='region', verbose_name='EC2 Region',
33
choices=boto.ec2.regions)
34
params['region'] = propget.get(prop, choices=boto.ec2.regions)
36
def get_instance_type(self, params):
37
if not params.get('instance_type', None):
38
prop = StringProperty(name='instance_type', verbose_name='Instance Type',
39
choices=InstanceTypes)
40
params['instance_type'] = propget.get(prop)
42
def get_quantity(self, params):
43
if not params.get('quantity', None):
44
prop = IntegerProperty(name='quantity', verbose_name='Number of Instances')
45
params['quantity'] = propget.get(prop)
47
def get_zone(self, params):
48
if not params.get('zone', None):
49
prop = StringProperty(name='zone', verbose_name='EC2 Availability Zone',
50
choices=self.ec2.get_all_zones)
51
params['zone'] = propget.get(prop)
53
def get(self, params):
54
self.get_region(params)
55
self.ec2 = params['region'].connect()
56
self.get_instance_type(params)
58
self.get_quantity(params)
60
if __name__ == "__main__":
61
obj = BuyReservation()
64
offerings = obj.ec2.get_all_reserved_instances_offerings(instance_type=params['instance_type'],
65
availability_zone=params['zone'].name)
66
print '\nThe following Reserved Instances Offerings are available:\n'
67
for offering in offerings:
69
prop = StringProperty(name='offering', verbose_name='Offering',
71
offering = propget.get(prop)
72
print '\nYou have chosen this offering:'
74
unit_price = float(offering.fixed_price)
75
total_price = unit_price * params['quantity']
76
print '!!! You are about to purchase %d of these offerings for a total of $%.2f !!!' % (params['quantity'], total_price)
77
answer = raw_input('Are you sure you want to do this? If so, enter YES: ')
78
if answer.strip().lower() == 'yes':
79
offering.purchase(params['quantity'])
81
print 'Purchase cancelled'