use of com.amazonaws.services.ec2.model.EbsInstanceBlockDevice in project cloudbreak by hortonworks.
the class AwsTaggingServiceTest method tesTagRootVolumesForInstancesMoreThanSingleRequestLimitAndNotAllVolumesFound.
@Test
public void tesTagRootVolumesForInstancesMoreThanSingleRequestLimitAndNotAllVolumesFound() {
int instanceCount = 1200;
CloudResource instance = CloudResource.builder().type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build();
Instance awsInstance = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sda1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
Instance awsInstanceWithInvalidRootDisk = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sdb1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
List<CloudResource> instanceList = new ArrayList<>(instanceCount);
for (int i = 0; i < instanceCount; i++) {
instanceList.add(instance);
}
List<Instance> awsInstances = new ArrayList<>(instanceCount);
for (int i = 0; i < 1100; i++) {
awsInstances.add(awsInstance);
}
for (int i = 0; i < 100; i++) {
awsInstances.add(awsInstanceWithInvalidRootDisk);
}
DescribeInstancesResult describeResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(awsInstances));
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
when(ec2Client.describeInstances(any())).thenReturn(describeResult);
awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, instanceList, Map.of());
verify(ec2Client, times(2)).createTags(tagRequestCaptor.capture());
List<CreateTagsRequest> requests = tagRequestCaptor.getAllValues();
assertEquals(1000, requests.get(0).getResources().size());
assertEquals(100, requests.get(1).getResources().size());
}
use of com.amazonaws.services.ec2.model.EbsInstanceBlockDevice in project SimianArmy by Netflix.
the class AWSClient method listAttachedVolumes.
@Override
public List<String> listAttachedVolumes(String instanceId, boolean includeRoot) {
Validate.notEmpty(instanceId);
LOGGER.info(String.format("Listing volumes attached to instance %s in region %s.", instanceId, region));
try {
List<String> volumeIds = new ArrayList<String>();
for (Instance instance : describeInstances(instanceId)) {
String rootDeviceName = instance.getRootDeviceName();
for (InstanceBlockDeviceMapping ibdm : instance.getBlockDeviceMappings()) {
EbsInstanceBlockDevice ebs = ibdm.getEbs();
if (ebs == null) {
continue;
}
String volumeId = ebs.getVolumeId();
if (Strings.isNullOrEmpty(volumeId)) {
continue;
}
if (!includeRoot && rootDeviceName != null && rootDeviceName.equals(ibdm.getDeviceName())) {
continue;
}
volumeIds.add(volumeId);
}
}
return volumeIds;
} catch (AmazonServiceException e) {
if (e.getErrorCode().equals("InvalidInstanceID.NotFound")) {
throw new NotFoundException("AWS instance " + instanceId + " not found", e);
}
throw e;
}
}
use of com.amazonaws.services.ec2.model.EbsInstanceBlockDevice in project cloudbreak by hortonworks.
the class AwsTaggingServiceTest method tesTagRootVolumesForInstancesMoreThanSingleRequestLimit.
@Test
public void tesTagRootVolumesForInstancesMoreThanSingleRequestLimit() {
int instanceCount = 1200;
CloudResource instance = CloudResource.builder().type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build();
Instance awsInstance = new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sda1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1");
List<CloudResource> instanceList = new ArrayList<>(instanceCount);
List<Instance> awsInstances = new ArrayList<>(instanceCount);
for (int i = 0; i < instanceCount; i++) {
instanceList.add(instance);
awsInstances.add(awsInstance);
}
DescribeInstancesResult describeResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(awsInstances));
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
when(ec2Client.describeInstances(any())).thenReturn(describeResult);
awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, instanceList, Map.of());
verify(ec2Client, times(2)).createTags(tagRequestCaptor.capture());
List<CreateTagsRequest> requests = tagRequestCaptor.getAllValues();
assertEquals(1000, requests.get(0).getResources().size());
assertEquals(200, requests.get(1).getResources().size());
}
use of com.amazonaws.services.ec2.model.EbsInstanceBlockDevice in project cloudbreak by hortonworks.
the class AwsTaggingServiceTest method tesTagRootVolumesForSingleInstance.
@Test
public void tesTagRootVolumesForSingleInstance() {
CloudResource instance = CloudResource.builder().type(ResourceType.AWS_INSTANCE).instanceId(INSTANCE_ID).name(INSTANCE_ID).status(CommonStatus.CREATED).build();
DescribeInstancesResult describeResult = new DescribeInstancesResult().withReservations(new Reservation().withInstances(new Instance().withInstanceId(INSTANCE_ID).withBlockDeviceMappings(new InstanceBlockDeviceMapping().withDeviceName("/dev/sda1").withEbs(new EbsInstanceBlockDevice().withVolumeId(VOLUME_ID))).withRootDeviceName("/dev/sda1")));
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
when(ec2Client.describeInstances(any())).thenReturn(describeResult);
Map<String, String> userTags = Map.of("key1", "val1", "key2", "val2");
awsTaggingService.tagRootVolumes(authenticatedContext(), ec2Client, List.of(instance), userTags);
verify(ec2Client, times(1)).createTags(tagRequestCaptor.capture());
CreateTagsRequest request = tagRequestCaptor.getValue();
assertEquals(1, request.getResources().size());
assertEquals(VOLUME_ID, request.getResources().get(0));
List<com.amazonaws.services.ec2.model.Tag> tags = request.getTags();
assertThat(tags, containsInAnyOrder(hasProperty("key", Matchers.is("key1")), hasProperty("key", Matchers.is("key2"))));
assertThat(tags, containsInAnyOrder(hasProperty("value", Matchers.is("val1")), hasProperty("value", Matchers.is("val2"))));
}
use of com.amazonaws.services.ec2.model.EbsInstanceBlockDevice in project cloudbreak by hortonworks.
the class EC2ClientActions method getInstanceVolumeIds.
public List<String> getInstanceVolumeIds(List<String> instanceIds, boolean rootVolumes) {
AmazonEC2 ec2Client = buildEC2Client();
DescribeInstancesResult describeInstancesResult = ec2Client.describeInstances(new DescribeInstancesRequest().withInstanceIds(instanceIds));
Map<String, Set<String>> instanceIdVolumeIdMap = describeInstancesResult.getReservations().stream().map(Reservation::getInstances).flatMap(Collection::stream).collect(Collectors.toMap(Instance::getInstanceId, instance -> instance.getBlockDeviceMappings().stream().filter(dev -> {
if (BooleanUtils.isTrue(rootVolumes)) {
return "/dev/xvda".equals(dev.getDeviceName());
} else {
return !"/dev/xvda".equals(dev.getDeviceName());
}
}).map(InstanceBlockDeviceMapping::getEbs).map(EbsInstanceBlockDevice::getVolumeId).collect(Collectors.toSet())));
instanceIdVolumeIdMap.forEach((instanceId, volumeIds) -> Log.log(LOGGER, format(" Attached volume IDs are %s for [%s] EC2 instance ", volumeIds.toString(), instanceId)));
return instanceIdVolumeIdMap.values().stream().flatMap(Collection::stream).collect(Collectors.toList());
}
Aggregations