use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method handleStart.
@Override
public void handleStart(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
VSphereIncrementalEnumerationRequest enumerationRequest = op.getBody(VSphereIncrementalEnumerationRequest.class);
ComputeEnumerateResourceRequest request = enumerationRequest.request;
validate(request);
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
Operation.createGet(parentUri).setCompletion(o -> {
thenWithParentState(request, o.getBody(ComputeService.ComputeStateWithDescription.class), mgr);
}, mgr).sendWith(this);
}
use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.
the class VSphereVirtualMachineEnumerationHelper method makeVmFromResults.
/**
* Make a ComputeState from the request and a vm found in vsphere.
*/
static ComputeState makeVmFromResults(EnumerationProgress enumerationProgress, VmOverlay vm) {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
ComputeState state = new ComputeState();
state.type = ComputeType.VM_GUEST;
state.environmentName = ComputeDescription.ENVIRONMENT_NAME_ON_PREMISE;
state.endpointLink = request.endpointLink;
AdapterUtils.addToEndpointLinks(state, request.endpointLink);
state.adapterManagementReference = request.adapterManagementReference;
state.parentLink = request.resourceLink();
state.resourcePoolLink = request.resourcePoolLink;
state.instanceAdapterReference = enumerationProgress.getParent().description.instanceAdapterReference;
state.enumerationAdapterReference = enumerationProgress.getParent().description.enumerationAdapterReference;
state.powerAdapterReference = enumerationProgress.getParent().description.powerAdapterReference;
state.regionId = enumerationProgress.getRegionId();
state.cpuCount = (long) vm.getNumCpu();
state.totalMemoryBytes = vm.getMemoryBytes();
state.hostName = vm.getHostName();
state.powerState = vm.getPowerState();
state.primaryMAC = vm.getPrimaryMac();
if (!vm.isTemplate()) {
state.address = vm.guessPublicIpV4Address();
}
state.id = vm.getInstanceUuid();
state.name = vm.getName();
CustomProperties.of(state).put(CustomProperties.MOREF, vm.getId()).put(CustomProperties.TYPE, VimNames.TYPE_VM).put(CustomProperties.VM_SOFTWARE_NAME, vm.getOS()).put(CustomProperties.DATACENTER_SELF_LINK, enumerationProgress.getDcLink()).put(CustomProperties.DATACENTER, enumerationProgress.getRegionId()).put(CustomProperties.VM_MEMORY_IN_GB, AdapterUtils.convertBytesToGB(vm.getMemoryBytes()));
VsphereEnumerationHelper.populateResourceStateWithAdditionalProps(state, enumerationProgress.getVcUuid());
String selfLink = enumerationProgress.getHostSystemTracker().getSelfLink(vm.getHost());
if (null != selfLink) {
CustomProperties.of(state).put(ComputeProperties.COMPUTE_HOST_LINK_PROP_NAME, selfLink);
}
return state;
}
use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.
the class VsphereComputeResourceEnumerationHelper method processFoundComputeResource.
/**
* Either creates a new Compute or update an already existing one. Existence is checked by
* querying for a compute with id equals to moref value of a cluster whose parent is the Compute
* from the request.
*/
public static void processFoundComputeResource(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, ComputeResourceOverlay cr, EnumerationClient client) {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
QueryTask task = queryForCluster(enumerationProgress, request.resourceLink(), cr.getId().getValue());
withTaskResults(service, task, result -> {
try {
if (result.documentLinks.isEmpty()) {
createNewComputeResource(service, enumerationProgress, cr, client);
} else {
ComputeState oldDocument = convertOnlyResultToDocument(result, ComputeState.class);
updateCluster(service, oldDocument, enumerationProgress, cr, client, true);
}
if (!cr.getVsanHostConfig().isEmpty()) {
processServerDisks(service, cr.getVsanHostConfig(), enumerationProgress);
}
} catch (Exception e) {
// if there's an error while processing compute resource
service.logSevere("Error occurred while processing update of compute resource!", e);
enumerationProgress.getComputeResourceTracker().track(cr.getId(), ResourceTracker.ERROR);
}
});
}
use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.
the class VSphereAdapterResourceEnumerationService method handlePatch.
/**
* On every enumeration request, we check if there's an existing stateless service and delete it.
* This is needed to ensure no two enumeration processes are happening for same endpoint.
* After deleting the service, we create a new stateless service by issuing POST on the factory of
* stateless service and passing the last path segment of endpoint document selflink.
*
* @param op the enumeration operation request
*/
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ComputeEnumerateResourceRequest request = op.getBody(ComputeEnumerateResourceRequest.class);
validate(request);
op.setStatusCode(Operation.STATUS_CODE_CREATED);
op.complete();
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
if (request.isMockRequest) {
// just finish the mock request
mgr.patchTask(TaskState.TaskStage.FINISHED);
return;
}
String eventBasedEnumerationServiceURI = UriUtils.buildUriPath(VSphereIncrementalEnumerationService.FACTORY_LINK, UriUtils.getLastPathSegment(request.endpointLink));
URI uri = buildUri(this.getHost(), eventBasedEnumerationServiceURI);
VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest enumerationRequest = new VSphereIncrementalEnumerationService.VSphereIncrementalEnumerationRequest();
enumerationRequest.documentSelfLink = UriUtils.getLastPathSegment(eventBasedEnumerationServiceURI);
enumerationRequest.request = request;
// if yes, patch it. If no, create new one via post to factory.
if (EnumerationAction.START.equals(request.enumerationAction)) {
Operation.createGet(uri).setCompletion((o, e) -> {
if (null != e || o.getStatusCode() == Operation.STATUS_CODE_NOT_FOUND) {
// service not found, create a new one
createNewEnumerationService(enumerationRequest, mgr);
} else {
logInfo("Patching incremental service for incremental enumeration for %s", enumerationRequest.documentSelfLink);
Operation patchRequest = Operation.createPatch(uri).setBody(enumerationRequest).setCompletion((operation, err) -> {
if (err != null) {
logSevere("Unable to send enumeration request to enumeration" + " service for endpoint %s", enumerationRequest.documentSelfLink);
mgr.patchTaskToFailure(err);
}
});
patchRequest.sendWith(this);
}
}).sendWith(this);
} else {
// Stop any existing enumeration process when REFRESH is received.
logFine("Deleting the incremental enumeration service.");
Operation deleteRequest = Operation.createDelete(uri).setCompletion((o, e) -> {
if (null != e) {
logWarning("Delete of enumeration service failed for endpoint " + enumerationRequest.documentSelfLink + " Message: %s", e.getMessage());
}
// The delete service call will take care of stopping any running enumeration.
if (request.enumerationAction == EnumerationAction.STOP) {
logInfo("Successfully stopped enumeration for endpoint " + request.endpointLink);
mgr.patchTask(TaskState.TaskStage.FINISHED);
} else {
logInfo("Creating the enumeration service for endpoint %s", request.endpointLink);
createNewEnumerationService(enumerationRequest, mgr);
}
});
deleteRequest.sendWith(this);
}
}
use of com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest in project photon-model by vmware.
the class VSphereHostSystemEnumerationHelper method handleHostSystemChanges.
public static void handleHostSystemChanges(VSphereIncrementalEnumerationService service, List<HostSystemOverlay> hostSystemOverlays, EnumerationProgress enumerationProgress, EnumerationClient client) {
enumerationProgress.expectHostSystemCount(hostSystemOverlays.size());
for (HostSystemOverlay hs : hostSystemOverlays) {
try {
if (ObjectUpdateKind.ENTER.equals(hs.getObjectUpdateKind())) {
createNewHostSystem(service, enumerationProgress, hs, client);
} else {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
QueryTask task = queryForHostSystem(enumerationProgress, request.resourceLink(), hs.getId().getValue());
withTaskResults(service, task, result -> {
if (!result.documentLinks.isEmpty()) {
try {
ComputeState oldDocument = convertOnlyResultToDocument(result, ComputeState.class);
if (ObjectUpdateKind.MODIFY.equals(hs.getObjectUpdateKind())) {
updateHostSystem(service, oldDocument, enumerationProgress, hs, false, client);
} else {
deleteHostSystem(service, enumerationProgress, hs, oldDocument);
}
} catch (Exception ex) {
service.logSevere("Error occurred while processing host system!", ex);
enumerationProgress.getHostSystemTracker().track();
}
} else {
enumerationProgress.getHostSystemTracker().track();
}
});
}
} catch (Exception ex) {
service.logSevere("Error occurred while processing host system!", ex);
enumerationProgress.getHostSystemTracker().track();
}
}
}
Aggregations