in lib/ansible/modules/cloud/alicloud/ali_ess_group.py [0:0]
def main():
argument_spec = ecs_argument_spec()
argument_spec.update(dict(
name=dict(type=str, aliases=['group_name']),
max_size=dict(type=int),
min_size=dict(type=int),
state=dict(type=str, default='present', choices=['present', 'active', 'inactive', 'absent']),
id=dict(type=str, aliases=['group_id']),
cooldown=dict(type=int, default=300, aliases=['default_cooldown']),
removal_policies=dict(type=list, default=['OldestScalingConfiguration','OldestInstance']),
load_balancer_ids=dict(type=list, aliases=['lb_ids']),
db_instance_ids=dict(type=list, aliases=['db_ids']),
vswitch_ids=dict(type=list, aliases=['subnet_ids']),
configuration_id=dict(type=str, aliases=['scaling_configuration_id'])
))
module = AnsibleModule(argument_spec=argument_spec)
if HAS_FOOTMARK is False:
module.fail_json(msg="Package 'footmark' required for the module ali_ess_group.")
ess = ess_connect(module)
state = module.params['state']
group_id = module.params['id']
group_name = module.params['name']
max_size = module.params['max_size']
min_size = module.params['min_size']
cooldown = module.params['cooldown']
removal_policies = module.params['removal_policies']
configuration_id = module.params['configuration_id']
changed = False
current = None
all_groups = []
if group_id or group_name:
groups = ess.describe_groups(scaling_group_ids=[group_id], scaling_group_names=[group_name])
if groups:
for group in groups:
all_groups.append(group.id)
if len(all_groups) > 1:
module.fail_json(msg="There are several scaling group in our record based on name {0}: {1}. "
"Please specified one using 'id' and try again.".format(group_name, all_groups))
current = groups[0]
if state == 'present':
if current is None:
try:
if max_size is None or max_size < 0 or max_size > 100:
module.fail_json(msg="'max_size': required field when state is 'present' and its value range [0, 100]. "
"Please check it and try again.")
if min_size is None or min_size < 0 or min_size > 100:
module.fail_json(msg="'min_size': required field when state is 'present' and its value range [0, 100]. "
"Please check it and try again.")
lb_ids = module.params['load_balancer_ids']
db_ids = module.params['db_instance_ids']
vsw_ids = module.params['vswitch_ids']
if lb_ids and not isinstance(lb_ids, list):
module.fail_json(msg="Filed 'load_balancer_ids' should be a list, aborting.")
if db_ids and not isinstance(db_ids, list):
module.fail_json(msg="Filed 'db_instance_ids' should be a list, aborting.")
if vsw_ids and not isinstance(vsw_ids, list):
module.fail_json(msg="Filed 'vswitch_ids' should be a list, aborting.")
current = ess.create_group(max_size=max_size, min_size=min_size, name=group_name,
default_cooldown=cooldown, removal_policies=removal_policies,
load_balancer_ids=lb_ids, db_instance_ids=db_ids, vswitch_ids=vsw_ids)
changed = True
except Exception as e:
module.fail_json(msg="Create scaling group got an error: {0}".format(e))
# Modify scaling group attribute
if group_name != current.name or max_size != current.max_size or min_size != current.min_size \
or configuration_id != current.configuration_id or cooldown != current.cooldown \
or removal_policies != current.removal_policies['removal_policy']:
changed = current.modify(max_size=max_size, min_size=min_size, name=group_name, default_cooldown=cooldown,
removal_policies=removal_policies, scaling_configuration_id=configuration_id)
module.exit_json(changed=changed, id=current.id, name=current.name, configuration_id=current.configuration_id,
group=get_details(current))
if current is None:
if group_id or group_name:
module.fail_json(msg="There are no scaling group in our record based on id {0} or name {1}. "
"Please check it and try again.".format(group_id, group_name))
module.fail_json(msg='Please specify a scaling group that you want to operate by parameters id or name, aborting')
if state == 'absent':
try:
module.exit_json(changed=current.terminate())
except Exception as e:
module.fail_json(msg='Delete scaling group {0} got an error: {1}'.format(current.id, e))
if state == 'active':
try:
if str.lower(current.status) == 'inactive' or current.configuration_id != configuration_id:
changed = current.enable(scaling_configuration_id=configuration_id)
current = ess.describe_groups(scaling_group_ids=[group_id])[0]
except Exception as e:
module.fail_json(msg='Active scaling group {0} got an error: {1}.'.format(current.id, e))
elif state == 'inactive':
try:
if str.lower(current.status) == 'active':
changed = current.disable()
current = ess.describe_groups(scaling_group_ids=[group_id])[0]
except Exception as e:
module.fail_json(msg='Inactive scaling group {0} got an error: {1}.'.format(current.id, e))
module.exit_json(changed=changed, id=current.id, name=current.name, configuration_id=current.configuration_id,
group=get_details(current))