Search in sources :

Example 1 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState 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 DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class AWSDiskService method deleteDisk.

/**
 * Deletes the diskstate and the corresponding volume on aws.
 */
private void deleteDisk(AWSDiskContext context) {
    if (context.diskRequest.isMockRequest) {
        deleteDiskState(context, AwsDiskStage.FINISHED);
        return;
    }
    DiskState diskState = context.disk;
    String diskId = diskState.id;
    if (diskId == null || !diskId.startsWith(AWS_VOLUME_ID_PREFIX)) {
        String message = "disk Id cannot be empty";
        this.logSevere("[AWSDiskService] " + message);
        throw new IllegalArgumentException(message);
    }
    if (diskState.status != DiskService.DiskStatus.AVAILABLE) {
        String message = String.format("disk cannot be deleted. Current status is %s", diskState.status.name());
        this.logSevere("[AWSDiskService] " + message);
        throw new IllegalArgumentException(message);
    }
    AsyncHandler<DeleteVolumeRequest, DeleteVolumeResult> deletionHandler = new AWSDiskDeletionHandler(this, context);
    DeleteVolumeRequest deleteVolumeRequest = new DeleteVolumeRequest().withVolumeId(diskId);
    context.client.deleteVolume(deleteVolumeRequest, deletionHandler);
}
Also used : DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) DeleteVolumeResult(com.amazonaws.services.ec2.model.DeleteVolumeResult) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState)

Example 3 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class AWSInstanceService method createInstanceStoreMappings.

/**
 * Creates device mappings for the instance-store disks.
 */
private List<BlockDeviceMapping> createInstanceStoreMappings(List<DiskState> instanceStoreDisks, List<String> usedDeviceNames, List<String> usedVirtualNames, String instanceType, Integer capacityMBytes, String platform, String virtualizationType) {
    List<BlockDeviceMapping> deviceMappings = new ArrayList<>();
    if (!instanceStoreDisks.isEmpty()) {
        this.logInfo(() -> String.format("[AWSInstanceService] Ignores the size and type of the " + "additional disk. Instance-store type of additional disks are " + "provisioned with the capacity supported by %s", instanceType));
        List<String> availableDeviceNames = AWSBlockDeviceNameMapper.getAvailableNames(AWSSupportedOS.get(platform), AWSSupportedVirtualizationTypes.get(virtualizationType), AWSStorageType.INSTANCE_STORE, instanceType, usedDeviceNames);
        List<String> availableVirtualNames = getAvailableVirtualNames(usedVirtualNames);
        if (availableDeviceNames.size() >= instanceStoreDisks.size() && availableVirtualNames.size() >= instanceStoreDisks.size()) {
            for (DiskState diskState : instanceStoreDisks) {
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
                String deviceName = availableDeviceNames.get(0);
                blockDeviceMapping.setDeviceName(deviceName);
                availableDeviceNames.remove(0);
                usedDeviceNames.add(deviceName);
                String virtualName = availableVirtualNames.get(0);
                blockDeviceMapping.setVirtualName(virtualName);
                availableVirtualNames.remove(0);
                deviceMappings.add(blockDeviceMapping);
                if (diskState.customProperties == null) {
                    diskState.customProperties = new HashMap<>();
                }
                diskState.customProperties.put(DEVICE_NAME, deviceName);
                diskState.customProperties.put(DEVICE_TYPE, AWSStorageType.INSTANCE_STORE.getName());
                diskState.capacityMBytes = capacityMBytes;
            }
        } else {
            String message = "Additional instance-store disks cannot be attached. " + "Not sufficient device names are available.";
            this.logWarning(() -> "[AWSInstanceService] " + message);
            throw new IllegalArgumentException(message);
        }
    }
    return deviceMappings;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)

Example 4 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class AWSInstanceService method createEbsDeviceMappings.

/**
 * Creates the device mappings for the ebs disks.
 */
