Search in sources :

Example 96 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class VSphereVMSnapshotEnumerationHelper method enumerateSnapshots.

static void enumerateSnapshots(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, List<VmOverlay> vms) {
    if (CollectionUtils.isEmpty(vms)) {
        return;
    }
    vms.forEach(vm -> {
        // process only those VMs that have snapshots attached.
        if (vm.getRootSnapshotList() != null) {
            ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
            QueryTask task = VSphereVirtualMachineEnumerationHelper.queryForVm(enumerationProgress, request.resourceLink(), vm.getInstanceUuid(), null);
            VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                ComputeState computeState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
                processSnapshots(service, enumerationProgress, vm, computeState.documentSelfLink);
            });
        }
    });
    enumerationProgress.getSnapshotTracker().arriveAndDeregister();
}
Also used : 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 97 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method createNewVm.

static void createNewVm(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, VmOverlay vm) {
    ComputeDescription desc = makeDescriptionForVm(service, enumerationProgress, vm);
    desc.tenantLinks = enumerationProgress.getTenantLinks();
    Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ComputeDescriptionService.FACTORY_LINK)).setBody(desc).sendWith(service);
    ComputeState state = makeVmFromResults(enumerationProgress, vm);
    state.descriptionLink = desc.documentSelfLink;
    state.tenantLinks = enumerationProgress.getTenantLinks();
    List<Operation> operations = new ArrayList<>();
    VsphereEnumerationHelper.submitWorkToVSpherePool(service, () -> {
        VsphereEnumerationHelper.populateTags(service, enumerationProgress, vm, state);
        state.networkInterfaceLinks = new ArrayList<>();
        Map<String, List<String>> nicToIPv4Addresses = vm.getMapNic2IpV4Addresses();
        for (VirtualEthernetCard nic : vm.getNics()) {
            VirtualDeviceBackingInfo backing = nic.getBacking();
            if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
                VirtualEthernetCardNetworkBackingInfo veth = (VirtualEthernetCardNetworkBackingInfo) backing;
                NetworkInterfaceState iface = new NetworkInterfaceState();
                iface.networkLink = enumerationProgress.getNetworkTracker().getSelfLink(veth.getNetwork());
                iface.name = nic.getDeviceInfo().getLabel();
                iface.documentSelfLink = buildUriPath(NetworkInterfaceService.FACTORY_LINK, service.getHost().nextUUID());
                iface.address = getPrimaryIPv4Address(nic, nicToIPv4Addresses);
                CustomProperties.of(iface).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink());
                Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), NetworkInterfaceService.FACTORY_LINK)).setBody(iface).sendWith(service);
                state.networkInterfaceLinks.add(iface.documentSelfLink);
            } else if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
                VirtualEthernetCardDistributedVirtualPortBackingInfo veth = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
                String portgroupKey = veth.getPort().getPortgroupKey();
                Operation op = addNewInterfaceState(service, enumerationProgress, state, portgroupKey, InterfaceStateMode.INTERFACE_STATE_WITH_DISTRIBUTED_VIRTUAL_PORT, SubnetState.class, SubnetState.class, getPrimaryIPv4Address(nic, nicToIPv4Addresses));
                if (op != null) {
                    operations.add(op);
                }
            } else if (backing instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
                VirtualEthernetCardOpaqueNetworkBackingInfo veth = (VirtualEthernetCardOpaqueNetworkBackingInfo) backing;
                String opaqueNetworkId = veth.getOpaqueNetworkId();
                Operation op = addNewInterfaceState(service, enumerationProgress, state, opaqueNetworkId, InterfaceStateMode.INTERFACE_STATE_WITH_OPAQUE_NETWORK, NetworkState.class, NetworkState.class, getPrimaryIPv4Address(nic, nicToIPv4Addresses));
                if (op != null) {
                    operations.add(op);
                }
            } else {
                // TODO add support for DVS
                service.logFine(() -> String.format("Will not add nic of type %s", backing.getClass().getName()));
            }
        }
        // Process all the disks attached to the VM
        List<VirtualDevice> disks = vm.getDisks();
        if (CollectionUtils.isNotEmpty(disks)) {
            state.diskLinks = new ArrayList<>(disks.size());
            for (VirtualDevice device : disks) {
                Operation vdOp = processVirtualDevice(service, null, device, enumerationProgress, state.diskLinks, VimUtils.convertMoRefToString(vm.getId()), null);
                if (vdOp != null) {
                    operations.add(vdOp);
                }
            }
        }
        service.logFine(() -> String.format("Found new VM %s", vm.getInstanceUuid()));
        if (operations.isEmpty()) {
            Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ComputeService.FACTORY_LINK)).setBody(state).setCompletion(trackVm(enumerationProgress)).sendWith(service);
        } else {
            OperationJoin.create(operations).setCompletion((operationMap, exception) -> {
                Operation.createPost(PhotonModelUriUtils.createInventoryUri(service.getHost(), ComputeService.FACTORY_LINK)).setBody(state).setCompletion(updateVirtualDisksAndTrackVm(service, enumerationProgress, operationMap)).sendWith(service);
            }).sendWith(service);
        }
    });
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) LifecycleState(com.vmware.photon.controller.model.resources.ComputeService.LifecycleState) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) Map(java.util.Map) URI(java.net.URI) VirtualDevice(com.vmware.vim25.VirtualDevice) NsxProperties(com.vmware.photon.controller.model.adapters.vsphere.network.NsxProperties) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) DiskStateExpanded(com.vmware.photon.controller.model.resources.DiskService.DiskStateExpanded) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ClientUtils.handleVirtualDiskUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDiskUpdate) List(java.util.List) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) CompletionHandler(com.vmware.xenon.common.Operation.CompletionHandler) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) ClientUtils.handleVirtualDeviceUpdate(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.handleVirtualDeviceUpdate) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) DiskService(com.vmware.photon.controller.model.resources.DiskService) VirtualFloppy(com.vmware.vim25.VirtualFloppy) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) ResourceCleanRequest(com.vmware.photon.controller.model.adapters.vsphere.VsphereResourceCleanerService.ResourceCleanRequest) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) ObjectUpdateKind(com.vmware.vim25.ObjectUpdateKind) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) ArrayList(java.util.ArrayList) VirtualCdrom(com.vmware.vim25.VirtualCdrom) ClientUtils.findMatchingDiskState(com.vmware.photon.controller.model.adapters.vsphere.ClientUtils.findMatchingDiskState) CollectionUtils(org.apache.commons.collections.CollectionUtils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) VimNames(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TYPE_PORTGROUP(com.vmware.photon.controller.model.adapters.vsphere.util.VimNames.TYPE_PORTGROUP) VirtualDisk(com.vmware.vim25.VirtualDisk) UriUtils.buildUriPath(com.vmware.xenon.common.UriUtils.buildUriPath) InterfaceStateMode(com.vmware.photon.controller.model.adapters.vsphere.VSphereIncrementalEnumerationService.InterfaceStateMode) ObjectUpdate(com.vmware.vim25.ObjectUpdate) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) VirtualEthernetCardOpaqueNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardOpaqueNetworkBackingInfo) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) ArrayList(java.util.ArrayList) VirtualDevice(com.vmware.vim25.VirtualDevice) Operation(com.vmware.xenon.common.Operation) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) List(java.util.List) ArrayList(java.util.ArrayList) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard)

