Search in sources :

Example 71 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureInstanceService method getVMDisks.

/**
 * Method will retrieve disks for targeted image
 */
private void getVMDisks(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
    if (ctx.child.diskLinks == null || ctx.child.diskLinks.size() == 0) {
        handleError(ctx, new IllegalStateException("a minimum of 1 disk is required"));
        return;
    }
    Collection<Operation> operations = new ArrayList<>();
    // iterate thru disks and create operations
    operations.addAll(ctx.child.diskLinks.stream().map(disk -> Operation.createGet(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.getHost(), disk)))).collect(Collectors.toList()));
    OperationJoin operationJoin = OperationJoin.create(operations).setCompletion((ops, exc) -> {
        if (exc != null) {
            handleError(ctx, new IllegalStateException("Error getting disk information", exc.values().iterator().next()));
            return;
        }
        ctx.dataDiskStates = new ArrayList<>();
        ctx.externalDataDisks = new ArrayList<>();
        ctx.persistentDisks = new ArrayList<>();
        for (Operation op : ops.values()) {
            DiskService.DiskStateExpanded disk = op.getBody(DiskService.DiskStateExpanded.class);
            // We treat the first disk in the boot order as the boot disk.
            if (disk.bootOrder != null && disk.bootOrder == 1) {
                if (ctx.bootDiskState != null) {
                    handleError(ctx, new IllegalStateException("Only 1 boot disk is allowed"));
                    return;
                }
                ctx.bootDiskState = disk;
                // default to false
                if (ctx.bootDiskState.persistent == null) {
                    ctx.bootDiskState.persistent = false;
                }
            } else {
                if (disk.status == DiskService.DiskStatus.AVAILABLE) {
                    // external disks are always
                    disk.persistent = true;
                    // persistent
                    ctx.externalDataDisks.add(disk);
                } else {
                    if (disk.persistent == null) {
                        // Default to non persistent disk
                        disk.persistent = false;
                    }
                    ctx.dataDiskStates.add(disk);
                }
            }
        }
        if (ctx.bootDiskState == null) {
            handleError(ctx, new IllegalStateException("Boot disk is required"));
            return;
        }
        if (!validateDiskStates(ctx)) {
            handleError(ctx, new IllegalStateException("Azure VMs can either " + "have managed disk or un-managed disk. Not Both."));
            return;
        }
        handleAllocation(ctx, nextStage);
    });
    operationJoin.sendWith(this);
}
Also used : OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) DiskService(com.vmware.photon.controller.model.resources.DiskService)

Example 72 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class AzureInstanceService method updateComputeState.

/**
 * Update {@code computeState.address} with Public IP, if presented.
 */