private List<BlockDeviceMapping> createEbsDeviceMappings(List<DiskState> ebsDisks, List<String> availableDiskNames) {
    List<BlockDeviceMapping> additionalDiskMappings = new ArrayList<>();
    if (availableDiskNames.size() >= ebsDisks.size()) {
        for (DiskState diskState : ebsDisks) {
            if (diskState.capacityMBytes > 0) {
                BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
                EbsBlockDevice ebsBlockDevice = new EbsBlockDevice();
                int diskSize = (int) diskState.capacityMBytes / 1024;
                ebsBlockDevice.setVolumeSize(diskSize);
                ebsBlockDevice.setDeleteOnTermination(!diskState.persistent);
                if (diskState.customProperties != null) {
                    String requestedVolumeType = diskState.customProperties.get(VOLUME_TYPE);
                    if (requestedVolumeType != null) {
                        validateSizeSupportedByVolumeType(diskSize, requestedVolumeType);
                        ebsBlockDevice.setVolumeType(requestedVolumeType);
                    }
                    String diskIops = diskState.customProperties.get(DISK_IOPS);
                    if (diskIops != null && !diskIops.isEmpty()) {
                        int iops = Integer.parseInt(diskIops);
                        if (iops > diskSize * MAX_IOPS_PER_GiB) {
                            String info = String.format("[AWSInstanceService] Requested " + "IOPS (%s) exceeds the maximum value supported" + " by %sGiB disk. Continues provisioning the " + "disk with %s iops", iops, diskSize, diskSize * MAX_IOPS_PER_GiB);
                            this.logInfo(() -> info);
                            iops = diskSize * MAX_IOPS_PER_GiB;
                        }
                        ebsBlockDevice.setIops(iops);
                    }
                }
                diskState.encrypted = diskState.encrypted == null ? false : diskState.encrypted;
                ebsBlockDevice.setEncrypted(diskState.encrypted);
                String deviceName = availableDiskNames.get(0);
                availableDiskNames.remove(0);
                blockDeviceMapping.setDeviceName(deviceName);
                blockDeviceMapping.setEbs(ebsBlockDevice);
                additionalDiskMappings.add(blockDeviceMapping);
                if (diskState.customProperties == null) {
                    diskState.customProperties = new HashMap<>();
                }
                diskState.customProperties.put(DEVICE_NAME, deviceName);
            } else {
                String message = "Additional disk size capacity has to be positive";
                this.logWarning(() -> "[AWSInstanceService] " + message);
                throw new IllegalArgumentException(message);
            }
        }
    } else {
        String message = "Additional ebs disks cannot be attached. Not sufficient " + "device names are available.";
        this.logWarning(() -> "[AWSInstanceService] " + message);
        throw new IllegalArgumentException(message);
    }
    return additionalDiskMappings;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) ArrayList(java.util.ArrayList) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) InstanceBlockDeviceMapping(com.amazonaws.services.ec2.model.InstanceBlockDeviceMapping)

Example 5 with DiskState

use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.

the class AzureComputeEnumerationAdapterService method createDataDiskState.

private DiskState createDataDiskState(EnumerationContext ctx, DataDisk dataDisk, boolean isManaged) {
    DiskState diskState = new DiskState();
    diskState.documentSelfLink = UriUtils.buildUriPath(DiskService.FACTORY_LINK, UUID.randomUUID().toString());
    diskState.name = dataDisk.name();
    if (dataDisk.diskSizeGB() != null) {
        diskState.capacityMBytes = dataDisk.diskSizeGB() * 1024;
    }
    diskState.status = DiskService.DiskStatus.ATTACHED;
    diskState.tenantLinks = ctx.parentCompute.tenantLinks;
    diskState.resourcePoolLink = ctx.request.resourcePoolLink;
    diskState.computeHostLink = ctx.parentCompute.documentSelfLink;
    diskState.endpointLink = ctx.request.endpointLink;
    AdapterUtils.addToEndpointLinks(diskState, ctx.request.endpointLink);
    diskState.customProperties = new HashMap<>();
    diskState.customProperties.put(AZURE_DATA_DISK_CACHING, dataDisk.caching().name());
    diskState.customProperties.put(DISK_CONTROLLER_NUMBER, String.valueOf(dataDisk.lun()));
    if (diskState.tagLinks == null) {
        diskState.tagLinks = new HashSet<>();
    }
    if (isManaged) {
        diskState.id = dataDisk.managedDisk().id();
        if (dataDisk.managedDisk().storageAccountType() != null) {
            diskState.customProperties.put(AZURE_MANAGED_DISK_TYPE, dataDisk.managedDisk().storageAccountType().toString());
        } else {
            // set to Standard_LRS default
            diskState.customProperties.put(AZURE_MANAGED_DISK_TYPE, StorageAccountTypes.STANDARD_LRS.toString());
        }
        diskState.tagLinks.add(ctx.managedDiskInternalTagLink);
    } else {
        diskState.id = AzureUtils.canonizeId(dataDisk.vhd().uri());
        diskState.tagLinks.add(ctx.vhdInternalTagLink);
    }
    return diskState;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState)

Aggregations

DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)77 ArrayList (java.util.ArrayList)24 Operation (com.vmware.xenon.common.Operation)23 DiskService (com.vmware.photon.controller.model.resources.DiskService)18 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)16 List (java.util.List)15 EnumerationAction (com.vmware.photon.controller.model.adapterapi.EnumerationAction)13 UriUtils (com.vmware.xenon.common.UriUtils)13 Utils (com.vmware.xenon.common.Utils)13 HashMap (java.util.HashMap)13 TimeUnit (java.util.concurrent.TimeUnit)13 QueryTask (com.vmware.xenon.services.common.QueryTask)12 Query (com.vmware.xenon.services.common.QueryTask.Query)12 HashSet (java.util.HashSet)12 Test (org.junit.Test)11 AzureConstants (com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants)10 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)10 ServiceDocumentQueryResult (com.vmware.xenon.common.ServiceDocumentQueryResult)10 AuthCredentialsService (com.vmware.xenon.services.common.AuthCredentialsService)10 Map (java.util.Map)10