Search in sources :

Example 6 with PhotonModelUriUtils.createInventoryUri

use of com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri in project photon-model by vmware.

the class GCPEnumerationAdapterService method checkLinkAndFinishDeleting.

/**
 * The helper function which checks if the deletion is finished.
 * If finished, go to Finished sub stage.
 * @param ctx The Enumeration Context.
 * @param deletionNextPageLink The next deletion page link.
 */
private void checkLinkAndFinishDeleting(EnumerationContext ctx, String deletionNextPageLink) {
    if (deletionNextPageLink != null) {
        URI nextPageInventoryLinkUri = PhotonModelUriUtils.createInventoryUri(this.getHost(), deletionNextPageLink);
        logFine(() -> String.format("Querying page [%s] for resources to be deleted", nextPageInventoryLinkUri));
        Operation.createGet(nextPageInventoryLinkUri).setCompletion((o, e) -> deleteQueryCompletionHandler(ctx, o.getBody(QueryTask.class), e)).sendWith(this);
        return;
    }
    logFine(() -> "No compute states match for deletion");
    ctx.subStage = EnumerationSubStages.FINISHED;
    handleSubStage(ctx);
}
Also used : GCPUriPaths(com.vmware.photon.controller.model.adapters.gcp.GCPUriPaths) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) GCPUtils.privateKeyFromPkcs8(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.privateKeyFromPkcs8) QueryTask(com.vmware.xenon.services.common.QueryTask) OSType(com.vmware.photon.controller.model.ComputeProperties.OSType) GCPInstance(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) DiskType(com.vmware.photon.controller.model.resources.DiskService.DiskType) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) ENVIRONMENT_NAME_GCP(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ENVIRONMENT_NAME_GCP) Utils(com.vmware.xenon.common.Utils) GeneralSecurityException(java.security.GeneralSecurityException) GCPInstancesList(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstancesList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) URI(java.net.URI) DEFAULT_DISK_SOURCE_IMAGE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SOURCE_IMAGE) GCPUtils.assignPowerState(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.assignPowerState) GCPUtils.extractActualInstanceType(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.extractActualInstanceType) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DISK_AUTO_DELETE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DISK_AUTO_DELETE) StatelessService(com.vmware.xenon.common.StatelessService) DEFAULT_DISK_SERVICE_REFERENCE(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_SERVICE_REFERENCE) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) GCPDisk(com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPDisk) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) LIST_VM_TEMPLATE_URI(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.LIST_VM_TEMPLATE_URI) UUID(java.util.UUID) ServiceHost(com.vmware.xenon.common.ServiceHost) List(java.util.List) AUTH_HEADER_BEARER_PREFIX(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.AUTH_HEADER_BEARER_PREFIX) PrivateKey(java.security.PrivateKey) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NumericRange(com.vmware.xenon.services.common.QueryTask.NumericRange) DiskService(com.vmware.photon.controller.model.resources.DiskService) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) HashMap(java.util.HashMap) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) GCPUtils(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils) MAX_RESULTS(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.MAX_RESULTS) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) PAGE_TOKEN(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.PAGE_TOKEN) UriPaths(com.vmware.photon.controller.model.UriPaths) EnumerationStages(com.vmware.photon.controller.model.adapters.util.enums.EnumerationStages) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) EnumerationAction(com.vmware.photon.controller.model.adapterapi.EnumerationAction) LinkedList(java.util.LinkedList) GCPAccessTokenResponse(com.vmware.photon.controller.model.adapters.gcp.podo.authorization.GCPAccessTokenResponse) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) GCPUtils.extractRegionFromZone(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.extractRegionFromZone) EncryptionUtils(com.vmware.photon.controller.model.security.util.EncryptionUtils) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) ComputeScopes(com.google.api.services.compute.ComputeScopes) Consumer(java.util.function.Consumer) CUSTOM_OS_TYPE(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_OS_TYPE) DEFAULT_DISK_CAPACITY(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.DEFAULT_DISK_CAPACITY) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) GCPUtils.assignIPAddress(com.vmware.photon.controller.model.adapters.gcp.utils.GCPUtils.assignIPAddress) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) JSONWebToken(com.vmware.photon.controller.model.adapters.gcp.utils.JSONWebToken) QueryTask(com.vmware.xenon.services.common.QueryTask) URI(java.net.URI) LIST_VM_TEMPLATE_URI(com.vmware.photon.controller.model.adapters.gcp.constants.GCPConstants.LIST_VM_TEMPLATE_URI)

