From 78aacdb5d7c879fc88e334ca2be32f7921e09ce5 Mon Sep 17 00:00:00 2001 From: Mike Demmitt Date: Thu, 12 Mar 2015 11:45:20 -0700 Subject: [PATCH] add the ability to set default ASG scaling policies and notification policies in a pillar. Small bugfix for the dynamo module --- salt/modules/boto_asg.py | 14 +++++-- salt/modules/boto_dynamodb.py | 8 ++-- salt/states/boto_asg.py | 78 ++++++++++++++++++++++++++++++++++- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/salt/modules/boto_asg.py b/salt/modules/boto_asg.py index 22a7aa0fc636..2f1ebc94f696 100644 --- a/salt/modules/boto_asg.py +++ b/salt/modules/boto_asg.py @@ -163,8 +163,9 @@ def create(name, launch_config_name, availability_zones, min_size, max_size, health_check_type=None, health_check_period=None, placement_group=None, vpc_zone_identifier=None, tags=None, termination_policies=None, suspended_processes=None, - scaling_policies=None, region=None, key=None, keyid=None, - profile=None): + scaling_policies=None, region=None, + notification_arn=None, notification_types=None, + key=None, keyid=None, profile=None): ''' Create an autoscale group. @@ -221,6 +222,9 @@ def create(name, launch_config_name, availability_zones, min_size, max_size, conn.create_auto_scaling_group(_asg) # create scaling policies _create_scaling_policies(conn, name, scaling_policies) + # create notifications + if notification_arn and notification_types: + conn.put_notification_configuration(_asg, notification_arn, notification_types) log.info('Created ASG {0}'.format(name)) return True except boto.exception.BotoServerError as e: @@ -236,8 +240,8 @@ def update(name, launch_config_name, availability_zones, min_size, max_size, placement_group=None, vpc_zone_identifier=None, tags=None, termination_policies=None, suspended_processes=None, scaling_policies=None, - region=None, key=None, keyid=None, - profile=None): + notification_arn=None, notification_types=None, + region=None, key=None, keyid=None, profile=None): ''' Update an autoscale group. @@ -292,6 +296,8 @@ def update(name, launch_config_name, availability_zones, min_size, max_size, placement_group=placement_group, tags=_tags, vpc_zone_identifier=vpc_zone_identifier, termination_policies=termination_policies) + if notification_arn and notification_types: + conn.put_notification_configuration(_asg, notification_arn, notification_types) _asg.update() # Seems the update call doesn't handle tags, so we'll need to update # that separately. diff --git a/salt/modules/boto_dynamodb.py b/salt/modules/boto_dynamodb.py index 5367b12a4b78..bf36838a4b9a 100644 --- a/salt/modules/boto_dynamodb.py +++ b/salt/modules/boto_dynamodb.py @@ -142,11 +142,11 @@ def create_table(table_name, region=None, key=None, keyid=None, profile=None, 'write': write_capacity_units } local_table_indexes = [] - # Add the table's key - local_table_indexes.append( - AllIndex(primary_index_name, parts=primary_index_fields) - ) if local_indexes: + # Add the table's key + local_table_indexes.append( + AllIndex(primary_index_name, parts=primary_index_fields) + ) for index in local_indexes: local_table_indexes.append(_extract_index(index)) global_table_indexes = [] diff --git a/salt/states/boto_asg.py b/salt/states/boto_asg.py index 82823a5d6d61..9c71b0e0a5d6 100644 --- a/salt/states/boto_asg.py +++ b/salt/states/boto_asg.py @@ -229,12 +229,17 @@ def present( termination_policies=None, suspended_processes=None, scaling_policies=None, + scaling_policies_from_pillar="boto_asg_scaling_policies", alarms=None, alarms_from_pillar='boto_asg_alarms', region=None, key=None, keyid=None, - profile=None): + profile=None + notification_arn=None, + notification_arn_from_pillar="boto_asg_notification_arn", + notification_types=None, + notification_types_from_pillar="boto_asg_notification_types"): ''' Ensure the autoscale group exists. @@ -309,6 +314,10 @@ def present( List of scaling policies. Each policy is a dict of key-values described by http://boto.readthedocs.org/en/latest/ref/autoscale.html#boto.ec2.autoscale.policy.ScalingPolicy + scaling_policies_from_pillar: + name of pillar dict that contains scaling policy settings. Scaling policies defined for + this specific state will override those from pillar. + alarms: a dictionary of name->boto_cloudwatch_alarm sections to be associated with this ASG. All attributes should be specified except for dimension which will be @@ -331,6 +340,26 @@ def present( profile A dict with region, key and keyid, or a pillar key (string) that contains a dict with region, key and keyid. + + notification_arn + The aws arn that notifications will be sent to + + notification_arn_from_pillar + name of the pillar dict that contains notifcation_arn settings. A notification_arn + defined for this specific state will override the one from pillar. + + notification_types + A list of event names that will trigger a notification. The list of valid + notification types is: + "autoscaling:EC2_INSTANCE_LAUNCH", + "autoscaling:EC2_INSTANCE_LAUNCH_ERROR", + "autoscaling:EC2_INSTANCE_TERMINATE", + "autoscaling:EC2_INSTANCE_TERMINATE_ERROR", + "autoscaling:TEST_NOTIFICATION" + + notification_types_from_pillar + name of the pillar dict that contains notifcation_types settings. Notification_types + defined for this specific state will override those from the pillar. ''' ret = {'name': name, 'result': True, 'comment': '', 'changes': {}} if vpc_zone_identifier: @@ -392,6 +421,16 @@ def present( ret['comment'] = msg ret['result'] = None return ret + notification_arn, notification_types = _determine_notification_info( + notification_arn, + notification_arn_from_pillar, + notification_types, + notification_types_from_pillar + ) + scaling_policies = _determine_scaling_policies( + scaling_policies, + scaling_policies_from_pillar + ) created = __salt__['boto_asg.create'](name, launch_config_name, availability_zones, min_size, max_size, desired_capacity, @@ -403,6 +442,7 @@ def present( termination_policies, suspended_processes, scaling_policies, region, + notification_arn, notification_types, key, keyid, profile) if created: ret['changes']['old'] = None @@ -461,6 +501,17 @@ def present( ret['comment'] = msg ret['result'] = None return ret + # add in alarms + notification_arn, notification_types = _determine_notification_info( + notification_arn, + notification_arn_from_pillar, + notification_types, + notification_types_from_pillar + ) + scaling_policies = _determine_scaling_policies( + scaling_policies, + scaling_policies_from_pillar + ) updated, msg = __salt__['boto_asg.update'](name, launch_config_name, availability_zones, min_size, max_size, desired_capacity, @@ -473,6 +524,7 @@ def present( termination_policies, suspended_processes, scaling_policies, region, + notification_arn, notification_types, key, keyid, profile) if asg['launch_config_name'] != launch_config_name: # delete the old launch_config_name @@ -499,6 +551,30 @@ def present( return ret +def _determine_scaling_policies(scaling_policies, scaling_policies_from_pillar): + '''helper method for present. ensure that scaling_policies are set''' + pillar_scaling_policies = __salt__['config.option'](scaling_policies_from_pillar, {}) + if not scaling_policies and len(pillar_scaling_policies) > 0: + scaling_policies = pillar_scaling_policies + return scaling_policies + + +def _determine_notification_info( + notification_arn, + notification_arn_from_pillar, + notification_types, + notification_types_from_pillar): + '''helper method for present. ensure that notification_configs are set''' + pillar_arn_list = __salt__['config.option'](notification_arn_from_pillar, {}) + pillar_arn = None + if len(pillar_arn_list) > 0: + pillar_arn = pillar_arn_list[0] + pillar_notification_types = __salt__['config.option'](notification_types_from_pillar, {}) + arn = notification_arn if notification_arn else pillar_arn + types = notification_types if notification_types else pillar_notification_types + return (arn, types) + + def _alarms_present(name, alarms, alarms_from_pillar, region, key, keyid, profile): '''helper method for present. ensure that cloudwatch_alarms are set''' # load data from alarms_from_pillar