@Override protected ListableStackResourceFactory createInstance() { String stackName = this.stackNameProvider.getStackName(); return new StaticStackResourceRegistry(stackName, getResourceMappings("", stackName)); }
private AutoDetectingStackNameProvider(AmazonCloudFormation amazonCloudFormationClient, AmazonEC2 amazonEc2Client, InstanceIdProvider instanceIdProvider) { this.amazonCloudFormationClient = amazonCloudFormationClient; this.amazonEc2Client = amazonEc2Client; this.instanceIdProvider = instanceIdProvider; afterPropertiesSet(); }
@Bean public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) { if (StringUtils.hasText(this.annotationAttributes.getString("stackName"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new StaticStackNameProvider(this.annotationAttributes.getString("stackName"))); } else { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new AutoDetectingStackNameProvider(amazonCloudFormation, this.amazonEc2)); } }
private Map<String, StackResource> getResourceMappings(String prefix, String stackName) { List<StackResourceSummary> stackResourceSummaries = getStackResourceSummaries(stackName); Map<String, StackResource> current = convertToStackResourceMappings(prefix, stackResourceSummaries); Map<String, StackResource> stackResourceMappings = new HashMap<>(current); for (Map.Entry<String, StackResource> e : current.entrySet()) { StackResource resource = e.getValue(); if ("AWS::CloudFormation::Stack".equals(resource.getType())) { stackResourceMappings.putAll(getResourceMappings(e.getKey(), resource.getPhysicalId())); } } return stackResourceMappings; }
@Override public void afterPropertiesSet() { String instanceId = this.instanceIdProvider.getCurrentInstanceId(); this.stackName = autoDetectStackName(instanceId); if (this.stackName == null) { throw new IllegalStateException("No stack resources found in stack for EC2 instance '" + instanceId + "'"); } }
private Map<String, StackResource> convertToStackResourceMappings(String prefix, List<StackResourceSummary> stackResourceSummaries) { Map<String, StackResource> stackResourceMappings = new HashMap<>(); for (StackResourceSummary stackResourceSummary : stackResourceSummaries) { String logicalResourceId = toNestedResourceId(prefix, stackResourceSummary.getLogicalResourceId()); stackResourceMappings.put(logicalResourceId, new StackResource(logicalResourceId, stackResourceSummary.getPhysicalResourceId(), stackResourceSummary.getResourceType())); } return stackResourceMappings; }
@Override protected Map<String, String> createInstance() throws Exception { LinkedHashMap<String, String> userTags = new LinkedHashMap<>(); DescribeStacksResult stacksResult = this.amazonCloudFormation.describeStacks(new DescribeStacksRequest().withStackName(this.stackNameProvider.getStackName())); for (Stack stack : stacksResult.getStacks()) { for (Tag tag : stack.getTags()) { userTags.put(tag.getKey(), tag.getValue()); } } return userTags; } }
@Bean public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) { if (StringUtils.hasText(this.annotationAttributes.getString("stackName"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new StaticStackNameProvider(this.annotationAttributes.getString("stackName"))); } else { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new AutoDetectingStackNameProvider(amazonCloudFormation, this.amazonEc2)); } }
@Override protected ListableStackResourceFactory createInstance() { String stackName = this.stackNameProvider.getStackName(); return new StaticStackResourceRegistry(stackName, getResourceMappings("", stackName)); }
private Map<String, StackResource> getResourceMappings(String prefix, String stackName) { List<StackResourceSummary> stackResourceSummaries = getStackResourceSummaries(stackName); Map<String, StackResource> current = convertToStackResourceMappings(prefix, stackResourceSummaries); Map<String, StackResource> stackResourceMappings = new HashMap<>(current); for (Map.Entry<String, StackResource> e : current.entrySet()) { StackResource resource = e.getValue(); if ("AWS::CloudFormation::Stack".equals(resource.getType())) { stackResourceMappings.putAll(getResourceMappings(e.getKey(), resource.getPhysicalId())); } } return stackResourceMappings; }
private AutoDetectingStackNameProvider(AmazonCloudFormation amazonCloudFormationClient, AmazonEC2 amazonEc2Client, InstanceIdProvider instanceIdProvider) { this.amazonCloudFormationClient = amazonCloudFormationClient; this.amazonEc2Client = amazonEc2Client; this.instanceIdProvider = instanceIdProvider; afterPropertiesSet(); }
@Override public void afterPropertiesSet() { String instanceId = this.instanceIdProvider.getCurrentInstanceId(); this.stackName = autoDetectStackName(instanceId); if (this.stackName == null) { throw new IllegalStateException("No stack resources found in stack for EC2 instance '" + instanceId + "'"); } }
private Map<String, StackResource> convertToStackResourceMappings(String prefix, List<StackResourceSummary> stackResourceSummaries) { Map<String, StackResource> stackResourceMappings = new HashMap<>(); for (StackResourceSummary stackResourceSummary : stackResourceSummaries) { String logicalResourceId = toNestedResourceId(prefix, stackResourceSummary.getLogicalResourceId()); stackResourceMappings.put(logicalResourceId, new StackResource(logicalResourceId, stackResourceSummary.getPhysicalResourceId(), stackResourceSummary.getResourceType())); } return stackResourceMappings; }
@Override protected Map<String, String> createInstance() throws Exception { LinkedHashMap<String, String> userTags = new LinkedHashMap<>(); DescribeStacksResult stacksResult = this.amazonCloudFormation.describeStacks(new DescribeStacksRequest().withStackName(this.stackNameProvider.getStackName())); for (Stack stack : stacksResult.getStacks()) { for (Tag tag : stack.getTags()) { userTags.put(tag.getKey(), tag.getValue()); } } return userTags; } }
@Bean @ConditionalOnMissingBean(StackResourceRegistry.class) public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) { if (StringUtils.hasText(environment.getProperty("cloud.aws.stack.name"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new StaticStackNameProvider(this.environment.getProperty("cloud.aws.stack.name"))); } if (environment.getProperty("cloud.aws.stack.auto") == null || "true".equalsIgnoreCase(environment.getProperty("cloud.aws.stack.auto"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new AutoDetectingStackNameProvider(amazonCloudFormation, this.amazonEC2)); } return null; }
@Bean @ConditionalOnMissingBean(StackResourceRegistry.class) public StackResourceRegistryFactoryBean stackResourceRegistryFactoryBean(AmazonCloudFormation amazonCloudFormation) { if (StringUtils.hasText(environment.getProperty("cloud.aws.stack.name"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new StaticStackNameProvider(this.environment.getProperty("cloud.aws.stack.name"))); } if (environment.getProperty("cloud.aws.stack.auto") == null || "true".equalsIgnoreCase(environment.getProperty("cloud.aws.stack.auto"))) { return new StackResourceRegistryFactoryBean(amazonCloudFormation, new AutoDetectingStackNameProvider(amazonCloudFormation, this.amazonEC2)); } return null; }