Search in sources :

Example 1 with EbsInstanceBlockDevice

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());
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) Test(org.junit.Test)

Example 2 with EbsInstanceBlockDevice

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;
    }
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) AmazonServiceException(com.amazonaws.AmazonServiceException) NotFoundException(com.netflix.simianarmy.NotFoundException)

Example 3 with EbsInstanceBlockDevice

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());
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) Test(org.junit.Test)

Example 4 with EbsInstanceBlockDevice

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"))));
}
Also used : Instance(com.amazonaws.services.ec2.model.Instance) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) CreateTagsRequest(com.amazonaws.services.ec2.model.CreateTagsRequest) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Tag(com.amazonaws.services.cloudformation.model.Tag) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) Test(org.junit.Test)

Example 5 with EbsInstanceBlockDevice

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());
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) EC2UnexpectedException(com.amazonaws.services.lambda.model.EC2UnexpectedException) EbsInstanceBlockDevice(com.amazonaws.services.ec2.model.EbsInstanceBlockDevice) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) WaiterParameters(com.amazonaws.waiters.WaiterParameters) LoggerFactory(org.slf4j.LoggerFactory) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) BooleanUtils(org.apache.commons.lang3.BooleanUtils) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) EC2Client(com.sequenceiq.it.cloudbreak.util.aws.amazonec2.client.EC2Client) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) TestFailException(com.sequenceiq.it.cloudbreak.exception.TestFailException) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest) Volume(com.amazonaws.services.ec2.model.Volume) InstanceState(com.amazonaws.services.ec2.model.InstanceState) Instance(com.amazonaws.services.ec2.model.Instance) WaiterUnrecoverableException(com.amazonaws.waiters.WaiterUnrecoverableException) SdxUtil(com.sequenceiq.it.cloudbreak.util.SdxUtil) Reservation(com.amazonaws.services.ec2.model.Reservation) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) Logger(org.slf4j.Logger) Collection(java.util.Collection) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) Log(com.sequenceiq.it.cloudbreak.log.Log) Set(java.util.Set) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) WaiterTimedOutException(com.amazonaws.waiters.WaiterTimedOutException) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) List(java.util.List) Component(org.springframework.stereotype.Component) PollingStrategy(com.amazonaws.waiters.PollingStrategy) Tag(com.amazonaws.services.ec2.model.Tag) Reservation(com.amazonaws.services.ec2.model.Reservation) Set(java.util.Set) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)6 EbsInstanceBlockDevice (com.amazonaws.services.ec2.model.EbsInstanceBlockDevice)5 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)5 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)4 Reservation (com.amazonaws.services.ec2.model.Reservation)4 ArrayList (java.util.ArrayList)4 CreateTagsRequest (com.amazonaws.services.ec2.model.CreateTagsRequest)3 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 Test (org.junit.Test)3 InstanceState (com.amazonaws.services.ec2.model.InstanceState)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 Tag (com.amazonaws.services.cloudformation.model.Tag)1 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeVolumesRequest (com.amazonaws.services.ec2.model.DescribeVolumesRequest)1 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)1 GroupIdentifier (com.amazonaws.services.ec2.model.GroupIdentifier)1 InstanceNetworkInterface (com.amazonaws.services.ec2.model.InstanceNetworkInterface)1 StateReason (com.amazonaws.services.ec2.model.StateReason)1