use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.
the class ProvisionContext method populateContextThen.
/**
* Populates the given initial context and invoke the onSuccess handler when built. At every step,
* if failure occurs the ProvisionContext's errorHandler is invoked to cleanup.
*
* @param ctx
* @param onSuccess
*/
public static void populateContextThen(Service service, ProvisionContext ctx, Consumer<ProvisionContext> onSuccess) {
// TODO fetch all required state in parallel using OperationJoin.
if (ctx.child == null) {
URI computeUri = UriUtils.extendUriWithQuery(ctx.computeReference, UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
computeUri = createInventoryUri(service.getHost(), computeUri);
AdapterUtils.getServiceState(service, computeUri, op -> {
ctx.child = op.getBody(ComputeStateWithDescription.class);
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
String templateLink = VimUtils.firstNonNull(CustomProperties.of(ctx.child).getString(CustomProperties.TEMPLATE_LINK), CustomProperties.of(ctx.child.description).getString(CustomProperties.TEMPLATE_LINK));
// in all other cases ignore the presence of the template
if (templateLink != null && ctx.templateMoRef == null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
URI computeUri = createInventoryUri(service.getHost(), templateLink);
AdapterUtils.getServiceState(service, computeUri, op -> {
ImageState body = op.getBody(ImageState.class);
ctx.templateMoRef = CustomProperties.of(body).getMoRef(CustomProperties.MOREF);
if (ctx.templateMoRef == null) {
String msg = String.format("The linked template %s does not contain a MoRef in its custom properties", templateLink);
ctx.fail(new IllegalStateException(msg));
} else {
populateContextThen(service, ctx, onSuccess);
}
}, ctx.errorHandler);
return;
}
// For creation based on linked clone of snapshot
if (ctx.snapshotMoRef == null) {
String snapshotLink = CustomProperties.of(ctx.child).getString(CustomProperties.SNAPSHOT_LINK);
if (snapshotLink != null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
URI snapshotUri = createInventoryUri(service.getHost(), snapshotLink);
AdapterUtils.getServiceState(service, snapshotUri, op -> {
SnapshotService.SnapshotState snapshotState = op.getBody(SnapshotService.SnapshotState.class);
ctx.snapshotMoRef = CustomProperties.of(snapshotState).getMoRef(CustomProperties.MOREF);
if (ctx.snapshotMoRef == null) {
String msg = String.format("The linked clone snapshot %s does not contain a MoRef in its custom properties", snapshotLink);
ctx.fail(new IllegalStateException(msg));
} else {
// Retrieve the reference endpoint moref from which the linkedclone has to be created.
String refComputeLink = snapshotState.computeLink;
if (refComputeLink != null) {
URI refComputeUri = createInventoryUri(service.getHost(), refComputeLink);
AdapterUtils.getServiceState(service, refComputeUri, opCompute -> {
ComputeStateWithDescription refComputeState = opCompute.getBody(ComputeStateWithDescription.class);
ctx.referenceComputeMoRef = CustomProperties.of(refComputeState).getMoRef(CustomProperties.MOREF);
if (ctx.referenceComputeMoRef == null) {
String msg = String.format("The linked clone endpoint ref %s does not contain a MoRef in its custom properties", refComputeLink);
ctx.fail(new IllegalStateException(msg));
}
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
}
}
}, ctx.errorHandler);
return;
}
}
if (ctx.parent == null && ctx.child.parentLink != null) {
URI computeUri = UriUtils.extendUriWithQuery(UriUtils.buildUri(service.getHost(), ctx.child.parentLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
computeUri = createInventoryUri(service.getHost(), computeUri);
AdapterUtils.getServiceState(service, computeUri, op -> {
ctx.parent = op.getBody(ComputeStateWithDescription.class);
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
if (ctx.vSphereCredentials == null) {
if (IAAS_API_ENABLED) {
if (ctx.operation == null) {
ctx.fail(new IllegalArgumentException("Caller operation cannot be empty"));
return;
}
SessionUtil.retrieveExternalToken(service, ctx.operation.getAuthorizationContext()).whenComplete((authCredentialsServiceState, throwable) -> {
if (throwable != null) {
ctx.errorHandler.accept(throwable);
return;
}
ctx.vSphereCredentials = authCredentialsServiceState;
populateContextThen(service, ctx, onSuccess);
});
} else {
if (ctx.parent.description.authCredentialsLink == null) {
ctx.fail(new IllegalStateException("authCredentialsLink is not defined in resource " + ctx.parent.description.documentSelfLink));
return;
}
URI credUri = createInventoryUri(service.getHost(), ctx.parent.description.authCredentialsLink);
AdapterUtils.getServiceState(service, credUri, op -> {
ctx.vSphereCredentials = op.getBody(AuthCredentialsServiceState.class);
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
}
return;
}
if (ctx.task == null) {
// Verify if this makes sense? These tasks should always be local to deployment?
AdapterUtils.getServiceState(service, ctx.provisioningTaskReference, op -> {
ctx.task = op.getBody(ServiceDocument.class);
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
if (ctx.nics == null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
if (ctx.child.networkInterfaceLinks == null || ctx.child.networkInterfaceLinks.isEmpty()) {
ctx.nics = Collections.emptyList();
populateContextThen(service, ctx, onSuccess);
return;
}
ctx.nics = new ArrayList<>();
Query query = Query.Builder.create().addInClause(ServiceDocument.FIELD_NAME_SELF_LINK, ctx.child.networkInterfaceLinks).build();
QueryTask qt = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.EXPAND_LINKS).addOption(QueryOption.SELECT_LINKS).addOption(QueryOption.INDEXED_METADATA).addLinkTerm(NetworkInterfaceState.FIELD_NAME_NETWORK_LINK).addLinkTerm(NetworkInterfaceState.FIELD_NAME_SUBNET_LINK).addLinkTerm(NetworkInterfaceState.FIELD_NAME_DESCRIPTION_LINK).build();
QueryUtils.startInventoryQueryTask(service, qt).whenComplete((o, e) -> {
if (e != null) {
ctx.errorHandler.accept(e);
return;
}
QueryResultsProcessor processor = QueryResultsProcessor.create(o);
for (NetworkInterfaceStateWithDetails nic : processor.documents(NetworkInterfaceStateWithDetails.class)) {
if (nic.networkInterfaceDescriptionLink != null) {
NetworkInterfaceDescription desc = processor.selectedDocument(nic.networkInterfaceDescriptionLink, NetworkInterfaceDescription.class);
nic.description = desc;
}
if (nic.subnetLink != null) {
SubnetState subnet = processor.selectedDocument(nic.subnetLink, SubnetState.class);
nic.subnet = subnet;
}
if (nic.networkLink != null) {
NetworkState network = processor.selectedDocument(nic.networkLink, NetworkState.class);
nic.network = network;
}
ctx.nics.add(nic);
}
populateContextThen(service, ctx, onSuccess);
});
return;
}
if (ctx.computeMoRef == null) {
String placementLink = CustomProperties.of(ctx.child).getString(ComputeProperties.PLACEMENT_LINK);
if (placementLink == null) {
Exception error = new IllegalStateException("A Compute resource must have a " + ComputeProperties.PLACEMENT_LINK + " custom property");
ctx.fail(error);
return;
}
URI expandedPlacementUri = UriUtils.extendUriWithQuery(createInventoryUri(service.getHost(), placementLink), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.TRUE.toString());
expandedPlacementUri = createInventoryUri(service.getHost(), expandedPlacementUri);
Operation.createGet(expandedPlacementUri).setCompletion((o, e) -> {
if (e != null) {
ctx.fail(e);
return;
}
ComputeStateWithDescription host = o.getBody(ComputeStateWithDescription.class);
// extract the target resource pool for the placement
CustomProperties hostCustomProperties = CustomProperties.of(host);
ctx.computeMoRef = hostCustomProperties.getMoRef(CustomProperties.MOREF);
if (ctx.computeMoRef == null) {
Exception error = new IllegalStateException(String.format("Compute @ %s does not contain a %s custom property", placementLink, CustomProperties.MOREF));
ctx.fail(error);
return;
}
if (host.description.regionId == null) {
Exception error = new IllegalStateException(String.format("Compute @ %s does not specify a region", placementLink));
ctx.fail(error);
return;
}
try {
ctx.datacenterMoRef = VimUtils.convertStringToMoRef(host.description.regionId);
} catch (IllegalArgumentException ex) {
ctx.fail(ex);
return;
}
populateContextThen(service, ctx, onSuccess);
}).sendWith(service);
return;
}
if (ctx.disks == null) {
// no disks attached
if (ctx.child.diskLinks == null || ctx.child.diskLinks.isEmpty()) {
ctx.disks = Collections.emptyList();
populateContextThen(service, ctx, onSuccess);
return;
}
ctx.disks = new ArrayList<>(ctx.child.diskLinks.size());
// collect disks in parallel
Stream<Operation> opsGetDisk = ctx.child.diskLinks.stream().map(link -> {
URI diskStateUri = UriUtils.buildUri(service.getHost(), link);
return Operation.createGet(createInventoryUri(service.getHost(), DiskStateExpanded.buildUri(diskStateUri)));
});
OperationJoin join = OperationJoin.create(opsGetDisk).setCompletion((os, errors) -> {
if (errors != null && !errors.isEmpty()) {
// fail on first error
ctx.errorHandler.accept(new IllegalStateException("Cannot get disk state", errors.values().iterator().next()));
return;
}
os.values().forEach(op -> ctx.disks.add(op.getBody(DiskStateExpanded.class)));
populateContextThen(service, ctx, onSuccess);
});
join.sendWith(service);
return;
}
String libraryItemLink = VimUtils.firstNonNull(CustomProperties.of(ctx.child).getString(CustomProperties.LIBRARY_ITEM_LINK), CustomProperties.of(ctx.child.description).getString(CustomProperties.LIBRARY_ITEM_LINK));
if (libraryItemLink != null && ctx.image == null && ctx.instanceRequestType == InstanceRequestType.CREATE) {
URI libraryUri = createInventoryUri(service.getHost(), libraryItemLink);
AdapterUtils.getServiceState(service, libraryUri, op -> {
ImageState body = op.getBody(ImageState.class);
ctx.image = body;
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
if (ctx.instanceRequestType == InstanceRequestType.CREATE) {
if (ctx.image == null) {
DiskStateExpanded bootDisk = ctx.disks.stream().filter(d -> d.imageLink != null).findFirst().orElse(null);
if (bootDisk != null) {
URI bootImageRef = createInventoryUri(service.getHost(), bootDisk.imageLink);
AdapterUtils.getServiceState(service, bootImageRef, op -> {
ImageState body = op.getBody(ImageState.class);
ctx.image = body;
populateContextThen(service, ctx, onSuccess);
}, ctx.errorHandler);
return;
}
}
}
// Order networks by deviceIndex so that nics are created in the same order
if (ctx.nics != null) {
// configure network
ctx.nics.sort((lnis, rnis) -> {
return Integer.compare(lnis.deviceIndex, rnis.deviceIndex);
});
}
// context populated, invoke handler
onSuccess.accept(ctx);
}
use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.
the class VSphereAdapterImageEnumerationService method processAllLibraries.
private void processAllLibraries(Set<String> oldImages, String endpointLink, String taskLink, LibraryClient libraryClient, List<String> tenantLinks) throws IOException, RpcException {
Phaser phaser = new Phaser(1);
for (String libId : libraryClient.listLibs()) {
ObjectNode libModel = libraryClient.loadLib(libId);
for (String itemId : libraryClient.listItemsInLib(libId)) {
ObjectNode itemModel = libraryClient.loadItem(itemId);
String type = VapiClient.getString(itemModel, "type", VapiClient.K_OPTIONAL);
if (!"ovf".equals(type)) {
// only ovfs can be deployed
continue;
}
ImageState state = makeImageFromItem(libModel, itemModel);
state.documentSelfLink = buildStableImageLink(endpointLink, state.id);
state.endpointLink = endpointLink;
state.tenantLinks = tenantLinks;
state.regionId = "";
oldImages.remove(state.documentSelfLink);
phaser.register();
Operation.createPost(PhotonModelUriUtils.createInventoryUri(getHost(), ImageService.FACTORY_LINK)).setBody(state).setCompletion((o, e) -> phaser.arrive()).sendWith(this);
}
}
phaser.arriveAndAwaitAdvance();
}
use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.
the class VSphereAdapterImageEnumerationService method makeImageFromTemplate.
private ImageState makeImageFromTemplate(VmOverlay vm) {
ImageState res = new ImageState();
res.name = "Template: " + vm.getName();
res.description = vm.getName();
res.id = vm.getInstanceUuid();
CustomProperties.of(res).put(CustomProperties.MOREF, vm.getId());
return res;
}
use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.
the class AzureInstanceService method createVM.
private void createVM(AzureInstanceContext ctx, AzureInstanceStage nextStage) {
ComputeDescriptionService.ComputeDescription description = ctx.child.description;
Map<String, String> customProperties = description.customProperties;
if (customProperties == null) {
handleError(ctx, new IllegalStateException("Custom properties not specified"));
return;
}
// DiskService.DiskStateExpanded bootDisk = ctx.bootDiskState;
if (ctx.bootDiskState == null) {
handleError(ctx, new IllegalStateException("Azure bootDisk not specified"));
return;
}
String cloudConfig = null;
if (ctx.bootDiskState.bootConfig != null && ctx.bootDiskState.bootConfig.files.length > CLOUD_CONFIG_DEFAULT_FILE_INDEX) {
cloudConfig = ctx.bootDiskState.bootConfig.files[CLOUD_CONFIG_DEFAULT_FILE_INDEX].contents;
}
VirtualMachineInner request = new VirtualMachineInner();
request.withLocation(ctx.resourceGroup.location());
SubResource availabilitySetSubResource = new SubResource().withId(ctx.availabilitySet.id());
request.withAvailabilitySet(availabilitySetSubResource);
// Set OS profile.
OSProfile osProfile = new OSProfile();
osProfile.withComputerName(ctx.vmName);
if (ctx.childAuth != null) {
osProfile.withAdminUsername(ctx.childAuth.userEmail);
osProfile.withAdminPassword(EncryptionUtils.decrypt(ctx.childAuth.privateKey));
}
if (cloudConfig != null) {
try {
osProfile.withCustomData(Base64.getEncoder().encodeToString(cloudConfig.getBytes(Utils.CHARSET)));
} catch (UnsupportedEncodingException e) {
logWarning(() -> "Error encoding user data");
return;
}
}
request.withOsProfile(osProfile);
// Set hardware profile.
HardwareProfile hardwareProfile = new HardwareProfile();
hardwareProfile.withVmSize(description.instanceType != null ? VirtualMachineSizeTypes.fromString(description.instanceType) : VirtualMachineSizeTypes.BASIC_A0);
request.withHardwareProfile(hardwareProfile);
// Set storage profile.
// Create destination OS VHD
final OSDisk osDisk = newAzureOsDisk(ctx);
final StorageProfile storageProfile = new StorageProfile();
storageProfile.withOsDisk(osDisk);
List<DataDisk> dataDisks = new ArrayList<>();
List<Integer> LUNsOnImage = new ArrayList<>();
storageProfile.withImageReference(ctx.imageSource.asImageReferenceInner());
if (ctx.imageSource.type == ImageSource.Type.PRIVATE_IMAGE) {
// set LUNs of data disks present on the custom image.
final ImageState imageState = ctx.imageSource.asImageState();
if (imageState != null && imageState.diskConfigs != null) {
for (DiskConfiguration diskConfig : imageState.diskConfigs) {
if (diskConfig.properties != null && diskConfig.properties.containsKey(AzureConstants.AZURE_DISK_LUN)) {
DataDisk imageDataDisk = new DataDisk();
int lun = Integer.parseInt(diskConfig.properties.get(AzureConstants.AZURE_DISK_LUN));
LUNsOnImage.add(lun);
imageDataDisk.withLun(lun);
imageDataDisk.withCreateOption(DiskCreateOptionTypes.FROM_IMAGE);
dataDisks.add(imageDataDisk);
}
}
}
String dataDiskCaching = ctx.bootDiskState.customProperties.get(AZURE_DATA_DISK_CACHING);
if (dataDiskCaching != null) {
dataDisks.stream().forEach(dataDisk -> dataDisk.withCaching(CachingTypes.fromString(dataDiskCaching)));
}
String diskType = ctx.bootDiskState.customProperties.get(AZURE_MANAGED_DISK_TYPE);
if (diskType != null) {
ManagedDiskParametersInner managedDiskParams = new ManagedDiskParametersInner();
managedDiskParams.withStorageAccountType(StorageAccountTypes.fromString(diskType));
dataDisks.stream().forEach(dataDisk -> dataDisk.withManagedDisk(managedDiskParams));
}
}
// choose LUN greater than the one specified in case of custom image. Else start from zero.
int LUNForAdditionalDisk = LUNsOnImage.size() == 0 ? 0 : Collections.max(LUNsOnImage) + 1;
dataDisks.addAll(newAzureDataDisks(ctx, LUNForAdditionalDisk));
storageProfile.withDataDisks(dataDisks);
request.withStorageProfile(storageProfile);
// Set network profile {{
NetworkProfile networkProfile = new NetworkProfile();
networkProfile.withNetworkInterfaces(new ArrayList<>());
for (AzureNicContext nicCtx : ctx.nics) {
NetworkInterfaceReferenceInner nicRef = new NetworkInterfaceReferenceInner();
nicRef.withId(nicCtx.nic.id());
// NOTE: First NIC is marked as Primary.
nicRef.withPrimary(networkProfile.networkInterfaces().isEmpty());
networkProfile.networkInterfaces().add(nicRef);
}
request.withNetworkProfile(networkProfile);
logFine(() -> String.format("Creating virtual machine with name [%s]", ctx.vmName));
AzureAsyncCallback<VirtualMachineInner> callback = new AzureAsyncCallback<VirtualMachineInner>() {
@Override
public void onError(Throwable e) {
// exception and try again with a shorter name
if (isIncorrectNameLength(e)) {
request.osProfile().withComputerName(generateWindowsComputerName(ctx.vmName));
getComputeManagementClientImpl(ctx).virtualMachines().createOrUpdateAsync(ctx.resourceGroup.name(), ctx.vmName, request, this);
return;
}
handleCloudError(String.format("Provisioning VM %s: FAILED. Details:", ctx.vmName), ctx, COMPUTE_NAMESPACE, e);
}
// Cannot tell for sure, but these checks should be enough
private boolean isIncorrectNameLength(Throwable e) {
if (e instanceof CloudException) {
CloudException ce = (CloudException) e;
CloudError body = ce.body();
if (body != null) {
String code = body.code();
String target = body.target();
return INVALID_PARAMETER.equals(code) && COMPUTER_NAME.equals(target) && request.osProfile().computerName().length() > WINDOWS_COMPUTER_NAME_MAX_LENGTH && body.message().toLowerCase().contains("windows");
}
}
return false;
}
private String generateWindowsComputerName(String vmName) {
String computerName = vmName;
if (vmName.length() > WINDOWS_COMPUTER_NAME_MAX_LENGTH) {
// Take the first 12 and the last 3 chars of the generated VM name
computerName = vmName.substring(0, 12) + vmName.substring(vmName.length() - 3, vmName.length());
}
return computerName;
}
@Override
public void onSuccess(VirtualMachineInner result) {
logFine(() -> String.format("Successfully created vm [%s]", result.name()));
ctx.provisionedVm = result;
ComputeState cs = new ComputeState();
// Azure for some case changes the case of the vm id.
ctx.vmId = result.id().toLowerCase();
cs.id = ctx.vmId;
cs.type = ComputeType.VM_GUEST;
cs.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
cs.lifecycleState = LifecycleState.READY;
if (ctx.child.customProperties == null) {
cs.customProperties = new HashMap<>();
} else {
cs.customProperties = ctx.child.customProperties;
}
cs.customProperties.put(RESOURCE_GROUP_NAME, ctx.resourceGroup.name());
Operation.CompletionHandler completionHandler = (ox, exc) -> {
if (exc != null) {
handleError(ctx, exc);
return;
}
handleAllocation(ctx, nextStage);
};
sendRequest(Operation.createPatch(ctx.computeRequest.resourceReference).setBody(cs).setCompletion(completionHandler));
}
};
getComputeManagementClientImpl(ctx).virtualMachines().createOrUpdateAsync(ctx.resourceGroup.name(), ctx.vmName, request, callback);
}
use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.
the class AzureTestUtil method createImageSource.
public static ImageSource createImageSource(VerificationHost host, EndpointState endpointState, String imageRefId) throws Throwable {
ImageSource imageSource;
ImageState bootImage = new ImageState();
bootImage.id = imageRefId;
bootImage.endpointType = endpointState.endpointType;
bootImage = TestUtils.doPost(host, bootImage, ImageState.class, UriUtils.buildUri(host, ImageService.FACTORY_LINK));
imageSource = ImageSource.fromImageState(bootImage);
return imageSource;
}
Aggregations