EC2RaiderImpl implements EC2Raider { @Deprecated public void terminateEc2InstancesByTags(String... tagName) { List<String> instanceIds = new ArrayList<String>(); DescribeInstancesRequest request = new DescribeInstancesRequest(); Filter filter1 = new Filter("tag:Name", Arrays.asList(tagName)); DescribeInstancesResult result = ec2Delegator.getEc2().describeInstances(request.withFilters(filter1)); List<Reservation> reservations = result.getReservations(); for (Reservation reservation : reservations) { List<Instance> instances = reservation.getInstances(); for (Instance instance : instances) { instanceIds.add(instance.getInstanceId()); } } if (instanceIds.isEmpty()) { throw new ResourceNotFoundException("No Instances are avaialble"); } TerminateInstancesRequest request1 = new TerminateInstancesRequest().withInstanceIds(instanceIds); TerminateInstancesResult response = ec2Delegator.getEc2().terminateInstances(request1); } 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; }
@Test public void testTerminateEc2InstancesByTags() throws Exception{ TerminateInstancesResult response = PowerMockito.mock(TerminateInstancesResult.class); DescribeInstancesResult describeInstancesResult = PowerMockito.mock(DescribeInstancesResult.class); PowerMockito.when(ec2Delegator.getEc2().describeInstances(Mockito.anyObject())).thenReturn(describeInstancesResult); Reservation reservation = new Reservation(); ArrayList<Instance> instances = new ArrayList(); instances.add(instance); reservation.setInstances(instances); ArrayList<Reservation> reservations = new ArrayList<>(); reservations.add(reservation); PowerMockito.when(ec2Delegator.getEc2().describeInstances(Mockito.anyObject()).getReservations()).thenReturn(reservations); PowerMockito.when(ec2Delegator.getEc2().terminateInstances(Mockito.anyObject())).thenReturn(response); ec2RaiderImplUnderTest.terminateEc2InstancesByTags("service1", "service2"); }
@Test (expected = ResourceNotFoundException.class) public void testTerminateEc2InstancesByWrongTags() throws Exception{ TerminateInstancesResult response = PowerMockito.mock(TerminateInstancesResult.class); DescribeInstancesResult describeInstancesResult = PowerMockito.mock(DescribeInstancesResult.class); PowerMockito.when(ec2Delegator.getEc2().describeInstances(Mockito.anyObject())).thenReturn(describeInstancesResult); ArrayList<Reservation> reservations = new ArrayList<>(); PowerMockito.when(ec2Delegator.getEc2().describeInstances(Mockito.anyObject()).getReservations()).thenReturn(reservations); PowerMockito.when(ec2Delegator.getEc2().terminateInstances(Mockito.anyObject())).thenReturn(response); ec2RaiderImplUnderTest.terminateEc2InstancesByTags("error"); }
@Test public void testTerminateEc2InstancesByTagsAndInstanceCounts() throws Exception{ TerminateInstancesResult response = PowerMockito.mock(TerminateInstancesResult.class); PowerMockito.when(ec2Delegator.getEc2().terminateInstances(Mockito.anyObject())).thenReturn(response); ec2RaiderImplUnderTest.terminateEc2InstancesByTags("service1",1); ec2RaiderImplUnderTest.terminateEc2InstancesByTags("service1",2); }