def create(ctx, iface, resource_config, **_): """Creates an AWS Cloudwatch Target""" # Create a copy of the resource config for clean manipulation. params = \ dict() if not resource_config else resource_config.copy() rule = params.get(RULE) if not rule: rule = utils.find_resource_id_by_type( ctx.instance, RULE_TYPE) params[RULE] = rule targets = params.get(TARGETS, []) for target in targets: target_arn = target.get(ARN, '') if not utils.validate_arn(target_arn): targs = \ utils.find_rels_by_node_name( ctx.instance, target_arn) if targs: target_arn = \ targs[0].target.instance.runtime_properties.get( EXTERNAL_RESOURCE_ARN, target_arn) targets.remove(target) target[ARN] = target_arn targets.append(target) # Actually create the resource iface.create(params)
def create(ctx, iface, resource_config, **_): '''Creates an AWS ELB load balancer''' # Build API params params = \ dict() if not resource_config else resource_config.copy() resource_id = \ iface.resource_id or \ utils.get_resource_id( ctx.node, ctx.instance, params.get('Name'), use_instance_id=True) params['Name'] = resource_id utils.update_resource_id(ctx.instance, resource_id) # LB attributes are only applied in modify operation. params.pop(LB_ATTR, {}) # Add Subnets subnets_from_params = params.get(SUBNETS, []) subnets = \ utils.find_rels_by_node_type( ctx.instance, SUBNET_TYPE) or utils.find_rels_by_node_name( ctx.instance, SUBNET_TYPE_DEPRECATED) for subnet in subnets: subnet_id = \ subnet.target.instance.runtime_properties[EXTERNAL_RESOURCE_ID] subnets_from_params.append(subnet_id) params[SUBNETS] = subnets_from_params # Add Security Groups secgroups_from_params = params.get(SECGROUPS, []) secgroups = \ utils.find_rels_by_node_type( ctx.instance, SECGROUP_TYPE) for secgroup in secgroups: secgroup_id = \ secgroup.target.instance.runtime_properties[EXTERNAL_RESOURCE_ID] secgroups_from_params.append(secgroup_id) params[SECGROUPS] = secgroups_from_params # Actually create the resource output = iface.create(params) lb_id = output['LoadBalancers'][0][RESOURCE_NAME] iface.resource_id = lb_id try: utils.update_resource_id( ctx.instance, lb_id) utils.update_resource_arn( ctx.instance, output['LoadBalancers'][0][LB_ARN]) except (IndexError, KeyError) as e: raise NonRecoverableError( '{0}: {1} or {2} not located in response: {3}'.format( str(e), RESOURCE_NAME, LB_ARN, output))
def create(ctx, iface, resource_config, **_): """Creates an AWS SNS Subscription""" # Create a copy of the resource config for clean manipulation. params = \ dict() if not resource_config else resource_config.copy() topic_arn = params.get(TOPIC_ARN) # Add the required TopicArn parameter. if not topic_arn: rel = \ utils.find_rel_by_node_type( ctx.instance, TOPIC_TYPE) topic_arn = \ rel.target.instance.runtime_properties.get( EXTERNAL_RESOURCE_ARN) ctx.instance.runtime_properties[TOPIC_ARN] = \ topic_arn params[TOPIC_ARN] = topic_arn topic_iface = SNSTopic(ctx_node=ctx.node, resource_id=topic_arn, client=iface.client, logger=ctx.logger) # Subscribe Endpoint is the arn of an endpoint endpoint_name = params.get('Endpoint') if not endpoint_name: raise NonRecoverableError( 'Endpoint ARN or node_name was not provided.') # If endpoint_name is not a valid arn get arn from relationship. if not utils.validate_arn(endpoint_name): rel = \ utils.find_rels_by_node_name( ctx.instance, endpoint_name)[0] endpoint_arn = \ rel.target.instance.runtime_properties.get( EXTERNAL_RESOURCE_ARN) params['Endpoint'] = endpoint_arn # Request the subscription request_arn = topic_iface.subscribe(params) utils.update_resource_id(ctx.instance, request_arn) utils.update_resource_arn(ctx.instance, request_arn)
def create(ctx, iface, resource_config, **_): '''Creates an AWS ELB target group''' # Build API params cfg = \ ctx.instance.runtime_properties['resource_config'] or resource_config params = cfg.copy() # TG attributes are only applied in modify operation. params.pop(GRP_ATTR, {}) if VPC_ID not in params.keys(): targs = \ utils.find_rels_by_node_type( ctx.instance, VPC_TYPE) or utils.find_rels_by_node_name( ctx.instance, VPC_TYPE_DEPRECATED) tg_attr = targs[0].target.instance.runtime_properties params[VPC_ID] = \ tg_attr.get(EXTERNAL_RESOURCE_ID) del targs # Actually create the resource res_id = iface.create(params) utils.update_resource_id(ctx.instance, res_id) utils.update_resource_arn(ctx.instance, res_id)
def test_find_rels_by_node_name(self): mock_instance, mock_child = self._prepare_for_find_rel() self.assertEqual( utils.find_rels_by_node_name(mock_instance.instance, 'aws'), [mock_child])