use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.
the class QueryUtilsTest method testQueryReferrers_noResults.
@Test
public void testQueryReferrers_noResults() throws Throwable {
ComputeDescription cd = ModelUtils.createComputeDescription(this, null, null);
Set<String> expected = Collections.emptySet();
doTest(cd, expected, Collections.singletonList("http://tenant"));
}
use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.
the class QueryUtilsTest method testQueryReferrers_error.
@Test
public void testQueryReferrers_error() throws Throwable {
ComputeDescription cd = ModelUtils.createComputeDescription(this, null, null);
ModelUtils.createCompute(this, cd);
ModelUtils.createCompute(this, cd);
Query queryForReferrers = QueryUtils.queryForReferrers(cd.documentSelfLink, ComputeState.class, ComputeState.FIELD_NAME_DESCRIPTION_LINK);
// The class under testing
QueryStrategy<ComputeState> queryStrategy = new QueryByPages<>(getHost(), queryForReferrers, ComputeState.class, Collections.emptyList(), null);
Set<String> actual = new HashSet<>();
// The method under testing
waitToComplete(queryStrategy.queryDocuments(cs -> {
if (actual.isEmpty()) {
actual.add(cs.documentSelfLink);
} else {
throw new RuntimeException("consume error");
}
}).handle((v, e) -> {
// Validate processed docs
assertThat(actual.size(), equalTo(1));
// Validate exception propagation
assertThat(e, notNullValue());
assertThat(e, instanceOf(CompletionException.class));
assertThat(e.getCause().getMessage(), equalTo("consume error"));
// Recover from exception to prevent its propagation to outer test method
return (Void) null;
}));
}
use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.
the class TestAWSSetupUtils method createAWSComputeHost.
/**
* Create a compute host description for an AWS instance
*/
public static ComputeState createAWSComputeHost(VerificationHost host, EndpointState endpointState, String zoneId, String regionId, boolean isAwsClientMock, String awsMockEndpointReference, Set<String> tagLinks) throws Throwable {
ComputeDescription awsComputeHostDesc = new ComputeDescription();
if (zoneId != null) {
awsComputeHostDesc.id = zoneId;
} else {
awsComputeHostDesc.id = UUID.randomUUID().toString();
}
awsComputeHostDesc.documentSelfLink = awsComputeHostDesc.id;
awsComputeHostDesc.name = ComputeDescription.ENVIRONMENT_NAME_AWS;
awsComputeHostDesc.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
awsComputeHostDesc.supportedChildren = new ArrayList<String>();
awsComputeHostDesc.supportedChildren.add(ComputeType.VM_GUEST.name());
awsComputeHostDesc.instanceAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_INSTANCE_ADAPTER);
awsComputeHostDesc.enumerationAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_ENUMERATION_ADAPTER);
awsComputeHostDesc.statsAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_STATS_ADAPTER);
awsComputeHostDesc.diskAdapterReference = UriUtils.buildUri(host, AWSUriPaths.AWS_DISK_ADAPTER);
awsComputeHostDesc.zoneId = zoneId;
awsComputeHostDesc.regionId = regionId;
awsComputeHostDesc.authCredentialsLink = endpointState.authCredentialsLink;
awsComputeHostDesc.tenantLinks = endpointState.tenantLinks;
awsComputeHostDesc = TestUtils.doPost(host, awsComputeHostDesc, ComputeDescriptionService.ComputeDescription.class, UriUtils.buildUri(host, ComputeDescriptionService.FACTORY_LINK));
ComputeState awsComputeHost = new ComputeState();
awsComputeHost.id = awsComputeHostDesc.id;
awsComputeHost.documentSelfLink = awsComputeHost.id;
awsComputeHost.name = awsComputeHostDesc.name;
awsComputeHost.type = ComputeType.ENDPOINT_HOST;
awsComputeHost.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
awsComputeHost.descriptionLink = awsComputeHostDesc.documentSelfLink;
awsComputeHost.tagLinks = tagLinks;
awsComputeHost.resourcePoolLink = endpointState.resourcePoolLink;
awsComputeHost.regionId = regionId;
awsComputeHost.endpointLink = endpointState.documentSelfLink;
awsComputeHost.endpointLinks = new HashSet<String>();
awsComputeHost.endpointLinks.add(endpointState.documentSelfLink);
awsComputeHost.tenantLinks = endpointState.tenantLinks;
if (isAwsClientMock) {
awsComputeHost.adapterManagementReference = UriUtils.buildUri(awsMockEndpointReference);
} else {
awsComputeHost.adapterManagementReference = UriUtils.buildUri(awsEndpointReference);
}
return TestUtils.doPost(host, awsComputeHost, ComputeService.ComputeState.class, UriUtils.buildUri(host, ComputeService.FACTORY_LINK));
}
use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription 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));
}
use of com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method createComputeDescriptions.
/**
* Creates relevant resources for given VMs.
*/
private void createComputeDescriptions(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.size() == 0 && ctx.regions.isEmpty()) {
// nothing to create
if (ctx.enumNextPageLink != null) {
ctx.subStage = ComputeEnumerationSubStages.LISTVMS;
handleSubStage(ctx);
return;
}
logFine(() -> "No virtual machine found for creation.");
ctx.subStage = ComputeEnumerationSubStages.PATCH_ADDITIONAL_FIELDS;
handleSubStage(ctx);
return;
}
logFine(() -> String.format("%d compute description with states to be created", ctx.virtualMachines.size()));
Iterator<Entry<String, VirtualMachineInner>> iterator = ctx.virtualMachines.entrySet().iterator();
Collection<Operation> opCollection = new ArrayList<>();
while (iterator.hasNext()) {
Entry<String, VirtualMachineInner> vmEntry = iterator.next();
VirtualMachineInner virtualMachine = vmEntry.getValue();
AuthCredentialsServiceState auth = new AuthCredentialsServiceState();
if (virtualMachine.osProfile() != null) {
auth.userEmail = virtualMachine.osProfile().adminUsername();
auth.privateKey = virtualMachine.osProfile().adminPassword();
}
auth.documentSelfLink = UUID.randomUUID().toString();
auth.tenantLinks = ctx.parentCompute.tenantLinks;
auth.customProperties = new HashMap<>();
if (ctx.request.endpointLink != null) {
auth.customProperties.put(CUSTOM_PROP_ENDPOINT_LINK, ctx.request.endpointLink);
}
String authLink = UriUtils.buildUriPath(AuthCredentialsService.FACTORY_LINK, auth.documentSelfLink);
Operation authOp = Operation.createPost(createInventoryUri(getHost(), AuthCredentialsService.FACTORY_LINK)).setBody(auth);
opCollection.add(authOp);
// TODO VSYM-631: Match existing descriptions for new VMs discovered on Azure
ComputeDescription computeDescription = new ComputeDescription();
computeDescription.id = UUID.randomUUID().toString();
computeDescription.name = virtualMachine.name();
computeDescription.regionId = virtualMachine.location();
computeDescription.authCredentialsLink = authLink;
computeDescription.endpointLink = ctx.request.endpointLink;
AdapterUtils.addToEndpointLinks(computeDescription, ctx.request.endpointLink);
computeDescription.documentSelfLink = computeDescription.id;
computeDescription.environmentName = ENVIRONMENT_NAME_AZURE;
if (virtualMachine.hardwareProfile() != null && virtualMachine.hardwareProfile().vmSize() != null) {
computeDescription.instanceType = virtualMachine.hardwareProfile().vmSize().toString();
}
computeDescription.instanceAdapterReference = ctx.parentCompute.description.instanceAdapterReference;
computeDescription.statsAdapterReference = ctx.parentCompute.description.statsAdapterReference;
computeDescription.diskAdapterReference = ctx.parentCompute.description.diskAdapterReference;
computeDescription.computeHostLink = ctx.parentCompute.documentSelfLink;
computeDescription.customProperties = new HashMap<>();
computeDescription.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
// TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-1268
String resourceGroupName = getResourceGroupName(virtualMachine.id());
computeDescription.customProperties.put(AZURE_RESOURCE_GROUP_NAME, resourceGroupName);
computeDescription.tenantLinks = ctx.parentCompute.tenantLinks;
Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescription);
ctx.computeDescriptionIds.put(virtualMachine.name(), computeDescription.id);
opCollection.add(compDescOp);
}
for (RegionInfo region : ctx.regions.values()) {
ComputeDescription computeDescriptionForRegion = createComputeDescriptionForRegion(ctx, region);
Operation compDescOp = Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK).setBody(computeDescriptionForRegion);
ctx.computeDescriptionIds.put(region.regionId, computeDescriptionForRegion.id);
opCollection.add(compDescOp);
}
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
logFine(() -> "Continue on to updating disks.");
ctx.subStage = next;
handleSubStage(ctx);
}).sendWith(this);
}
Aggregations