use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection in project photon-model by vmware.
the class VSphereAdapterSnapshotService method createSnapshot.
private void createSnapshot(Connection connection, SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
ManagedObjectReference vmMoRef = CustomProperties.of(context.computeDescription).getMoRef(CustomProperties.MOREF);
if (vmMoRef == null) {
deferredResult.fail(new IllegalStateException("Cannot find VM to snapshot"));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Creating snapshot for compute resource %s", context.computeDescription.name);
task = connection.getVimPort().createSnapshotTask(vmMoRef, context.snapshotState.name, context.snapshotState.description, context.snapshotMemory, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
deferredResult.fail(e);
return;
}
CustomProperties.of(context.snapshotState).put(CustomProperties.MOREF, (ManagedObjectReference) info.getResult());
// mark this as current snapshot
context.snapshotState.isCurrent = true;
// create a new xenon SnapshotState
logInfo(String.format("Creating a new snapshot state for compute : %s", context.computeDescription.name));
Operation createSnapshotState = Operation.createPost(PhotonModelUriUtils.createInventoryUri(getHost(), SnapshotService.FACTORY_LINK)).setBody(context.snapshotState);
if (context.existingSnapshotState != null) {
// un-mark old snapshot as current snapshot
context.existingSnapshotState.isCurrent = false;
Operation patchOldSnapshot = Operation.createPatch(UriUtils.buildUri(getHost(), context.existingSnapshotState.documentSelfLink)).setBody(context.existingSnapshotState);
OperationSequence.create(createSnapshotState).next(patchOldSnapshot).setCompletion((o, e) -> {
if (e != null && !e.isEmpty()) {
deferredResult.fail(e.values().iterator().next());
}
deferredResult.complete(context);
}).sendWith(this);
} else {
context.computeDescription.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, "true");
// patch compute adding the property '_hasSnapshots' with true
Operation patchCompute = Operation.createPatch(UriUtils.buildUri(getHost(), context.snapshotState.computeLink)).setBody(context.computeDescription);
OperationSequence.create(createSnapshotState).next(patchCompute).setCompletion((o, e) -> {
if (e != null && !e.isEmpty()) {
deferredResult.fail(e.values().iterator().next());
}
logInfo(String.format("Created a new snapshot state for compute : %s", context.computeDescription.name));
deferredResult.complete(context);
}).sendWith(this);
}
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method handlePatch.
@Override
public void handlePatch(Operation patch) {
// complete the patch immediately.
patch.complete();
logInfo("Received PATCH for incremental enumeration!");
VSphereIncrementalEnumerationRequest enumerationRequest = patch.getBody(VSphereIncrementalEnumerationRequest.class);
ComputeEnumerateResourceRequest request = enumerationRequest.request;
URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
logInfo("Creating task manager!");
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
logInfo(" Requesting GET on compute state with description!.");
Operation.createGet(parentUri).setCompletion(o -> {
logInfo("Submitting job to threadpool!");
VsphereEnumerationHelper.submitWorkToVSpherePool(this, () -> {
logInfo("Incremental enumeration job started for endpoint %s", enumerationRequest.request.endpointLink);
ComputeStateWithDescription computeStateWithDesc = o.getBody(ComputeStateWithDescription.class);
VapiConnection vapiConnection = VapiConnection.createFromVimConnection(this.connection);
logInfo("Establishing VAPI connection for endpoint %s", enumerationRequest.request.endpointLink);
try {
vapiConnection.login();
} catch (IOException | RpcException rpce) {
logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(rpce)));
mgr.patchTaskToFailure(rpce);
// self delete service so that full enumeration kicks in next invocation.
selfDeleteService();
return;
}
try {
// Get instanceUuid of the vCenter
AboutInfo vCenter = this.connection.getServiceContent().getAbout();
for (CollectorDetails collectorDetails : this.collectors) {
logInfo("Retrieving resources incremental data for data center: %s", collectorDetails.datacenter);
EnumerationProgress enumerationProgress = new EnumerationProgress(new HashSet<>(), request, computeStateWithDesc, vapiConnection, collectorDetails.datacenter, vCenter.getInstanceUuid());
EnumerationClient client = new EnumerationClient(this.connection, computeStateWithDesc, VimUtils.convertStringToMoRef(collectorDetails.datacenter));
List<ObjectUpdate> resourcesUpdates = collectResourcesData(collectorDetails);
List<ObjectUpdate> vmUpdates = collectVMData(collectorDetails);
logInfo("Received resources updates for datacenter: %s : %s", collectorDetails.datacenter, resourcesUpdates.size());
logInfo("Received vm updates for datacenter: %s : %s", collectorDetails.datacenter, vmUpdates.size());
logInfo("Resources Updates: %s", Utils.toJson(resourcesUpdates));
logInfo("VM Updates: %s", Utils.toJson(vmUpdates));
if (!resourcesUpdates.isEmpty()) {
SegregatedOverlays segregatedOverlays = segregateObjectUpdates(enumerationProgress, resourcesUpdates);
this.logInfo("Processing incremental changes for folders for datacenter [%s]!", collectorDetails.datacenter);
VsphereFolderEnumerationHelper.handleFolderChanges(this, segregatedOverlays.folders, enumerationProgress, client);
logInfo("Processing incremental changes for networks for datacenter [%s]!", collectorDetails.datacenter);
VSphereNetworkEnumerationHelper.handleNetworkChanges(this, segregatedOverlays.networks, enumerationProgress, client);
logInfo("Processing incremental changes for Datastores for datacenter [%s]!", collectorDetails.datacenter);
VsphereDatastoreEnumerationHelper.handleDatastoreChanges(this, segregatedOverlays.datastores, enumerationProgress);
logInfo("Processing incremental changes for compute resource for datacenter [%s]!", collectorDetails.datacenter);
VsphereComputeResourceEnumerationHelper.handleComputeResourceChanges(this, segregatedOverlays.clusters, enumerationProgress, client);
logInfo("Processing incremental changes for host system for datacenter [%s]!", collectorDetails.datacenter);
VSphereHostSystemEnumerationHelper.handleHostSystemChanges(this, segregatedOverlays.hosts, enumerationProgress, client);
logInfo("Processing incremental changes for resource pool for datacenter [%s]!", collectorDetails.datacenter);
VSphereResourcePoolEnumerationHelper.handleResourcePoolChanges(this, segregatedOverlays.resourcePools, enumerationProgress, client);
}
if (!vmUpdates.isEmpty()) {
logInfo("Processing incremental changes for virtual machines for datacenter [%s]!", collectorDetails.datacenter);
VSphereVirtualMachineEnumerationHelper.handleVMChanges(this, vmUpdates, enumerationProgress, client);
}
// sync storage profiles
logInfo("Syncing storage profiles for datacenter [%s]!", collectorDetails.datacenter);
VsphereStoragePolicyEnumerationHelper.syncStorageProfiles(this, client, enumerationProgress);
}
mgr.patchTask(TaskStage.FINISHED);
} catch (Exception exception) {
String msg = "Error processing PropertyCollector results during incremental retrieval";
logWarning(() -> msg + ": " + exception.toString());
mgr.patchTaskToFailure(exception);
// self delete service so that full enumeration kicks in next invocation.
// TODO: This is not complete. We need to enable owner selection on this service.
selfDeleteService();
return;
} finally {
vapiConnection.close();
}
});
}, mgr).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection in project photon-model by vmware.
the class VSphereAdapterSnapshotService method revertSnapshot.
private void revertSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
SnapshotState existingSnapshotState = context.existingSnapshotState;
// Physical snapshot processing
ManagedObjectReference snapshotMoref = CustomProperties.of(snapshot).getMoRef(CustomProperties.MOREF);
if (snapshotMoref == null) {
deferredResult.fail(new IllegalStateException(String.format("Cannot find the snapshot %s to revert to", snapshotMoref)));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Reverting to snapshot with name %s", context.snapshotState.name);
task = connection.getVimPort().revertToSnapshotTask(snapshotMoref, null, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
logSevere("Reverting to the snapshot %s failed", context.snapshotState.name);
deferredResult.fail(e);
return;
}
// cause concurrency issue
if (snapshot.isCurrent) {
deferredResult.complete(context);
} else {
snapshot.isCurrent = true;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setBody(snapshot).setReferer(getUri()));
if (existingSnapshotState != null) {
existingSnapshotState.isCurrent = false;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), existingSnapshotState.documentSelfLink)).setBody(existingSnapshotState).setReferer(getUri()));
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
}
Aggregations