use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class BaseResourceOperationTest method registerComputeState.
protected ComputeState registerComputeState(ComputeState computeState) throws Throwable {
Operation operation = sendOperationSynchronously(Operation.createPost(super.host, ComputeService.FACTORY_LINK).setBody(computeState));
if (operation.getStatusCode() == Operation.STATUS_CODE_OK) {
ComputeState result = operation.getBody(ComputeState.class);
markForDelete(result.documentSelfLink);
return result;
}
throw new IllegalStateException(String.valueOf(operation.getBodyRaw()));
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState 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.resources.ComputeService.ComputeState in project photon-model by vmware.
the class TestAzureProvisionTask method assertVmNetworksConfiguration.
private void assertVmNetworksConfiguration(AzureNicSpecs azureNicSpec, String vmName) throws Throwable {
// This assert is only suitable for real (non-mocking env).
if (this.isMock) {
return;
}
getHost().log(Level.INFO, "%s: Assert network configuration for [%s] VM", this.currentTestName.getMethodName(), this.vmState.name);
ComputeState vm = getHost().getServiceState(null, ComputeState.class, UriUtils.buildUri(getHost(), this.vmState.documentSelfLink));
NetworkInterfaceStateWithDescription primaryNicState = getHost().getServiceState(null, NetworkInterfaceStateWithDescription.class, NetworkInterfaceStateWithDescription.buildUri(UriUtils.buildUri(getHost(), vm.networkInterfaceLinks.get(0))));
// In case that private ip is set explicitly.
assertStaticPrivateIPAddress(azureNicSpec, primaryNicState.address);
assertNotNull("Primary NIC private IP should be set.", primaryNicState.address);
if (primaryNicState.description.assignPublicIpAddress == null || primaryNicState.description.assignPublicIpAddress == Boolean.TRUE) {
assertNotNull("VM address should be set.", vm.address);
assertNotEquals("VM address should not be the same as primary NIC private IP.", vm.address, primaryNicState.address);
} else {
assertNull("VM address should be empty.", vm.address);
}
assertNotNull("Primary NIC security group should be set.", primaryNicState.securityGroupLinks != null);
for (int i = 1; i < vm.networkInterfaceLinks.size(); i++) {
NetworkInterfaceState nonPrimaryNicState = getHost().getServiceState(null, NetworkInterfaceState.class, UriUtils.buildUri(getHost(), vm.networkInterfaceLinks.get(i)));
assertNotNull("Non-primary NIC" + i + " IP should not be set to the privatese ip.", nonPrimaryNicState.address);
assertNull("Non-primary NIC" + i + " security group should not be set.", nonPrimaryNicState.securityGroupLinks);
}
// Ensure that from the list of provided network resource groups,
// and security group resource groups, the one with the correct type has been chosen.
// Verifying the resources can be obtained from this RG, ensures they have been placed
// correctly.
NetworkManagementClientImpl networkClient = getAzureSdkClients().getNetworkManagementClientImpl();
final String vmRGName = vm.customProperties.get(ComputeProperties.RESOURCE_GROUP_NAME);
VirtualNetworkInner provisionedNetwork = AzureTestUtil.getAzureVirtualNetwork(networkClient, vmRGName, AzureTestUtil.AZURE_NETWORK_NAME);
assertNotNull("Azure virtual network object '" + vmRGName + "/" + AzureTestUtil.AZURE_NETWORK_NAME + "' is not found.", provisionedNetwork);
final String sgName = AzureTestUtil.AZURE_SECURITY_GROUP_NAME + "-" + vmName;
NetworkSecurityGroupInner provisionedSG = AzureTestUtil.getAzureSecurityGroup(networkClient, vmRGName, sgName);
assertNotNull("Azure security group object '" + vmRGName + "/" + sgName + "' is not found.", provisionedSG);
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class AzurePowerServiceTest method assertVMCurrentAddressEmpty.
private void assertVMCurrentAddressEmpty() {
ComputeState compute = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
assertTrue(compute.address.isEmpty());
}
use of com.vmware.photon.controller.model.resources.ComputeService.ComputeState in project photon-model by vmware.
the class TestAzureStatsCollection method testStatsCollection.
/**
* Periodically runs and verifies stats collection and prints logs for node stats.
* @throws Throwable
*/
@Test
public void testStatsCollection() throws Throwable {
runEnumeration(this.host, computeHost.documentSelfLink, computeHost.resourcePoolLink, endpointState, this.isMock);
ServiceDocumentQueryResult result = queryDocumentsAndAssertExpectedCount(this.host, this.vmCount, ComputeService.FACTORY_LINK, false);
/*
* Azure provides new stats data points every one minute for VMs with detailed monitoring enabled.
* This interval is significantly longer for VMs without detailed monitoring.
* Currently, we cannot enable detailed monitoring through code.
* We currently use a VM with custom name "EnumTestVM-DoNotDelete" for Azure tests, thus a prerequisite
* for this test is having a VM with the above name on test account.
* Here we query ComputeState and check if VM with the required custom name is enumerated.
* The test only runs if the VM is present and enumerated.
*/
for (Entry<String, Object> key : result.documents.entrySet()) {
ComputeState document = Utils.fromJson(key.getValue(), ComputeState.class);
if (!document.documentSelfLink.equals(computeHost.documentSelfLink) && document.id.toLowerCase().contains(CUSTOM_DIAGNOSTIC_ENABLED_VM.toLowerCase())) {
this.enumeratedComputeLink = document.documentSelfLink;
break;
}
}
if (this.enumeratedComputeLink == null) {
this.host.log(Level.SEVERE, "VM named EnumTestVM-DoNotDelete is either not present on the " + "azure account specified or was not enumerated.");
return;
}
if (!this.isMock) {
runStatsCollectionPeriodicallyAndCollectNodeStats();
this.host.waitFor("Timeout while waiting for test run duration", () -> {
TimeUnit.MINUTES.sleep(this.testRunDurationInMinutes);
this.host.getScheduledExecutor().shutdown();
this.host.getScheduledExecutor().awaitTermination(EXECUTOR_TERMINATION_WAIT_DURATION_MINUTES, TimeUnit.MINUTES);
return true;
});
}
}
Aggregations