use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class TestVSphereDiskService method testDiskCreateAndDeleteWithNoDatastore.
@Test
public void testDiskCreateAndDeleteWithNoDatastore() throws Throwable {
prepareEnvironment();
DiskState diskState = createDisk("AdditionalDisk1", DiskService.DiskType.HDD, ADDITIONAL_DISK_SIZE, buildCustomProperties());
testDiskCreateAndDelete(diskState);
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createDiskStateHelper.
private DeferredResult<Operation> createDiskStateHelper(StorageEnumContext context, ListBlobItem blob, DiskState oldDiskState) {
try {
// Associate blob with the resource group state (container) it belongs to
String containerId = blob.getContainer().getUri().toString();
Query.Builder qBuilder = newResourceGroupQueryBuilder(context).addFieldClause(ResourceGroupState.FIELD_NAME_ID, containerId);
QueryTop<ResourceGroupState> queryLocalStates = new QueryTop<>(getHost(), qBuilder.build(), ResourceGroupState.class, context.parentCompute.tenantLinks, null, /* endpointLink */
context.parentCompute.documentSelfLink).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
return queryLocalStates.collectLinks(Collectors.toSet()).thenCompose(rgLinks -> {
logFine(() -> String.format("Found %d matching resource groups", rgLinks.size()));
String containerLink = null;
if (rgLinks.size() > 0) {
containerLink = rgLinks.iterator().next();
if (rgLinks.size() > 1) {
logWarning(() -> String.format("Found multiple instances of the same" + " resource group %s", containerId));
}
if (oldDiskState != null) {
if (rgLinks.contains(oldDiskState.storageDescriptionLink)) {
containerLink = oldDiskState.storageDescriptionLink;
}
}
}
DiskState diskState = createDiskStateObject(context, blob, containerLink, oldDiskState);
if (oldDiskState != null) {
return updateDiskState(context, diskState);
} else {
return createDiskState(context, diskState);
}
});
} catch (URISyntaxException e) {
logWarning(() -> String.format("Could not set storage description link for blob: %s", blob.getUri().toString()));
} catch (StorageException e) {
logWarning(() -> String.format("Could not set storage description link for disk blob: %s", blob.getUri().toString()));
}
return DeferredResult.completed(null);
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createDiskStateObject.
private DiskState createDiskStateObject(StorageEnumContext context, ListBlobItem blob, String containerLink, DiskState oldDiskState) {
DiskState diskState = new DiskState();
diskState.name = getAzureBlobName(blob);
if (containerLink != null) {
diskState.storageDescriptionLink = containerLink;
}
diskState.resourcePoolLink = context.request.resourcePoolLink;
diskState.computeHostLink = context.parentCompute.documentSelfLink;
diskState.endpointLink = context.request.endpointLink;
if (oldDiskState != null) {
// first copy the existing ones
diskState.endpointLinks = oldDiskState.endpointLinks;
}
// then append new one
AdapterUtils.addToEndpointLinks(diskState, context.request.endpointLink);
diskState.tenantLinks = context.parentCompute.tenantLinks;
long bLength = 0;
if (blob instanceof CloudBlob) {
CloudBlob blobItem = (CloudBlob) blob;
bLength = blobItem.getProperties().getLength();
}
diskState.status = DiskService.DiskStatus.AVAILABLE;
diskState.capacityMBytes = bLength / B_TO_MB_FACTOR;
diskState.customProperties = new HashMap<>();
String storageType = isDisk(diskState.name) ? AZURE_STORAGE_DISKS : AZURE_STORAGE_BLOBS;
diskState.storageType = storageType;
if (oldDiskState != null) {
diskState.tagLinks = (oldDiskState.tagLinks == null) ? new HashSet<>() : oldDiskState.tagLinks;
} else {
diskState.tagLinks = new HashSet<>();
}
switch(storageType) {
case AZURE_STORAGE_DISKS:
if (context.internalVhdTypeTag != null) {
diskState.tagLinks.add(context.internalVhdTypeTag);
}
break;
case AZURE_STORAGE_BLOBS:
if (context.internalVhdTypeTag != null) {
diskState.tagLinks.add(context.internalBlobTypeTag);
}
break;
default:
break;
}
// following two properties are set to defaults - can't retrieve that information from
// existing calls
diskState.type = DEFAULT_DISK_TYPE;
if (oldDiskState != null) {
if (diskState.storageDescriptionLink == null) {
diskState.storageDescriptionLink = oldDiskState.storageDescriptionLink;
}
diskState.id = oldDiskState.id;
diskState.documentSelfLink = oldDiskState.documentSelfLink;
diskState.regionId = oldDiskState.regionId;
} else {
diskState.id = canonizeId(blob.getUri().toString());
StorageAccount storageAccount = context.storageAccountBlobUriMap.get(diskState.id);
if (storageAccount != null) {
diskState.regionId = storageAccount.location;
}
diskState.documentSelfLink = UUID.randomUUID().toString();
}
return diskState;
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method disassociateDiskStates.
/*
* Disassociate local disk states that no longer exist in Azure
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the blob discovered as part of get blob call.
*
* A disassociate on a disk state is invoked only if it meets two criteria: - Timestamp older
* than current enumeration cycle. - blob is not present on Azure.
*/
private void disassociateDiskStates(StorageEnumContext context, StorageEnumStages next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_COMPUTE_HOST_LINK, context.parentCompute.documentSelfLink).addRangeClause(DiskState.FIELD_NAME_UPDATE_TIME_MICROS, QueryTask.NumericRange.createLessThanRange(context.enumerationStartTimeInMicros));
Query.Builder typeFilterQuery = Query.Builder.create(Occurance.MUST_OCCUR);
Query blobFilter = Query.Builder.create(Occurance.SHOULD_OCCUR).addFieldClause(AZURE_STORAGE_TYPE, AZURE_STORAGE_BLOBS).build();
QueryTask.Query diskFilter = QueryTask.Query.Builder.create(QueryTask.Query.Occurance.SHOULD_OCCUR).addFieldClause(AZURE_STORAGE_TYPE, AZURE_STORAGE_DISKS).build();
typeFilterQuery.addClause(blobFilter);
typeFilterQuery.addClause(diskFilter);
qBuilder.addClause(typeFilterQuery.build());
QueryByPages<DiskState> queryLocalStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, context.parentCompute.tenantLinks, context.request.endpointLink).setMaxPageSize(getQueryResultLimit());
queryLocalStates.setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
List<DeferredResult<Operation>> ops = new ArrayList<>();
queryLocalStates.queryDocuments(ds -> {
if (context.blobIds.contains(ds.id)) {
return;
}
ops.add(disassociateIfNotAttachedToCompute(context, ds));
}).thenCompose(r -> DeferredResult.allOf(ops)).whenComplete((r, e) -> {
logFine(() -> "Finished disassociation of disk states for Azure");
context.subStage = next;
handleSubStage(context);
});
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureInstanceService method updateDiskStates.
/**
* Update {@code computeState.diskState[i].id} with Azure Disks' VHD URI.
*/
private DeferredResult<AzureInstanceContext> updateDiskStates(AzureInstanceContext ctx) {
if (ctx.provisionedVm == null) {
// Do nothing.
return DeferredResult.completed(ctx);
}
List<DeferredResult<Operation>> diskStateDRs = new ArrayList<>();
// Update boot DiskState with Azure osDisk VHD URI in case of unmanaged disks and
// disks id in case of managed disks
{
final OSDisk azureOsDisk = ctx.provisionedVm.storageProfile().osDisk();
final DiskState diskStateToUpdate = new DiskState();
diskStateToUpdate.documentSelfLink = ctx.bootDiskState.documentSelfLink;
diskStateToUpdate.persistent = ctx.bootDiskState.persistent;
diskStateToUpdate.regionId = ctx.provisionedVm.location();
diskStateToUpdate.endpointLink = ctx.endpoint.documentSelfLink;
AdapterUtils.addToEndpointLinks(diskStateToUpdate, ctx.endpoint.documentSelfLink);
// The actual value being updated
if (ctx.useManagedDisks()) {
diskStateToUpdate.id = azureOsDisk.managedDisk().id();
} else {
diskStateToUpdate.id = AzureUtils.canonizeId(azureOsDisk.vhd().uri());
}
diskStateToUpdate.status = DiskService.DiskStatus.ATTACHED;
Operation updateDiskState = Operation.createPatch(ctx.service, diskStateToUpdate.documentSelfLink).setBody(diskStateToUpdate);
DeferredResult<Operation> updateDR = ctx.service.sendWithDeferredResult(updateDiskState).whenComplete((op, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Updating boot DiskState [%s] with VHD URI [%s]: FAILED with %s", ctx.bootDiskState.name, diskStateToUpdate.id, Utils.toString(exc)));
} else {
logFine(() -> String.format("Updating boot DiskState [%s] with VHD URI [%s]: SUCCESS", ctx.bootDiskState.name, diskStateToUpdate.id));
}
});
diskStateDRs.add(updateDR);
}
for (DataDisk azureDataDisk : ctx.provisionedVm.storageProfile().dataDisks()) {
// Find corresponding DiskState by name
Optional<DiskState> dataDiskOpt = ctx.dataDiskStates.stream().map(DiskState.class::cast).filter(dS -> azureDataDisk.name().equals(dS.name)).findFirst();
Optional<DiskState> externalDiskOpt = ctx.externalDataDisks.stream().map(DiskState.class::cast).filter(dS -> azureDataDisk.name().equals(dS.name)).findFirst();
// update disk state
if (dataDiskOpt.isPresent()) {
diskStateDRs.add(createDiskToUpdate(ctx, dataDiskOpt, azureDataDisk));
} else if (externalDiskOpt.isPresent()) {
diskStateDRs.add(createDiskToUpdate(ctx, externalDiskOpt, azureDataDisk));
} else {
// additional disks were created by the custom image. Will always be of
// managed disk type. Create new disk state.
DiskState diskStateToCreate = new DiskState();
diskStateToCreate.id = azureDataDisk.managedDisk().id();
diskStateToCreate.name = azureDataDisk.name();
diskStateToCreate.regionId = ctx.provisionedVm.location();
diskStateToCreate.customProperties = new HashMap<>();
diskStateToCreate.customProperties.put(DISK_CONTROLLER_NUMBER, String.valueOf(azureDataDisk.lun()));
if (azureDataDisk.managedDisk().storageAccountType() != null) {
diskStateToCreate.customProperties.put(AZURE_MANAGED_DISK_TYPE, azureDataDisk.managedDisk().storageAccountType().toString());
} else {
// set to Standard_LRS default
diskStateToCreate.customProperties.put(AZURE_MANAGED_DISK_TYPE, StorageAccountTypes.STANDARD_LRS.toString());
}
diskStateToCreate.customProperties.put(AZURE_DATA_DISK_CACHING, azureDataDisk.caching().toString());
if (azureDataDisk.diskSizeGB() != null) {
diskStateToCreate.capacityMBytes = azureDataDisk.diskSizeGB() * 1024;
}
diskStateToCreate.status = DiskService.DiskStatus.ATTACHED;
diskStateToCreate.persistent = ctx.bootDiskState.persistent;
diskStateToCreate.endpointLink = ctx.endpoint.documentSelfLink;
AdapterUtils.addToEndpointLinks(diskStateToCreate, ctx.endpoint.documentSelfLink);
Operation createDiskState = Operation.createPost(ctx.service, DiskService.FACTORY_LINK).setBody(diskStateToCreate);
DeferredResult<Operation> createDR = ctx.service.sendWithDeferredResult(createDiskState).whenComplete((op, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Creating data DiskState [%s] with disk id [%s]: FAILED " + "with %s", azureDataDisk.name(), azureDataDisk.managedDisk().id(), Utils.toString(exc)));
} else {
logFine(() -> String.format("Creating data DiskState [%s] with disk id [%s]: SUCCESS", azureDataDisk.name(), azureDataDisk.managedDisk().id()));
// update compute state with data disks present on custom image
ComputeState cs = new ComputeState();
List<String> disksLinks = new ArrayList<>();
DiskState diskState = op.getBody(DiskState.class);
disksLinks.add(diskState.documentSelfLink);
cs.diskLinks = disksLinks;
Operation.CompletionHandler completionHandler = (ox, ex) -> {
if (ex != null) {
handleError(ctx, ex);
return;
}
};
sendRequest(Operation.createPatch(ctx.computeRequest.resourceReference).setBody(cs).setCompletion(completionHandler));
}
});
diskStateDRs.add(createDR);
}
}
return DeferredResult.allOf(diskStateDRs).thenApply(ignored -> ctx);
}
Aggregations