Search in sources :

Example 21 with DiskState

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;
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Operation(com.vmware.xenon.common.Operation)

Example 22 with DiskState

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);
    }));
}
Also used : ProvisionDiskTaskState(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState) Operation(com.vmware.xenon.common.Operation) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) DiskInstanceRequest(com.vmware.photon.controller.model.adapterapi.DiskInstanceRequest) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) SubStage(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) URI(java.net.URI) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) URI(java.net.URI)

Example 23 with DiskState

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);
            }
        }
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) RouterState(com.vmware.photon.controller.model.resources.RouterService.RouterState) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) LoadBalancerState(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) HashSet(java.util.HashSet) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState)

Example 24 with DiskState

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);
}
Also used : DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) TestProvisionAWSDisk.createAWSDiskState(com.vmware.photon.controller.model.adapters.awsadapter.TestProvisionAWSDisk.createAWSDiskState) Test(org.junit.Test)

Example 25 with DiskState

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;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) TestProvisionAWSDisk.createAWSDiskState(com.vmware.photon.controller.model.adapters.awsadapter.TestProvisionAWSDisk.createAWSDiskState)

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