Search in sources :

Example 1 with IAAS_API_ENABLED

use of com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED in project photon-model by vmware.

the class VSphereAdapterSnapshotService method performSnapshotOperation.

private DeferredResult<SnapshotContext> performSnapshotOperation(SnapshotContext context) {
    DeferredResult<SnapshotContext> result = new DeferredResult<>();
    VSphereIOThreadPool pool = VSphereIOThreadPoolAllocator.getPool(this);
    DeferredResult<AuthCredentialsService.AuthCredentialsServiceState> credentials;
    if (IAAS_API_ENABLED) {
        credentials = SessionUtil.retrieveExternalToken(this, context.operation.getAuthorizationContext());
    } else {
        URI authUri = createInventoryUri(this.getHost(), context.parentComputeDescription.description.authCredentialsLink);
        Operation op = Operation.createGet(authUri);
        credentials = this.sendWithDeferredResult(op, AuthCredentialsService.AuthCredentialsServiceState.class);
    }
    switch(context.requestType) {
        case CREATE:
            BiConsumer<AuthCredentialsService.AuthCredentialsServiceState, Throwable> create = (authCredentialsServiceState, throwable) -> {
                if (throwable != null) {
                    result.fail(throwable);
                    return;
                }
                pool.submit(context.parentComputeDescription.adapterManagementReference, authCredentialsServiceState, (connection, e) -> {
                    if (e != null) {
                        result.fail(e);
                    } else {
                        createSnapshot(connection, context, result);
                    }
                });
            };
            credentials.whenComplete(create);
            break;
        case DELETE:
            BiConsumer<AuthCredentialsService.AuthCredentialsServiceState, Throwable> delete = (authCredentialsServiceState, throwable) -> {
                if (throwable != null) {
                    result.fail(throwable);
                    return;
                }
                pool.submit(context.parentComputeDescription.adapterManagementReference, authCredentialsServiceState, (connection, e) -> {
                    if (e != null) {
                        result.fail(e);
                    } else {
                        deleteSnapshot(context, connection, result);
                    }
                });
            };
            credentials.whenComplete(delete);
            break;
        case REVERT:
            BiConsumer<AuthCredentialsService.AuthCredentialsServiceState, Throwable> revert = (authCredentialsServiceState, throwable) -> {
                if (throwable != null) {
                    result.fail(throwable);
                    return;
                }
                pool.submit(context.parentComputeDescription.adapterManagementReference, authCredentialsServiceState, (connection, e) -> {
                    if (e != null) {
                        result.fail(e);
                    } else {
                        revertSnapshot(context, connection, result);
                    }
                });
            };
            credentials.whenComplete(revert);
            break;
        default:
            result.fail(new IllegalStateException("Unsupported requestType " + context.requestType));
    }
    return result;
}
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) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) URI(java.net.URI) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 2 with IAAS_API_ENABLED

use of com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED in project photon-model by vmware.

the class VSphereVMDiskContext method populateVMDiskContextThen.

/**
 * Populates the given initial context and invoke the onSuccess handler when built. At every
 * step, if failure occurs the VSphereVMDiskContext's errorHandler is invoked to cleanup.
 */
