use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureInstanceService method createDiskToUpdate.
/**
* Creates and returns a new diskState object which is updated with id, LUN, status and documentSelfLink
*/
private DeferredResult<Operation> createDiskToUpdate(AzureInstanceContext ctx, Optional<DiskState> diskOpt, DataDisk azureDataDisk) {
// update VHD uri or disk id respectively for un-managed and managed disks
DiskState diskState = diskOpt.get();
final DiskState diskStateToUpdate = new DiskState();
diskStateToUpdate.documentSelfLink = diskState.documentSelfLink;
diskStateToUpdate.persistent = diskState.persistent;
if (ctx.useManagedDisks()) {
diskStateToUpdate.id = azureDataDisk.managedDisk().id();
} else {
diskStateToUpdate.id = AzureUtils.canonizeId(azureDataDisk.vhd().uri());
}
// The LUN value of disk
if (diskStateToUpdate.customProperties == null) {
diskStateToUpdate.customProperties = new HashMap<>();
}
diskStateToUpdate.customProperties.put(DISK_CONTROLLER_NUMBER, String.valueOf(azureDataDisk.lun()));
diskStateToUpdate.status = DiskService.DiskStatus.ATTACHED;
diskStateToUpdate.regionId = ctx.provisionedVm.location();
diskStateToUpdate.endpointLink = ctx.endpoint.documentSelfLink;
AdapterUtils.addToEndpointLinks(diskStateToUpdate, ctx.endpoint.documentSelfLink);
Operation updateDiskState = Operation.createPatch(createInventoryUri(getHost(), diskStateToUpdate.documentSelfLink)).setBody(diskStateToUpdate);
DeferredResult<Operation> updateDR = ctx.service.sendWithDeferredResult(updateDiskState).whenComplete((op, exc) -> {
if (exc != null) {
logSevere(() -> String.format("Updating data DiskState [%s] with VHD URI [%s]: FAILED with %s", diskState.name, diskStateToUpdate.id, Utils.toString(exc)));
} else {
logFine(() -> String.format("Updating data DiskState [%s] with VHD URI [%s]: SUCCESS", diskState.name, diskStateToUpdate.id));
}
});
return updateDR;
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class ProvisionDiskTaskService method validateDiskAndStart.
private void validateDiskAndStart(ProvisionDiskTaskState state, Operation startPost) {
URI diskUri = UriUtils.buildUri(getHost(), state.diskLink);
sendRequest(Operation.createGet(createInventoryUri(this.getHost(), diskUri)).setCompletion((o, e) -> {
if (e != null) {
logWarning(() -> String.format("Failure retrieving disk state (%s): %s", diskUri, e.toString()));
o.complete();
failTask(e);
return;
}
DiskState disk = o.getBody(DiskState.class);
state.diskAdapterReference = disk.diskAdapterReference;
startPost.complete();
if (disk.capacityMBytes < 0) {
failTask(new IllegalArgumentException("disk capacity is mandatory for a disk"));
return;
}
if (state.taskSubStage == ProvisionDiskTaskState.SubStage.CREATING_DISK && state.diskAdapterReference == null) {
failTask(new IllegalArgumentException("diskState does not have create service specified"));
return;
}
sendSelfPatch(TaskStage.STARTED, state.taskSubStage, null);
}));
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class ResourceGroomerTaskService method populateEndpointLinksByDocumentLinks.
/**
* Helper for creating corresponding object and parsing the response for given documentKind
* to store selfLink and endpointLink.
*/
private static void populateEndpointLinksByDocumentLinks(Map<String, Object> documents, Map<String, Set<String>> endpointLinksByDocumentLinks, Map<String, String> endpointLinkByDocumentLinks) {
for (Object document : documents.values()) {
ServiceDocument doc = Utils.fromJson(document, ServiceDocument.class);
Set<String> endpointLinks = new HashSet<>();
if (doc.documentKind.equals(COMPUTE_STATE_DOCUMENT_KIND)) {
ComputeState state = Utils.fromJson(document, ComputeState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(DISK_STATE_DOCUMENT_KIND)) {
DiskState state = Utils.fromJson(document, DiskState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(COMPUTE_DESCRIPTION_DOCUMENT_KIND)) {
ComputeDescription state = Utils.fromJson(document, ComputeDescription.class);
// only deleting discovered resources
if (!(state.customProperties != null && (ResourceEnumerationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK)) || EndpointAllocationTaskService.FACTORY_LINK.equals(state.customProperties.get(SOURCE_TASK_LINK))))) {
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_STATE_DOCUMENT_KIND)) {
NetworkState state = Utils.fromJson(document, NetworkState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(NETWORK_INTERFACE_STATE_DOCUMENT_KIND)) {
NetworkInterfaceState state = Utils.fromJson(document, NetworkInterfaceState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SECURITY_GROUP_STATE_DOCUMENT_KIND)) {
SecurityGroupState state = Utils.fromJson(document, SecurityGroupState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(SUBNET_STATE_DOCUMENT_KIND)) {
SubnetState state = Utils.fromJson(document, SubnetState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(LOAD_BALANCER_DOCUMENT_KIND)) {
LoadBalancerState state = Utils.fromJson(document, LoadBalancerState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(STORAGE_DESCRIPTION_DOCUMENT_KIND)) {
StorageDescription state = Utils.fromJson(document, StorageDescription.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(RESOURCE_GROUP_DOCUMENT_KIND)) {
ResourceGroupState state = Utils.fromJson(document, ResourceGroupState.class);
if (state.customProperties != null && state.customProperties.containsKey(ResourceGroupService.PROPERTY_NAME_IS_USER_CREATED)) {
continue;
}
if (state.customProperties != null && state.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT)) {
// skip resources that have never been attached to a particular endpoint
continue;
}
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(IMAGE_STATE_KIND)) {
ImageState state = Utils.fromJson(document, ImageState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(ROUTER_STATE_KIND)) {
RouterState state = Utils.fromJson(document, RouterState.class);
if (state.endpointLinks != null) {
state.endpointLinks.remove(null);
endpointLinks.addAll(state.endpointLinks);
}
endpointLinksByDocumentLinks.put(state.documentSelfLink, endpointLinks);
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.endpointLink != null ? state.endpointLink : EMPTY_STRING);
} else if (doc.documentKind.equals(AUTH_CREDENTIALS_SERVICE_STATE_KIND)) {
AuthCredentialsServiceState state = Utils.fromJson(document, AuthCredentialsServiceState.class);
if (state.customProperties != null && state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK) != null) {
endpointLinkByDocumentLinks.put(state.documentSelfLink, state.customProperties.get(CUSTOM_PROP_ENDPOINT_LINK));
} else {
endpointLinkByDocumentLinks.put(state.documentSelfLink, EMPTY_STRING);
}
}
}
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AWSComputeDiskDay2ServiceTest method tesDiskOperationsNegativeScenarios.
/**
* This test performs the following steps in sequence.
* 1. provision a VM with one bootdisk in us-east-1a.
* 2. Create an independent disk in us-east-1b and then attach it to the VM in us-east-1a.
* 3. Attach operation should fail when run in real mode because aws throws an error. In mock mode
* attach should be successful because there is no validation in DiskDay2 service that checks the
* zone mismatch.
*/
@Test
public void tesDiskOperationsNegativeScenarios() throws Throwable {
// provisioning a vm
provisionVM(this.zoneId);
DiskState diskspec = createAWSDiskState(this.host, this.endpointState, this.currentTestName.getMethodName() + "_disk1", Boolean.TRUE, this.diskZoneId, regionId);
// create a disk
provisionSingleDisk(diskspec);
DiskTaskService.DiskTaskState.SubStage expectedTerminalState = DiskTaskService.DiskTaskState.SubStage.FINISHED;
if (!this.isMock) {
// attaching a disk in us-east-1b to a vm in us-east-1a should fail.
expectedTerminalState = DiskTaskService.DiskTaskState.SubStage.FAILED;
}
performDiskOperationAndVerify(this.vmState.documentSelfLink, Arrays.asList(diskspec.documentSelfLink), ResourceOperation.ATTACH_DISK.operation, expectedTerminalState);
this.disksToCleanUp.add(diskspec.documentSelfLink);
// VM has only boot disk.
deleteVMAndVerifyDisks(this.vmState.documentSelfLink, this.vmState.diskLinks);
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AWSComputeDiskDay2ServiceTest method detachDiskAndVerify.
private ComputeState detachDiskAndVerify(ComputeState vmStateAfterAttach, List<String> detachedDiskLinks, List<String> availableDiskLinks) throws Throwable {
performDiskOperationAndVerify(vmStateAfterAttach.documentSelfLink, detachedDiskLinks, ResourceOperation.DETACH_DISK.operation, DiskTaskService.DiskTaskState.SubStage.FINISHED);
ComputeState vmStateAfterDetach = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
assertEquals(vmStateAfterAttach.diskLinks.size() - detachedDiskLinks.size(), vmStateAfterDetach.diskLinks.size());
detachedDiskLinks.forEach(detachedDiskLink -> {
DiskState detachedDisk = this.host.getServiceState(null, DiskState.class, UriUtils.buildUri(this.host, detachedDiskLink));
assertEquals("disk status not matching", DiskService.DiskStatus.AVAILABLE, detachedDisk.status);
availableDiskLinks.add(detachedDisk.documentSelfLink);
});
return vmStateAfterDetach;
}
Aggregations