com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping Java Examples

The following examples show how to use com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EC2ClientActions.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public List<String> getInstanceVolumeIds(List<String> instanceIds) {
    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 -> !"/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());
}
 
Example #2
Source File: AwsTaggingServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@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"))
    ));
}
 
Example #3
Source File: AwsTaggingServiceTest.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #4
Source File: AwsInstanceProcess.java    From primecloud-controller with GNU General Public License v2.0 4 votes vote down vote up
public void createTag(AwsProcessClient awsProcessClient, Long instanceNo) {
    // Eucalyptusの場合はタグを付けない
    PlatformAws platformAws = awsProcessClient.getPlatformAws();
    if (BooleanUtils.isTrue(platformAws.getEuca())) {
        return;
    }

    Instance instance = instanceDao.read(instanceNo);
    AwsInstance awsInstance = awsInstanceDao.read(instanceNo);
    User user = userDao.read(awsProcessClient.getUserNo());
    Farm farm = farmDao.read(instance.getFarmNo());

    // インスタンスにタグを追加する
    List<Tag> tags = new ArrayList<Tag>();
    tags.add(new Tag("Name", instance.getFqdn()));
    tags.add(new Tag("UserName", user.getUsername()));
    tags.add(new Tag("CloudName", farm.getDomainName()));
    tags.add(new Tag("ServerName", instance.getFqdn()));
    awsCommonProcess.createTag(awsProcessClient, awsInstance.getInstanceId(), tags);

    com.amazonaws.services.ec2.model.Instance instance2 = awsCommonProcess.describeInstance(awsProcessClient,
            awsInstance.getInstanceId());

    // EBSにタグを追加する
    for (InstanceBlockDeviceMapping mapping : instance2.getBlockDeviceMappings()) {
        if (mapping.getEbs() == null) {
            continue;
        }

        String deviceName = mapping.getDeviceName();
        if (deviceName.lastIndexOf("/") != -1) {
            deviceName = deviceName.substring(deviceName.lastIndexOf("/") + 1);
        }

        tags = new ArrayList<Tag>();
        tags.add(new Tag("Name", instance.getFqdn() + "_" + deviceName));
        tags.add(new Tag("UserName", user.getUsername()));
        tags.add(new Tag("CloudName", farm.getDomainName()));
        tags.add(new Tag("ServerName", instance.getFqdn()));
        awsCommonProcess.createTag(awsProcessClient, mapping.getEbs().getVolumeId(), tags);
    }
}
 
Example #5
Source File: InstanceImpl.java    From aws-sdk-java-resources with Apache License 2.0 4 votes vote down vote up
@Override
public List<InstanceBlockDeviceMapping> getBlockDeviceMappings() {
    return (List<InstanceBlockDeviceMapping>)
            resource.getAttribute("BlockDeviceMappings");
}
 
Example #6
Source File: AwsTaggingService.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private Optional<InstanceBlockDeviceMapping> getRootVolumeId(com.amazonaws.services.ec2.model.Instance instance) {
    return instance.getBlockDeviceMappings().stream().filter(mapping -> mapping.getDeviceName().equals(instance.getRootDeviceName())).findFirst();
}
 
Example #7
Source File: AwsTaggingServiceTest.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
@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());
}
 
Example #8
Source File: Instance.java    From aws-sdk-java-resources with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the value of the BlockDeviceMappings attribute. If this resource is
 * not yet loaded, a call to {@code load()} is made to retrieve the value of
 * the attribute.
 */
List<InstanceBlockDeviceMapping> getBlockDeviceMappings();