Search in sources :

Example 26 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereAdapterInstanceService method handleCreateInstance.

private void handleCreateInstance(ProvisionContext ctx) {
    ctx.pool.submit(ctx.getAdapterManagementReference(), ctx.vSphereCredentials, (connection, ce) -> {
        if (ctx.fail(ce)) {
            return;
        }
        try {
            InstanceClient client = new InstanceClient(connection, ctx);
            ComputeState state;
            if (ctx.templateMoRef != null) {
                state = client.createInstanceFromTemplate(ctx.templateMoRef);
            } else if (ctx.image != null) {
                ManagedObjectReference moRef = CustomProperties.of(ctx.image).getMoRef(CustomProperties.MOREF);
                if (moRef != null) {
                    // the image is backed by a template VM
                    state = client.createInstanceFromTemplate(moRef);
                } else {
                    // library item
                    state = client.createInstanceFromLibraryItem(ctx.image);
                }
            } else if (ctx.snapshotMoRef != null) {
                state = client.createInstanceFromSnapshot();
            } else {
                state = client.createInstance();
            }
            if (state == null) {
                // assume they will patch the task if they have provisioned the vm
                return;
            }
            // populate state, MAC address being very important
            VmOverlay vmOverlay = client.enrichStateFromVm(state);
            Operation[] finishTask = new Operation[1];
            for (NetworkInterfaceStateWithDetails nic : ctx.nics) {
                // request guest customization while vm of powered off
                SubnetState subnet = nic.subnet;
                if (subnet != null && nic.description != null && nic.description.assignment == IpAssignment.STATIC) {
                    CustomizationClient cc = new CustomizationClient(connection, ctx.child, vmOverlay.getGuestId());
                    CustomizationSpec template = new CustomizationSpec();
                    cc.customizeNic(vmOverlay.getPrimaryMac(), ctx.child.hostName, nic.address, subnet, template);
                    cc.customizeDns(subnet.dnsServerAddresses, subnet.dnsSearchDomains, template);
                    ManagedObjectReference task = cc.customizeGuest(client.getVm(), template);
                    TaskInfo taskInfo = VimUtils.waitTaskEnd(connection, task);
                    if (taskInfo.getState() == TaskInfoState.ERROR) {
                        VimUtils.rethrow(taskInfo.getError());
                    }
                }
            }
            // power on machine before enrichment
            if (ctx.child.powerState == PowerState.ON) {
                new PowerStateClient(connection).changePowerState(client.getVm(), PowerState.ON, null, 0);
                state.powerState = PowerState.ON;
                Operation op = ctx.mgr.createTaskPatch(TaskStage.FINISHED);
                Boolean awaitIp = CustomProperties.of(ctx.child).getBoolean(ComputeProperties.CUSTOM_PROP_COMPUTE_AWAIT_IP, true);
                if (awaitIp) {
                    Runnable runnable = createCheckForIpTask(ctx.pool, op, client.getVm(), connection.createUnmanagedCopy(), ctx.child.documentSelfLink, ctx);
                    ctx.pool.schedule(runnable, IP_CHECK_INTERVAL_SECONDS, TimeUnit.SECONDS);
                } else {
                    finishTask[0] = op;
                }
            } else {
                // only finish the task without waiting for IP
                finishTask[0] = ctx.mgr.createTaskPatch(TaskStage.FINISHED);
            }
            updateNicsAfterProvisionSuccess(vmOverlay.getNics(), ctx);
            updateDiskLinksAfterProvisionSuccess(state, vmOverlay.getDisks(), ctx);
            state.lifecycleState = LifecycleState.READY;
            // Find the host link where the computed is provisioned and patch the
            // compute state.
            queryHostDocumentAndUpdateCompute(ctx, vmOverlay.getHost()).thenCompose(computeState -> {
                ComputeState hostState = computeState.iterator().next();
                CustomProperties.of(state).put(VC_UUID, CustomProperties.of(hostState).getString(VC_UUID)).put(DATACENTER_SELF_LINK, CustomProperties.of(hostState).getString(DATACENTER_SELF_LINK)).put(COMPUTE_HOST_LINK_PROP_NAME, hostState.documentSelfLink);
                return createComputeResourcePatch(state, ctx.computeReference);
            }).whenComplete((o, e) -> {
                if (e != null) {
                    ctx.fail(e);
                    return;
                }
                if (finishTask.length > 0) {
                    finishTask[0].sendWith(this);
                }
            });
        } catch (Exception e) {
            ctx.fail(e);
        }
    });
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) IpAssignment(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.IpAssignment) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) DiskType(com.vmware.photon.controller.model.resources.DiskService.DiskType) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) COMPUTE_HOST_LINK_PROP_NAME(com.vmware.photon.controller.model.ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) VirtualDevice(com.vmware.vim25.VirtualDevice) TaskInfo(com.vmware.vim25.TaskInfo) NsxProperties(com.vmware.photon.controller.model.adapters.vsphere.network.NsxProperties) CustomizationSpec(com.vmware.vim25.CustomizationSpec) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Collectors(java.util.stream.Collectors) ClientUtils.handleVirtualDiskUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDiskUpdate) List(java.util.List) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) DeferredResult(com.vmware.xenon.common.DeferredResult) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) ClientUtils.handleVirtualDeviceUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDeviceUpdate) VC_UUID(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties.VC_UUID) VirtualFloppy(com.vmware.vim25.VirtualFloppy) VimPath(com.vmware.photon.controller.model.adapters.vsphere.util.VimPath) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) HashMap(java.util.HashMap) GetMoRef(com.vmware.photon.controller.model.adapters.vsphere.util.connection.GetMoRef) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) StringUtil(io.netty.util.internal.StringUtil) DvsProperties(com.vmware.photon.controller.model.adapters.vsphere.network.DvsProperties) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) ComputeInstanceRequest(com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest) VirtualCdrom(com.vmware.vim25.VirtualCdrom) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) 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) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) DATACENTER_SELF_LINK(com.vmware.photon.controller.model.adapters.vsphere.CustomProperties.DATACENTER_SELF_LINK) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) NetworkInterfaceStateWithDetails(com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails) TimeUnit(java.util.concurrent.TimeUnit) VirtualDisk(com.vmware.vim25.VirtualDisk) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) Collections(java.util.Collections) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) NetworkInterfaceStateWithDetails(com.vmware.photon.controller.model.adapters.vsphere.ProvisionContext.NetworkInterfaceStateWithDetails) Operation(com.vmware.xenon.common.Operation) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) TaskInfo(com.vmware.vim25.TaskInfo) CustomizationSpec(com.vmware.vim25.CustomizationSpec) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 27 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method updateNetwork.