Example 7 with PhotonModelUriUtils.createInventoryUri

use of com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri in project photon-model by vmware.

the class VSphereAdapterSnapshotService method processNextStepsForDeleteOperation.

private void processNextStepsForDeleteOperation(SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
    final SnapshotState snapshot = context.snapshotState;
    // Update the isCurrent
    if (snapshot.isCurrent && snapshot.parentLink != null) {
        logInfo("Updating the parent of the snapshot %s to current", snapshot.name);
        SnapshotState parentSnapshot = new SnapshotState();
        parentSnapshot.isCurrent = Boolean.TRUE;
        context.snapshotOperations.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshot.parentLink)).setBody(parentSnapshot).setReferer(getUri()));
    }
    // Check if the deleted snapshot is the last available snapshot
    DeferredResult<Boolean> result = isLastSnapshotForCompute(context);
    Operation[] patchComputeOp = new Operation[1];
    result.whenComplete((b, e) -> {
        if (e != null) {
            logSevere(e);
            deferredResult.fail(e);
            return;
        }
        if (b) {
            ComputeStateWithDescription compute = context.computeDescription;
            compute.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, Boolean.FALSE.toString());
            // patch compute adding property that it _hasSnapshots
            logInfo("Updating the state of compute resource: %s", compute.name);
            patchComputeOp[0] = Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.computeLink)).setBody(compute).setReferer(getUri());
            context.snapshotOperations.add(patchComputeOp[0]);
        }
        OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
            if (exc != null) {
                this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
                deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
                return;
            }
            deferredResult.complete(context);
        };
        context.snapshotOperations.add(Operation.createDelete(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setReferer(getUri()));
        OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
        joinOp.setCompletion(joinCompletion);
        joinOp.sendWith(this.getHost());
    });
}
Also used : Service(com.vmware.xenon.common.Service) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) StringUtils(org.apache.commons.lang3.StringUtils) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType) BiConsumer(java.util.function.BiConsumer) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) TaskInfo(com.vmware.vim25.TaskInfo) OperationSequence(com.vmware.xenon.common.OperationSequence) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) MapUtils(org.apache.commons.collections.MapUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Optional(java.util.Optional) VSphereConstants(com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants) FactoryService(com.vmware.xenon.common.FactoryService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) OperationJoin(com.vmware.xenon.common.OperationJoin) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 8 with PhotonModelUriUtils.createInventoryUri

use of com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method updateVm.

static void updateVm(VSphereIncrementalEnumerationService service, ComputeState oldDocument, EnumerationProgress enumerationProgress, VmOverlay vm, boolean fullUpdate) {
    ComputeState state;
    if (fullUpdate) {
        state = makeVmFromResults(enumerationProgress, vm);
    } else {
        state = makeVmFromChanges(vm);
    }
    state.documentSelfLink = oldDocument.documentSelfLink;
    state.resourcePoolLink = null;
    state.lifecycleState = LifecycleState.READY;
    state.networkInterfaceLinks = oldDocument.networkInterfaceLinks;
    state.diskLinks = oldDocument.diskLinks;
    if (oldDocument.tenantLinks == null) {
        state.tenantLinks = enumerationProgress.getTenantLinks();
    }
    service.logFine(() -> String.format("Syncing VM %s", state.documentSelfLink));
    if (CollectionUtils.isNotEmpty(state.diskLinks)) {
        // Now check how many disks are added / deleted / needs to be updated.
        List<Operation> ops = state.diskLinks.stream().map(link -> {
            URI diskStateUri = UriUtils.buildUri(service.getHost(), link);
            return Operation.createGet(createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(diskStateUri)));
        }).collect(Collectors.toList());
        OperationJoin.create(ops).setCompletion((operations, failures) -> {
            if (failures != null) {
                service.logFine(() -> String.format("Error in sync disks of VM %s", state.documentSelfLink));
                patchOnComputeState(service, state, oldDocument, enumerationProgress, vm);
            } else {
                List<DiskService.DiskStateExpanded> currentDisks = operations.values().stream().map(op -> op.getBody(DiskService.DiskStateExpanded.class)).collect(Collectors.toList());
                List<Operation> diskUpdateOps = new ArrayList<>(currentDisks.size());
                // Select all disks to delete
                List<String> disksToDelete = new ArrayList<>(state.diskLinks);
                // Process the update of disks and then patch the compute
                for (VirtualDevice device : vm.getDisks()) {
                    DiskService.DiskStateExpanded matchedDs = findMatchingDiskState(device, currentDisks);
                    // remove the active disk links
                    if (null != matchedDs) {
                        disksToDelete.remove(matchedDs.documentSelfLink);
                    }
                    Operation vdOp = processVirtualDevice(service, matchedDs, device, enumerationProgress, state.diskLinks, VimUtils.convertMoRefToString(vm.getId()), oldDocument);
                    if (vdOp != null) {
                        diskUpdateOps.add(vdOp);
                    }
                }
                // Delete disk states whose disks are deleted in vsphere.
                for (String diskToDelete : disksToDelete) {
                    state.diskLinks.remove(diskToDelete);
                    diskUpdateOps.add(Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), diskToDelete)));
                }
                OperationJoin.create(diskUpdateOps).setCompletion((operationMap, exception) -> {
                    patchOnComputeState(service, state, oldDocument, enumerationProgress, vm);
                }).sendWith(service);
            }
        }).sendWith(service);
    } else {
        patchOnComputeState(service, state, oldDocument, enumerationProgress, vm);
    }
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) VirtualDevice(com.vmware.vim25.VirtualDevice) NsxProperties(com.vmware.photon.controller.model.adapters.vsphere.network.NsxProperties) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ClientUtils.handleVirtualDiskUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDiskUpdate) List(java.util.List) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) ClientUtils.handleVirtualDeviceUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDeviceUpdate) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) VirtualFloppy(com.vmware.vim25.VirtualFloppy) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) ArrayList(java.util.ArrayList) VirtualCdrom(com.vmware.vim25.VirtualCdrom) ClientUtils.findMatchingDiskState(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.findMatchingDiskState) CollectionUtils(org.apache.commons.collections.CollectionUtils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TYPE_PORTGROUP(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_PORTGROUP) VirtualDisk(com.vmware.vim25.VirtualDisk) UriUtils.buildUriPath(com.vmware.xenon.common.UriUtils.buildUriPath) InterfaceStateMode(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.InterfaceStateMode) ObjectUpdate(com.vmware.vim25.ObjectUpdate) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) 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) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) VirtualDevice(com.vmware.vim25.VirtualDevice) List(java.util.List) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) DiskService(com.vmware.photon.controller.model.resources.DiskService)

Aggregations

PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)8 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)8 Operation (com.vmware.xenon.common.Operation)8 OperationJoin (com.vmware.xenon.common.OperationJoin)8 UriUtils (com.vmware.xenon.common.UriUtils)8 URI (java.net.URI)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)7 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)7 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)7 QueryTask (com.vmware.xenon.services.common.QueryTask)7 Collectors (java.util.stream.Collectors)7 SnapshotService (com.vmware.photon.controller.model.resources.SnapshotService)6 SnapshotState (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)6 DeferredResult (com.vmware.xenon.common.DeferredResult)6 ServiceDocument (com.vmware.xenon.common.ServiceDocument)6 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)5 TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)5 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)5