Search in sources :

Example 21 with Task

use of com.vmware.vim25.mo.Task in project photon-model by vmware.

the class VimUtils method waitTaskEnd.

public static TaskInfo waitTaskEnd(Connection connection, ManagedObjectReference task) throws InvalidCollectorVersionFaultMsg, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    WaitForValues waitForValues = new WaitForValues(connection);
    Object[] info = waitForValues.wait(task, new String[] { VimPath.task_info }, new String[] { VimPath.task_info_state }, new Object[][] { new Object[] { TaskInfoState.SUCCESS, TaskInfoState.ERROR } });
    return (TaskInfo) info[0];
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) WaitForValues(com.vmware.photon.controller.model.adapters.vsphere.util.connection.WaitForValues)

Example 22 with Task

use of com.vmware.vim25.mo.Task in project photon-model by vmware.

the class VsphereComputeResourceEnumerationHelper method handleComputeResourceChanges.

public static void handleComputeResourceChanges(VSphereIncrementalEnumerationService service, List<ComputeResourceOverlay> computeResourceOverlays, EnumerationProgress ctx, EnumerationClient client) {
    ctx.expectComputeResourceCount(computeResourceOverlays.size());
    // process compute resource changes
    for (ComputeResourceOverlay cluster : computeResourceOverlays) {
        try {
            service.logInfo("Handling compute resource change %s", Utils.toJson(cluster));
            // TODO If a host is part of cluster then the host needs to be populated with cluster link.
            if (ObjectUpdateKind.ENTER == cluster.getObjectUpdateKind()) {
                // create a cluster object only for DRS enabled clusters
                if (cluster.isDrsEnabled()) {
                    service.logInfo("Creating new cluster for %s", Utils.toJson(cluster));
                    createNewComputeResource(service, ctx, cluster, client);
                }
            } else {
                // If an existing compute resource is modified or removed.
                ComputeEnumerateResourceRequest request = ctx.getRequest();
                QueryTask task = queryForCluster(ctx, request.resourceLink(), cluster.getId().getValue());
                withTaskResults(service, task, result -> {
                    service.logInfo("Queried for existing clusters for %s", Utils.toJson(cluster));
                    try {
                        if (!result.documentLinks.isEmpty()) {
                            ComputeState oldDocument = convertOnlyResultToDocument(result, ComputeState.class);
                            // check if the compute resource is modified.
                            if (ObjectUpdateKind.MODIFY.equals(cluster.getObjectUpdateKind())) {
                                service.logInfo("Updating existing cluster for %s", Utils.toJson(cluster));
                                updateCluster(service, oldDocument, ctx, cluster, client, false);
                            } else {
                                // Compute resource has been removed. remove the compute resource from photon model here
                                // Delete only compute state document as compute description can be shared among compute resources.
                                service.logInfo("Deleting existing cluster for %s", Utils.toJson(cluster));
                                Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), oldDocument.documentSelfLink)).setCompletion((o, e) -> {
                                    trackComputeResource(ctx, cluster).handle(o, e);
                                }).sendWith(service);
                            }
                        } else {
                            ctx.getComputeResourceTracker().track();
                        }
                    } catch (Exception ex) {
                        service.logSevere("Error occurred while processing update of compute resource!", ex);
                        ctx.getComputeResourceTracker().track(cluster.getId(), ResourceTracker.ERROR);
                    }
                });
            }
        } catch (Exception e) {
            service.logSevere("Error occurred while processing update of compute resource!", e);
            ctx.getComputeResourceTracker().track(cluster.getId(), ResourceTracker.ERROR);
        }
    }
    // checkpoint compute resource
    try {
        ctx.getComputeResourceTracker().await();
    } catch (InterruptedException e) {
        service.logSevere("Interrupted during incremental enumeration for networks!", e);
    }
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ArrayList(java.util.ArrayList) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) Utils(com.vmware.xenon.common.Utils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) TYPE_SERVER_DISK(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_SERVER_DISK) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) VsanHostConfigInfo(com.vmware.vim25.VsanHostConfigInfo) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VsphereEnumerationHelper.getConnectedDatastoresAndNetworks(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.getConnectedDatastoresAndNetworks) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) VsphereEnumerationHelper.updateLocalTags(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.updateLocalTags) List(java.util.List) VsphereEnumerationHelper.submitWorkToVSpherePool(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.submitWorkToVSpherePool) UriUtils.buildUriPath(com.vmware.xenon.common.UriUtils.buildUriPath) HostScsiDisk(com.vmware.vim25.HostScsiDisk) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) Collections(java.util.Collections) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)

Example 23 with Task

use of com.vmware.vim25.mo.Task in project photon-model by vmware.

the class VsphereDatastoreEnumerationHelper method handleDatastoreChanges.

