private Set<NodeMetadata> addTagsToInstancesInRegion(Map<String, String> common, Set<? extends NodeMetadata> input, String region, String group) { Map<String, ? extends NodeMetadata> instancesById = Maps.uniqueIndex(input, instanceId); ImmutableSet.Builder<NodeMetadata> builder = ImmutableSet.<NodeMetadata> builder(); if (generateInstanceNames && !common.containsKey("Name")) { for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) { String id = entry.getKey(); NodeMetadata instance = entry.getValue(); Map<String, String> tags = ImmutableMap.<String, String> builder().putAll(common) .put("Name", id.replaceAll(".*-", group + "-")).build(); logger.debug(">> applying tags %s to instance %s in region %s", tags, id, region); client.getTagApiForRegion(region).get().applyToResources(tags, ImmutableSet.of(id)); builder.add(addTagsForInstance(tags, instancesById.get(id))); } } else { Iterable<String> ids = instancesById.keySet(); logger.debug(">> applying tags %s to instances %s in region %s", common, ids, region); client.getTagApiForRegion(region).get().applyToResources(common, ids); for (NodeMetadata in : input) builder.add(addTagsForInstance(common, in)); } if (logger.isDebugEnabled()) { Multimap<String, String> filter = new TagFilterBuilder().resourceIds(instancesById.keySet()).build(); FluentIterable<Tag> tags = client.getTagApiForRegion(region).get().filter(filter); logger.debug("<< applied tags in region %s: %s", region, resourceToTagsAsMap(tags)); } return builder.build(); }
private Set<NodeMetadata> addTagsToInstancesInRegion(Map<String, String> common, Set<? extends NodeMetadata> input, String region, String group) { Map<String, ? extends NodeMetadata> instancesById = Maps.uniqueIndex(input, instanceId); ImmutableSet.Builder<NodeMetadata> builder = ImmutableSet.<NodeMetadata> builder(); if (generateInstanceNames && !common.containsKey("Name")) { for (Map.Entry<String, ? extends NodeMetadata> entry : instancesById.entrySet()) { String id = entry.getKey(); NodeMetadata instance = entry.getValue(); Map<String, String> tags = ImmutableMap.<String, String> builder().putAll(common) .put("Name", id.replaceAll(".*-", group + "-")).build(); logger.debug(">> applying tags %s to instance %s in region %s", tags, id, region); client.getTagApiForRegion(region).get().applyToResources(tags, ImmutableSet.of(id)); builder.add(addTagsForInstance(tags, instancesById.get(id))); } } else { Iterable<String> ids = instancesById.keySet(); logger.debug(">> applying tags %s to instances %s in region %s", common, ids, region); client.getTagApiForRegion(region).get().applyToResources(common, ids); for (NodeMetadata in : input) builder.add(addTagsForInstance(common, in)); } if (logger.isDebugEnabled()) { Multimap<String, String> filter = new TagFilterBuilder().resourceIds(instancesById.keySet()).build(); FluentIterable<Tag> tags = client.getTagApiForRegion(region).get().filter(filter); logger.debug("<< applied tags in region %s: %s", region, resourceToTagsAsMap(tags)); } return builder.build(); }
.put("Name", name).build(); logger.debug(">> applying tags %s to instance %s in region %s", tags, id, region); client.getTagApiForRegion(region).get().applyToResources(tags, ImmutableSet.of(id)); builder.add(addTagsForInstance(tags, instancesById.get(id))); Iterable<String> ids = instancesById.keySet(); logger.debug(">> applying tags %s to instances %s in region %s", common, ids, region); client.getTagApiForRegion(region).get().applyToResources(common, ids); for (NodeMetadata in : input) builder.add(addTagsForInstance(common, in));
@Test public void testCreate() { gateway = gwClient.createInternetGateway(TEST_REGION, InternetGatewayOptions.NONE); assertNotNull(gateway, "Gateway was not successfully created"); assertEquals(gateway.tags().size(), 0, "Freshly created gateway has tags"); assertEquals(gateway.attachmentSet().size(), 0, "Freshly created gateway is attached"); tagger.applyToResources(ImmutableMap.of("Name", simpleName), ImmutableList.of(gateway.id())); getAnonymousLogger().info("Created gateway " + simpleName + " with id " + gateway.id()); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testApplyWithValuesWhenResponseIs404() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(404).build(); EC2Api apiWhenDontExist = requestSendsResponse(applyWithValues, applyResponse); apiWhenDontExist.getTagApi().get().applyToResources(ImmutableMap.of("tag", "value"), ImmutableSet.of("i-43532")); }
@Test public void testCreateSubnetInRegion() { vpc = vpcClient.createVpc(region, "10.21.0.0/16", CreateVpcOptions.NONE); // tag the VPC for ease of identification in console if things go wrong tagApi.applyToResources(ImmutableMap.of("Name", simpleName), ImmutableList.of(vpc.id())); subnet = subnetClient.createSubnetInRegion(region, vpc.id(), "10.21.0.0/20"); assertNotNull(subnet); assertEquals(subnet.getCidrBlock(), "10.21.0.0/20"); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testApplyWhenResponseIs404() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(404).build(); EC2Api apiWhenDontExist = requestSendsResponse(apply, applyResponse); apiWhenDontExist.getTagApi().get().applyToResources(ImmutableSet.of("tag"), ImmutableSet.of("i-43532")); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testApplyWithValuesWhenResponseIs404() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(404).build(); EC2Api apiWhenDontExist = requestSendsResponse(applyWithValues, applyResponse); apiWhenDontExist.getTagApi().get().applyToResources(ImmutableMap.of("tag", "value"), ImmutableSet.of("i-43532")); }
public void testApplyWhenResponseIs2xx() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(200).build(); EC2Api apiWhenExist = requestSendsResponse(apply, applyResponse); apiWhenExist.getTagApi().get().applyToResources(ImmutableSet.of("tag"), ImmutableSet.of("i-43532")); }
@Test(expectedExceptions = ResourceNotFoundException.class) public void testApplyWhenResponseIs404() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(404).build(); EC2Api apiWhenDontExist = requestSendsResponse(apply, applyResponse); apiWhenDontExist.getTagApi().get().applyToResources(ImmutableSet.of("tag"), ImmutableSet.of("i-43532")); }
public void testApplyWithValuesWhenResponseIs2xx() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(200).build(); EC2Api apiWhenExist = requestSendsResponse(applyWithValues, applyResponse); apiWhenExist.getTagApi().get().applyToResources(ImmutableMap.of("tag", "value"), ImmutableSet.of("i-43532")); }
public void testApplyWhenResponseIs2xx() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(200).build(); EC2Api apiWhenExist = requestSendsResponse(apply, applyResponse); apiWhenExist.getTagApi().get().applyToResources(ImmutableSet.of("tag"), ImmutableSet.of("i-43532")); }
public void testApplyWithValuesWhenResponseIs2xx() throws Exception { HttpResponse applyResponse = HttpResponse.builder().statusCode(200).build(); EC2Api apiWhenExist = requestSendsResponse(applyWithValues, applyResponse); apiWhenExist.getTagApi().get().applyToResources(ImmutableMap.of("tag", "value"), ImmutableSet.of("i-43532")); }
public void testApplyTag() { api().applyToResources(ImmutableSet.of("foo"), ImmutableSet.of(resource.id)); tag = api().filter(new TagFilterBuilder().resourceId(resource.id).key("foo").build()).get(0); Logger.getAnonymousLogger().info("created tag: " + tag); assertEquals(tag.getKey(), "foo"); assertEquals(tag.getResourceId(), resource.id); assertEquals(tag.getResourceType(), resource.type); assertFalse(tag.getValue().isPresent()); }
@Test(dependsOnMethods = "testCreate") public void testAttach() { vpc = vpcClient.createVpc(TEST_REGION, "10.20.30.0/24", CreateVpcOptions.NONE); assertNotNull(vpc, "Failed to create VPC to test attachments"); tagger.applyToResources(ImmutableMap.of("Name", simpleName), ImmutableList.of(vpc.id())); final Boolean attached = gwClient.attachInternetGateway(TEST_REGION, gateway.id(), vpc.id()); assertTrue(attached, "Gateway " + gateway.id() + " failed to attach to VPC " + vpc.id()); }
public void testApplyTagWithValue() { api().applyToResources(ImmutableMap.of("type", "bar"), ImmutableSet.of(resource.id)); tag2 = api().filter(new TagFilterBuilder().resourceId(resource.id).key("type").build()).get(0); Logger.getAnonymousLogger().info("created tag: " + tag2); assertEquals(tag2.getKey(), "type"); assertEquals(tag2.getResourceId(), resource.id); assertEquals(tag2.getResourceType(), resource.type); assertEquals(tag2.getValue().get(), "bar"); }
public void testApplyTagWithValue() { api().applyToResources(ImmutableMap.of("type", "bar"), ImmutableSet.of(resource.id)); tag2 = api().filter(new TagFilterBuilder().resourceId(resource.id).key("type").build()).get(0); Logger.getAnonymousLogger().info("created tag: " + tag2); assertEquals(tag2.getKey(), "type"); assertEquals(tag2.getResourceId(), resource.id); assertEquals(tag2.getResourceType(), resource.type); assertEquals(tag2.getValue().get(), "bar"); }
public void testApplyTag() { api().applyToResources(ImmutableSet.of("foo"), ImmutableSet.of(resource.id)); tag = api().filter(new TagFilterBuilder().resourceId(resource.id).key("foo").build()).get(0); Logger.getAnonymousLogger().info("created tag: " + tag); assertEquals(tag.getKey(), "foo"); assertEquals(tag.getResourceId(), resource.id); assertEquals(tag.getResourceType(), resource.type); assertFalse(tag.getValue().isPresent()); }
@Test(dependsOnMethods = "testDescribe") public void testCreate() { // When you create a new route table for the VPC it automatically gets a route to match the VPC CIDR routeTable = routeTableApi.createRouteTable(TEST_REGION, vpc.id()); assertNotNull(routeTable, "Gateway was not successfully created"); assertEquals(routeTable.vpcId(), vpc.id(), "RouteTable VPC ID " + routeTable.vpcId() + " does not match VPC's ID " + vpc.id()); final List<Route> routes = routeTable.routeSet(); assertEquals(routes.size(), 1, "Unexpected number of routes in new table: " + routes.size()); assertEquals(routes.get(0).destinationCidrBlock(), vpc.cidrBlock(), "CIDR for route table " + routes.get(0).destinationCidrBlock() + " does not match VPC CIDR" + vpc.cidrBlock()); assertEquals(routes.get(0).state(), Route.RouteState.ACTIVE, "Route should be active"); assertEquals(routeTable.tags().size(), 0, "Freshly created routeTable has tags"); tagger.applyToResources(ImmutableMap.of("Name", simpleName), ImmutableList.of(routeTable.id())); getAnonymousLogger().info("Created routeTable " + simpleName + " with id " + routeTable.id()); }
@Test public void testDescribe() { vpc = vpcClient.createVpc(TEST_REGION, VPC_CIDR); assertNotNull(vpc, "Failed to create VPC to test attachments"); tagger.applyToResources(ImmutableMap.of("Name", simpleName), ImmutableList.of(vpc.id())); // When you create a VPC it automatically gets a route table whose single route has the CIDR of the VPC // and whose "target" is "local". final FluentIterable<RouteTable> routeTables = routeTableApi.describeRouteTables(TEST_REGION); assertNotNull(routeTables, "Failed to return list of RouteTables"); Optional<RouteTable> vpcRT = Iterables.tryFind(routeTables, new Predicate<RouteTable>() { @Override public boolean apply(RouteTable input) { return vpc.id().equals(input.vpcId()); } }); assertTrue(vpcRT.isPresent(), "Could not find VPC " + vpc.id() + " in described route tables"); RouteTable rt = vpcRT.get(); assertEquals(rt.associationSet().size(), 1, "Route for test VPC has wrong number of associations, should be 1: " + rt.associationSet()); assertTrue(rt.associationSet().get(0).main(), "Association for route " + rt.id() + "should be 'main'"); assertEquals(rt.routeSet().size(), 1, "Wrong number of routes in default route table for VPC " + vpc.id()); final String defaultCidr = rt.routeSet().get(0).destinationCidrBlock(); assertEquals(defaultCidr, vpc.cidrBlock(), "Route in default route table does not match CIDR of VPC, " + defaultCidr + " should be " + vpc.cidrBlock()); }