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();
}
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);
}
});
}
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();
}
});
}
}
}
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);
}
});
}
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;
}
Aggregations