~ubuntu-branches/ubuntu/trusty/python-boto/trusty

« back to all changes in this revision

Viewing changes to boto/ec2/autoscale/__init__.py

  • Committer: Package Import Robot
  • Author(s): Eric Evans
  • Date: 2013-05-10 23:38:14 UTC
  • mfrom: (1.1.10) (14.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130510233814-701dvlop7xfh88i7
Tags: 2.9.2-1
New upstream release (Closes: #700743).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (c) 2009-2011 Reza Lotun http://reza.lotun.name/
2
2
# Copyright (c) 2011 Jann Kleen
 
3
# Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
 
4
# Copyright (c) 2012 Amazon.com, Inc. or its affiliates.  All Rights Reserved
3
5
#
4
6
# Permission is hereby granted, free of charge, to any person obtaining a
5
7
# copy of this software and associated documentation files (the
32
34
from boto.ec2.regioninfo import RegionInfo
33
35
from boto.ec2.autoscale.request import Request
34
36
from boto.ec2.autoscale.launchconfig import LaunchConfiguration
35
 
from boto.ec2.autoscale.group import AutoScalingGroup, ProcessType
 
37
from boto.ec2.autoscale.group import AutoScalingGroup
 
38
from boto.ec2.autoscale.group import ProcessType
36
39
from boto.ec2.autoscale.activity import Activity
37
 
from boto.ec2.autoscale.policy import AdjustmentType, MetricCollectionTypes, ScalingPolicy
 
40
from boto.ec2.autoscale.policy import AdjustmentType
 
41
from boto.ec2.autoscale.policy import MetricCollectionTypes
 
42
from boto.ec2.autoscale.policy import ScalingPolicy
 
43
from boto.ec2.autoscale.policy import TerminationPolicies
38
44
from boto.ec2.autoscale.instance import Instance
39
45
from boto.ec2.autoscale.scheduled import ScheduledUpdateGroupAction
40
46
from boto.ec2.autoscale.tag import Tag
46
52
    'sa-east-1': 'autoscaling.sa-east-1.amazonaws.com',
47
53
    'eu-west-1': 'autoscaling.eu-west-1.amazonaws.com',
48
54
    'ap-northeast-1': 'autoscaling.ap-northeast-1.amazonaws.com',
49
 
    'ap-southeast-1': 'autoscaling.ap-southeast-1.amazonaws.com'}
 
55
    'ap-southeast-1': 'autoscaling.ap-southeast-1.amazonaws.com',
 
56
    'ap-southeast-2': 'autoscaling.ap-southeast-2.amazonaws.com',
 
57
}
 
58
 
50
59
 
51
60
def regions():
52
61
    """
63
72
        regions.append(region)
64
73
    return regions
65
74
 
 
75
 
66
76
def connect_to_region(region_name, **kw_params):
67
77
    """
68
78
    Given a valid region name, return a
79
89
            return region.connect(**kw_params)
80
90
    return None
81
91
 
 
92
 
82
93
class AutoScaleConnection(AWSQueryConnection):
83
94
    APIVersion = boto.config.get('Boto', 'autoscale_version', '2011-01-01')
84
95
    DefaultRegionEndpoint = boto.config.get('Boto', 'autoscale_endpoint',
85
 
                                            'autoscaling.amazonaws.com')
86
 
    DefaultRegionName =  boto.config.get('Boto', 'autoscale_region_name',
87
 
                                         'us-east-1')
 
96
                                            'autoscaling.us-east-1.amazonaws.com')
 
97
    DefaultRegionName = boto.config.get('Boto', 'autoscale_region_name',
 
98
                                        'us-east-1')
88
99
 
89
100
    def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
90
101
                 is_secure=True, port=None, proxy=None, proxy_port=None,
91
102
                 proxy_user=None, proxy_pass=None, debug=0,
92
 
                 https_connection_factory=None, region=None, path='/'):
 
103
                 https_connection_factory=None, region=None, path='/',
 
104
                 security_token=None, validate_certs=True):
93
105
        """
94
106
        Init method to create a new connection to the AutoScaling service.
95
107
 
106
118
                                    is_secure, port, proxy, proxy_port,
107
119
                                    proxy_user, proxy_pass,
108
120
                                    self.region.endpoint, debug,
109
 
                                    https_connection_factory, path=path)
 
121
                                    https_connection_factory, path=path,
 
122
                                    security_token=security_token,
 
123
                                    validate_certs=validate_certs)
110
124
 
111
125
    def _required_auth_capability(self):
112
 
        return ['ec2']
 
126
        return ['hmac-v4']
113
127
 
114
128
    def build_list_params(self, params, items, label):
115
129
        """
