protected SecurityGroup refresh(final RegionAndName securityGroup) { return Iterables.getOnlyElement(client.getSecurityGroupApi().get().describeSecurityGroupsInRegionWithFilter(securityGroup.getRegion(), ImmutableMultimap.of("group-name", securityGroup.getName()))); } }
@Override public String apply(String input) { checkNotNull(input, "input"); String[] parts = AWSUtils.parseHandle(input); String region = parts[0]; String name = parts[1]; return Iterables.getOnlyElement(api.getSecurityGroupApi().get() .describeSecurityGroupsInRegionWithFilter(region, of("group-name", name))).getId(); } }
@Override public String apply(String input) { checkNotNull(input, "input"); String[] parts = AWSUtils.parseHandle(input); String region = parts[0]; String name = parts[1]; return Iterables.getOnlyElement(api.getSecurityGroupApi().get() .describeSecurityGroupsInRegionWithFilter(region, of("group-name", name))).getId(); } }
protected SecurityGroup refresh(final RegionAndName securityGroup) { return Iterables.getOnlyElement(client.getSecurityGroupApi().get().describeSecurityGroupsInRegionWithFilter(securityGroup.getRegion(), ImmutableMultimap.of("group-name", securityGroup.getName()))); } }
/** * @throws IllegalStateException If the security group was in use */ @VisibleForTesting void deleteSecurityGroup(String region, String group) { checkNotNull(emptyToNull(region), "region must be defined"); checkNotNull(emptyToNull(group), "group must be defined"); String groupName = namingConvention.create().sharedNameForGroup(group); Multimap<String, String> securityGroupFilterByName = ImmutableMultimap.of("group-name", groupName); Set<SecurityGroup> securityGroupsToDelete = client.getSecurityGroupApi().get() .describeSecurityGroupsInRegionWithFilter(region, securityGroupFilterByName); if (securityGroupsToDelete.size() > 1) { logger.warn("When trying to delete security group %s found more than one matching the name. Will delete all - %s.", group, securityGroupsToDelete); } for (SecurityGroup securityGroup : securityGroupsToDelete) { logger.debug(">> deleting securityGroup(%s)", groupName); client.getSecurityGroupApi().get().deleteSecurityGroupInRegionById(region, securityGroup.getId()); securityGroupMap.invalidate(new RegionNameAndIngressRules(region, groupName, null, false, null)); logger.debug("<< deleted securityGroup(%s)", groupName); } }
/** * @throws IllegalStateException If the security group was in use */ @VisibleForTesting void deleteSecurityGroup(String region, String group) { checkNotNull(emptyToNull(region), "region must be defined"); checkNotNull(emptyToNull(group), "group must be defined"); String groupName = namingConvention.create().sharedNameForGroup(group); Multimap<String, String> securityGroupFilterByName = ImmutableMultimap.of("group-name", groupName); Set<SecurityGroup> securityGroupsToDelete = client.getSecurityGroupApi().get() .describeSecurityGroupsInRegionWithFilter(region, securityGroupFilterByName); if (securityGroupsToDelete.size() > 1) { logger.warn("When trying to delete security group %s found more than one matching the name. Will delete all - %s.", group, securityGroupsToDelete); } for (SecurityGroup securityGroup : securityGroupsToDelete) { logger.debug(">> deleting securityGroup(%s)", groupName); client.getSecurityGroupApi().get().deleteSecurityGroupInRegionById(region, securityGroup.getId()); securityGroupMap.invalidate(new RegionNameAndIngressRules(region, groupName, null, false, null)); logger.debug("<< deleted securityGroup(%s)", groupName); } }
@Test(expectedExceptions = AWSResponseException.class) void testFilterInvalid() { for (String region : ec2Api.getConfiguredRegions()) { Set<SecurityGroup> allResults = client.describeSecurityGroupsInRegion(region); assertNotNull(allResults); if (!allResults.isEmpty()) { final SecurityGroup group = getLast(allResults); // in case there are multiple groups with the same name, which is the case with VPC ImmutableSet<SecurityGroup> expected = FluentIterable.from(allResults) .filter(new Predicate<SecurityGroup>() { @Override public boolean apply(SecurityGroup in) { return group.getName().equals(in.getName()); } }).toSet(); ImmutableSet<SecurityGroup> result = ImmutableSet.copyOf(client.describeSecurityGroupsInRegionWithFilter(region, ImmutableMultimap.<String, String>builder() .put("invalid-filter", group.getName()).build())); } } }
public void testFilterWhenResponseIs404() { HttpResponse filterResponse = HttpResponse.builder().statusCode(404).build(); EC2Api apiWhenNotExist = requestsSendResponses(describeRegionsRequest, describeRegionsResponse, filter, filterResponse); assertEquals(apiWhenNotExist.getSecurityGroupApi().get().describeSecurityGroupsInRegionWithFilter("us-east-1", ImmutableMultimap.<String, String>builder() .put("owner-id", "993194456877") .build()), ImmutableSet.of()); }
@Test void testFilter() { for (String region : ec2Api.getConfiguredRegions()) { Set<SecurityGroup> allResults = client.describeSecurityGroupsInRegion(region); assertNotNull(allResults); if (!allResults.isEmpty()) { final SecurityGroup group = getLast(allResults); // in case there are multiple groups with the same name, which is the case with VPC ImmutableSet<SecurityGroup> expected = FluentIterable.from(allResults) .filter(new Predicate<SecurityGroup>() { @Override public boolean apply(SecurityGroup in) { return group.getName().equals(in.getName()); } }).toSet(); ImmutableSet<SecurityGroup> result = ImmutableSet.copyOf(client.describeSecurityGroupsInRegionWithFilter(region, ImmutableMultimap.<String, String>builder() .put("group-name", group.getName()).build())); // the above command has a chance of returning less groups than the original assertTrue(expected.containsAll(result), "group(s) for name not found"); } } }
public void testFilterWhenResponseIs2xx() { HttpResponse filterResponse = HttpResponse.builder().statusCode(200) .payload(payloadFromResourceWithContentType("/describe_securitygroups_extension_single.xml", "text/xml")).build(); EC2Api apiWhenExist = requestsSendResponses(describeRegionsRequest, describeRegionsResponse, filter, filterResponse); SecurityGroup group = getOnlyElement(apiWhenExist.getSecurityGroupApi().get().describeSecurityGroupsInRegionWithFilter("us-east-1", ImmutableMultimap.<String, String>builder() .put("owner-id", "993194456877") .build())); assertEquals(group.getId(), "sg-3c6ef654"); }