Search in sources :

Example 1 with ConnectionCallback

use of com.vmware.photon.controller.model.adapters.vsphere.VSphereIOThreadPool.ConnectionCallback 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

IAAS_API_ENABLED (com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED)1 SubnetInstanceRequest (com.vmware.photon.controller.model.adapterapi.SubnetInstanceRequest)1 CustomProperties (com.vmware.photon.controller.model.adapters.vsphere.CustomProperties)1 ConnectionCallback (com.vmware.photon.controller.model.adapters.vsphere.VSphereIOThreadPool.ConnectionCallback)1 VimUtils (com.vmware.photon.controller.model.adapters.vsphere.VimUtils)1 VimPath (com.vmware.photon.controller.model.adapters.vsphere.util.VimPath)1 GetMoRef (com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef)1 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)1 SessionUtil (com.vmware.photon.controller.model.resources.SessionUtil)1 SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)1 AssertUtil (com.vmware.photon.controller.model.util.AssertUtil)1 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)1 DVPortgroupConfigSpec (com.vmware.vim25.DVPortgroupConfigSpec)1 DistributedVirtualPortgroupPortgroupType (com.vmware.vim25.DistributedVirtualPortgroupPortgroupType)1 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)1 TaskInfo (com.vmware.vim25.TaskInfo)1 TaskInfoState (com.vmware.vim25.TaskInfoState)1 DeferredResult (com.vmware.xenon.common.DeferredResult)1