129
143
            ['us-east-1b',...]
130
144
        """
131
145
        # different from EC2 list params
132
 
        for i in xrange(1, len(items)+1):
133
 
            if isinstance(items[i-1], dict):
134
 
                for k, v in items[i-1].iteritems():
 
146
        for i in xrange(1, len(items) + 1):
 
147
            if isinstance(items[i - 1], dict):
 
148
                for k, v in items[i - 1].iteritems():
135
149
                    if isinstance(v, dict):
136
150
                        for kk, vv in v.iteritems():
137
151
                            params['%s.member.%d.%s.%s' % (label, i, k, kk)] = vv
138
152
                    else:
139
153
                        params['%s.member.%d.%s' % (label, i, k)] = v
140
 
            elif isinstance(items[i-1], basestring):
141
 
                params['%s.member.%d' % (label, i)] = items[i-1]
 
154
            elif isinstance(items[i - 1], basestring):
 
155
                params['%s.member.%d' % (label, i)] = items[i - 1]
142
156
 
143
157
    def _update_group(self, op, as_group):
144
158
        params = {'AutoScalingGroupName': as_group.name,
160
174
            params['DefaultCooldown'] = as_group.default_cooldown
161
175
        if as_group.placement_group:
162
176
            params['PlacementGroup'] = as_group.placement_group
 
177
        if as_group.termination_policies:
 
178
            self.build_list_params(params, as_group.termination_policies,
 
179
                                   'TerminationPolicies')
163
180
        if op.startswith('Create'):
164
181
            # you can only associate load balancers with an autoscale
165
182
            # group at creation time
166
183
            if as_group.load_balancers:
167
184
                self.build_list_params(params, as_group.load_balancers,
168
185
                                       'LoadBalancerNames')
 
186
            if as_group.tags:
 
187
                for i, tag in enumerate(as_group.tags):
 
188
                    tag.build_params(params, i + 1)
169
189
        return self.get_object(op, params, Request)
170
190
 
171
191
    def create_auto_scaling_group(self, as_group):
213
233
            params['InstanceMonitoring.Enabled'] = 'true'
214
234
        else:
215
235
            params['InstanceMonitoring.Enabled'] = 'false'
 
236
        if launch_config.spot_price is not None:
 
237
            params['SpotPrice'] = str(launch_config.spot_price)
 
238
        if launch_config.instance_profile_name is not None:
 
239
            params['IamInstanceProfile'] = launch_config.instance_profile_name
216
240
        return self.get_object('CreateLaunchConfiguration', params,
217
241
                               Request, verb='POST')
218
242
 
333
357
        name = autoscale_group
334
358
        if isinstance(autoscale_group, AutoScalingGroup):
335
359
            name = autoscale_group.name
336
 
        params = {'AutoScalingGroupName' : name}
 
360
        params = {'AutoScalingGroupName': name}
337
361
        if max_records:
338
362
            params['MaxRecords'] = max_records
339
363
        if next_token:
343
367
        return self.get_list('DescribeScalingActivities',
344
368
                             params, [('member', Activity)])
345
369
 
 
370
    def get_termination_policies(self):
 
371
        """Gets all valid termination policies.
 
372
 
 
373
        These values can then be used as the termination_policies arg
 
374
        when creating and updating autoscale groups.
 
375
        """
 
376
        return self.get_object('DescribeTerminationPolicyTypes',
 
377
                               {}, TerminationPolicies)
 
378
 
346
379
    def delete_scheduled_action(self, scheduled_action_name,
347
380
                                autoscale_group=None):
348
381
        """
419
452
 
420
453
        :rtype: list
421
454
        :returns: List of
422
 
            :class:`boto.ec2.autoscale.activity.Activity` objects.
 
455
            :class:`boto.ec2.autoscale.instance.Instance` objects.
423
456
        """
424
457
        params = {}
425
458
        if instance_ids:
488
521
        :param as_group: The auto scaling group to suspend processes on.
489
522
 
490
523
        :type scaling_processes: list
491
 
        :param scaling_processes: Processes you want to suspend. If omitted, all
492
 
            processes will be suspended.
 
524
        :param scaling_processes: Processes you want to suspend. If omitted,
 
525
            all processes will be suspended.
493
526
        """
494
527
        params = {'AutoScalingGroupName': as_group}
495
528
        if scaling_processes:
496
 
            self.build_list_params(params, scaling_processes, 'ScalingProcesses')
 
529
            self.build_list_params(params, scaling_processes,
 
530
                                   'ScalingProcesses')
