@Test public void getObject_stackWithTagsDefined_createTagsMap() throws Exception { //Arrange AmazonCloudFormation cloudFormation = mock(AmazonCloudFormation.class); StackNameProvider stackNameProvider = mock(StackNameProvider.class); when(stackNameProvider.getStackName()).thenReturn("testStack"); when(cloudFormation.describeStacks(new DescribeStacksRequest().withStackName("testStack"))). thenReturn(new DescribeStacksResult().withStacks(new Stack().withTags( new Tag().withKey("key1").withValue("value1"), new Tag().withKey("key2").withValue("value2") ))); StackResourceUserTagsFactoryBean factoryBean = new StackResourceUserTagsFactoryBean(cloudFormation, stackNameProvider); //Act factoryBean.afterPropertiesSet(); Map<String, String> factoryBeanObject = factoryBean.getObject(); //Assert assertEquals("value1", factoryBeanObject.get("key1")); assertEquals("value2", factoryBeanObject.get("key2")); }
@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; }
private DescribeStackResourcesResult getStackResources(String stackName) throws InterruptedException, IOException { try { DescribeStacksResult describeStacksResult = this.amazonCloudFormationClient.describeStacks(new DescribeStacksRequest().withStackName(stackName)); for (Stack stack : describeStacksResult.getStacks()) { if (isAvailable(stack)) { return this.amazonCloudFormationClient.describeStackResources(new DescribeStackResourcesRequest().withStackName(stack.getStackName())); } if (isError(stack)) { if (this.stackCreatedByThisInstance) { throw new IllegalArgumentException("Could not create stack"); } this.amazonCloudFormationClient.deleteStack(new DeleteStackRequest().withStackName(stack.getStackName())); return getStackResources(stackName); } if (isInProgress(stack)) { //noinspection BusyWait Thread.sleep(5000L); return getStackResources(stackName); } } } catch (AmazonClientException e) { String templateBody = FileCopyUtils.copyToString(new InputStreamReader(new ClassPathResource(TEMPLATE_PATH).getInputStream())); this.amazonCloudFormationClient.createStack(new CreateStackRequest().withTemplateBody(templateBody).withOnFailure(OnFailure.DELETE). withStackName(stackName).withTags(new Tag().withKey("tag1").withValue("value1")). withParameters(new Parameter().withParameterKey("RdsPassword").withParameterValue(this.rdsPassword))); this.stackCreatedByThisInstance = true; } return getStackResources(stackName); }
public Collection<Tag> prepareTags(AuthenticatedContext ac, Map<String, String> userDefinedTags) { List<com.amazonaws.services.cloudformation.model.Tag> tags = new ArrayList<>(); tags.add(prepareTag(CLOUDBREAK_CLUSTER_TAG, ac.getCloudContext().getName())); if (!Strings.isNullOrEmpty(defaultCloudformationTag)) { tags.add(prepareTag(CLOUDBREAK_ID, defaultCloudformationTag)); } tags.addAll(Stream.concat(customTags.entrySet().stream(), userDefinedTags.entrySet().stream()) .map(entry -> prepareTag(entry.getKey(), entry.getValue())) .collect(Collectors.toList())); return tags; }
@Test public void testWhenCustomTagsDefined() { ReflectionTestUtils.setField(awsTagPreparationService, "defaultCloudformationTag", "test1"); ReflectionTestUtils.setField(awsTagPreparationService, "customCloudformationTags", Lists.asList("test2:abc", new String[]{"test3:def"})); awsTagPreparationService.init(); Collection<Tag> tags = awsTagPreparationService.prepareTags(authenticatedContext(), Maps.newHashMap()); Assert.assertEquals(4, tags.size()); }
@Test public void testWhenCustomAndUserTagsDefined() { ReflectionTestUtils.setField(awsTagPreparationService, "defaultCloudformationTag", "test1"); ReflectionTestUtils.setField(awsTagPreparationService, "customCloudformationTags", Lists.asList("test2:abc", new String[]{"test3:def"})); awsTagPreparationService.init(); Map<String, String> userDefined = Maps.newHashMap(); userDefined.put("userdefinedkey", "userdefinedvalue"); Collection<Tag> tags = awsTagPreparationService.prepareTags(authenticatedContext(), userDefined); Assert.assertEquals(5, tags.size()); }
@Test public void testWhenCustomTagsNotDefined() { ReflectionTestUtils.setField(awsTagPreparationService, "defaultCloudformationTag", "test1"); ReflectionTestUtils.setField(awsTagPreparationService, "customCloudformationTags", new ArrayList<>()); awsTagPreparationService.init(); Collection<Tag> tags = awsTagPreparationService.prepareTags(authenticatedContext(), Maps.newHashMap()); Assert.assertEquals(2, tags.size()); }
@Test public void testWhenDefaultAndCustomTagsNotDefined() { ReflectionTestUtils.setField(awsTagPreparationService, "defaultCloudformationTag", ""); ReflectionTestUtils.setField(awsTagPreparationService, "customCloudformationTags", new ArrayList<>()); awsTagPreparationService.init(); Collection<Tag> tags = awsTagPreparationService.prepareTags(authenticatedContext(), Maps.newHashMap()); Assert.assertEquals(1, tags.size()); }
public void addTagsTo(Collection<Tag> tagCollection) { if (!commentTag.isEmpty()) { addTag(tagCollection, COMMENT_TAG, commentTag); } if (indexTag.isPresent()) { addTag(tagCollection, AwsFacade.INDEX_TAG, indexTag.get().toString()); } }
private void populateEntriesIfProjectMatches(List<Stack> stacks) { logger.info(String.format("Populating stack entries for %s stacks", stacks.size())); for(Stack stack : stacks) { logger.info(String.format("Checking stack %s for tag", stack.getStackName())); List<Tag> tags = stack.getTags(); Map<String, String> keyValues = convertToMap(tags); int count = 3; String env = ""; String proj = ""; Integer build = null; for(Tag tag : tags) { String key = tag.getKey(); String value = tag.getValue(); if (key.equals(AwsFacade.ENVIRONMENT_TAG)) { env = value; count--; } else if (key.equals(AwsFacade.PROJECT_TAG)) { proj = value; count--; } else if (key.equals(AwsFacade.BUILD_TAG)) { build = Integer.parseInt(value); count--; } if (count==0) break; // small optimisation } //String index = keyValues.get(AwsFacade.INDEX_TAG); addEntryIfProjectAndEnvMatches(stack, env, proj, build, keyValues); } }
@Test public void shouldAddCommentTag() { tagging.setCommentTag("This is a comment tag"); Collection<Tag> results = getResultsAndCheckSize(); assertTrue(results.contains(EnvironmentSetupForTests.createCfnStackTAG("CFN_COMMENT", "This is a comment tag"))); }
@Test public void shouldAddIndexTag() { tagging.setIndexTag(42); Collection<Tag> results = getResultsAndCheckSize(); assertTrue(results.contains(EnvironmentSetupForTests.createCfnStackTAG("CFN_ASSIST_DELTA", "42"))); }
private com.amazonaws.services.cloudformation.model.Tag prepareTag(String key, String value) { return new com.amazonaws.services.cloudformation.model.Tag().withKey(key).withValue(value); }
private void addTag(Collection<Tag> tagCollection, String tag, String value) { logger.info(String.format("Adding %s: %s", tag, value)); tagCollection.add(createTag(tag, value)); }
private Tag createTag(String key, String value) { Tag tag = new Tag(); tag.setKey(key); tag.setValue(value); return tag; }
private HashMap<String, String> convertToMap(List<Tag> tags) { HashMap<String, String> result = new HashMap<>(); tags.forEach(tag -> result.put(tag.getKey(), tag.getValue())); return result; }
private Collection<Tag> getResultsAndCheckSize() { Collection<Tag> results = new LinkedHashSet<>(); tagging.addTagsTo(results); assertEquals(1, results.size()); return results; }
/** * Allows you to add any number of nested preconfigured StackTag elements. * * @param stackTag * a preconfigured StackTag object. */ public void addConfiguredStackTag(StackTag stackTag) { tags.add(new Tag().withKey(stackTag.getKey()).withValue( stackTag.getValue())); }