private static void updateNetwork(VSphereIncrementalEnumerationService service, NetworkState oldDocument, EnumerationProgress enumerationProgress, NetworkOverlay net) {
    NetworkState networkState = makeNetworkStateFromResults(service, enumerationProgress, net);
    // restore original selfLink
    networkState.documentSelfLink = oldDocument.documentSelfLink;
    networkState.resourcePoolLink = null;
    if (oldDocument.tenantLinks == null) {
        networkState.tenantLinks = enumerationProgress.getTenantLinks();
    }
    service.logFine(() -> String.format("Syncing Network %s", net.getName()));
    Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), oldDocument.documentSelfLink)).setBody(networkState).setCompletion(trackNetwork(enumerationProgress, net)).sendWith(service);
    // represent a Network also as a subnet
    SubnetState subnet = createSubnetStateForNetwork(networkState, enumerationProgress, net);
    if (subnet != null) {
        Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), subnet.documentSelfLink)).setBody(subnet).sendWith(service);
    }
}
Also used : NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 28 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method makeSubnetStateFromResults.

private static SubnetState makeSubnetStateFromResults(EnumerationProgress enumerationProgress, NetworkOverlay net) {
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    SubnetState state = new SubnetState();
    state.id = state.name = net.getName();
    state.endpointLink = enumerationProgress.getRequest().endpointLink;
    AdapterUtils.addToEndpointLinks(state, enumerationProgress.getRequest().endpointLink);
    ManagedObjectReference parentSwitch = net.getParentSwitch();
    state.networkLink = buildStableDvsLink(parentSwitch, request.endpointLink);
    state.regionId = enumerationProgress.getRegionId();
    CustomProperties customProperties = CustomProperties.of(state).put(CustomProperties.MOREF, net.getId()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.TYPE, net.getId().getType());
    customProperties.put(DvsProperties.PORT_GROUP_KEY, net.getPortgroupKey());
    return state;
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 29 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method createNewNetwork.

private static void createNewNetwork(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, NetworkOverlay net) {
    NetworkState networkState = makeNetworkStateFromResults(service, enumerationProgress, net);
    networkState.tenantLinks = enumerationProgress.getTenantLinks();
    Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), NetworkService.FACTORY_LINK)).setBody(networkState).setCompletion((o, e) -> {
        trackNetwork(enumerationProgress, net).handle(o, e);
        Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ResourceGroupService.FACTORY_LINK)).setBody(makeNetworkGroup(net, enumerationProgress)).sendWith(service);
    }).sendWith(service);
    service.logFine(() -> String.format("Found new Network %s", net.getName()));
    SubnetState subnet = createSubnetStateForNetwork(networkState, enumerationProgress, net);
    if (subnet != null) {
        Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), SubnetService.FACTORY_LINK)).setBody(subnet).sendWith(service);
    }
}
Also used : 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) ServiceDocument(com.vmware.xenon.common.ServiceDocument) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) DvsProperties(com.vmware.photon.controller.model.adapters.vsphere.network.DvsProperties) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) VsphereEnumerationHelper.convertOnlyResultToDocument(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.convertOnlyResultToDocument) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) URI(java.net.URI) NsxProperties(com.vmware.photon.controller.model.adapters.vsphere.network.NsxProperties) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) SubnetService(com.vmware.photon.controller.model.resources.SubnetService) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) AdapterUriUtil(com.vmware.photon.controller.model.adapters.util.AdapterUriUtil) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) OpaqueNetworkSummary(com.vmware.vim25.OpaqueNetworkSummary) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) UriUtils(com.vmware.xenon.common.UriUtils) VsphereEnumerationHelper.withTaskResults(com.vmware.photon.controller.model.adapters.vsphere.VsphereEnumerationHelper.withTaskResults) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) MoRefKeyedMap(com.vmware.photon.controller.model.adapters.vsphere.util.MoRefKeyedMap) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Example 30 with SubnetState

