use of com.vmware.photon.controller.model.resources.ImageService.ImageState in project photon-model by vmware.
the class TestAWSSetupUtils method createAWSVMResource.
/**
* Create a compute resource for an AWS instance.
*/
public static ComputeService.ComputeState createAWSVMResource(VerificationHost host, ComputeState computeHost, EndpointState endpointState, @SuppressWarnings("rawtypes") Class clazz, String vmName, String zoneId, String regionId, Set<String> tagLinks, AwsNicSpecs nicSpecs, boolean addNewSecurityGroup, Map<String, Object> awsTestContext, boolean persistDiskOnVmDelete, boolean withAdditionalDisks) throws Throwable {
// Step 1: Create an auth credential to login to the VM
AuthCredentialsServiceState auth = new AuthCredentialsServiceState();
auth.type = DEFAULT_AUTH_TYPE;
auth.userEmail = DEFAULT_COREOS_USER;
auth.privateKey = TestUtils.loadTestResource(clazz, DEFAULT_COREOS_PRIVATE_KEY_FILE);
auth = TestUtils.doPost(host, auth, AuthCredentialsServiceState.class, UriUtils.buildUri(host, AuthCredentialsService.FACTORY_LINK));
// Step 2: Create a VM desc
ComputeDescription awsVMDesc = new ComputeDescription();
awsVMDesc.id = instanceType;
awsVMDesc.name = vmName;
awsVMDesc.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
awsVMDesc.instanceType = instanceType;
awsVMDesc.supportedChildren = new ArrayList<>();
awsVMDesc.supportedChildren.add(ComputeType.DOCKER_CONTAINER.name());
awsVMDesc.customProperties = new HashMap<>();
awsVMDesc.customProperties.put(AWSConstants.AWS_SECURITY_GROUP, securityGroup);
// set zone to east
awsVMDesc.zoneId = zoneId;
awsVMDesc.regionId = regionId;
awsVMDesc.authCredentialsLink = auth.documentSelfLink;
awsVMDesc.tenantLinks = endpointState.tenantLinks;
awsVMDesc.endpointLink = endpointState.documentSelfLink;
awsVMDesc.endpointLinks = new HashSet<String>();
awsVMDesc.endpointLinks.add(endpointState.documentSelfLink);
// set the create service to the aws instance service
awsVMDesc.instanceAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_INSTANCE_ADAPTER);
awsVMDesc.statsAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_STATS_ADAPTER);
awsVMDesc = TestUtils.doPost(host, awsVMDesc, ComputeDescription.class, UriUtils.buildUri(host, ComputeDescriptionService.FACTORY_LINK));
// Step 3: create boot disk
List<String> vmDisks = new ArrayList<>();
ImageState bootImage;
{
// Create PUBLIC image state
bootImage = new ImageState();
bootImage.id = imageId;
bootImage.endpointType = endpointState.endpointType;
bootImage.regionId = regionId;
bootImage = TestUtils.doPost(host, bootImage, ImageState.class, UriUtils.buildUri(host, ImageService.FACTORY_LINK));
}
DiskState rootDisk = new DiskState();
rootDisk.id = UUID.randomUUID().toString();
rootDisk.documentSelfLink = rootDisk.id;
rootDisk.name = DEFAULT_ROOT_DISK_NAME;
rootDisk.bootOrder = 1;
rootDisk.sourceImageReference = URI.create(imageId);
rootDisk.imageLink = bootImage.documentSelfLink;
rootDisk.bootConfig = new DiskState.BootConfig();
rootDisk.bootConfig.label = DEFAULT_CONFIG_LABEL;
DiskState.BootConfig.FileEntry file = new DiskState.BootConfig.FileEntry();
file.path = DEFAULT_CONFIG_PATH;
file.contents = TestUtils.loadTestResource(clazz, DEFAULT_USER_DATA_FILE);
rootDisk.bootConfig.files = new DiskState.BootConfig.FileEntry[] { file };
rootDisk.capacityMBytes = BOOT_DISK_SIZE_IN_MEBI_BYTES;
// add custom properties to root disk from profile
rootDisk.customProperties = new HashMap<>();
rootDisk.customProperties.put(DEVICE_TYPE, "ebs");
rootDisk.customProperties.put(VOLUME_TYPE, "io1");
rootDisk.customProperties.put(IOPS, "500");
rootDisk.regionId = regionId;
rootDisk.endpointLink = endpointState.documentSelfLink;
rootDisk.endpointLinks = new HashSet<String>();
rootDisk.endpointLinks.add(endpointState.documentSelfLink);
rootDisk.computeHostLink = endpointState.computeHostLink;
rootDisk.tenantLinks = endpointState.tenantLinks;
rootDisk = TestUtils.doPost(host, rootDisk, DiskService.DiskState.class, UriUtils.buildUri(host, DiskService.FACTORY_LINK));
vmDisks.add(rootDisk.documentSelfLink);
if (withAdditionalDisks) {
List<DiskState> additionalDisks = getAdditionalDiskConfiguration(host, endpointState, computeHost, persistDiskOnVmDelete);
for (DiskState additionalDisk : additionalDisks) {
vmDisks.add(additionalDisk.documentSelfLink);
}
}
// Create NIC States
List<String> nicLinks = null;
if (nicSpecs != null) {
nicLinks = createAWSNicStates(host, computeHost, endpointState, awsVMDesc.name, nicSpecs, addNewSecurityGroup, awsTestContext).stream().map(nic -> nic.documentSelfLink).collect(Collectors.toList());
}
// Create compute state
ComputeState resource;
{
resource = new ComputeState();
resource.id = UUID.randomUUID().toString();
resource.name = awsVMDesc.name;
resource.type = ComputeType.VM_GUEST;
resource.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
resource.descriptionLink = awsVMDesc.documentSelfLink;
resource.parentLink = computeHost.documentSelfLink;
resource.resourcePoolLink = computeHost.resourcePoolLink;
resource.networkInterfaceLinks = nicLinks;
resource.diskLinks = vmDisks;
resource.tagLinks = tagLinks;
resource.regionId = awsVMDesc.regionId;
resource.endpointLink = endpointState.documentSelfLink;
resource.endpointLinks = new HashSet<String>();
resource.endpointLinks.add(endpointState.documentSelfLink);
resource.tenantLinks = endpointState.tenantLinks;
}
return TestUtils.doPost(host, resource, ComputeService.ComputeState.class, UriUtils.buildUri(host, ComputeService.FACTORY_LINK));
}
Aggregations