@Test public void getNetworkInterfaceToSecurityGroupMap () { String instanceId = "this is my instance id string"; NetworkInterface networkInterface1, networkInterface2; GroupIdentifier group1a, group1b, group2a, group2b; group1a = new GroupIdentifier().withGroupId("group-1-a"); group1b = new GroupIdentifier().withGroupId("group-1-b"); group2a = new GroupIdentifier().withGroupId("group-2-a"); group2b = new GroupIdentifier().withGroupId("group-2-b"); networkInterface1 = new NetworkInterface().withNetworkInterfaceId("network-interface-a") .withGroups(group1a, group1b); networkInterface2 = new NetworkInterface().withNetworkInterfaceId("network-interface-b") .withGroups(group2a, group2b); ArgumentCaptor<DescribeNetworkInterfacesRequest> captor = ArgumentCaptor.forClass(DescribeNetworkInterfacesRequest.class); doReturn(new DescribeNetworkInterfacesResult().withNetworkInterfaces(networkInterface1, networkInterface2)).when(amazonEC2) .describeNetworkInterfaces(captor .capture()); Map<String, Set<String>> networkInterfaceToSecurityGroupsMap = awsEC2Platform.getNetworkInterfaceToSecurityGroupsMap(instanceId); assertThat(captor.getValue() .getFilters(), containsInAnyOrder(new Filter("attachment.instance-id", List.of(instanceId)))); Map expected = Map.of("network-interface-a", Set.of("group-1-a", "group-1-b"), "network-interface-b", Set.of("group-2-a", "group-2-b")); assertEquals(expected, networkInterfaceToSecurityGroupsMap); }
public Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap (String instanceId) { log.debug("Looking up security groups for instance {}", instanceId); return amazonEC2.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("attachment.instance-id", List .of(instanceId)))) .getNetworkInterfaces() .stream() .collect(Collectors.groupingBy(NetworkInterface::getNetworkInterfaceId, Collectors.flatMapping((Function<? super NetworkInterface, ? extends Stream<String>>) networkInterface -> networkInterface .getGroups() .stream() .map(GroupIdentifier::getGroupId), Collectors.toSet()))); }
AwsEC2Platform extends Platform implements SshBasedExperiment<AwsEC2Container> { public Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap (String instanceId) { log.debug("Looking up security groups for instance {}", instanceId); return amazonEC2.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("attachment.instance-id", List .of(instanceId)))) .getNetworkInterfaces() .stream() .collect(Collectors.groupingBy(NetworkInterface::getNetworkInterfaceId, Collectors.flatMapping((Function<? super NetworkInterface, ? extends Stream<String>>) networkInterface -> networkInterface .getGroups() .stream() .map(GroupIdentifier::getGroupId), Collectors.toSet()))); } }
AwsEC2Platform extends Platform implements SshBasedExperiment<AwsEC2Container> { public Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap (String instanceId) { log.debug("Looking up security groups for instance {}", instanceId); return amazonEC2.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("attachment.instance-id", List .of(instanceId)))) .getNetworkInterfaces() .stream() .collect(Collectors.groupingBy(NetworkInterface::getNetworkInterfaceId, Collectors.flatMapping((Function<? super NetworkInterface, ? extends Stream<String>>) networkInterface -> networkInterface .getGroups() .stream() .map(GroupIdentifier::getGroupId), Collectors.toSet()))); } @Autowired  AwsEC2Platform(AmazonEC2 amazonEC2, ContainerManager containerManager, AwsEC2SelfAwareness awsEC2SelfAwareness); private  AwsEC2Platform(); }
AwsEC2Platform extends Platform implements SshBasedExperiment<AwsEC2Container> { public Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap (String instanceId) { log.debug("Looking up security groups for instance {}", instanceId); return amazonEC2.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("attachment.instance-id", List .of(instanceId)))) .getNetworkInterfaces() .stream() .collect(Collectors.groupingBy(NetworkInterface::getNetworkInterfaceId, Collectors.flatMapping((Function<? super NetworkInterface, ? extends Stream<String>>) networkInterface -> networkInterface .getGroups() .stream() .map(GroupIdentifier::getGroupId), Collectors.toSet()))); } @Autowired  AwsEC2Platform(AmazonEC2 amazonEC2, ContainerManager containerManager, AwsEC2SelfAwareness awsEC2SelfAwareness); private  AwsEC2Platform(); void setFilter(@NotNull Map<String, List<String>> filter); void setGroupingTags(List<String> groupingTags); void setSSHPrivateKeys(@NotNull Map<String, String> sshPrivateKeys); Map<String, String> getImageIdToUsernameMap(); void setImageIdToUsernameMap(Map<String, String> imageIdToUsernameMap); @Override ApiStatus getApiStatus(); @JsonIgnore Collection<SubnetUtils.SubnetInfo> getRoutableCidrBlocks(); void setRoutableCidrBlocks(Collection<String> routableCidrBlocks); boolean isAddressRoutable(String privateIPAddress); @Override PlatformLevel getPlatformLevel(); @Override PlatformHealth getPlatformHealth(); @Override List<Container> generateRoster(); @Override boolean isContainerRecycled(Container container); ContainerHealth checkHealth(String instanceId); void stopInstance(String... instanceIds); void terminateInstance(String... instanceIds); void startInstance(String... instanceIds); void restartInstance(String... instanceIds); void setSecurityGroupIds(String networkInterfaceId, Collection<String> securityGroupIds); String getChaosSecurityGroupForInstance(String instanceId); boolean isContainerTerminated(String instanceId); boolean isAutoscalingGroupAtDesiredInstances(String autoScalingGroupName); boolean hasKey(String keyName); @Override void recycleContainer(AwsEC2Container container); void triggerAutoscalingUnhealthy(String instanceId); @Override String getEndpoint(AwsEC2Container container); @Override SSHCredentials getSshCredentials(AwsEC2Container container); boolean isStarted(AwsEC2Container awsEC2Container); Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap(String instanceId); boolean verifySecurityGroupIdsOfNetworkInterfaceMap(String instanceId, Map<String, Set<String>> originalSecurityGroups); }
AwsEC2Platform extends Platform implements SshBasedExperiment<AwsEC2Container> { public Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap (String instanceId) { log.debug("Looking up security groups for instance {}", instanceId); return amazonEC2.describeNetworkInterfaces(new DescribeNetworkInterfacesRequest().withFilters(new Filter("attachment.instance-id", List .of(instanceId)))) .getNetworkInterfaces() .stream() .collect(Collectors.groupingBy(NetworkInterface::getNetworkInterfaceId, Collectors.flatMapping((Function<? super NetworkInterface, ? extends Stream<String>>) networkInterface -> networkInterface .getGroups() .stream() .map(GroupIdentifier::getGroupId), Collectors.toSet()))); } @Autowired  AwsEC2Platform(AmazonEC2 amazonEC2, ContainerManager containerManager, AwsEC2SelfAwareness awsEC2SelfAwareness); private  AwsEC2Platform(); void setFilter(@NotNull Map<String, List<String>> filter); void setGroupingTags(List<String> groupingTags); void setSSHPrivateKeys(@NotNull Map<String, String> sshPrivateKeys); Map<String, String> getImageIdToUsernameMap(); void setImageIdToUsernameMap(Map<String, String> imageIdToUsernameMap); @Override ApiStatus getApiStatus(); @JsonIgnore Collection<SubnetUtils.SubnetInfo> getRoutableCidrBlocks(); void setRoutableCidrBlocks(Collection<String> routableCidrBlocks); boolean isAddressRoutable(String privateIPAddress); @Override PlatformLevel getPlatformLevel(); @Override PlatformHealth getPlatformHealth(); @Override List<Container> generateRoster(); @Override boolean isContainerRecycled(Container container); ContainerHealth checkHealth(String instanceId); void stopInstance(String... instanceIds); void terminateInstance(String... instanceIds); void startInstance(String... instanceIds); void restartInstance(String... instanceIds); void setSecurityGroupIds(String networkInterfaceId, Collection<String> securityGroupIds); String getChaosSecurityGroupForInstance(String instanceId); boolean isContainerTerminated(String instanceId); boolean isAutoscalingGroupAtDesiredInstances(String autoScalingGroupName); boolean hasKey(String keyName); @Override void recycleContainer(AwsEC2Container container); void triggerAutoscalingUnhealthy(String instanceId); @Override String getEndpoint(AwsEC2Container container); @Override SSHCredentials getSshCredentials(AwsEC2Container container); boolean isStarted(AwsEC2Container awsEC2Container); Map<String, Set<String>> getNetworkInterfaceToSecurityGroupsMap(String instanceId); boolean verifySecurityGroupIdsOfNetworkInterfaceMap(String instanceId, Map<String, Set<String>> originalSecurityGroups);  }