/**
 * Handles changes to datastores. Changes to name, capacity and free space are handled.
 * new datastore addition and existing datastore deletion is handled.
 *
 * @param service             the vsphere incremental service
 * @param datastores          the list of datastore overlays
 * @param enumerationProgress the context of enumeration
 */
public static void handleDatastoreChanges(VSphereIncrementalEnumerationService service, List<DatastoreOverlay> datastores, EnumerationProgress enumerationProgress) {
    enumerationProgress.expectDatastoreCount(datastores.size());
    for (DatastoreOverlay datastore : datastores) {
        // check if its a new datastore
        if (ObjectUpdateKind.ENTER == datastore.getObjectUpdateKind()) {
            createNewStorageDescription(service, enumerationProgress, datastore);
        } else {
            // "name" may not necessarily present in the object update. so passed as "null"
            QueryTask task = queryForStorage(enumerationProgress, null, VimUtils.convertMoRefToString(datastore.getId()), null);
            withTaskResults(service, task, result -> {
                if (!result.documentLinks.isEmpty()) {
                    // Object is either modified or deleted
                    StorageDescriptionService.StorageDescription oldDocument = convertOnlyResultToDocument(result, StorageDescriptionService.StorageDescription.class);
                    // if the data store is modified
                    if (ObjectUpdateKind.MODIFY == datastore.getObjectUpdateKind()) {
                        // Handle the property changes here
                        updateStorageDescription(service, oldDocument, enumerationProgress, datastore, false);
                    } else {
                        // if it's not modified, it should've been deleted in the vCenter.
                        // So, delete the document from photon store
                        Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), oldDocument.documentSelfLink)).setCompletion((o, e) -> {
                            enumerationProgress.getDatastoreTracker().track();
                        }).sendWith(service);
                    }
                } else {
                    enumerationProgress.getDatastoreTracker().track();
                }
            });
        }
    }
    try {
        enumerationProgress.getDatastoreTracker().await();
    } catch (InterruptedException e) {
        service.logSevere("Interrupted during incremental enumeration for networks!", e);
    }
}
Also used : STORAGE_USED_BYTES(com.vmware.photon.controller.model.constants.PhotonModelConstants.STORAGE_USED_BYTES) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) ServiceDocument(com.vmware.xenon.common.ServiceDocument) HashMap(java.util.HashMap) ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) CUSTOM_PROP_STORAGE_SHARED(com.vmware.photon.controller.model.ComputeProperties.CUSTOM_PROP_STORAGE_SHARED) Utils(com.vmware.xenon.common.Utils) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) StatsUtil(com.vmware.photon.controller.model.tasks.monitoring.StatsUtil) 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) SingleResourceStatsCollectionTaskService(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService) TimeUnit(java.util.concurrent.TimeUnit) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) STORAGE_AVAILABLE_BYTES(com.vmware.photon.controller.model.constants.PhotonModelConstants.STORAGE_AVAILABLE_BYTES) List(java.util.List) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) UriUtils(com.vmware.xenon.common.UriUtils) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) StorageDescriptionService(com.vmware.photon.controller.model.resources.StorageDescriptionService)

Example 24 with Task

use of com.vmware.vim25.mo.Task in project photon-model by vmware.

the class VsphereStoragePolicyEnumerationHelper method updateDataStoreWithStoragePolicyGroup.

static void updateDataStoreWithStoragePolicyGroup(VSphereIncrementalEnumerationService service, EnumerationProgress ctx, StoragePolicyOverlay sp, String selfLink) {
    List<Operation> getOps = new ArrayList<>();
    sp.getDatastoreIds().stream().forEach(id -> {
        if (null != ctx.getDatastoreTracker()) {
            String dataStoreLink = ctx.getDatastoreTracker().getSelfLink(id, VimNames.TYPE_DATASTORE);
            if (dataStoreLink != null && !ResourceTracker.ERROR.equals(dataStoreLink)) {
                getOps.add(Operation.createGet(PhotonModelUriUtils.createInventoryUri(service.getHost(), dataStoreLink)));
            }
        } else {
            ManagedObjectReference dataStoreMoref = new ManagedObjectReference();
            dataStoreMoref.setType(VimNames.TYPE_DATASTORE);
            dataStoreMoref.setValue(id);
            QueryTask task = VsphereDatastoreEnumerationHelper.queryForStorage(ctx, null, VimUtils.convertMoRefToString(dataStoreMoref), selfLink);
            VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                List<Operation> getOperations = new ArrayList<>();
                if (result.documentLinks.size() > 0) {
                    for (String documentSelfLink : result.documentLinks) {
                        getOperations.add(Operation.createGet(PhotonModelUriUtils.createInventoryUri(service.getHost(), documentSelfLink)));
                    }
                    // TODO : Refactor this to avoid extra call to fetch datastores
                    updateDS(service, ctx, selfLink, getOperations);
                }
            });
        }
    });
    updateDS(service, ctx, selfLink, getOps);
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) ArrayList(java.util.ArrayList) Operation(com.vmware.xenon.common.Operation) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 25 with Task

