use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister in project photon-model by vmware.
the class DatacenterEnumeratorService method handlePatch.
@Override
public void handlePatch(Operation patch) {
if (!patch.hasBody()) {
patch.fail(new IllegalArgumentException("body is required"));
return;
}
VSphereIOThreadPoolAllocator.getPool(this).submit(() -> {
EnumerateDatacentersRequest req = patch.getBody(EnumerateDatacentersRequest.class);
BasicConnection connection = new BasicConnection();
try {
EnumerateDatacentersResponse res = new EnumerateDatacentersResponse();
if (req.isMock) {
res.datacenters = Collections.singletonList("dc-1");
res.moRefs = Collections.singletonList("Datacenter:dc-1");
} else {
connection.setURI(URI.create("https://" + req.host + "/sdk"));
connection.setUsername(req.username);
connection.setPassword(req.password);
connection.setIgnoreSslErrors(true);
connection.connect();
DatacenterLister lister = new DatacenterLister(connection);
res.moRefs = new ArrayList<>();
res.datacenters = new ArrayList<>();
lister.listAllDatacenters().stream().forEach(el -> {
res.datacenters.add(DatacenterLister.prettifyPath(el.path));
res.moRefs.add(VimUtils.convertMoRefToString(el.object));
});
}
patch.setBody(res);
patch.complete();
} catch (Exception e) {
patch.fail(e);
} finally {
connection.closeQuietly();
}
});
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister in project photon-model by vmware.
the class VSphereAdapterImageEnumerationService method refreshResourcesOnce.
private void refreshResourcesOnce(Set<String> oldImages, ImageEnumerateRequest request, ComputeStateWithDescription parent, Connection connection, TaskManager mgr) {
DatacenterLister lister = new DatacenterLister(connection);
try {
for (Element element : lister.listAllDatacenters()) {
ManagedObjectReference datacenter = element.object;
try {
EnumerationClient client = new EnumerationClient(connection, parent, datacenter);
processAllTemplates(oldImages, request.resourceLink(), request.taskLink(), client, parent.tenantLinks);
} catch (Throwable e) {
mgr.patchTaskToFailure("Error processing vm templates in " + element.path, e);
return;
}
}
} catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg e) {
mgr.patchTaskToFailure("Error getting datacenters", e);
return;
}
VapiConnection vapi = VapiConnection.createFromVimConnection(connection);
try {
vapi.login();
LibraryClient libraryClient = vapi.newLibraryClient();
processAllLibraries(oldImages, request.resourceLink(), request.taskLink(), libraryClient, parent.tenantLinks);
mgr.patchTask(TaskStage.FINISHED);
} catch (Throwable t) {
mgr.patchTaskToFailure("Error processing library items", t);
return;
} finally {
vapi.close();
}
// garbage collection runs async
garbageCollectUntouchedImages(oldImages);
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method refreshResourcesOnce.
/**
* This method executes in a thread managed by {@link VSphereIOThreadPoolAllocator}
*/
private void refreshResourcesOnce(Set<String> resourceLinks, ComputeEnumerateResourceRequest request, Connection connection, ComputeService.ComputeStateWithDescription parent, TaskManager mgr) {
VapiConnection vapiConnection = VapiConnection.createFromVimConnection(connection);
try {
vapiConnection.login();
} catch (IOException | RpcException e) {
logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(e)));
mgr.patchTaskToFailure(e);
return;
}
DatacenterLister lister = new DatacenterLister(connection);
try {
// Get instanceUuid of the vCenter
AboutInfo vCenter = this.connection.getServiceContent().getAbout();
for (Element element : lister.listAllDatacenters()) {
ManagedObjectReference datacenter = element.object;
log(Level.INFO, "Processing datacenter %s (%s)", element.path, VimUtils.convertMoRefToString(element.object));
EnumerationClient client = new EnumerationClient(connection, parent, datacenter);
EnumerationProgress enumerationProgress = new EnumerationProgress(resourceLinks, request, parent, vapiConnection, VimUtils.convertMoRefToString(datacenter), vCenter.getInstanceUuid());
// since we are processing DC sequentially one at a time
enumerationProgress.expectDatacenterCount(1);
VsphereDatacenterEnumerationHelper.processDatacenterInfo(this, element, enumerationProgress);
enumerationProgress.getDcTracker().await();
logInfo("Proceeding to refresh resources on datacenter: %s", enumerationProgress.getDcLink());
refreshResourcesOnDatacenter(client, enumerationProgress, mgr);
}
} catch (Exception e) {
logWarning(String.format("Error during enumeration: %s", Utils.toString(e)));
mgr.patchTaskToFailure(e);
}
try {
vapiConnection.close();
} catch (Exception e) {
logWarning(() -> String.format("Error occurred when closing vAPI connection: %s", Utils.toString(e)));
}
// after all dc's are enumerated untouched resource links are the only ones left
// in resourceLinks
garbageCollectUntouchedComputeResources(request, resourceLinks, mgr);
// cleanup the connection if the enumeration action is REFRESH.
if (EnumerationAction.REFRESH.equals(request.enumerationAction)) {
cleanupConnection();
}
}
use of com.vmware.photon.controller.model.adapters.vsphere.util.finders.DatacenterLister in project photon-model by vmware.
the class VSphereRegionEnumerationAdapterService method handlePost.
@Override
public void handlePost(Operation post) {
if (!post.hasBody()) {
post.fail(new IllegalArgumentException("body is required"));
return;
}
EndpointState request = post.getBody(EndpointState.class);
DeferredResult<AuthCredentialsServiceState> credentialsDr;
if (request.authCredentialsLink == null) {
credentialsDr = new DeferredResult<>();
credentialsDr.complete(new AuthCredentialsServiceState());
} else {
Operation getCredentials = Operation.createGet(createInventoryUri(this.getHost(), request.authCredentialsLink));
credentialsDr = sendWithDeferredResult(getCredentials, AuthCredentialsServiceState.class);
}
credentialsDr.whenComplete((AuthCredentialsServiceState creds, Throwable t) -> {
if (t != null) {
post.fail(t);
return;
}
VSphereIOThreadPoolAllocator.getPool(this).submit(() -> {
BasicConnection connection = new BasicConnection();
try {
EndpointAdapterUtils.Retriever retriever = EndpointAdapterUtils.Retriever.of(request.endpointProperties);
VSphereEndpointAdapterService.endpoint().accept(request, retriever);
VSphereEndpointAdapterService.credentials().accept(creds, retriever);
connection.setURI(URI.create("https://" + request.endpointProperties.get(HOST_NAME_KEY) + "/sdk"));
connection.setUsername(creds.privateKeyId);
connection.setPassword(EncryptionUtils.decrypt(creds.privateKey));
connection.setIgnoreSslErrors(true);
connection.connect();
DatacenterLister lister = new DatacenterLister(connection);
RegionEnumerationResponse res = new RegionEnumerationResponse();
res.regions = lister.listAllDatacenters().stream().map(dc -> new RegionEnumerationResponse.RegionInfo(DatacenterLister.prettifyPath(dc.path), VimUtils.convertMoRefToString(dc.object))).collect(Collectors.toList());
post.setBody(res);
post.complete();
} catch (Exception e) {
post.fail(e);
} finally {
connection.closeQuietly();
}
});
});
}
Aggregations