497
531
        return self.get_status('SuspendProcesses', params)
498
532
 
499
533
    def resume_processes(self, as_group, scaling_processes=None):
510
544
        params = {'AutoScalingGroupName': as_group}
511
545
 
512
546
        if scaling_processes:
513
 
            self.build_list_params(params, scaling_processes, 'ScalingProcesses')
 
547
            self.build_list_params(params, scaling_processes,
 
548
                                   'ScalingProcesses')
514
549
        return self.get_status('ResumeProcesses', params)
515
550
 
516
551
    def create_scheduled_group_action(self, as_group, name, time,
614
649
            params['HonorCooldown'] = honor_cooldown
615
650
        return self.get_status('ExecutePolicy', params)
616
651
 
 
652
    def put_notification_configuration(self, autoscale_group, topic, notification_types):
 
653
        """
 
654
        Configures an Auto Scaling group to send notifications when
 
655
        specified events take place.
 
656
 
 
657
        :type as_group: str or
 
658
            :class:`boto.ec2.autoscale.group.AutoScalingGroup` object
 
659
        :param as_group: The Auto Scaling group to put notification
 
660
            configuration on.
 
661
 
 
662
        :type topic: str
 
663
        :param topic: The Amazon Resource Name (ARN) of the Amazon Simple
 
664
            Notification Service (SNS) topic.
 
665
 
 
666
        :type notification_types: list
 
667
        :param notification_types: The type of events that will trigger
 
668
            the notification.
 
669
        """
 
670
 
 
671
        name = autoscale_group
 
672
        if isinstance(autoscale_group, AutoScalingGroup):
 
673
            name = autoscale_group.name
 
674
 
 
675
        params = {'AutoScalingGroupName': name,
 
676
                  'TopicARN': topic}
 
677
        self.build_list_params(params, notification_types, 'NotificationTypes')
 
678
        return self.get_status('PutNotificationConfiguration', params)
 
679
 
617
680
    def set_instance_health(self, instance_id, health_status,
618
681
                            should_respect_grace_period=True):
619
682
        """
640
703
            params['ShouldRespectGracePeriod'] = 'false'
641
704
        return self.get_status('SetInstanceHealth', params)
642
705
 
 
706
    def set_desired_capacity(self, group_name, desired_capacity, honor_cooldown=False):
 
707
        """
 
708
        Adjusts the desired size of the AutoScalingGroup by initiating scaling
 
709
        activities. When reducing the size of the group, it is not possible to define
 
710
        which Amazon EC2 instances will be terminated. This applies to any Auto Scaling
 
711
        decisions that might result in terminating instances.
 
712
 
 
713
        :type group_name: string
 
714
        :param group_name: name of the auto scaling group
 
715
 
 
716
        :type desired_capacity: integer
 
717
        :param desired_capacity: new capacity setting for auto scaling group
 
718
 
 
719
        :type honor_cooldown: boolean
 
720
        :param honor_cooldown: by default, overrides any cooldown period
 
721
        """
 
722
        params = {'AutoScalingGroupName': group_name,
 
723
                  'DesiredCapacity': desired_capacity}
 
724
        if honor_cooldown:
 
725
            params['HonorCooldown'] = json.dumps('True')
 
726
 
 
727
        return self.get_status('SetDesiredCapacity', params)
 
728
 
643
729
    # Tag methods
644
730
 
645
731
    def get_all_tags(self, filters=None, max_records=None, next_token=None):
646
732
        """
647
733
        Lists the Auto Scaling group tags.
648
734
 
649
 
        This action supports pagination by returning a token if there are more
650
 
        pages to retrieve. To get the next page, call this action again with             the returned token as the NextToken parameter.
 
735
        This action supports pagination by returning a token if there
 
736
        are more pages to retrieve. To get the next page, call this
 
737
        action again with the returned token as the NextToken
 
738
        parameter.
651
739
 
652
740
        :type filters: dict
653
741
        :param filters: The value of the filter type used to identify
677
765
        """
678
766
        params = {}
679
767
        for i, tag in enumerate(tags):
680
 
            tag.build_params(params, i+1)
 
768
            tag.build_params(params, i + 1)
681
769
        return self.get_status('CreateOrUpdateTags', params, verb='POST')
682
770
 
683
771
    def delete_tags(self, tags):
689
777
        """
690
778
        params = {}
691
779
        for i, tag in enumerate(tags):
692
 
            tag.build_params(params, i+1)
 
780
            tag.build_params(params, i + 1)
693
781
        return self.get_status('DeleteTags', params, verb='POST')
694
 
 
695