use of com.vmware.photon.controller.model.resources.SubnetService.SubnetState in project photon-model by vmware.

the class VSphereNetworkEnumerationHelper method updateSubnet.

private static void updateSubnet(VSphereIncrementalEnumerationService service, SubnetState oldDocument, EnumerationProgress enumerationProgress, NetworkOverlay net, boolean fullUpdate) {
    SubnetState state;
    if (fullUpdate) {
        state = makeSubnetStateFromResults(enumerationProgress, net);
    } else {
        state = makeSubnetStateFromChanges(net);
    }
    state.documentSelfLink = oldDocument.documentSelfLink;
    if (oldDocument.tenantLinks == null) {
        state.tenantLinks = enumerationProgress.getTenantLinks();
    }
    service.logFine(() -> String.format("Syncing Subnet(Portgroup) %s", net.getName()));
    Operation.createPatch(PhotonModelUriUtils.createInventoryUri(service.getHost(), oldDocument.documentSelfLink)).setBody(state).setCompletion(trackNetwork(enumerationProgress, net)).sendWith(service);
}
Also used : SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Aggregations

SubnetState (com.vmware.photon.controller.model.resources.SubnetService.SubnetState)61 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)22 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)16 Operation (com.vmware.xenon.common.Operation)14 Query (com.vmware.xenon.services.common.QueryTask.Query)12 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)11 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)11 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)11 URI (java.net.URI)11 DeferredResult (com.vmware.xenon.common.DeferredResult)10 HashMap (java.util.HashMap)10 List (java.util.List)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 StatelessService (com.vmware.xenon.common.StatelessService)9 SubnetService (com.vmware.photon.controller.model.resources.SubnetService)8 UriUtils (com.vmware.xenon.common.UriUtils)8 TagsUtil.newTagState (com.vmware.photon.controller.model.adapters.util.TagsUtil.newTagState)7 NetworkService (com.vmware.photon.controller.model.resources.NetworkService)7 TagState (com.vmware.photon.controller.model.resources.TagService.TagState)7