use of com.vmware.vim25.mo.Task in project photon-model by vmware.

the class CreatePortgroupFlow method createPortgroupInVsphere.

private ConnectionCallback createPortgroupInVsphere(DeferredResult<Void> result) {
    return (connection, error) -> {
        if (error != null) {
            result.fail(error);
            return;
        }
        // extract moref of the parent DVS switch
        ManagedObjectReference dvsRef = CustomProperties.of(this.networkState).getMoRef(CustomProperties.MOREF);
        DVPortgroupConfigSpec pgSpec = createDefaultPortgroupSpec();
        ManagedObjectReference task;
        try {
            task = connection.getVimPort().createDVPortgroupTask(dvsRef, pgSpec);
        } catch (Exception e) {
            result.fail(e);
            return;
        }
        TaskInfo taskInfo;
        try {
            taskInfo = VimUtils.waitTaskEnd(connection, task);
        } catch (Exception e) {
            result.fail(e);
            return;
        }
        if (taskInfo.getState() != TaskInfoState.SUCCESS) {
            IllegalStateException e = new IllegalStateException(taskInfo.getError().getLocalizedMessage());
            result.fail(e);
            return;
        }
        ManagedObjectReference pg = (ManagedObjectReference) taskInfo.getResult();
        AssertUtil.assertNotNull(pg, "MoRef of dvPortGroup");
        String pgKey = null;
        String dvsUuid = null;
        try {
            GetMoRef get = new GetMoRef(connection);
            Map<String, Object> propValues = get.entityProps(pg, VimPath.pg_config_key, VimPath.pg_config_distributedVirtualSwitch);
            pgKey = (String) propValues.get(VimPath.pg_config_key);
            ManagedObjectReference parentDvSwitch = (ManagedObjectReference) propValues.get(VimPath.pg_config_distributedVirtualSwitch);
            if (parentDvSwitch != null) {
                dvsUuid = get.entityProp(parentDvSwitch, VimPath.dvs_uuid);
            }
        } catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg ignore) {
            getService().logWarning("Cannot retrieve dvPortGroup properties of [%s]: %s", VimUtils.convertMoRefToString(pg), ignore.getLocalizedMessage());
        }
        // store the moref as custom property
        CustomProperties.of(this.subnetState).put(CustomProperties.MOREF, pg).put(DvsProperties.PORT_GROUP_KEY, pgKey).put(DvsProperties.DVS_UUID, dvsUuid);
        OperationContext.setFrom(getOperationContext());
        Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getService().getHost(), this.subnetState.documentSelfLink)).setBody(this.subnetState).setCompletion((o, e) -> {
            if (e != null) {
                result.fail(e);
                return;
            }
            result.complete(null);
            getTaskManager().patchTask(TaskStage.FINISHED);
        }).sendWith(getService());
    };
}
Also used : VimPath(com.vmware.photon.controller.model.adapters.vsphere.util.VimPath) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) OperationContext(com.vmware.xenon.common.OperationContext) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) DistributedVirtualPortgroupPortgroupType(com.vmware.vim25.DistributedVirtualPortgroupPortgroupType) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) AssertUtil(com.vmware.photon.controller.model.util.AssertUtil) TaskInfo(com.vmware.vim25.TaskInfo) VimUtils(com.vmware.photon.controller.model.adapters.vsphere.VimUtils) ConnectionCallback(com.vmware.photon.controller.model.adapters.vsphere.VSphereIOThreadPool.ConnectionCallback) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) CustomProperties(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) SubnetInstanceRequest(com.vmware.photon.controller.model.adapterapi.SubnetInstanceRequest) DeferredResult(com.vmware.xenon.common.DeferredResult) DVPortgroupConfigSpec(com.vmware.vim25.DVPortgroupConfigSpec) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) TaskInfoState(com.vmware.vim25.TaskInfoState) TaskInfo(com.vmware.vim25.TaskInfo) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) Map(java.util.Map) DVPortgroupConfigSpec(com.vmware.vim25.DVPortgroupConfigSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)48 Task (com.vmware.vim25.mo.Task)27 TaskInfo (com.vmware.vim25.TaskInfo)23 ArrayList (java.util.ArrayList)21 RemoteException (java.rmi.RemoteException)18 QueryTask (com.vmware.xenon.services.common.QueryTask)14 Operation (com.vmware.xenon.common.Operation)13 List (java.util.List)13 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)11 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)10 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)10 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)9 DiskService (com.vmware.photon.controller.model.resources.DiskService)9 TaskInfoState (com.vmware.vim25.TaskInfoState)9 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)8 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)8 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)7 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)7