protected static void populateVMDiskContextThen(Service service, VSphereVMDiskContext ctx, Consumer<VSphereVMDiskContext> onSuccess) {
    if (ctx.computeDesc == null) {
        URI computeUri = createInventoryUri(service.getHost(), UriUtils.extendUriWithQuery(ctx.request.resourceReference, UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString()));
        AdapterUtils.getServiceState(service, computeUri, op -> {
            ctx.computeDesc = op.getBody(ComputeStateWithDescription.class);
            if (CustomProperties.of(ctx.computeDesc).getString(CustomProperties.MOREF, null) == null) {
                ctx.fail(new IllegalStateException(String.format("VM Moref is not defined in resource %s", ctx.computeDesc.documentSelfLink)));
                return;
            }
            populateVMDiskContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    if (ctx.diskState == null) {
        URI diskUri = createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(UriUtils.buildUri(service.getHost(), ctx.request.payload.get(DISK_LINK))));
        AdapterUtils.getServiceState(service, diskUri, op -> {
            ctx.diskState = op.getBody(DiskService.DiskStateExpanded.class);
            // the VM. So CD_ROM can be in any status.
            if (!ctx.request.isMockRequest) {
                if (ctx.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
                    EnumSet<DiskService.DiskType> notSupportedTypes = EnumSet.of(DiskService.DiskType.SSD, DiskService.DiskType.NETWORK);
                    if (notSupportedTypes.contains(ctx.diskState.type)) {
                        ctx.fail(new IllegalStateException(String.format("Not supported disk type %s.", ctx.diskState.type)));
                        return;
                    }
                    if (ctx.diskState.type == DiskService.DiskType.HDD) {
                        if (ctx.diskState.status != DiskService.DiskStatus.AVAILABLE) {
                            ctx.fail(new IllegalStateException(String.format("Disk %s is not in AVAILABLE status to attach to VM.", ctx.diskState.documentSelfLink)));
                            return;
                        } else if (CustomProperties.of(ctx.diskState).getString(DISK_FULL_PATH, null) == null || CustomProperties.of(ctx.diskState).getString(DISK_DATASTORE_NAME, null) == null) {
                            ctx.fail(new IllegalStateException(String.format("Disk %s is missing path details to attach to VM.", ctx.diskState.documentSelfLink)));
                            return;
                        }
                    }
                } else {
                    // Allowing only HDD based disk to be detached
                    if (ctx.diskState.type != DiskService.DiskType.HDD) {
                        ctx.fail(new IllegalStateException(String.format("Not supported disk type %s for detach.", ctx.diskState.type)));
                        return;
                    }
                    if (ctx.diskState.status != DiskService.DiskStatus.ATTACHED) {
                        ctx.fail(new IllegalStateException(String.format("Disk %s is not in ATTACHED status to detach from VM.", ctx.diskState.documentSelfLink)));
                        return;
                    }
                }
            }
            // fetch the content from the content service
            if (ctx.diskState.type == DiskService.DiskType.CDROM) {
                String contentUriStr = CustomProperties.of(ctx.diskState).getString(DISK_CONTENT_LINK, null);
                if (contentUriStr != null) {
                    ctx.contentLink = contentUriStr;
                    URI contentUri = PhotonModelUriUtils.createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), contentUriStr));
                    AdapterUtils.getServiceState(service, contentUri, MEDIA_TYPE_APPLICATION_OCTET_STREAM, op2 -> {
                        ctx.contentToUpload = op2.getBody(byte[].class);
                        populateVMDiskContextThen(service, ctx, onSuccess);
                    }, ctx.errorHandler);
                } else {
                    populateVMDiskContextThen(service, ctx, onSuccess);
                }
            } else {
                populateVMDiskContextThen(service, ctx, onSuccess);
            }
        }, ctx.errorHandler);
        return;
    }
    // If it is CD-ROM attach then collect all the disk links objects if insertCDRom is true
    if (ctx.computeDiskStates == null) {
        Boolean insertCdRom = CustomProperties.of(ctx.diskState).getBoolean(INSERT_CDROM, false);
        if (ctx.diskState.type == DiskService.DiskType.CDROM && insertCdRom && ctx.computeDesc.diskLinks != null && !ctx.computeDesc.diskLinks.isEmpty()) {
            ctx.computeDiskStates = new ArrayList<>(ctx.computeDesc.diskLinks.size());
            // collect disks in parallel
            Stream<Operation> opsGetDisk = ctx.computeDesc.diskLinks.stream().map(link -> {
                URI diskStateUri = createInventoryUri(service.getHost(), link);
                return Operation.createGet(createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(diskStateUri)));
            });
            OperationJoin join = OperationJoin.create(opsGetDisk).setCompletion((os, errors) -> {
                if (errors != null && !errors.isEmpty()) {
                    // fail on first error
                    ctx.errorHandler.accept(new IllegalStateException("Cannot get disk state", errors.values().iterator().next()));
                    return;
                }
                os.values().forEach(op -> ctx.computeDiskStates.add(op.getBody(DiskService.DiskStateExpanded.class)));
                populateVMDiskContextThen(service, ctx, onSuccess);
            });
            join.sendWith(service);
        } else {
            ctx.computeDiskStates = Collections.emptyList();
            populateVMDiskContextThen(service, ctx, onSuccess);
        }
        return;
    }
    if (ctx.parentComputeDesc == null && ctx.computeDesc.parentLink != null) {
        URI computeUri = createInventoryUri(service.getHost(), UriUtils.extendUriWithQuery(UriUtils.buildUri(service.getHost(), ctx.computeDesc.parentLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString()));
        AdapterUtils.getServiceState(service, computeUri, op -> {
            ctx.parentComputeDesc = op.getBody(ComputeStateWithDescription.class);
            populateVMDiskContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    if (ctx.vSphereCredentials == null) {
        if (IAAS_API_ENABLED) {
            if (ctx.operation == null) {
                ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
                return;
            }
            SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
                if (throwable != null) {
                    ctx.errorHandler.accept(throwable);
                    return;
                }
                ctx.vSphereCredentials = authCredentialsServiceState;
                populateVMDiskContextThen(service, ctx, onSuccess);
            });
        } else {
            if (ctx.parentComputeDesc.description.authCredentialsLink == null) {
                ctx.fail(new IllegalStateException(String.format("authCredentialsLink is not defined in resource %s", ctx.parentComputeDesc.description.documentSelfLink)));
                return;
            }
            URI credUri = createInventoryUri(service.getHost(), ctx.parentComputeDesc.description.authCredentialsLink);
            AdapterUtils.getServiceState(service, credUri, op -> {
                ctx.vSphereCredentials = op.getBody(AuthCredentialsServiceState.class);
                populateVMDiskContextThen(service, ctx, onSuccess);
            }, ctx.errorHandler);
        }
        return;
    }
    if (ctx.datacenterMoRef == null) {
        try {
            String regionId = ctx.diskState.regionId;
            if (regionId == null || regionId.isEmpty()) {
                if (ctx.computeDesc.regionId != null) {
                    regionId = ctx.computeDesc.regionId;
                } else if (ctx.parentComputeDesc.regionId != null) {
                    regionId = ctx.parentComputeDesc.regionId;
                }
            }
            ctx.datacenterMoRef = VimUtils.convertStringToMoRef(regionId);
        } catch (IllegalArgumentException ex) {
            ctx.fail(ex);
            return;
        }
        populateVMDiskContextThen(service, ctx, onSuccess);
        return;
    }
    if (ctx.computePlacementHost == null) {
        String placementLink = CustomProperties.of(ctx.computeDesc).getString(ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME);
        // compute host link will be not null here.
        URI expandedPlacementUri = UriUtils.extendUriWithQuery(PhotonModelUriUtils.createInventoryUri(service.getHost(), placementLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
        expandedPlacementUri = PhotonModelUriUtils.createInventoryUri(service.getHost(), expandedPlacementUri);
        AdapterUtils.getServiceState(service, expandedPlacementUri, op -> {
            ctx.computePlacementHost = op.getBody(ComputeStateWithDescription.class);
            if (ctx.computePlacementHost.groupLinks != null) {
                ctx.computeGroupLinks = ctx.computePlacementHost.groupLinks.stream().filter(link -> link.contains(PREFIX_DATASTORE)).collect(Collectors.toSet());
            }
            populateVMDiskContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    // populate datastore name
    if (ctx.datastoreName == null) {
        if (ctx.diskState.customProperties != null && ctx.diskState.customProperties.get(DISK_DATASTORE_NAME) != null) {
            ctx.datastoreName = ctx.diskState.customProperties.get(DISK_DATASTORE_NAME);
            populateVMDiskContextThen(service, ctx, onSuccess);
        } else if (ctx.diskState.storageDescription != null) {
            ctx.datastoreName = ctx.diskState.storageDescription.id;
            populateVMDiskContextThen(service, ctx, onSuccess);
        } else if (ctx.diskState.resourceGroupStates != null && !ctx.diskState.resourceGroupStates.isEmpty()) {
            // There will always be only one resource group state existing for a disk
            ResourceGroupService.ResourceGroupState resource = ctx.diskState.resourceGroupStates.iterator().next();
            ClientUtils.getDatastoresForProfile(service, resource.documentSelfLink, ctx.diskState.endpointLink, ctx.diskState.tenantLinks, ctx.errorHandler, (result) -> {
                if (result.documents != null && result.documents.size() > 0) {
                    // pick the first datastore and proceed.
                    ctx.datastoreName = Utils.fromJson(result.documents.values().iterator().next(), StorageDescriptionService.StorageDescription.class).id;
                } else {
                    // Since no result found default to the available datastore.
                    ctx.datastoreName = "";
                }
                populateVMDiskContextThen(service, ctx, onSuccess);
            });
        } else if (ctx.computeGroupLinks != null) {
            // try to get the datastore form the placement link of compute
            String datastoreLink = ctx.computeGroupLinks.iterator().next();
            URI dsUri = PhotonModelUriUtils.createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), datastoreLink));
            AdapterUtils.getServiceState(service, dsUri, op -> {
                ResourceGroupService.ResourceGroupState rgState = op.getBody(ResourceGroupService.ResourceGroupState.class);
                ctx.datastoreName = rgState.id;
                populateVMDiskContextThen(service, ctx, onSuccess);
            }, ctx.errorHandler);
        } else {
            ctx.datastoreName = "";
            populateVMDiskContextThen(service, ctx, onSuccess);
        }
        return;
    }
    // context populated, invoke handler
    onSuccess.accept(ctx);
}
Also used : Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) INSERT_CDROM(com.vmware.photon.controller.model.constants.PhotonModelConstants.INSERT_CDROM) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) DISK_FULL_PATH(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties.DISK_FULL_PATH) Utils(com.vmware.xenon.common.Utils) DISK_DATASTORE_NAME(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties.DISK_DATASTORE_NAME) URI(java.net.URI) EnumSet(java.util.EnumSet) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DISK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.DISK_LINK) MEDIA_TYPE_APPLICATION_OCTET_STREAM(com.vmware.xenon.common.Operation.MEDIA_TYPE_APPLICATION_OCTET_STREAM) Set(java.util.Set) Collectors(java.util.stream.Collectors) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Consumer(java.util.function.Consumer) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) List(java.util.List) Stream(java.util.stream.Stream) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DISK_CONTENT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.DISK_CONTENT_LINK) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) UriUtils(com.vmware.xenon.common.UriUtils) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) DiskService(com.vmware.photon.controller.model.resources.DiskService) PREFIX_DATASTORE(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.PREFIX_DATASTORE) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) 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) URI(java.net.URI) DiskService(com.vmware.photon.controller.model.resources.DiskService) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService)