private DeferredResult<AzureInstanceContext> updateComputeState(AzureInstanceContext ctx) {
    if (ctx.getPrimaryNic().publicIP == null) {
        // Do nothing.
        return DeferredResult.completed(ctx);
    }
    ComputeState computeState = new ComputeState();
    computeState.address = ctx.getPrimaryNic().publicIP.ipAddress();
    computeState.name = ctx.vmName;
    Operation updateCS = Operation.createPatch(ctx.computeRequest.resourceReference).setBody(computeState);
    return ctx.service.sendWithDeferredResult(updateCS).thenApply(ignore -> {
        logFine(() -> String.format("Updating Compute state [%s] with Public IP [%s]: SUCCESS", ctx.vmName, computeState.address));
        return ctx;
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Operation(com.vmware.xenon.common.Operation)

Example 73 with Operation

use of com.vmware.xenon.common.Operation 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 74 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class EndpointAllocationTaskService method getAdapterUri.

/**
 * Retrieves the Adapter Reference from the Photon Adapter Registry
 *
 * @return DeferredResult<URI> URI can be null
 */
private DeferredResult<URI> getAdapterUri(AdapterTypePath adapterTypePath, String endpointType) {
    // Use 'endpointType' (such as aws, azure) as AdapterConfig id/selfLink!
    String configLink = UriUtils.buildUriPath(PhotonModelAdaptersConfigAccessService.SELF_LINK, endpointType);
    Operation getConfigOp = Operation.createGet(this, configLink);
    return sendWithDeferredResult(getConfigOp, PhotonModelAdapterConfig.class).thenApply(adapterConfig -> {
        String ref = null;
        if (adapterConfig != null && adapterConfig.adapterEndpoints != null) {
            ref = adapterConfig.adapterEndpoints.get(adapterTypePath.key);
        }
        final URI uri = ref != null ? UriUtils.buildUri(ref) : null;
        logInfo(() -> String.format("[getAdapterUri] adapterRef = %s: SUCCESS", uri));
        return uri;
    });
}
Also used : Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig)

Example 75 with Operation

use of com.vmware.xenon.common.Operation in project photon-model by vmware.

the class EndpointAllocationTaskService method createEndpoint.

private void createEndpoint(EndpointAllocationTaskState currentState) {
    List<String> createdDocumentLinks = new ArrayList<>();
    EndpointState es = currentState.endpointState;
    Map<String, String> endpointProperties = currentState.endpointState.endpointProperties;
    es.endpointProperties = null;
    if (es.documentSelfLink == null) {
        es.documentSelfLink = UriUtils.buildUriPath(EndpointService.FACTORY_LINK, this.getHost().nextUUID());
    }
    // merge endpoint and task tenant links
    if (es.tenantLinks == null || es.tenantLinks.isEmpty()) {
        es.tenantLinks = currentState.tenantLinks;
    } else if (currentState.tenantLinks != null) {
        currentState.tenantLinks.forEach(tl -> {
            if (!es.tenantLinks.contains(tl)) {
                es.tenantLinks.add(tl);
            }
        });
    }
    Operation endpointOp = Operation.createPost(this, EndpointService.FACTORY_LINK);
    ComputeDescription computeDescription = configureDescription(currentState, es);
    ComputeState computeState = configureCompute(currentState, es, endpointProperties);
    Operation cdOp = createComputeDescriptionOp(currentState, computeDescription.documentSelfLink);
    Operation compOp = createComputeStateOp(currentState, computeState.documentSelfLink);
    // pool link
    if (currentState.enumerationRequest != null && currentState.enumerationRequest.resourcePoolLink != null) {
        es.resourcePoolLink = currentState.enumerationRequest.resourcePoolLink;
        computeState.resourcePoolLink = es.resourcePoolLink;
    }
    OperationSequence sequence;
    if (es.authCredentialsLink == null) {
        AuthCredentialsServiceState auth = configureAuth(es);
        Operation authOp = Operation.createPost(createInventoryUri(this.getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
        sequence = OperationSequence.create(authOp).setCompletion((ops, exs) -> {
            if (exs != null) {
                long firstKey = exs.keySet().iterator().next();
                exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create auth credentials: %s", ex.getMessage())));
                sendFailurePatch(this, currentState, exs.get(firstKey));
                return;
            }
            Operation o = ops.get(authOp.getId());
            AuthCredentialsServiceState authState = o.getBody(AuthCredentialsServiceState.class);
            computeDescription.authCredentialsLink = authState.documentSelfLink;
            es.authCredentialsLink = authState.documentSelfLink;
            cdOp.setBody(computeDescription);
        }).next(cdOp);
    } else {
        cdOp.setBody(computeDescription);
        sequence = OperationSequence.create(cdOp);
    }
    sequence = sequence.setCompletion((ops, exs) -> {
        if (exs != null) {
            long firstKey = exs.keySet().iterator().next();
            exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute description: %s", ex.getMessage())));
            sendFailurePatch(this, currentState, exs.get(firstKey));
            return;
        }
        Operation o = ops.get(cdOp.getId());
        ComputeDescription desc = o.getBody(ComputeDescription.class);
        if (!currentState.accountAlreadyExists) {
            createdDocumentLinks.add(desc.documentSelfLink);
        }
        computeState.descriptionLink = desc.documentSelfLink;
        es.computeDescriptionLink = desc.documentSelfLink;
    });
    // Don't create resource pool, if a resource pool link was passed.
    if (es.resourcePoolLink == null) {
        Operation poolOp = createResourcePoolOp(es);
        sequence = sequence.next(poolOp).setCompletion((ops, exs) -> {
            if (exs != null) {
                long firstKey = exs.keySet().iterator().next();
                exs.values().forEach(ex -> logWarning(() -> String.format("Error creating resource" + " pool: %s", ex.getMessage())));
                sendFailurePatch(this, currentState, exs.get(firstKey));
                return;
            }
            Operation o = ops.get(poolOp.getId());
            ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
            createdDocumentLinks.add(poolState.documentSelfLink);
            es.resourcePoolLink = poolState.documentSelfLink;
            computeState.resourcePoolLink = es.resourcePoolLink;
            compOp.setBody(computeState);
        });
    } else {
        Operation getPoolOp = Operation.createGet(this, es.resourcePoolLink);
        sequence = sequence.next(getPoolOp).setCompletion((ops, exs) -> {
            if (exs != null) {
                long firstKey = exs.keySet().iterator().next();
                exs.values().forEach(ex -> logWarning(() -> String.format("Error retrieving resource" + " pool: %s", ex.getMessage())));
                sendFailurePatch(this, currentState, exs.get(firstKey));
                return;
            }
            Operation o = ops.get(getPoolOp.getId());
            ResourcePoolState poolState = o.getBody(ResourcePoolState.class);
            if (poolState.customProperties != null) {
                String endpointLink = poolState.customProperties.get(ENDPOINT_LINK_PROP_NAME);
                if (endpointLink != null && endpointLink.equals(es.documentSelfLink)) {
                    sendFailurePatch(this, currentState, new IllegalStateException("Passed resource pool is associated with a different endpoint."));
                    return;
                }
            }
            es.resourcePoolLink = poolState.documentSelfLink;
            computeState.resourcePoolLink = es.resourcePoolLink;
            compOp.setBody(computeState);
        });
    }
    sequence.next(compOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            long firstKey = exs.keySet().iterator().next();
            exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create compute state: %s", ex.getMessage())));
            sendFailurePatch(this, currentState, exs.get(firstKey));
            return;
        }
        Operation csOp = ops.get(compOp.getId());
        ComputeState c = csOp.getBody(ComputeState.class);
        if (!currentState.accountAlreadyExists) {
            createdDocumentLinks.add(c.documentSelfLink);
        }
        es.computeLink = c.documentSelfLink;
        endpointOp.setBody(es);
    }).next(endpointOp).setCompletion((ops, exs) -> {
        if (exs != null) {
            long firstKey = exs.keySet().iterator().next();
            exs.values().forEach(ex -> logWarning(() -> String.format("Error in " + "sequence to create endpoint state: %s", ex.getMessage())));
            sendFailurePatch(this, currentState, exs.get(firstKey));
            return;
        }
        Operation esOp = ops.get(endpointOp.getId());
        EndpointState endpoint = esOp.getBody(EndpointState.class);
        createdDocumentLinks.add(endpoint.documentSelfLink);
        // propagate the endpoint properties to the next stage
        endpoint.endpointProperties = endpointProperties;
        EndpointAllocationTaskState state = createUpdateSubStageTask(SubStage.INVOKE_ADAPTER);
        state.endpointState = endpoint;
        state.createdDocumentLinks = createdDocumentLinks;
        sendSelfPatch(state);
    }).sendWith(this);
}
Also used : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) CertificateInfoServiceErrorResponse(com.vmware.photon.controller.model.support.CertificateInfoServiceErrorResponse) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) ServiceDocument(com.vmware.xenon.common.ServiceDocument) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) Map(java.util.Map) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) ResourcePoolService(com.vmware.photon.controller.model.resources.ResourcePoolService) URI(java.net.URI) SINGLE_ASSIGNMENT(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SINGLE_ASSIGNMENT) EnumSet(java.util.EnumSet) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) ENDPOINT_LINK_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.ENDPOINT_LINK_PROP_NAME) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) ServiceHost(com.vmware.xenon.common.ServiceHost) PhotonModelAdaptersConfigAccessService(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersConfigAccessService) OPTIONAL(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.OPTIONAL) List(java.util.List) RequestType(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.RequestType) TaskUtils.sendFailurePatch(com.vmware.photon.controller.model.tasks.TaskUtils.sendFailurePatch) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) TaskState(com.vmware.xenon.common.TaskState) TaskService(com.vmware.xenon.services.common.TaskService) AdapterTypePath(com.vmware.photon.controller.model.UriPaths.AdapterTypePath) STORE_ONLY(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption.STORE_ONLY) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) OperationSequence(com.vmware.xenon.common.OperationSequence) SERVICE_USE(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption.SERVICE_USE) ResourceEnumerationTaskState(com.vmware.photon.controller.model.tasks.ResourceEnumerationTaskService.ResourceEnumerationTaskState) Operation(com.vmware.xenon.common.Operation) CertificateInfo(com.vmware.photon.controller.model.support.CertificateInfo) ScheduledTaskState(com.vmware.photon.controller.model.tasks.ScheduledTaskService.ScheduledTaskState) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) TimeUnit(java.util.concurrent.TimeUnit) PhotonModelAdapterConfig(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryService.PhotonModelAdapterConfig) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) LocalizableValidationException(com.vmware.xenon.common.LocalizableValidationException) PropertyIndexingOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyIndexingOption) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) OperationSequence(com.vmware.xenon.common.OperationSequence) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)

Aggregations

Operation (com.vmware.xenon.common.Operation)391 URI (java.net.URI)142 ArrayList (java.util.ArrayList)132 QueryTask (com.vmware.xenon.services.common.QueryTask)118 List (java.util.List)118 Utils (com.vmware.xenon.common.Utils)111 StatelessService (com.vmware.xenon.common.StatelessService)108 UriUtils (com.vmware.xenon.common.UriUtils)106 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)98 Map (java.util.Map)92 HashMap (java.util.HashMap)90 OperationJoin (com.vmware.xenon.common.OperationJoin)86 Query (com.vmware.xenon.services.common.QueryTask.Query)86 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)82 Collectors (java.util.stream.Collectors)79 HashSet (java.util.HashSet)78 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)73 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)70 DeferredResult (com.vmware.xenon.common.DeferredResult)69 Consumer (java.util.function.Consumer)69