private static ManagedObjectReference searchSnapshot( List<VirtualMachineSnapshotTree> tree, String id) { if (tree == null) { return null; } for (VirtualMachineSnapshotTree snapshot : tree) { if (snapshot.getSnapshot().getValue().equals(id)) { return snapshot.getSnapshot(); } ManagedObjectReference mor = searchSnapshot( snapshot.getChildSnapshotList(), id); if (mor != null) { return mor; } } return null; }
private void processSnapshot(VirtualMachineSnapshotTree current, String parentLink, EnumerationProgress enumerationProgress, VmOverlay vm, String vmSelfLink) { QueryTask task = queryForSnapshot(enumerationProgress, current.getId().toString(), vmSelfLink); withTaskResults(task, (ServiceDocumentQueryResult result) -> { SnapshotState snapshotState = constructSnapshot(current, parentLink, vmSelfLink, enumerationProgress, vm); if (result.documentLinks.isEmpty()) { createSnapshot(snapshotState) .thenCompose(createdSnapshotState -> trackAndProcessChildSnapshots(current, enumerationProgress, vm, vmSelfLink, createdSnapshotState)); } else { SnapshotState oldState = convertOnlyResultToDocument(result, SnapshotState.class); updateSnapshot(enumerationProgress, vm, oldState, snapshotState, current.getId().toString()) .thenCompose(updatedSnapshotState -> trackAndProcessChildSnapshots(current, enumerationProgress, vm, vmSelfLink, updatedSnapshotState)); } enumerationProgress.getSnapshotTracker().arrive(); }); }
static VirtualMachineSnapshot getSnapshotInTree( VirtualMachine vm, String snapName) { if (vm == null || snapName == null) { return null; } VirtualMachineSnapshotTree[] snapTree = vm.getSnapshot().getRootSnapshotList(); if(snapTree!=null) { ManagedObjectReference mor = findSnapshotInTree( snapTree, snapName); if(mor!=null) { return new VirtualMachineSnapshot( vm.getServerConnection(), mor); } } return null; }
public boolean removeAllSnapshots() throws Exception { VirtualMachineSnapshotInfo snapshotInfo = getSnapshotInfo(); if (snapshotInfo != null && snapshotInfo.getRootSnapshotList() != null) { List<VirtualMachineSnapshotTree> tree = snapshotInfo.getRootSnapshotList(); for (VirtualMachineSnapshotTree treeNode : tree) { ManagedObjectReference morTask = _context.getService().removeSnapshotTask(treeNode.getSnapshot(), true, true); boolean result = _context.getVimClient().waitForTask(morTask); if (result) { _context.waitForTaskProgressDone(morTask); } else { s_logger.error("VMware removeSnapshot_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask)); return false; } } } return true; }
public static ManagedObjectReference findSnapshotInTree(List<VirtualMachineSnapshotTree> snapTree, String findName) { assert (findName != null); ManagedObjectReference snapMor = null; if (snapTree == null) return snapMor; for (int i = 0; i < snapTree.size() && snapMor == null; i++) { VirtualMachineSnapshotTree node = snapTree.get(i); if (node.getName().equals(findName)) { snapMor = node.getSnapshot(); } else { List<VirtualMachineSnapshotTree> childTree = node.getChildSnapshotList(); snapMor = findSnapshotInTree(childTree, findName); } } return snapMor; }
public void removeVirtualMachineSnapshot(VirtualMachine vm, String nameVm) throws Exception { logger.info("Launching old snapshot removing process for {}", nameVm); if(vm.getSnapshot() != null) { logger.info("Deleting snapshot ..."); VirtualMachineSnapshotTree[] _stree = vm.getSnapshot().getRootSnapshotList(); if(_stree != null) { for(VirtualMachineSnapshotTree _st : _stree) { if(_st.getName().equals(nameVm)) { logger.info("Old snahpot {} found"); VirtualMachineSnapshot _vmsSnap = new VirtualMachineSnapshot(vm.getServerConnection(), _st.getSnapshot()); Task _taskSnap = _vmsSnap.removeSnapshot_Task(true); logger.info("Removing process launched..."); if(_taskSnap.waitForTask() != Task.SUCCESS) { logger.error("Error on snapshot removing process. {}",_taskSnap.getTaskInfo().getError().getLocalizedMessage()); throw new Exception(_taskSnap.getTaskInfo().getError().getLocalizedMessage()); } logger.info("Snapshot removed successfully"); } } } } }
public List<VirtualMachineSnapshotTree> getRootSnapshotList() { ArrayOfVirtualMachineSnapshotTree arrayOfVirtualMachineSnapshotTree = (ArrayOfVirtualMachineSnapshotTree) getOrDefault( VimPath.vm_snapshot_rootSnapshotList, null); if (arrayOfVirtualMachineSnapshotTree != null) { return arrayOfVirtualMachineSnapshotTree.getVirtualMachineSnapshotTree(); } return null; }
private void processSnapshots(EnumerationProgress enumerationProgress, VmOverlay vm, String vmSelfLink) { if (vmSelfLink != null) { List<VirtualMachineSnapshotTree> rootSnapshotList = vm.getRootSnapshotList(); if (rootSnapshotList != null) { enumerationProgress.getSnapshotTracker().register(); for (VirtualMachineSnapshotTree snapshotTree : rootSnapshotList) { processSnapshot(snapshotTree, null, enumerationProgress, vm, vmSelfLink); } } } }
private DeferredResult<Object> trackAndProcessChildSnapshots(VirtualMachineSnapshotTree current, EnumerationProgress enumerationProgress, VmOverlay vm, String vmSelfLink, SnapshotState updatedSnapshotState) { trackSnapshot(enumerationProgress, vm); List<VirtualMachineSnapshotTree> childSnapshotList = current.getChildSnapshotList(); if (!CollectionUtils.isEmpty(childSnapshotList)) { for (VirtualMachineSnapshotTree childSnapshot : childSnapshotList) { enumerationProgress.getSnapshotTracker().register(); processSnapshot(childSnapshot, updatedSnapshotState.documentSelfLink, enumerationProgress, vm, vmSelfLink); } } return DeferredResult.completed(null); }
private SnapshotState constructSnapshot(VirtualMachineSnapshotTree current, String parentLink, String vmSelfLink, EnumerationProgress enumerationProgress, VmOverlay vm) { SnapshotState snapshot = new SnapshotState(); snapshot.computeLink = vmSelfLink; snapshot.parentLink = parentLink; snapshot.description = current.getDescription(); //TODO how to determine if the snapshot is current //snapshot.isCurrent = current.isQuiesced() snapshot.creationTimeMicros = current.getCreateTime().toGregorianCalendar().getTimeInMillis(); //TODO How to fetch custom properties //snapshot.customProperties = current.get //TODO what are snapshot grouplinks //snapshot.groupLinks snapshot.name = current.getName(); snapshot.regionId = enumerationProgress.getRegionId(); snapshot.id = current.getId().toString(); populateTags(enumerationProgress, vm, snapshot); snapshot.tenantLinks = enumerationProgress.getTenantLinks(); if (snapshot.endpointLinks == null) { snapshot.endpointLinks = new HashSet<String>(); } snapshot.endpointLinks.add(enumerationProgress.getRequest().endpointLink); CustomProperties.of(snapshot) .put(CustomProperties.MOREF, VimUtils.convertMoRefToString(current.getSnapshot())) .put(CustomProperties.TYPE, current.getSnapshot().getType()); return snapshot; }
static ManagedObjectReference findSnapshotInTree( VirtualMachineSnapshotTree[] snapTree, String snapName) { for(int i=0; i <snapTree.length; i++) { VirtualMachineSnapshotTree node = snapTree[i]; if(snapName.equals(node.getName())) { return node.getSnapshot(); } else { VirtualMachineSnapshotTree[] childTree = node.getChildSnapshotList(); if(childTree!=null) { ManagedObjectReference mor = findSnapshotInTree( childTree, snapName); if(mor!=null) { return mor; } } } } return null; }
static void listSnapshots(VirtualMachine vm) { if(vm==null) { return; } VirtualMachineSnapshotInfo snapInfo = vm.getSnapshot(); VirtualMachineSnapshotTree[] snapTree = snapInfo.getRootSnapshotList(); printSnapshots(snapTree); }
static void printSnapshots( VirtualMachineSnapshotTree[] snapTree) { for (int i = 0; snapTree!=null && i < snapTree.length; i++) { VirtualMachineSnapshotTree node = snapTree[i]; System.out.println("Snapshot Name : " + node.getName()); VirtualMachineSnapshotTree[] childTree = node.getChildSnapshotList(); if(childTree!=null) { printSnapshots(childTree); } } }
public ManagedObjectReference getSnapshotMor(String snapshotName) throws Exception { VirtualMachineSnapshotInfo info = getSnapshotInfo(); if (info != null) { List<VirtualMachineSnapshotTree> snapTree = info.getRootSnapshotList(); return VmwareHelper.findSnapshotInTree(snapTree, snapshotName); } return null; }
public boolean hasSnapshot() throws Exception { VirtualMachineSnapshotInfo info = getSnapshotInfo(); if (info != null) { ManagedObjectReference currentSnapshot = info.getCurrentSnapshot(); if (currentSnapshot != null) { return true; } List<VirtualMachineSnapshotTree> rootSnapshotList = info.getRootSnapshotList(); if (rootSnapshotList != null && rootSnapshotList.size() > 0) { return true; } } return false; }
@NotNull Map<String, VirtualMachineSnapshotTree> getSnapshotList(String vmName) throws VmwareCheckedCloudException;
public Map<String, String> getVirtualMachine(String name) throws Exception { if(name == null || name.isEmpty()) { throw new Exception("invalid virtual machine name"); } Map<String,String> _machine = new HashMap<String, String>(); ServiceInstance si = new ServiceInstance(new URL(this._url), this._user, this._password, true); try { Folder rootFolder = si.getRootFolder(); VirtualMachine _vm = (VirtualMachine) new InventoryNavigator(rootFolder).searchManagedEntity("VirtualMachine", name); if(_vm == null) { throw new Exception("virtual machine not found"); } StringBuilder _sb = new StringBuilder(); _machine.put("name", _vm.getName()); for(Datastore _ds : _vm.getDatastores()) { if(_sb.length() > 0) { _sb.append(":"); } _sb.append(_ds.getName()); } _machine.put("datastore", _sb.toString()); _sb = new StringBuilder(); for(Network _nw : _vm.getNetworks()) { if(_sb.length() > 0) { _sb.append(":"); } _sb.append(_nw.getName()); } _machine.put("network", _sb.toString()); _sb = new StringBuilder(); VirtualMachineSnapshotTree[] _stree = _vm.getSnapshot().getRootSnapshotList(); if(_stree != null) { for(VirtualMachineSnapshotTree _st : _stree) { if(_sb.length() > 0) { _sb.append(":"); } _sb.append(_st.getName()); } _machine.put("snapshot", _sb.toString()); } else { _machine.put("snapshot", ""); } } catch(Exception _ex) { throw new Exception("hypervisor error - " + _ex.getMessage()); } finally { si.getServerConnection().logout(); } return _machine; }