Example 3 with IAAS_API_ENABLED

use of com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED in project photon-model by vmware.

the class ProvisionContext method populateContextThen.

/**
 * Populates the given initial context and invoke the onSuccess handler when built. At every step,
 * if failure occurs the ProvisionContext's errorHandler is invoked to cleanup.
 *
 * @param ctx
 * @param onSuccess
 */
public static void populateContextThen(Service service, ProvisionContext ctx, Consumer<ProvisionContext> onSuccess) {
    // TODO fetch all required state in parallel using OperationJoin.
    if (ctx.child == null) {
        URI computeUri = UriUtils.extendUriWithQuery(ctx.computeReference, UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
        computeUri = createInventoryUri(service.getHost(), computeUri);
        AdapterUtils.getServiceState(service, computeUri, op -> {
            ctx.child = op.getBody(ComputeStateWithDescription.class);
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    String templateLink = VimUtils.firstNonNull(CustomProperties.of(ctx.child).getString(CustomProperties.TEMPLATE_LINK), CustomProperties.of(ctx.child.description).getString(CustomProperties.TEMPLATE_LINK));
    // in all other cases ignore the presence of the template
    if (templateLink != null && ctx.templateMoRef == null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
        URI computeUri = createInventoryUri(service.getHost(), templateLink);
        AdapterUtils.getServiceState(service, computeUri, op -> {
            ImageState body = op.getBody(ImageState.class);
            ctx.templateMoRef = CustomProperties.of(body).getMoRef(CustomProperties.MOREF);
            if (ctx.templateMoRef == null) {
                String msg = String.format("The linked template %s does not contain a MoRef in its custom properties", templateLink);
                ctx.fail(new IllegalStateException(msg));
            } else {
                populateContextThen(service, ctx, onSuccess);
            }
        }, ctx.errorHandler);
        return;
    }
    // For creation based on linked clone of snapshot
    if (ctx.snapshotMoRef == null) {
        String snapshotLink = CustomProperties.of(ctx.child).getString(CustomProperties.SNAPSHOT_LINK);
        if (snapshotLink != null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
            URI snapshotUri = createInventoryUri(service.getHost(), snapshotLink);
            AdapterUtils.getServiceState(service, snapshotUri, op -> {
                SnapshotService.SnapshotState snapshotState = op.getBody(SnapshotService.SnapshotState.class);
                ctx.snapshotMoRef = CustomProperties.of(snapshotState).getMoRef(CustomProperties.MOREF);
                if (ctx.snapshotMoRef == null) {
                    String msg = String.format("The linked clone snapshot %s does not contain a MoRef in its custom properties", snapshotLink);
                    ctx.fail(new IllegalStateException(msg));
                } else {
                    // Retrieve the reference endpoint moref from which the linkedclone has to be created.
                    String refComputeLink = snapshotState.computeLink;
                    if (refComputeLink != null) {
                        URI refComputeUri = createInventoryUri(service.getHost(), refComputeLink);
                        AdapterUtils.getServiceState(service, refComputeUri, opCompute -> {
                            ComputeStateWithDescription refComputeState = opCompute.getBody(ComputeStateWithDescription.class);
                            ctx.referenceComputeMoRef = CustomProperties.of(refComputeState).getMoRef(CustomProperties.MOREF);
                            if (ctx.referenceComputeMoRef == null) {
                                String msg = String.format("The linked clone endpoint ref %s does not contain a MoRef in its custom properties", refComputeLink);
                                ctx.fail(new IllegalStateException(msg));
                            }
                            populateContextThen(service, ctx, onSuccess);
                        }, ctx.errorHandler);
                    }
                }
            }, ctx.errorHandler);
            return;
        }
    }
    if (ctx.parent == null && ctx.child.parentLink != null) {
        URI computeUri = UriUtils.extendUriWithQuery(UriUtils.buildUri(service.getHost(), ctx.child.parentLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
        computeUri = createInventoryUri(service.getHost(), computeUri);
        AdapterUtils.getServiceState(service, computeUri, op -> {
            ctx.parent = op.getBody(ComputeStateWithDescription.class);
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    if (ctx.vSphereCredentials == null) {
        if (IAAS_API_ENABLED) {
            if (ctx.operation == null) {
                ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
                return;
            }
            SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
                if (throwable != null) {
                    ctx.errorHandler.accept(throwable);
                    return;
                }
                ctx.vSphereCredentials = authCredentialsServiceState;
                populateContextThen(service, ctx, onSuccess);
            });
        } else {
            if (ctx.parent.description.authCredentialsLink == null) {
                ctx.fail(new IllegalStateException("authCredentialsLink is not defined in resource " + ctx.parent.description.documentSelfLink));
                return;
            }
            URI credUri = createInventoryUri(service.getHost(), ctx.parent.description.authCredentialsLink);
            AdapterUtils.getServiceState(service, credUri, op -> {
                ctx.vSphereCredentials = op.getBody(AuthCredentialsServiceState.class);
                populateContextThen(service, ctx, onSuccess);
            }, ctx.errorHandler);
        }
        return;
    }
    if (ctx.task == null) {
        // Verify if this makes sense? These tasks should always be local to deployment?
        AdapterUtils.getServiceState(service, ctx.provisioningTaskReference, op -> {
            ctx.task = op.getBody(ServiceDocument.class);
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    if (ctx.nics == null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
        if (ctx.child.networkInterfaceLinks == null || ctx.child.networkInterfaceLinks.isEmpty()) {
            ctx.nics = Collections.emptyList();
            populateContextThen(service, ctx, onSuccess);
            return;
        }
        ctx.nics = new ArrayList<>();
        Query query = Query.Builder.create().addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, ctx.child.networkInterfaceLinks).build();
        QueryTask qt = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.EXPAND_LINKS).addOption(QueryOption.SELECT_LINKS).addOption(QueryOption.INDEXED_METADATA).addLinkTerm(NetworkInterfaceState.FIELD_NAME_NETWORK_LINK).addLinkTerm(NetworkInterfaceState.FIELD_NAME_SUBNET_LINK).addLinkTerm(NetworkInterfaceState.FIELD_NAME_DESCRIPTION_LINK).build();
        QueryUtils.startInventoryQueryTask(service, qt).whenComplete((o, e) -> {
            if (e != null) {
                ctx.errorHandler.accept(e);
                return;
            }
            QueryResultsProcessor processor = QueryResultsProcessor.create(o);
            for (NetworkInterfaceStateWithDetails nic : processor.documents(NetworkInterfaceStateWithDetails.class)) {
                if (nic.networkInterfaceDescriptionLink != null) {
                    NetworkInterfaceDescription desc = processor.selectedDocument(nic.networkInterfaceDescriptionLink, NetworkInterfaceDescription.class);
                    nic.description = desc;
                }
                if (nic.subnetLink != null) {
                    SubnetState subnet = processor.selectedDocument(nic.subnetLink, SubnetState.class);
                    nic.subnet = subnet;
                }
                if (nic.networkLink != null) {
                    NetworkState network = processor.selectedDocument(nic.networkLink, NetworkState.class);
                    nic.network = network;
                }
                ctx.nics.add(nic);
            }
            populateContextThen(service, ctx, onSuccess);
        });
        return;
    }
    if (ctx.computeMoRef == null) {
        String placementLink = CustomProperties.of(ctx.child).getString(ComputeProperties.PLACEMENT_LINK);
        if (placementLink == null) {
            Exception error = new IllegalStateException("A Compute resource must have a " + ComputeProperties.PLACEMENT_LINK + " custom property");
            ctx.fail(error);
            return;
        }
        URI expandedPlacementUri = UriUtils.extendUriWithQuery(createInventoryUri(service.getHost(), placementLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
        expandedPlacementUri = createInventoryUri(service.getHost(), expandedPlacementUri);
        Operation.createGet(expandedPlacementUri).setCompletion((o, e) -> {
            if (e != null) {
                ctx.fail(e);
                return;
            }
            ComputeStateWithDescription host = o.getBody(ComputeStateWithDescription.class);
            // extract the target resource pool for the placement
            CustomProperties hostCustomProperties = CustomProperties.of(host);
            ctx.computeMoRef = hostCustomProperties.getMoRef(CustomProperties.MOREF);
            if (ctx.computeMoRef == null) {
                Exception error = new IllegalStateException(String.format("Compute @ %s does not contain a %s custom property", placementLink, CustomProperties.MOREF));
                ctx.fail(error);
                return;
            }
            if (host.description.regionId == null) {
                Exception error = new IllegalStateException(String.format("Compute @ %s does not specify a region", placementLink));
                ctx.fail(error);
                return;
            }
            try {
                ctx.datacenterMoRef = VimUtils.convertStringToMoRef(host.description.regionId);
            } catch (IllegalArgumentException ex) {
                ctx.fail(ex);
                return;
            }
            populateContextThen(service, ctx, onSuccess);
        }).sendWith(service);
        return;
    }
    if (ctx.disks == null) {
        // no disks attached
        if (ctx.child.diskLinks == null || ctx.child.diskLinks.isEmpty()) {
            ctx.disks = Collections.emptyList();
            populateContextThen(service, ctx, onSuccess);
            return;
        }
        ctx.disks = new ArrayList<>(ctx.child.diskLinks.size());
        // collect disks in parallel
        Stream<Operation> opsGetDisk = ctx.child.diskLinks.stream().map(link -> {
            URI diskStateUri = UriUtils.buildUri(service.getHost(), link);
            return Operation.createGet(createInventoryUri(service.getHost(), DiskStateExpanded.buildUri(diskStateUri)));
        });
        OperationJoin join = OperationJoin.create(opsGetDisk).setCompletion((os, errors) -> {
            if (errors != null && !errors.isEmpty()) {
                // fail on first error
                ctx.errorHandler.accept(new IllegalStateException("Cannot get disk state", errors.values().iterator().next()));
                return;
            }
            os.values().forEach(op -> ctx.disks.add(op.getBody(DiskStateExpanded.class)));
            populateContextThen(service, ctx, onSuccess);
        });
        join.sendWith(service);
        return;
    }
    String libraryItemLink = VimUtils.firstNonNull(CustomProperties.of(ctx.child).getString(CustomProperties.LIBRARY_ITEM_LINK), CustomProperties.of(ctx.child.description).getString(CustomProperties.LIBRARY_ITEM_LINK));
    if (libraryItemLink != null && ctx.image == null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
        URI libraryUri = createInventoryUri(service.getHost(), libraryItemLink);
        AdapterUtils.getServiceState(service, libraryUri, op -> {
            ImageState body = op.getBody(ImageState.class);
            ctx.image = body;
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    if (ctx.instanceRequestType == InstanceRequestType.CREATE) {
        if (ctx.image == null) {
            DiskStateExpanded bootDisk = ctx.disks.stream().filter(d -> d.imageLink != null).findFirst().orElse(null);
            if (bootDisk != null) {
                URI bootImageRef = createInventoryUri(service.getHost(), bootDisk.imageLink);
                AdapterUtils.getServiceState(service, bootImageRef, op -> {
                    ImageState body = op.getBody(ImageState.class);
                    ctx.image = body;
                    populateContextThen(service, ctx, onSuccess);
                }, ctx.errorHandler);
                return;
            }
        }
    }
    // Order networks by deviceIndex so that nics are created in the same order
    if (ctx.nics != null) {
        // configure network
        ctx.nics.sort((lnis, rnis) -> {
            return Integer.compare(lnis.deviceIndex, rnis.deviceIndex);
        });
    }
    // context populated, invoke handler
    onSuccess.accept(ctx);
}
Also used : Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) JoinedCompletionHandler(com.vmware.xenon.common.OperationJoin.JoinedCompletionHandler) ArrayList(java.util.ArrayList) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) ResourceRequest(com.vmware.photon.controller.model.adapterapi.ResourceRequest) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) URI(java.net.URI) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) InstanceRequestType(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest.InstanceRequestType) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Consumer(java.util.function.Consumer) List(java.util.List) Stream(java.util.stream.Stream) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) UriUtils(com.vmware.xenon.common.UriUtils) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) Query(com.vmware.xenon.services.common.QueryTask.Query) OperationJoin(com.vmware.xenon.common.OperationJoin) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) Operation(com.vmware.xenon.common.Operation) URI(java.net.URI) ServiceDocument(com.vmware.xenon.common.ServiceDocument) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) QueryTask(com.vmware.xenon.services.common.QueryTask) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState)

Example 4 with IAAS_API_ENABLED

use of com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED in project photon-model by vmware.

the class DiskContext method populateContextThen.

/**
 * Populates the given initial context and invoke the onSuccess handler when built. At every
 * step, if failure occurs the DiskContext's errorHandler is invoked to cleanup.
 */
public static void populateContextThen(Service service, DiskContext ctx, Consumer<DiskContext> onSuccess) {
    // Step 1: Get disk details
    if (ctx.diskState == null) {
        URI diskUri = createInventoryUri(service.getHost(), DiskService.DiskStateExpanded.buildUri(ctx.diskReference));
        AdapterUtils.getServiceState(service, diskUri, op -> {
            ctx.diskState = op.getBody(DiskService.DiskStateExpanded.class);
            EnumSet<DiskService.DiskType> notSupportedTypes = EnumSet.of(DiskService.DiskType.SSD, DiskService.DiskType.NETWORK);
            if (notSupportedTypes.contains(ctx.diskState.type)) {
                ctx.fail(new IllegalStateException(String.format("Not supported disk type %s.", ctx.diskState.type)));
                return;
            }
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    // the disk.
    if (ctx.datastoreName == null && ctx.diskInstanceRequest.requestType == DiskInstanceRequest.DiskRequestType.CREATE) {
        if (ctx.diskState.storageDescription != null) {
            ctx.datastoreName = ctx.diskState.storageDescription.id;
            populateContextThen(service, ctx, onSuccess);
        } else if (ctx.diskState.resourceGroupStates != null && !ctx.diskState.resourceGroupStates.isEmpty()) {
            // There will always be only one resource group state existing for a disk
            ResourceGroupState resource = ctx.diskState.resourceGroupStates.iterator().next();
            ClientUtils.getDatastoresForProfile(service, resource.documentSelfLink, ctx.diskState.endpointLink, ctx.diskState.tenantLinks, ctx.errorHandler, (result) -> {
                if (result.documents != null && result.documents.size() > 0) {
                    // pick the first datastore and proceed.
                    StorageDescription dsStorageDesc = Utils.fromJson(result.documents.values().iterator().next(), StorageDescription.class);
                    ctx.datastoreName = dsStorageDesc.id;
                    ctx.diskState.storageDescriptionLink = dsStorageDesc.documentSelfLink;
                } else {
                    // Since no result found default to the available datastore.
                    ctx.datastoreName = "";
                }
                populateContextThen(service, ctx, onSuccess);
            });
        } else if (CustomProperties.of(ctx.diskState).getString(CustomProperties.DISK_DATASTORE_NAME) != null) {
            ctx.datastoreName = CustomProperties.of(ctx.diskState).getString(CustomProperties.DISK_DATASTORE_NAME);
            populateContextThen(service, ctx, onSuccess);
        } else {
            // Mark empty so that it can fall back to any available datastore from the system.
            ctx.datastoreName = "";
            populateContextThen(service, ctx, onSuccess);
        }
        return;
    }
    // Step 3: Get Credentials
    if (ctx.vSphereCredentials == null) {
        if (IAAS_API_ENABLED) {
            if (ctx.operation == null) {
                ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
                return;
            }
            SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
                if (throwable != null) {
                    ctx.errorHandler.accept(throwable);
                    return;
                }
                ctx.vSphereCredentials = authCredentialsServiceState;
                populateContextThen(service, ctx, onSuccess);
            });
        } else {
            if (ctx.diskState.authCredentialsLink == null || ctx.diskState.authCredentialsLink.isEmpty()) {
                ctx.fail(new IllegalArgumentException("Auth credentials cannot be empty"));
                return;
            }
            URI credUri = createInventoryUri(service.getHost(), ctx.diskState.authCredentialsLink);
            AdapterUtils.getServiceState(service, credUri, op -> {
                ctx.vSphereCredentials = op.getBody(AuthCredentialsServiceState.class);
                populateContextThen(service, ctx, onSuccess);
            }, ctx.errorHandler);
        }
        return;
    }
    // Step 4: Get the endpoint compute link
    if (ctx.endpointComputeLink == null) {
        URI endpointUri = createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), ctx.diskState.endpointLink));
        AdapterUtils.getServiceState(service, endpointUri, op -> {
            EndpointService.EndpointState endpointState = op.getBody(EndpointService.EndpointState.class);
            ctx.endpointComputeLink = endpointState.computeLink;
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    // Step 5: Get the adapter reference to from the endpoint compute link
    if (ctx.adapterManagementReference == null) {
        URI computeUri = createInventoryUri(service.getHost(), UriUtils.buildUri(service.getHost(), ctx.endpointComputeLink));
        AdapterUtils.getServiceState(service, computeUri, op -> {
            ComputeService.ComputeState computeState = op.getBody(ComputeService.ComputeState.class);
            ctx.adapterManagementReference = computeState.adapterManagementReference;
            populateContextThen(service, ctx, onSuccess);
        }, ctx.errorHandler);
        return;
    }
    // Step 6: Obtain reference to the datacenter moref.
    if (ctx.datacenterMoRef == null) {
        try {
            ctx.datacenterMoRef = VimUtils.convertStringToMoRef(ctx.diskState.regionId);
        } catch (IllegalArgumentException ex) {
            ctx.fail(ex);
            return;
        }
    }
    onSuccess.accept(ctx);
}
Also used : AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) DiskInstanceRequest(com.vmware.photon.controller.model.adapterapi.DiskInstanceRequest) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Consumer(java.util.function.Consumer) Utils(com.vmware.xenon.common.Utils) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) URI(java.net.URI) DiskService(com.vmware.photon.controller.model.resources.DiskService) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) EnumSet(java.util.EnumSet) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) URI(java.net.URI) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)

Aggregations

IAAS_API_ENABLED (com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED)4 TaskManager (com.vmware.photon.controller.model.adapters.util.TaskManager)4 SessionUtil (com.vmware.photon.controller.model.resources.SessionUtil)4 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 Operation (com.vmware.xenon.common.Operation)4 OperationJoin (com.vmware.xenon.common.OperationJoin)4 Service (com.vmware.xenon.common.Service)4 UriUtils (com.vmware.xenon.common.UriUtils)4 Utils (com.vmware.xenon.common.Utils)4 URI (java.net.URI)4 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)3 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)3 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)3 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Consumer (java.util.function.Consumer)3 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)2 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)2