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);
});
}
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;
}
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;
}
}
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);
}
Aggregations