use of com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME in project photon-model by vmware.
the class AzureTestUtil method createVMResourceFromSpec.
/**
* Separate method to create VM from given spec
*/
public static ComputeState createVMResourceFromSpec(VMResourceSpec spec) throws Throwable {
final String defaultVmRGName = spec.azureVmName;
// TODO Modify createDefaultResourceGroupState() to have only spec parameter passed
final ResourceGroupState defaultVmRG = createDefaultResourceGroupState(spec.host, defaultVmRGName, spec.computeHost, spec.endpointState, ResourceGroupStateType.AzureResourceGroup);
final String defaultVmRGLink = defaultVmRG.documentSelfLink;
if (spec.networkRGLink == null) {
// The RG where the VM is deployed is also used as RG for the Network!
spec.networkRGLink = defaultVmRGLink;
}
// The RG where the VM is deployed is also used as RG for the SecurityGroup!
final String sgRGLink = defaultVmRGLink;
// Create resource group with a different type. It should be filtered out.
ResourceGroupState azureStorageContainerRG = createDefaultResourceGroupState(spec.host, AZURE_STORAGE_CONTAINER_RG_NAME, spec.computeHost, spec.endpointState, ResourceGroupStateType.AzureStorageContainer);
final Set<String> networkRGLinks = new HashSet<>();
networkRGLinks.add(spec.networkRGLink);
networkRGLinks.add(azureStorageContainerRG.documentSelfLink);
final Set<String> sgRGLinks = new HashSet<>();
sgRGLinks.add(sgRGLink);
sgRGLinks.add(azureStorageContainerRG.documentSelfLink);
AuthCredentialsServiceState azureVMAuth = new AuthCredentialsServiceState();
azureVMAuth.userEmail = AZURE_ADMIN_USERNAME;
azureVMAuth.privateKey = AZURE_ADMIN_PASSWORD;
azureVMAuth = TestUtils.doPost(spec.host, azureVMAuth, AuthCredentialsServiceState.class, UriUtils.buildUri(spec.host, AuthCredentialsService.FACTORY_LINK));
// Create a VM desc
ComputeDescription azureVMDesc = new ComputeDescription();
azureVMDesc.id = UUID.randomUUID().toString();
azureVMDesc.documentSelfLink = azureVMDesc.id;
azureVMDesc.name = azureVMDesc.id;
azureVMDesc.regionId = AZURE_RESOURCE_GROUP_LOCATION;
azureVMDesc.authCredentialsLink = azureVMAuth.documentSelfLink;
azureVMDesc.tenantLinks = spec.endpointState.tenantLinks;
azureVMDesc.endpointLink = spec.endpointState.documentSelfLink;
azureVMDesc.endpointLinks = new HashSet<>();
azureVMDesc.endpointLinks.add(spec.endpointState.documentSelfLink);
azureVMDesc.computeHostLink = spec.endpointState.computeHostLink;
azureVMDesc.instanceType = AZURE_VM_SIZE;
azureVMDesc.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
azureVMDesc.customProperties = new HashMap<>();
// set the create service to the azure instance service
azureVMDesc.instanceAdapterReference = UriUtils.buildUri(spec.host, AzureUriPaths.AZURE_INSTANCE_ADAPTER);
azureVMDesc.powerAdapterReference = UriUtils.buildUri(spec.host, AzureUriPaths.AZURE_POWER_ADAPTER);
azureVMDesc = TestUtils.doPost(spec.host, azureVMDesc, ComputeDescription.class, UriUtils.buildUri(spec.host, ComputeDescriptionService.FACTORY_LINK));
DiskState rootDisk = new DiskState();
rootDisk.name = spec.azureVmName + "-boot-disk";
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.type = DiskType.HDD;
rootDisk.storageType = AZURE_STORAGE_DISKS;
// Custom OSDisk size of 32 GBs
rootDisk.capacityMBytes = AZURE_CUSTOM_OSDISK_SIZE;
rootDisk.bootOrder = 1;
rootDisk.endpointLink = spec.endpointState.documentSelfLink;
rootDisk.endpointLinks = new HashSet<>();
rootDisk.endpointLinks.add(spec.endpointState.documentSelfLink);
rootDisk.computeHostLink = spec.endpointState.computeHostLink;
rootDisk.tenantLinks = spec.endpointState.tenantLinks;
if (spec.isManagedDisk) {
rootDisk.tagLinks = createTagStateSet(spec.host, spec.endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_managed_disk.name());
} else {
rootDisk.tagLinks = createTagStateSet(spec.host, spec.endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_vhd.name());
}
rootDisk.customProperties = new HashMap<>();
rootDisk.customProperties.put(AZURE_OSDISK_CACHING, DEFAULT_OS_DISK_CACHING.name());
if (spec.imageSource.type == Type.PRIVATE_IMAGE) {
if (spec.isManagedDisk) {
rootDisk.imageLink = spec.imageSource.asImageState().documentSelfLink;
rootDisk.customProperties.put(AzureConstants.AZURE_MANAGED_DISK_TYPE, SkuName.STANDARD_LRS.toString());
}
} else if (spec.imageSource.type == Type.PUBLIC_IMAGE) {
if (spec.isManagedDisk) {
rootDisk.imageLink = spec.imageSource.asImageState().documentSelfLink;
rootDisk.customProperties.put(AzureConstants.AZURE_MANAGED_DISK_TYPE, SkuName.STANDARD_LRS.toString());
} else {
rootDisk.imageLink = spec.imageSource.asImageState().documentSelfLink;
if (spec.storageAccountName == null || spec.resourceGroupForStorageAccount == null) {
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, (spec.azureVmName + "sa").replaceAll("[_-]", "").toLowerCase());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, defaultVmRGName);
} else {
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, spec.storageAccountName);
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, spec.resourceGroupForStorageAccount);
}
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_TYPE, AZURE_STORAGE_ACCOUNT_TYPE);
}
} else if (spec.imageSource.type == Type.IMAGE_REFERENCE) {
rootDisk.sourceImageReference = URI.create(spec.imageSource.asRef());
}
rootDisk = TestUtils.doPost(spec.host, rootDisk, DiskState.class, UriUtils.buildUri(spec.host, DiskService.FACTORY_LINK));
List<String> vmDisks = new ArrayList<>();
vmDisks.add(rootDisk.documentSelfLink);
// create additional disks
if (spec.numberOfAdditionalDisks > 0) {
// TODO Need to modify createAdditionalDisks() to have only spec passed as parameter
vmDisks.addAll(createAdditionalDisks(spec.host, spec.azureVmName, spec.endpointState, spec.numberOfAdditionalDisks, spec.persistentDisks, spec.isManagedDisk));
}
// Add external existing data disks (if present) to the list for attaching
if (null != spec.externalDiskLinks && spec.externalDiskLinks.size() > 0) {
vmDisks.addAll(spec.externalDiskLinks);
}
// Create NICs
List<String> nicLinks = createDefaultNicStates(spec.host, spec.computeHost, spec.endpointState, networkRGLinks, sgRGLinks, spec.nicSpecs, spec.azureVmName).stream().map(nic -> nic.documentSelfLink).collect(Collectors.toList());
// Finally create the compute resource state to provision using all constructs above.
ComputeState computeState = new ComputeState();
computeState.id = UUID.randomUUID().toString();
computeState.name = spec.azureVmName;
computeState.parentLink = spec.computeHost.documentSelfLink;
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
computeState.descriptionLink = azureVMDesc.documentSelfLink;
computeState.resourcePoolLink = spec.computeHost.resourcePoolLink;
computeState.diskLinks = vmDisks;
computeState.networkInterfaceLinks = nicLinks;
computeState.customProperties = Collections.singletonMap(RESOURCE_GROUP_NAME, defaultVmRGName);
computeState.groupLinks = Collections.singleton(defaultVmRGLink);
computeState.endpointLink = spec.endpointState.documentSelfLink;
computeState.endpointLinks = new HashSet<>();
computeState.endpointLinks.add(spec.endpointState.documentSelfLink);
computeState.computeHostLink = spec.endpointState.computeHostLink;
computeState.tenantLinks = spec.endpointState.tenantLinks;
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
return TestUtils.doPost(spec.host, computeState, ComputeState.class, UriUtils.buildUri(spec.host, ComputeService.FACTORY_LINK));
}
use of com.vmware.photon.controller.model.ComputeProperties.RESOURCE_GROUP_NAME 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.ComputeProperties.RESOURCE_GROUP_NAME in project photon-model by vmware.
the class AzureTestUtil method createDefaultVMResource.
public static ComputeState createDefaultVMResource(VerificationHost host, String azureVMName, ComputeState computeHost, EndpointState endpointState, AzureNicSpecs nicSpecs, String networkRGLink, ImageSource imageSource, int numberOfAdditionalDisks, ComputeDescription azureVMDesc, String azureRgName) throws Throwable {
if (azureRgName == null) {
azureRgName = azureVMName;
}
final ResourceGroupState defaultVmRG = createDefaultResourceGroupState(host, azureRgName, computeHost, endpointState, ResourceGroupStateType.AzureResourceGroup);
final String defaultVmRGLink = defaultVmRG.documentSelfLink;
if (networkRGLink == null) {
// The RG where the VM is deployed is also used as RG for the Network!
networkRGLink = defaultVmRGLink;
}
// The RG where the VM is deployed is also used as RG for the SecurityGroup!
final String sgRGLink = defaultVmRGLink;
// Create resource group with a different type. It should be filtered out.
ResourceGroupState azureStorageContainerRG = createDefaultResourceGroupState(host, AZURE_STORAGE_CONTAINER_RG_NAME, computeHost, endpointState, ResourceGroupStateType.AzureStorageContainer);
final Set<String> networkRGLinks = new HashSet<>();
networkRGLinks.add(networkRGLink);
networkRGLinks.add(azureStorageContainerRG.documentSelfLink);
final Set<String> sgRGLinks = new HashSet<>();
sgRGLinks.add(sgRGLink);
sgRGLinks.add(azureStorageContainerRG.documentSelfLink);
if (azureVMDesc == null) {
azureVMDesc = buildComputeDescription(host, computeHost, endpointState, null);
}
List<String> vmDisks = new ArrayList<>();
DiskState rootDisk = new DiskState();
rootDisk.name = azureVMName + "-boot-disk";
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.type = DiskType.HDD;
// Custom OSDisk size of 32 GBs
rootDisk.capacityMBytes = AZURE_CUSTOM_OSDISK_SIZE;
if (imageSource.type == Type.PRIVATE_IMAGE || imageSource.type == Type.PUBLIC_IMAGE) {
rootDisk.imageLink = imageSource.asImageState().documentSelfLink;
} else if (imageSource.type == Type.IMAGE_REFERENCE) {
rootDisk.sourceImageReference = URI.create(imageSource.asRef());
}
rootDisk.bootOrder = 1;
rootDisk.endpointLink = endpointState.documentSelfLink;
rootDisk.endpointLinks = new HashSet<>();
rootDisk.endpointLinks.add(endpointState.documentSelfLink);
rootDisk.computeHostLink = computeHost.documentSelfLink;
rootDisk.tenantLinks = endpointState.tenantLinks;
rootDisk.storageType = AZURE_STORAGE_DISKS;
rootDisk.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_vhd.name());
rootDisk.customProperties = new HashMap<>();
rootDisk.customProperties.put(AZURE_OSDISK_CACHING, DEFAULT_OS_DISK_CACHING.name());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_NAME, (azureVMName + "sa").replaceAll("[_-]", "").toLowerCase());
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_RG_NAME, azureRgName);
rootDisk.customProperties.put(AzureConstants.AZURE_STORAGE_ACCOUNT_TYPE, AZURE_STORAGE_ACCOUNT_TYPE);
rootDisk = TestUtils.doPost(host, rootDisk, DiskState.class, UriUtils.buildUri(host, DiskService.FACTORY_LINK));
vmDisks.add(rootDisk.documentSelfLink);
// create additional disks
vmDisks.addAll(createAdditionalDisks(host, azureVMName, endpointState, numberOfAdditionalDisks, VMResourceSpec.PersistentDisks.NONE, false));
// Create NICs
List<String> nicLinks = createDefaultNicStates(host, computeHost, endpointState, networkRGLinks, sgRGLinks, nicSpecs, azureVMName).stream().map(nic -> nic.documentSelfLink).collect(Collectors.toList());
// Finally create the compute resource state to provision using all constructs above.
ComputeState computeState = new ComputeState();
computeState.id = UUID.randomUUID().toString();
computeState.name = azureVMName;
computeState.parentLink = computeHost.documentSelfLink;
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
computeState.descriptionLink = azureVMDesc.documentSelfLink;
computeState.resourcePoolLink = computeHost.resourcePoolLink;
computeState.diskLinks = vmDisks;
computeState.networkInterfaceLinks = nicLinks;
computeState.customProperties = Collections.singletonMap(RESOURCE_GROUP_NAME, azureRgName);
computeState.groupLinks = Collections.singleton(defaultVmRGLink);
computeState.endpointLink = endpointState.documentSelfLink;
computeState.endpointLinks = new HashSet<>();
computeState.endpointLinks.add(endpointState.documentSelfLink);
computeState.computeHostLink = computeHost.documentSelfLink;
computeState.tenantLinks = endpointState.tenantLinks;
computeState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureConstants.AzureResourceType.azure_vm.toString());
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis());
return TestUtils.doPost(host, computeState, ComputeState.class, UriUtils.buildUri(host, ComputeService.FACTORY_LINK));
}
Aggregations