use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method mapBucketToDiskState.
/**
* Map an S3 bucket to a photon-model disk state.
*/
private DiskState mapBucketToDiskState(Bucket bucket, S3StorageEnumerationContext aws) {
DiskState diskState = new DiskState();
diskState.id = bucket.getName();
diskState.name = bucket.getName();
diskState.storageType = STORAGE_TYPE_S3;
diskState.regionId = aws.regionsByBucketName.get(bucket.getName());
diskState.authCredentialsLink = aws.endpointAuth.documentSelfLink;
diskState.resourcePoolLink = aws.request.original.resourcePoolLink;
diskState.endpointLink = aws.request.original.endpointLink;
if (diskState.endpointLinks == null) {
diskState.endpointLinks = new HashSet<>();
}
diskState.endpointLinks.add(aws.request.original.endpointLink);
diskState.tenantLinks = aws.parentCompute.tenantLinks;
diskState.computeHostLink = aws.parentCompute.documentSelfLink;
diskState.tagLinks = new HashSet<>();
if (bucket.getCreationDate() != null) {
diskState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(bucket.getCreationDate().getTime());
}
if (bucket.getOwner() != null && bucket.getOwner().getDisplayName() != null) {
diskState.customProperties = new HashMap<>();
diskState.customProperties.put(BUCKET_OWNER_NAME, bucket.getOwner().getDisplayName());
}
// Set internal type tag for all S3 disk states only if POST for the TagState was successful.
if (aws.internalTypeTagSelfLink != null) {
diskState.tagLinks.add(aws.internalTypeTagSelfLink);
}
return diskState;
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class TestProvisionAWSDisk method testDiskProvision.
/**
* This test verified the disk creation and deletion on aws.
*/
@Test
public void testDiskProvision() throws Throwable {
createEndpoint();
this.diskState = createAWSDiskState(this.host, this.endpointState, this.currentTestName.getMethodName() + "_disk1", Boolean.TRUE, null, regionId);
String taskLink = com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getProvisionDiskTask(this.diskState.documentSelfLink, ProvisionDiskTaskState.SubStage.CREATING_DISK, this.host, this.isMock, this.endpointState.tenantLinks);
this.host.waitForFinishedTask(ProvisionDiskTaskState.class, taskLink);
// check that the disk has been created
ProvisioningUtils.queryDiskInstances(this.host, 1);
DiskState disk = getDisk(this.host, this.diskState.documentSelfLink);
if (!this.isMock) {
String volumeId = disk.id;
assertTrue(volumeId.startsWith(VOLUMEID_PREFIX));
List<Volume> volumes = getAwsDisksByIds(this.client, this.host, Collections.singletonList(volumeId));
Volume volume = volumes.get(0);
// check disk size
assertEquals("Disk size is not matching with the volume size on aws", disk.capacityMBytes, volume.getSize() * 1024);
// check volume type
assertEquals("Disk type is not matching the volume type on aws", disk.customProperties.get("volumeType"), volume.getVolumeType());
// check iops of disk
String diskIops = disk.customProperties.get(DISK_IOPS);
if (diskIops != null) {
int requestedIops = Integer.parseInt(diskIops);
int MAX_SUPPORTED_IOPS = (int) (disk.capacityMBytes / 1024) * 50;
int provisionedIops = Math.min(requestedIops, MAX_SUPPORTED_IOPS);
assertEquals("Disk iops is not matching with the volume on aws", provisionedIops, volume.getIops().intValue());
}
assertEquals("availability zones are not matching", disk.zoneId, volume.getAvailabilityZone());
assertTrue("disk status is not matching", disk.status == DiskService.DiskStatus.AVAILABLE);
assertEquals("disk encryption status not matching", 0, disk.encrypted.compareTo(isEncrypted));
}
// delete the disk using the AWSDiskService.Delete
taskLink = com.vmware.photon.controller.model.adapters.awsadapter.TestUtils.getProvisionDiskTask(this.diskState.documentSelfLink, ProvisionDiskTaskState.SubStage.DELETING_DISK, this.host, this.isMock, this.endpointState.tenantLinks);
this.host.waitForFinishedTask(ProvisionDiskTaskState.class, taskLink);
// check that the disk has been deleted
ProvisioningUtils.queryDiskInstances(this.host, 0);
if (!this.isMock) {
String volumeId = disk.id;
List<Volume> volumes = getAwsDisksByIds(this.client, this.host, Collections.singletonList(volumeId));
// assert that the volume is deleted on aws.
assertNull(volumes);
}
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState 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.DiskService.DiskState in project photon-model by vmware.
the class AzureTestUtil method assertDiskExist.
/**
* Assert that a managed / un-managed disk with the provided name exist in the document store.
*
* @param factoryLink
* Factory link to the stateful service which states to check.
* @param name
* name of the resource to assert if exists.
* @param shouldExists
* whether to assert if a resource exists or not.
*/
public static void assertDiskExist(VerificationHost host, String factoryLink, String name, boolean shouldExists) {
ServiceDocumentQueryResult result = host.getExpandedFactoryState(UriUtils.buildUri(host, factoryLink));
boolean exists = false;
for (Object document : result.documents.values()) {
DiskState diskState = Utils.fromJson(document, DiskState.class);
if (diskState.name.contains(name)) {
exists = true;
break;
}
}
assertEquals("Expected: " + shouldExists + ", but was: " + exists, shouldExists, exists);
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class AzureTestUtil method createDefaultDiskState.
/**
* Create a disk state
*/
public static DiskState createDefaultDiskState(VerificationHost host, String diskName, String storageContainerLink, ComputeState computeHost, EndpointState endpointState) throws Throwable {
DiskState diskState = new DiskState();
diskState.id = UUID.randomUUID().toString();
diskState.documentSelfLink = diskState.id;
diskState.name = diskName;
diskState.computeHostLink = computeHost.documentSelfLink;
diskState.resourcePoolLink = computeHost.resourcePoolLink;
diskState.tenantLinks = endpointState.tenantLinks;
diskState.endpointLink = endpointState.documentSelfLink;
diskState.endpointLinks = new HashSet<>();
diskState.endpointLinks.add(endpointState.documentSelfLink);
List<String> tenantLinks = Collections.singletonList(EndpointType.azure.name() + "-tenant");
diskState.tagLinks = createTagStateSet(host, endpointState.tenantLinks, TAG_KEY_TYPE, AzureResourceType.azure_vhd.name());
diskState.tenantLinks = tenantLinks;
diskState.storageDescriptionLink = storageContainerLink;
diskState.type = DEFAULT_DISK_TYPE;
diskState.storageType = AZURE_STORAGE_DISKS;
diskState.capacityMBytes = DEFAULT_DISK_CAPACITY;
return TestUtils.doPost(host, diskState, DiskState.class, UriUtils.buildUri(host, DiskService.FACTORY_LINK));
}
Aggregations