Search in sources :

Example 1 with InstanceBlockDeviceMapping

use of com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping in project photon-model by vmware.

the class AWSComputeStateCreationAdapterService method collectEbsDiskLinks.

/**
 * Collect EBS selfLinks by querying for all EBS disk IDs. Map these selfLinks to
 * their respective instances. These will be used for reconciliation with local state while
 * creating/updating compute state's diskLinks.
 */
private void collectEbsDiskLinks(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
    List<String> diskIds = new ArrayList<>();
    Map<String, Instance> instancesByDiskIds = new HashMap<>();
    context.request.instancesToBeCreated.stream().forEach(instance -> {
        instance.getBlockDeviceMappings().stream().forEach(instanceBlockDeviceMapping -> {
            String id = instanceBlockDeviceMapping.getEbs().getVolumeId();
            instancesByDiskIds.put(id, instance);
            diskIds.add(id);
        });
    });
    context.request.instancesToBeUpdated.values().stream().forEach(instance -> {
        instance.getBlockDeviceMappings().stream().forEach(instanceBlockDeviceMapping -> {
            String id = instanceBlockDeviceMapping.getEbs().getVolumeId();
            instancesByDiskIds.put(id, instance);
            diskIds.add(id);
        });
    });
    // No disks found for current page of instances.
    if (diskIds.isEmpty()) {
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
        return;
    }
    Query ebsQuery = Query.Builder.create().addKindFieldClause(DiskState.class).addCollectionItemClause(ResourceState.FIELD_NAME_ENDPOINT_LINKS, context.request.endpointLink).addInClause(DiskState.FIELD_NAME_ID, diskIds).build();
    QueryTask ebsQueryTask = QueryTask.Builder.createDirectTask().setQuery(ebsQuery).addOption(QueryOption.EXPAND_CONTENT).build();
    ebsQueryTask.tenantLinks = context.request.tenantLinks;
    QueryUtils.startInventoryQueryTask(this, ebsQueryTask).whenComplete((qrt, e) -> {
        if (e != null) {
            logWarning("Error querying diskLinks for endpoint %s", context.request.endpointLink);
            context.creationStage = next;
            handleComputeStateCreateOrUpdate(context);
            return;
        }
        if (qrt.results != null && qrt.results.documentCount > 0) {
            qrt.results.documents.entrySet().forEach(entry -> {
                DiskState state = Utils.fromJson(entry.getValue(), DiskState.class);
                Instance instance = instancesByDiskIds.get(state.id);
                if (instance != null) {
                    if (context.diskLinksByInstances.containsKey(instance)) {
                        context.diskLinksByInstances.get(instance).add(entry.getKey());
                    } else {
                        context.diskLinksByInstances.put(instance, new ArrayList<>(Arrays.asList(entry.getKey())));
                    }
                }
            });
        }
        context.creationStage = next;
        handleComputeStateCreateOrUpdate(context);
    });
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) AWSEnumerationUtils.getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery) Instance(com.amazonaws.services.ec2.model.Instance) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList)

Example 2 with InstanceBlockDeviceMapping

use of com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping in project photon-model by vmware.

the class AWSComputeDiskDay2Service method getAvailableDeviceName.

private String getAvailableDeviceName(DiskContext context, String instanceId) {
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest().withInstanceIds(instanceId);
    DescribeInstancesResult instancesResult = context.amazonEC2Client.describeInstances(describeInstancesRequest);
    List<InstanceBlockDeviceMapping> blockDeviceMappings = null;
    AWSSupportedOS platform = null;
    AWSSupportedVirtualizationTypes virtualizationTypes = null;
    String instanceType = null;
    for (Reservation reservation : instancesResult.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (instance.getInstanceId().equals(instanceId)) {
                blockDeviceMappings = instance.getBlockDeviceMappings();
                platform = AWSSupportedOS.get(instance.getPlatform());
                virtualizationTypes = AWSSupportedVirtualizationTypes.get(instance.getVirtualizationType());
                instanceType = instance.getInstanceType();
                break;
            }
        }
    }
    String deviceName = null;
    if (blockDeviceMappings != null) {
        List<String> usedDeviceNames = getUsedDeviceNames(blockDeviceMappings);
        List<String> availableDiskNames = AWSBlockDeviceNameMapper.getAvailableNames(platform, virtualizationTypes, AWSStorageType.EBS, instanceType, usedDeviceNames);
        deviceName = availableDiskNames.get(0);
    }
    return deviceName;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) AWSSupportedVirtualizationTypes(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) AWSSupportedOS(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS)

Example 3 with InstanceBlockDeviceMapping

use of com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping 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 4 with InstanceBlockDeviceMapping

use of com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping in project photon-model by vmware.

the class TestAWSProvisionTask method getVolume.

protected Volume getVolume(AmazonEC2AsyncClient client, Instance awsInstance, String deviceName) {
    InstanceBlockDeviceMapping bootDiskMapping = awsInstance.getBlockDeviceMappings().stream().filter(blockDeviceMapping -> blockDeviceMapping.getDeviceName().equals(deviceName)).findAny().orElse(null);
    // The ami used in this test is an ebs-backed AMI
    assertNotNull("Device type should be ebs type", bootDiskMapping.getEbs());
    String bootVolumeId = bootDiskMapping.getEbs().getVolumeId();
    DescribeVolumesRequest describeVolumesRequest = new DescribeVolumesRequest().withVolumeIds(bootVolumeId);
    DescribeVolumesResult describeVolumesResult = client.describeVolumes(describeVolumesRequest);
    return describeVolumesResult.getVolumes().get(0);
}
Also used : InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeVolumesRequest(com.amazonaws.services.ec2.model.DescribeVolumesRequest)

Aggregations

Instance (com.amazonaws.services.ec2.model.Instance)3 InstanceBlockDeviceMapping (com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1 DescribeVolumesRequest (com.amazonaws.services.ec2.model.DescribeVolumesRequest)1 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)1 Reservation (com.amazonaws.services.ec2.model.Reservation)1 NotFoundException (com.netflix.simianarmy.NotFoundException)1 AWSSupportedOS (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedOS)1 AWSSupportedVirtualizationTypes (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AWSSupportedVirtualizationTypes)1 AWSEnumerationUtils.getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSEnumerationUtils.getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery)1 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)1 QueryTask (com.vmware.xenon.services.common.QueryTask)1 Query (com.vmware.xenon.services.common.QueryTask.Query)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1