@Test public void testGetNonFilteredActiveInstances() { List<EC2InstanceTO> ec2InstanceTOList = ec2RaiderImplUnderTest.getFilteredActiveInstances(new ArrayList<>()); Assert.assertFalse(ec2InstanceTOList.isEmpty()); Assert.assertEquals(ec2InstanceTOList.get(0).getInstanceId(), "1234"); Assert.assertEquals(ec2InstanceTOList.get(0).getPrivateIpAddress(), "10.1.1.1"); Assert.assertEquals(ec2InstanceTOList.get(0).getAvailabilityZone(), "us-west-2c"); }
@Override public List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords) { DescribeInstancesRequest request = new DescribeInstancesRequest(); ArrayList<String> vals = new ArrayList<>(); vals.add("running"); Filter filter = new Filter("instance-state-name", vals); List<Reservation> res = ec2Delegator.getEc2().describeInstances(request.withFilters(filter)).getReservations(); List<String> ids = new ArrayList<>(); for (Reservation reservation : res) { List<Instance> instances = reservation.getInstances(); for (Instance in : instances) { List<Tag> tags = in.getTags(); Optional<Tag> name = tags.stream().filter(y -> y.getKey().equalsIgnoreCase("name")).findFirst(); boolean flag = true; for (String word : filteredWords) { if (name.isPresent() && name.get().getValue().toLowerCase().contains(word.toLowerCase())) { flag = false; break; } } if (flag) { ids.add(in.getInstanceId()); } } } return getEC2InstancesByIds(ids.stream().distinct().collect(Collectors.toList())); }
EC2RaiderImpl implements EC2Raider { @Override public List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords) { DescribeInstancesRequest request = new DescribeInstancesRequest(); ArrayList<String> vals = new ArrayList<>(); vals.add("running"); Filter filter = new Filter("instance-state-name", vals); List<Reservation> res = ec2Delegator.getEc2().describeInstances(request.withFilters(filter)).getReservations(); List<String> ids = new ArrayList<>(); for (Reservation reservation : res) { List<Instance> instances = reservation.getInstances(); for (Instance in : instances) { List<Tag> tags = in.getTags(); Optional<Tag> name = tags.stream().filter(y -> y.getKey().equalsIgnoreCase("name")).findFirst(); boolean flag = true; for (String word : filteredWords) { if (name.isPresent() && name.get().getValue().toLowerCase().contains(word.toLowerCase())) { flag = false; break; } } if (flag) { ids.add(in.getInstanceId()); } } } return getEC2InstancesByIds(ids.stream().distinct().collect(Collectors.toList())); } }
EC2RaiderImpl implements EC2Raider { @Override public List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords) { DescribeInstancesRequest request = new DescribeInstancesRequest(); ArrayList<String> vals = new ArrayList<>(); vals.add("running"); Filter filter = new Filter("instance-state-name", vals); List<Reservation> res = ec2Delegator.getEc2().describeInstances(request.withFilters(filter)).getReservations(); List<String> ids = new ArrayList<>(); for (Reservation reservation : res) { List<Instance> instances = reservation.getInstances(); for (Instance in : instances) { List<Tag> tags = in.getTags(); Optional<Tag> name = tags.stream().filter(y -> y.getKey().equalsIgnoreCase("name")).findFirst(); boolean flag = true; for (String word : filteredWords) { if (name.isPresent() && name.get().getValue().toLowerCase().contains(word.toLowerCase())) { flag = false; break; } } if (flag) { ids.add(in.getInstanceId()); } } } return getEC2InstancesByIds(ids.stream().distinct().collect(Collectors.toList())); } EC2RaiderImpl(); }
EC2RaiderImpl implements EC2Raider { @Override public List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords) { DescribeInstancesRequest request = new DescribeInstancesRequest(); ArrayList<String> vals = new ArrayList<>(); vals.add("running"); Filter filter = new Filter("instance-state-name", vals); List<Reservation> res = ec2Delegator.getEc2().describeInstances(request.withFilters(filter)).getReservations(); List<String> ids = new ArrayList<>(); for (Reservation reservation : res) { List<Instance> instances = reservation.getInstances(); for (Instance in : instances) { List<Tag> tags = in.getTags(); Optional<Tag> name = tags.stream().filter(y -> y.getKey().equalsIgnoreCase("name")).findFirst(); boolean flag = true; for (String word : filteredWords) { if (name.isPresent() && name.get().getValue().toLowerCase().contains(word.toLowerCase())) { flag = false; break; } } if (flag) { ids.add(in.getInstanceId()); } } } return getEC2InstancesByIds(ids.stream().distinct().collect(Collectors.toList())); } EC2RaiderImpl(); @Override List<EC2InstanceTO> getEc2InstanceIdsWithCompulsoryTagsForAvailabilityZone(String availabilityZone, List<Tag> ignoreTags, List<Tag> compulsoryTags); @Override List<EC2InstanceTO> getEc2InstancesForAvailabilityZone(String availabilityZone, List<String> instanceIdsToIgnore); @Override List<String> getEc2InstanceIPsForAvailabilityZone(String availabilityZone, List<String> instanceIdsToIgnore); @Override List<String> getInstancesIPsForAZWithName(String name, String availabilityZone); @Override List<String> getInstanceIdsForTags(List<Tag> tags); @Override List<EC2InstanceTO> getInstancesByName(String name); @Override void terminateEc2InstancesById(String... instanceIds); @Override void terminateEc2InstancesById(String instanceId); @Override void terminateEc2InstancesById(List<String> instanceIds); @Override void terminateEC2InstancesByName(String... names); @Override void terminateEC2InstancesByName(String name, int numberOfInstances); @Override List<EC2InstanceTO> getInstancesFromAnyTags(List<Tag> tags); @Override EC2InstanceTO getEC2InstanceById(String instanceId); @Override List<EC2InstanceTO> getEC2InstancesByIds(List<String> instanceIds); @Override List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords); @Override void stopEc2Instances(String instanceId); @Override void restartEc2Instances(String instanceId); @Override String getInstanceStatusById(String instanceId); @Override void detachSecurityGroup(String instanceId, String securityGroup); @Override void attachSecurityGroup(String instanceId, String securityGroup); @Override List<String> getSecurityGroups(String instanceId); @Override void detachSecurityGroups(String instanceId, String... securityGroups); @Override void attachSecurityGroups(String instanceId, String... securityGroups); @Override Instance getInstanceDetailsById(String instanceId); @Override EC2Status getInstanceStatus(String name); @Override Map getEC2InstanceState(String name); @Deprecated List getInstancesIpsForAZ(String tag, String availabilityZoneName); @Deprecated List<EC2InstanceTO> getInstancesIdsForOneTag(String tagName); @Deprecated void terminateEc2InstancesByTags(String... tagName); @Deprecated void terminateEc2InstancesByTags(String tagName, int numberOfInstances); @Deprecated List<EC2InstanceTO> getInstancesForAZ(String tag, String availabilityZoneName); @Deprecated EC2InstanceTO getEC2StatusByTag(String tag); }
EC2RaiderImpl implements EC2Raider { @Override public List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords) { DescribeInstancesRequest request = new DescribeInstancesRequest(); ArrayList<String> vals = new ArrayList<>(); vals.add("running"); Filter filter = new Filter("instance-state-name", vals); List<Reservation> res = ec2Delegator.getEc2().describeInstances(request.withFilters(filter)).getReservations(); List<String> ids = new ArrayList<>(); for (Reservation reservation : res) { List<Instance> instances = reservation.getInstances(); for (Instance in : instances) { List<Tag> tags = in.getTags(); Optional<Tag> name = tags.stream().filter(y -> y.getKey().equalsIgnoreCase("name")).findFirst(); boolean flag = true; for (String word : filteredWords) { if (name.isPresent() && name.get().getValue().toLowerCase().contains(word.toLowerCase())) { flag = false; break; } } if (flag) { ids.add(in.getInstanceId()); } } } return getEC2InstancesByIds(ids.stream().distinct().collect(Collectors.toList())); } EC2RaiderImpl(); @Override List<EC2InstanceTO> getEc2InstanceIdsWithCompulsoryTagsForAvailabilityZone(String availabilityZone, List<Tag> ignoreTags, List<Tag> compulsoryTags); @Override List<EC2InstanceTO> getEc2InstancesForAvailabilityZone(String availabilityZone, List<String> instanceIdsToIgnore); @Override List<String> getEc2InstanceIPsForAvailabilityZone(String availabilityZone, List<String> instanceIdsToIgnore); @Override List<String> getInstancesIPsForAZWithName(String name, String availabilityZone); @Override List<String> getInstanceIdsForTags(List<Tag> tags); @Override List<EC2InstanceTO> getInstancesByName(String name); @Override void terminateEc2InstancesById(String... instanceIds); @Override void terminateEc2InstancesById(String instanceId); @Override void terminateEc2InstancesById(List<String> instanceIds); @Override void terminateEC2InstancesByName(String... names); @Override void terminateEC2InstancesByName(String name, int numberOfInstances); @Override List<EC2InstanceTO> getInstancesFromAnyTags(List<Tag> tags); @Override EC2InstanceTO getEC2InstanceById(String instanceId); @Override List<EC2InstanceTO> getEC2InstancesByIds(List<String> instanceIds); @Override List<EC2InstanceTO> getFilteredActiveInstances(List<String> filteredWords); @Override void stopEc2Instances(String instanceId); @Override void restartEc2Instances(String instanceId); @Override String getInstanceStatusById(String instanceId); @Override void detachSecurityGroup(String instanceId, String securityGroup); @Override void attachSecurityGroup(String instanceId, String securityGroup); @Override List<String> getSecurityGroups(String instanceId); @Override void detachSecurityGroups(String instanceId, String... securityGroups); @Override void attachSecurityGroups(String instanceId, String... securityGroups); @Override Instance getInstanceDetailsById(String instanceId); @Override EC2Status getInstanceStatus(String name); @Override Map getEC2InstanceState(String name); @Deprecated List getInstancesIpsForAZ(String tag, String availabilityZoneName); @Deprecated List<EC2InstanceTO> getInstancesIdsForOneTag(String tagName); @Deprecated void terminateEc2InstancesByTags(String... tagName); @Deprecated void terminateEc2InstancesByTags(String tagName, int numberOfInstances); @Deprecated List<EC2InstanceTO> getInstancesForAZ(String tag, String availabilityZoneName); @Deprecated EC2InstanceTO getEC2StatusByTag(String tag); static final String AVAILABILITY_ZONE_FILTER_KEY; }