def main(): argument_spec = ecs_argument_spec() argument_spec.update(dict( state=dict(type=str, default='present', choices=['present', 'absent']), group_id=dict(type=str, required=True), instance_ids=dict(type=list), creation_type=dict(type=str, default='Attached', choices=['AutoCreated', 'Attached'], aliases=['type']) )) module = AnsibleModule(argument_spec=argument_spec) if HAS_FOOTMARK is False: module.fail_json(msg="Package 'footmark' required for the module ali_ess_instance.") ess = ess_connect(module) state = module.params['state'] group_id = module.params['group_id'] instance_ids = module.params['instance_ids'] creation_type = module.params['creation_type'] if state == 'present' and not instance_ids: module.fail_json(msg="Field 'instance_ids' is required when state is 'present'. Aborting.") changed = False adding = instance_ids removing = [] all = [] old = ess.describe_instances(scaling_group_id=group_id) if old: for inst in old: if instance_ids: if inst.id in instance_ids: adding.remove(inst.id) removing.append(inst.id) if state == 'absent' and creation_type and inst.creation_type == creation_type: removing.append(inst.id) all.append(inst.id) if state == 'present': if adding: try: changed = ess.attach_instances(scaling_group_id=group_id, instance_ids=adding) module.exit_json(changed=changed) except Exception as e: module.fail_json(msg="Adding ECS instances to scaling group got an error: {0}.".format(e)) if not removing: removing = all if removing: try: changed = ess.remove_instances(scaling_group_id=group_id, instance_ids=removing) except Exception as e: module.fail_json(msg='Removing ECS instances from scaling group got an error: {0}.'.format(e)) module.exit_json(changed=changed)
def main(): argument_spec = ecs_argument_spec() argument_spec.update( dict(group_id=dict(type='str', aliases=['scaling_group_id']), instance_type=dict(type='str', aliases=['type']), image_id=dict(type='str', aliases=['image']), name=dict(type='str', aliases=['configuration_name']), internet_charge_type=dict( type='str', default="PayByBandwidth", choices=["PayByBandwidth", "PayByTraffic"]), max_bandwidth_in=dict(type='int', default=200), max_bandwidth_out=dict(type='int', default=0), system_disk_category=dict(type='str', default='cloud_efficiency'), system_disk_size=dict(type='int', default='40'), tags=dict(type='dict'), state=dict(default='present', choices=['present', 'absent']), id=dict(type='str', aliases=['configuration_id']), key_name=dict(type='str', aliases=['keypair']), user_data=dict(type='str'), data_disks=dict(type='list'), security_group_id=dict(type='str'), ram_role_name=dict(type='str', aliases=['ram_role']))) module = AnsibleModule(argument_spec=argument_spec) if HAS_FOOTMARK is False: module.fail_json( msg= "Package 'footmark' required for the module alicloud_ess_configuration." ) ess = ess_connect(module) state = module.params['state'] cfg_id = module.params['id'] cfg_name = module.params['name'] scaling_group = module.params['group_id'] changed = False current = None all_cfgs = [] if cfg_id or cfg_name: cfgs = ess.describe_configurations( scaling_group_id=scaling_group, scaling_configuration_ids=[cfg_id], scaling_configuration_names=[cfg_name]) if cfgs: if len(cfgs) > 1: for cfg in cfgs: all_cfgs.append(cfg.id) module.fail_json( msg= "There are several scaling configurations in our record based on name {0}: {1}. " "Please specified one using 'id' and try again.".format( cfg_name, all_cfgs)) current = cfgs[0] if state == 'present': if current is None: try: data_disks = module.params['data_disks'] if not isinstance(data_disks, list): module.fail_json( msg="Filed 'data_disks' should be a list, aborting.") if not isinstance(module.params['tags'], dict): module.fail_json( msg="Filed 'tags' should be a dict, aborting.") current = ess.create_configuration( scaling_group_id=scaling_group, image_id=module.params['image_id'], instance_type=module.params['instance_type'], security_group_id=module.params['security_group_id'], name=cfg_name, internet_charge_type=module.params['internet_charge_type'], max_bandwidth_in=module.params['max_bandwidth_in'], max_bandwidth_out=module.params['max_bandwidth_out'], system_disk_category=module.params['system_disk_category'], system_disk_size=module.params['system_disk_size'], data_disks=data_disks, tags=module.params['tags'], key_pair_name=module.params['key_name'], ram_role_name=module.params['ram_role_name'], user_data=module.params['user_data']) changed = True except Exception as e: module.fail_json( msg="Create scaling configuration got an error: {0}". format(e)) module.exit_json(changed=changed, id=current.id, name=current.name, group_id=current.group_id, configuration=get_details(current)) if current is None: if cfg_id or cfg_name: module.fail_json( msg= "There are no scaling configuration in our record based on id {0} or name {1}. " "Please check it and try again.".format(cfg_id, cfg_name)) module.fail_json( msg= 'Please specify a scaling configuration that you want to terminate by parameters id or name, aborting' ) try: module.exit_json(changed=current.terminate()) except Exception as e: module.fail_json( msg='Delete scaling configuration {0} got an error: {1}'.format( current.id, e))
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 alicloud_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))
def main(): argument_spec = ecs_argument_spec() argument_spec.update( dict(group_id=dict(type='str', aliases=['scaling_group_id']), adjustment_type=dict(type='str', aliases=['type'], choices=[ 'QuantityChangeInCapacity', 'PercentChangeInCapacity', 'TotalCapacity' ]), adjustment_value=dict(type='int', aliases=['value']), name=dict(type='str', aliases=['rule_name']), cooldown=dict(type='int'), state=dict(type='str', default='present', choices=['present', 'absent']), id=dict(type='str', aliases=['rule_id']))) module = AnsibleModule(argument_spec=argument_spec) if HAS_FOOTMARK is False: module.fail_json( msg="Package 'footmark' required for the module alicloud_ess_rule." ) ess = ess_connect(module) state = module.params['state'] rule_id = module.params['id'] rule_name = module.params['name'] scaling_group = module.params['group_id'] adjustment_type = module.params['adjustment_type'] adjustment_value = module.params['adjustment_value'] cooldown = module.params['cooldown'] changed = False current = None all_rules = [] if rule_id or rule_name: rules = ess.describe_rules(scaling_group_id=scaling_group, scaling_rule_ids=[rule_id], scaling_rule_names=[rule_name]) if rules: if len(rules) > 1: for r in rules: all_rules.append(r.id) module.fail_json( msg= "There are several scaling rules in our record based on name {0}: {1}. " "Please specified one using 'id' and try again.".format( rule_name, all_rules)) current = rules[0] if state == 'present': if current is None: try: if not scaling_group: module.exit_json( msg= "'group_id': required field when state is present, aborting." ) if not adjustment_type: module.exit_json( msg= "'adjustment_type': required field when state is present, aborting." ) if not adjustment_value: module.exit_json( msg= "'adjustment_value': required field when state is present, aborting." ) current = ess.create_rule(scaling_group_id=scaling_group, adjustment_type=adjustment_type, adjustment_value=adjustment_value, name=rule_name, cooldown=cooldown) changed = True except Exception as e: module.fail_json( msg="Create scaling rule got an error: {0}".format(e)) else: try: changed = current.modify(adjustment_type=adjustment_type, adjustment_value=adjustment_value, name=rule_name, cooldown=cooldown) if changed: current = current.update(validate=True) except Exception as e: module.fail_json( msg="Modify scaling rule got an error: {0}".format(e)) module.exit_json(changed=changed, id=current.id, name=current.name, group_id=current.group_id, rule=get_details(current)) if current is None: if rule_id or rule_name: module.fail_json( msg= "There are no scaling rule in our record based on id {0} or name {1}. " "Please check it and try again.".format(rule_id, rule_name)) module.fail_json( msg= 'Please specify a scaling rule that you want to terminate by parameters id or name, aborting' ) try: module.exit_json(changed=current.terminate()) except Exception as e: module.fail_json( msg='Delete scaling rule {0} got an error: {1}'.format( current.id, e))
def main(): argument_spec = ecs_argument_spec() argument_spec.update( dict(rule_id=dict(type='str'), launch_time=dict(type='str'), launch_expiration=dict(type='int', defalut=600, aliases=['expiration']), name=dict(type='str', aliases=['task_name']), description=dict(type='str'), recurrence_type=dict(type='str', choices=['Daily', 'Weekly', 'Monthly'], aliases=['type']), recurrence_value=dict(type='str', aliases=['value']), recurrence_endtime=dict(type='str', aliases=['endtime']), state=dict(type='str', default='present', choices=['present', 'absent']), enabled=dict(type='bool', default=True), id=dict(type='str', aliases=['task_id']))) module = AnsibleModule(argument_spec=argument_spec) if HAS_FOOTMARK is False: module.fail_json( msg="Package 'footmark' required for the module ali_ess_task.") ess = ess_connect(module) state = module.params['state'] task_id = module.params['id'] task_name = module.params['name'] rule_id = module.params['rule_id'] launch_time = module.params['launch_time'] launch_expiration = module.params['launch_expiration'] recurrence_type = module.params['recurrence_type'] recurrence_value = module.params['recurrence_value'] recurrence_endtime = module.params['recurrence_endtime'] enabled = module.params['enabled'] description = module.params['description'] # Get scaling rule ari according rule ID rule_ari = None if rule_id: rules = ess.describe_rules(scaling_rule_ids=[rule_id]) rule_ari = rules[0].ari count = 0 if recurrence_type: count += 1 if recurrence_value: count += 1 if recurrence_endtime: count += 1 if count in (1, 2): module.fail_json( msg= "'recurrence_type', 'recurrence_value' and 'recurrence_endtime' must be specified or not at the same time" ) changed = False current = None all_tasks = [] if task_id or task_name: tasks = ess.describe_scheduled_tasks(scheduled_task_ids=[task_id], scheduled_task_names=[task_name], scaling_rule_aris=[rule_ari]) if tasks: if len(tasks) > 1: for task in tasks: all_tasks.append(task.id) module.fail_json( msg= "There are several scheduled tasks in our record based on name {0}: {1}. " "Please specified one using 'id' and try again.".format( task_name, all_tasks)) current = tasks[0] if state == 'present': if current is None: try: if not rule_id: module.exit_json( msg= "'rule_id': required field when state is present, aborting." ) if not rule_ari: module.exit_json( msg= "There is no scheduled task in our record based on rule id {0}, aborting." "Please check it and try again.".format(rule_id)) if not launch_time: module.exit_json( msg= "'launch_time': required field when state is present, aborting." ) current = ess.create_scheduled_task( scaling_rule_ari=rule_ari, launch_time=launch_time, name=task_name, description=description, launch_expiration_time=launch_expiration, recurrence_type=recurrence_type, recurrence_value=recurrence_value, recurrence_end_time=recurrence_endtime, task_enabled=enabled) changed = True except Exception as e: module.fail_json( msg="Create scheduled task got an error: {0}".format(e)) else: try: changed = current.modify( scaling_rule_ari=rule_ari, launch_time=launch_time, name=task_name, description=description, launch_expiration_time=launch_expiration, recurrence_type=recurrence_type, recurrence_value=recurrence_value, recurrence_end_time=recurrence_endtime, task_enabled=enabled) if changed: current = current.update(validate=True) except Exception as e: module.fail_json( msg="Modify scheduled rule got an error: {0}".format(e)) module.exit_json(changed=changed, id=current.id, name=current.name, task=get_details(current)) if current is None: if task_id or task_name: module.fail_json( msg= "There are no scheduled task in our record based on id {0} or name {1}. " "Please check it and try again.".format(task_id, task_name)) module.fail_json( msg= 'Please specify a scheduled task that you want to terminate by field id or name, aborting' ) try: module.exit_json(changed=current.terminate()) except Exception as e: module.fail_json( msg='Delete scheduled task {0} got an error: {1}'.format( current.id, e))