Example 98 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method handleVMChanges.

public static void handleVMChanges(VSphereIncrementalEnumerationService service, List<ObjectUpdate> resourcesUpdates, EnumerationProgress enumerationProgress, EnumerationClient client) {
    List<VmOverlay> vmOverlays = new ArrayList<>();
    for (ObjectUpdate objectUpdate : resourcesUpdates) {
        if (VimUtils.isVirtualMachine(objectUpdate.getObj())) {
            VmOverlay vm = new VmOverlay(objectUpdate);
            if (vm.getInstanceUuid() != null || !objectUpdate.getKind().equals(ObjectUpdateKind.ENTER)) {
                vmOverlays.add(vm);
            }
        }
    }
    for (VmOverlay vmOverlay : vmOverlays) {
        if (ObjectUpdateKind.ENTER == vmOverlay.getObjectUpdateKind()) {
            createNewVm(service, enumerationProgress, vmOverlay);
        } else {
            ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
            QueryTask task = queryForVm(enumerationProgress, request.resourceLink(), null, vmOverlay.getId());
            VsphereEnumerationHelper.withTaskResults(service, task, result -> {
                if (!result.documentLinks.isEmpty()) {
                    ComputeState oldDocument = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
                    if (ObjectUpdateKind.MODIFY == vmOverlay.getObjectUpdateKind()) {
                        updateVm(service, oldDocument, enumerationProgress, vmOverlay, false);
                    } else {
                        deleteVM(enumerationProgress, vmOverlay, service, oldDocument);
                    }
                } else {
                    enumerationProgress.getVmTracker().arrive();
                }
            });
        }
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) ArrayList(java.util.ArrayList) ObjectUpdate(com.vmware.vim25.ObjectUpdate)

Example 99 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method processFoundVm.

static void processFoundVm(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, VmOverlay vm) {
    long latestAcceptableModification = System.currentTimeMillis() - VM_FERMENTATION_PERIOD_MILLIS;
    ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
    QueryTask task = queryForVm(enumerationProgress, request.resourceLink(), vm.getInstanceUuid(), vm.getId());
    VsphereEnumerationHelper.withTaskResults(service, task, result -> {
        if (result.documentLinks.isEmpty()) {
            if (vm.getLastReconfigureMillis() < latestAcceptableModification) {
                // If vm is not settled down don't create a new resource
                createNewVm(service, enumerationProgress, vm);
            } else {
                enumerationProgress.getVmTracker().arrive();
            }
        } else {
            // always update state of a vm even if modified recently
            ComputeState oldDocument = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
            updateVm(service, oldDocument, enumerationProgress, vm, true);
        }
    });
}
Also used : 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 100 with ComputeState

use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.

the class VSphereVirtualMachineEnumerationHelper method makeVmFromChanges.

private static ComputeState makeVmFromChanges(VmOverlay vm) {
    ComputeState state = new ComputeState();
    // CPU count can be changed
    state.cpuCount = (long) vm.getNumCpu();
    // Memory can be changed
    if (vm.getMemoryBytes() != 0) {
        state.totalMemoryBytes = vm.getMemoryBytes();
        CustomProperties.of(state).put(CustomProperties.VM_MEMORY_IN_GB, AdapterUtils.convertBytesToGB(vm.getMemoryBytes()));
    }
    // Power state can be changed
    state.powerState = vm.getPowerStateOrNull();
    // Name can be changed
    state.name = vm.getNameOrNull();
    // OS can be changed
    if (null != vm.getOS()) {
        CustomProperties.of(state).put(CustomProperties.VM_SOFTWARE_NAME, vm.getOS());
    }
    return state;
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Aggregations

ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)214 Operation (com.vmware.xenon.common.Operation)93 ArrayList (java.util.ArrayList)63 QueryTask (com.vmware.xenon.services.common.QueryTask)58 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)57 HashMap (java.util.HashMap)54 List (java.util.List)51 Map (java.util.Map)50 Utils (com.vmware.xenon.common.Utils)45 Test (org.junit.Test)45 UriUtils (com.vmware.xenon.common.UriUtils)44 URI (java.net.URI)42 Collectors (java.util.stream.Collectors)42 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)40 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)40 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)40 StatelessService (com.vmware.xenon.common.StatelessService)40 Query (com.vmware.xenon.services.common.QueryTask.Query)40 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)38 ComputeType (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType)36