def main(): argument_spec = ec2_argument_spec() argument_spec.update(dict(task_definition=dict(required=True, type='str'))) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) if not HAS_BOTO3: module.fail_json(msg='boto3 is required.') region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) ecs_td = ecs.describe_task_definition( taskDefinition=module.params['task_definition'])['taskDefinition'] ecs_td_snake = {} for k, v in ecs_td.items(): ecs_td_snake[_camel_to_snake(k)] = v ecs_td_facts_result = dict(changed=False, ansible_facts=ecs_td_snake) module.exit_json(**ecs_td_facts_result)
def create_scaling_policy(connection, module): scaling_policy = connection.describe_scaling_policies( ServiceNamespace=module.params.get('service_namespace'), ResourceId=module.params.get('resource_id'), ScalableDimension=module.params.get('scalable_dimension'), PolicyNames=[module.params.get('policy_name')], MaxResults=1) changed = False if scaling_policy['ScalingPolicies']: scaling_policy = scaling_policy['ScalingPolicies'][0] # check if the input parameters are equal to what's already configured for attr in ('PolicyName', 'ServiceNamespace', 'ResourceId', 'ScalableDimension', 'PolicyType', 'StepScalingPolicyConfiguration', 'TargetTrackingScalingPolicyConfiguration'): if attr in scaling_policy and scaling_policy[ attr] != module.params.get(_camel_to_snake(attr)): changed = True scaling_policy[attr] = module.params.get(_camel_to_snake(attr)) else: changed = True scaling_policy = { 'PolicyName': module.params.get('policy_name'), 'ServiceNamespace': module.params.get('service_namespace'), 'ResourceId': module.params.get('resource_id'), 'ScalableDimension': module.params.get('scalable_dimension'), 'PolicyType': module.params.get('policy_type'), 'StepScalingPolicyConfiguration': module.params.get('step_scaling_policy_configuration'), 'TargetTrackingScalingPolicyConfiguration': module.params.get('target_tracking_scaling_policy_configuration') } if changed: try: if (module.params.get('step_scaling_policy_configuration')): connection.put_scaling_policy( PolicyName=scaling_policy['PolicyName'], ServiceNamespace=scaling_policy['ServiceNamespace'], ResourceId=scaling_policy['ResourceId'], ScalableDimension=scaling_policy['ScalableDimension'], PolicyType=scaling_policy['PolicyType'], StepScalingPolicyConfiguration=scaling_policy[ 'StepScalingPolicyConfiguration']) elif (module.params.get( 'target_tracking_scaling_policy_configuration')): connection.put_scaling_policy( PolicyName=scaling_policy['PolicyName'], ServiceNamespace=scaling_policy['ServiceNamespace'], ResourceId=scaling_policy['ResourceId'], ScalableDimension=scaling_policy['ScalableDimension'], PolicyType=scaling_policy['PolicyType'], TargetTrackingScalingPolicyConfiguration=scaling_policy[ 'TargetTrackingScalingPolicyConfiguration']) except Exception as e: module.fail_json(msg=str(e), exception=traceback.format_exc()) try: response = connection.describe_scaling_policies( ServiceNamespace=module.params.get('service_namespace'), ResourceId=module.params.get('resource_id'), ScalableDimension=module.params.get('scalable_dimension'), PolicyNames=[module.params.get('policy_name')], MaxResults=1) except Exception as e: module.fail_json(msg=str(e), exception=traceback.format_exc()) if (response['ScalingPolicies']): snaked_response = camel_dict_to_snake_dict( response['ScalingPolicies'][0]) else: snaked_response = {} module.exit_json(changed=changed, response=snaked_response)
def create_scaling_policy(connection, module): try: scaling_policy = connection.describe_scaling_policies( ServiceNamespace=module.params.get('service_namespace'), ResourceId=module.params.get('resource_id'), ScalableDimension=module.params.get('scalable_dimension'), PolicyNames=[module.params.get('policy_name')], MaxResults=1) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to describe scaling policies") changed = False if scaling_policy['ScalingPolicies']: scaling_policy = scaling_policy['ScalingPolicies'][0] # check if the input parameters are equal to what's already configured for attr in ('PolicyName', 'ServiceNamespace', 'ResourceId', 'ScalableDimension', 'PolicyType', 'StepScalingPolicyConfiguration', 'TargetTrackingScalingPolicyConfiguration'): if attr in scaling_policy and scaling_policy[ attr] != module.params.get(_camel_to_snake(attr)): changed = True scaling_policy[attr] = module.params.get(_camel_to_snake(attr)) else: changed = True scaling_policy = { 'PolicyName': module.params.get('policy_name'), 'ServiceNamespace': module.params.get('service_namespace'), 'ResourceId': module.params.get('resource_id'), 'ScalableDimension': module.params.get('scalable_dimension'), 'PolicyType': module.params.get('policy_type'), 'StepScalingPolicyConfiguration': module.params.get('step_scaling_policy_configuration'), 'TargetTrackingScalingPolicyConfiguration': module.params.get('target_tracking_scaling_policy_configuration') } if changed: try: if (module.params.get('step_scaling_policy_configuration')): connection.put_scaling_policy( PolicyName=scaling_policy['PolicyName'], ServiceNamespace=scaling_policy['ServiceNamespace'], ResourceId=scaling_policy['ResourceId'], ScalableDimension=scaling_policy['ScalableDimension'], PolicyType=scaling_policy['PolicyType'], StepScalingPolicyConfiguration=scaling_policy[ 'StepScalingPolicyConfiguration']) elif (module.params.get( 'target_tracking_scaling_policy_configuration')): connection.put_scaling_policy( PolicyName=scaling_policy['PolicyName'], ServiceNamespace=scaling_policy['ServiceNamespace'], ResourceId=scaling_policy['ResourceId'], ScalableDimension=scaling_policy['ScalableDimension'], PolicyType=scaling_policy['PolicyType'], TargetTrackingScalingPolicyConfiguration=scaling_policy[ 'TargetTrackingScalingPolicyConfiguration']) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to create scaling policy") try: response = connection.describe_scaling_policies( ServiceNamespace=module.params.get('service_namespace'), ResourceId=module.params.get('resource_id'), ScalableDimension=module.params.get('scalable_dimension'), PolicyNames=[module.params.get('policy_name')], MaxResults=1) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to describe scaling policies") if (response['ScalingPolicies']): snaked_response = camel_dict_to_snake_dict( response['ScalingPolicies'][0]) else: snaked_response = {} return {"changed": changed, "response": snaked_response}
def test_camel_to_snake_and_back(self): for (k, v) in EXPECTED_REVERSIBLE.items(): self.assertEqual( _snake_to_camel(_camel_to_snake(k, reversible=True), capitalize_first=True), k)
def test_reversible_camel_to_snake(self): for (k, v) in EXPECTED_REVERSIBLE.items(): self.assertEqual(_camel_to_snake(k, reversible=True), v)
def test_camel_to_snake(self): for (k, v) in EXPECTED_SNAKIFICATION.items(): self.assertEqual(_camel_to_snake(k), v)
def create_scaling_policy(connection, module): try: scaling_policy = connection.describe_scaling_policies( ServiceNamespace=module.params.get('service_namespace'), ResourceId=module.params.get('resource_id'), ScalableDimension=module.params.get('scalable_dimension'), PolicyNames=[module.params.get('policy_name')], MaxResults=1 ) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to describe scaling policies") changed = False if scaling_policy['ScalingPolicies']: scaling_policy = scaling_policy['ScalingPolicies'][0] # check if the input parameters are equal to what's already configured for attr in ('PolicyName', 'ServiceNamespace', 'ResourceId', 'ScalableDimension', 'PolicyType', 'StepScalingPolicyConfiguration', 'TargetTrackingScalingPolicyConfiguration'): if attr in scaling_policy and scaling_policy[attr] != module.params.get(_camel_to_snake(attr)): changed = True scaling_policy[attr] = module.params.get(_camel_to_snake(attr)) else: changed = True scaling_policy = { 'PolicyName': module.params.get('policy_name'), 'ServiceNamespace': module.params.get('service_namespace'), 'ResourceId': module.params.get('resource_id'), 'ScalableDimension': module.params.get('scalable_dimension'), 'PolicyType': module.params.get('policy_type'), 'StepScalingPolicyConfiguration': module.params.get('step_scaling_policy_configuration'), 'TargetTrackingScalingPolicyConfiguration': module.params.get('target_tracking_scaling_policy_configuration') } if changed: try: if (module.params.get('step_scaling_policy_configuration')): connection.put_scaling_policy( PolicyName=scaling_policy['PolicyName'], ServiceNamespace=scaling_policy['ServiceNamespace'], ResourceId=scaling_policy['ResourceId'], ScalableDimension=scaling_policy['ScalableDimension'], PolicyType=scaling_policy['PolicyType'], StepScalingPolicyConfiguration=scaling_policy['StepScalingPolicyConfiguration'] ) elif (module.params.get('target_tracking_scaling_policy_configuration')): connection.put_scaling_policy( PolicyName=scaling_policy['PolicyName'], ServiceNamespace=scaling_policy['ServiceNamespace'], ResourceId=scaling_policy['ResourceId'], ScalableDimension=scaling_policy['ScalableDimension'], PolicyType=scaling_policy['PolicyType'], TargetTrackingScalingPolicyConfiguration=scaling_policy['TargetTrackingScalingPolicyConfiguration'] ) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to create scaling policy") try: response = connection.describe_scaling_policies( ServiceNamespace=module.params.get('service_namespace'), ResourceId=module.params.get('resource_id'), ScalableDimension=module.params.get('scalable_dimension'), PolicyNames=[module.params.get('policy_name')], MaxResults=1 ) except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: module.fail_json_aws(e, msg="Failed to describe scaling policies") if (response['ScalingPolicies']): snaked_response = camel_dict_to_snake_dict(response['ScalingPolicies'][0]) else: snaked_response = {} return {"changed": changed, "response": snaked_response}
def test_camel_to_snake_and_back(self): for (k, v) in EXPECTED_REVERSIBLE.items(): self.assertEqual(_snake_to_camel(_camel_to_snake(k, reversible=True), capitalize_first=True), k)