Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add the ability to set default ASG scaling policies and notification …
…policies in a pillar. Small bugfix for the dynamo module
  • Loading branch information
mdemmitt-lyft committed Mar 12, 2015
1 parent 396e8a5 commit 78aacdb
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
14 changes: 10 additions & 4 deletions salt/modules/boto_asg.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions salt/modules/boto_dynamodb.py
Expand Up @@ -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 = []
Expand Down
78 changes: 77 additions & 1 deletion salt/states/boto_asg.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 78aacdb

Please sign in to comment.