use of com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance in project photon-model by vmware.
the class GCPEnumerationAdapterService method update.
/**
* Updates matching compute states for given VMs.
* @param ctx The Enumeration Context.
*/
private void update(EnumerationContext ctx) {
logFine(() -> "Updating Local VMs");
AtomicInteger numOfUpdates = new AtomicInteger(ctx.computeStates.size());
ctx.computeStates.forEach(computeState -> {
Long instanceId = Long.parseLong(computeState.id);
GCPInstance GCPInstance = ctx.virtualMachines.remove(instanceId);
updateHelper(ctx, computeState, GCPInstance, numOfUpdates);
});
}
use of com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance in project photon-model by vmware.
the class GCPEnumerationAdapterService method createHelper.
/**
* This helper function creates a compute state according to the corresponding
* instance on the cloud. It will also creates an auth credential and a compute
* description with it. Moreover, if there is a root disk of the instance on
* cloud, it will creates a root disk locally. Otherwise, it will create a
* default root disk with the compute state. All the creation operations will
* run in parallel. After all remote instances get created, it will jump to
* list vm stage if there is a next page of list vms. Otherwise it will jump
* to the delete stage.
* @param ctx The Enumeration Context.
* @param virtualMachine The virtual machine to be created.
* @param size The number of remaining virtual machines to be created.
*/
private void createHelper(EnumerationContext ctx, GCPInstance virtualMachine, AtomicInteger size) {
List<Operation> operations = new ArrayList<>();
// TODO VSYM-1106: refactor the creation logic here.
// Create compute description.
// Map GCP instance data to compute description.
ComputeDescription computeDescription = new ComputeDescription();
computeDescription.id = UUID.randomUUID().toString();
computeDescription.name = virtualMachine.name;
computeDescription.zoneId = virtualMachine.zone;
// TODO VSYM-1139: dynamically acquire all gcp zones, regions and mappings.
computeDescription.regionId = extractRegionFromZone(virtualMachine.zone);
computeDescription.instanceType = extractActualInstanceType(virtualMachine.machineType);
computeDescription.authCredentialsLink = ctx.parentAuth.documentSelfLink;
computeDescription.documentSelfLink = computeDescription.id;
computeDescription.environmentName = ENVIRONMENT_NAME_GCP;
computeDescription.instanceAdapterReference = UriUtils.buildUri(ServiceHost.LOCAL_HOST, this.getHost().getPort(), GCPUriPaths.GCP_INSTANCE_ADAPTER, null);
computeDescription.statsAdapterReference = UriUtils.buildUri(ServiceHost.LOCAL_HOST, this.getHost().getPort(), GCPUriPaths.GCP_STATS_ADAPTER, null);
computeDescription.tenantLinks = ctx.computeHostDesc.tenantLinks;
Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
operations.add(compDescOp);
// Create root disk.
DiskService.DiskState rootDisk = new DiskService.DiskState();
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.customProperties = new HashMap<>();
boolean foundRoot = false;
if (virtualMachine.disks != null && !virtualMachine.disks.isEmpty()) {
for (GCPDisk gcpDisk : virtualMachine.disks) {
if (gcpDisk.boot) {
foundRoot = true;
rootDisk.name = gcpDisk.deviceName;
rootDisk.customProperties.put(DISK_AUTO_DELETE, gcpDisk.autoDelete.toString());
break;
}
}
}
if (!foundRoot) {
rootDisk.name = rootDisk.id;
}
// These are required fields in disk service.
// They cannot be accessed during vm enumeration.
rootDisk.type = DiskType.HDD;
rootDisk.capacityMBytes = DEFAULT_DISK_CAPACITY;
rootDisk.sourceImageReference = URI.create(DEFAULT_DISK_SOURCE_IMAGE);
rootDisk.customizationServiceReference = URI.create(DEFAULT_DISK_SERVICE_REFERENCE);
// No matter we find root disk or not, the root disk should be booted first.
rootDisk.bootOrder = 1;
rootDisk.tenantLinks = ctx.computeHostDesc.tenantLinks;
Operation diskOp = Operation.createPost(getHost(), DiskService.FACTORY_LINK).setBody(rootDisk);
operations.add(diskOp);
List<String> vmDisks = new ArrayList<>();
vmDisks.add(UriUtils.buildUriPath(DiskService.FACTORY_LINK, rootDisk.documentSelfLink));
// Create compute state
ComputeState resource = new ComputeState();
resource.id = virtualMachine.id.toString();
resource.type = ComputeType.VM_GUEST;
resource.environmentName = ComputeDescription.ENVIRONMENT_NAME_GCP;
resource.name = virtualMachine.name;
resource.parentLink = ctx.enumRequest.resourceLink();
resource.descriptionLink = UriUtils.buildUriPath(ComputeDescriptionService.FACTORY_LINK, computeDescription.documentSelfLink);
resource.resourcePoolLink = ctx.enumRequest.resourcePoolLink;
resource.diskLinks = vmDisks;
resource.customProperties = new HashMap<>();
String osType = getNormalizedOSType(virtualMachine);
if (osType != null) {
resource.customProperties.put(CUSTOM_OS_TYPE, osType);
}
resource.tenantLinks = ctx.computeHostDesc.tenantLinks;
assignIPAddress(resource, virtualMachine);
assignPowerState(resource, virtualMachine.status);
Operation resourceOp = Operation.createPost(getHost(), ComputeService.FACTORY_LINK).setBody(resource);
operations.add(resourceOp);
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
if (size.decrementAndGet() == 0) {
ctx.virtualMachines.clear();
if (ctx.enumNextPageLink != null) {
ctx.subStage = EnumerationSubStages.LIST_REMOTE_VMS;
} else {
logFine(() -> "Finished creating compute states");
ctx.subStage = EnumerationSubStages.DELETE_LOCAL_VMS;
}
handleSubStage(ctx);
}
}).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance in project photon-model by vmware.
the class GCPEnumerationAdapterService method enumerate.
/**
* Enumerate VMs from Google Cloud Platform.
* @param ctx The Enumeration Context.
*/
private void enumerate(EnumerationContext ctx) {
logFine(() -> "Enumerating VMs from GCP");
URI uri;
if (ctx.enumNextPageLink != null) {
uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(String.format(LIST_VM_TEMPLATE_URI, ctx.projectId, ctx.zoneId)), MAX_RESULTS, VM_PAGE_SIZE, PAGE_TOKEN, ctx.enumNextPageLink);
} else {
uri = UriUtils.extendUriWithQuery(UriUtils.buildUri(String.format(LIST_VM_TEMPLATE_URI, ctx.projectId, ctx.zoneId)), MAX_RESULTS, VM_PAGE_SIZE);
}
Operation.createGet(uri).addRequestHeader(Operation.AUTHORIZATION_HEADER, AUTH_HEADER_BEARER_PREFIX + ctx.accessToken).setCompletion((op, er) -> {
if (er != null) {
handleError(ctx, er);
return;
}
GCPInstancesList GCPInstancesList = op.getBody(GCPInstancesList.class);
List<GCPInstance> GCPInstances = GCPInstancesList.items;
if (GCPInstances == null || GCPInstances.size() == 0) {
ctx.subStage = EnumerationSubStages.DELETE_LOCAL_VMS;
handleSubStage(ctx);
return;
}
ctx.enumNextPageLink = GCPInstancesList.nextPageToken;
logFine(() -> String.format("Retrieved %d VMs from GCP", GCPInstances.size()));
logFine(() -> String.format("Next page link %s", ctx.enumNextPageLink));
for (GCPInstance GCPInstance : GCPInstances) {
ctx.virtualMachines.put(GCPInstance.id, GCPInstance);
ctx.vmIds.add(GCPInstance.id);
}
logFine(() -> String.format("Processing %d VMs", ctx.vmIds.size()));
ctx.subStage = EnumerationSubStages.QUERY_LOCAL_VMS;
handleSubStage(ctx);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.gcp.podo.vm.GCPInstance in project photon-model by vmware.
the class GCPEnumerationAdapterService method updateHelper.
/**
* This function will update the root disk data of a compute state. If the
* instance on the cloud does not have any disks or does not have a boot
* disk, the update will skip this instance. After all local vms are updated
* it will jump to create stage.
*
* So far we update ip address, power state in compute state and instance type
* in compute description and disk custom properties
* @param ctx The Enumeration Context.
* @param computeState The compute state to be updated.
* @param vm The virtual machine used to update compute state.
* @param numOfUpdates The number of remaining compute states to be updated.
*/
private void updateHelper(EnumerationContext ctx, ComputeState computeState, GCPInstance vm, AtomicInteger numOfUpdates) {
List<Operation> operations = new ArrayList<>();
ComputeState computeStatePatch = new ComputeState();
assignIPAddress(computeStatePatch, vm);
assignPowerState(computeStatePatch, vm.status);
operations.add(Operation.createPatch(getHost(), computeState.documentSelfLink).setBody(computeStatePatch));
ComputeDescription computeDescription = new ComputeDescription();
computeDescription.instanceType = extractActualInstanceType(vm.machineType);
operations.add(Operation.createPatch(getHost(), computeState.descriptionLink).setBody(computeDescription));
if (vm.disks != null && !vm.disks.isEmpty()) {
for (GCPDisk gcpDisk : vm.disks) {
if (gcpDisk.boot) {
DiskState diskState = new DiskState();
diskState.customProperties = new HashMap<>();
diskState.customProperties.put(DISK_AUTO_DELETE, gcpDisk.autoDelete.toString());
diskState.documentSelfLink = computeState.diskLinks.get(0);
operations.add(Operation.createPatch(getHost(), diskState.documentSelfLink).setBody(diskState));
break;
}
}
}
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
countAndFinishUpdating(ctx, numOfUpdates);
}).sendWith(this);
}
Aggregations