use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class ResourceUtilsTest method testNoEndpointFlag.
@Test
public void testNoEndpointFlag() throws Throwable {
// validate initial state with no endpoint - the flag should be set
DiskState disk = new DiskState();
disk.name = "disk";
disk = postServiceSynchronously(DiskService.FACTORY_LINK, disk, DiskState.class);
assertTrue(disk.customProperties != null && disk.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT));
// validate PATCH with no endpoint - the flag should stay
DiskState diskPatch = new DiskState();
diskPatch.name = "new-name";
disk = patchServiceSynchronously(disk.documentSelfLink, diskPatch, DiskState.class);
assertTrue(disk.customProperties != null && disk.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT));
// validate PATCH with endpoint - the flag should be removed
diskPatch = new DiskState();
diskPatch.endpointLink = "endpoint-link";
disk = patchServiceSynchronously(disk.documentSelfLink, diskPatch, DiskState.class);
assertTrue(disk.customProperties == null || !disk.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT));
// validate PATCH that removes the endpoint - the flag should not be re-added
diskPatch = new DiskState();
diskPatch.name = "brand-new-name";
diskPatch.endpointLink = ResourceUtils.NULL_LINK_VALUE;
disk = patchServiceSynchronously(disk.documentSelfLink, diskPatch, DiskState.class);
assertTrue(disk.customProperties == null || !disk.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT));
// validate that the flag cannot be re-added through a PUT request
disk.customProperties = new HashMap<>();
disk.customProperties.put(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT, Boolean.TRUE.toString());
putServiceSynchronously(disk.documentSelfLink, disk);
disk = getServiceSynchronously(disk.documentSelfLink, DiskState.class);
assertTrue(disk.customProperties == null || !disk.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT));
// validate initial state with endpoint - the flag should not be set
DiskState newDisk = new DiskState();
newDisk.name = "disk";
newDisk.endpointLink = "endpoint-link";
newDisk = postServiceSynchronously(DiskService.FACTORY_LINK, newDisk, DiskState.class);
assertTrue(newDisk.customProperties == null || !newDisk.customProperties.containsKey(ResourceUtils.CUSTOM_PROP_NO_ENDPOINT));
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class TestAWSEnumerationTask method validateS3Enumeration.
// Validate S3 bucket enumeration by querying DiskState and comparing result to expected number of documents
// and validate the size of tagLinks.
private void validateS3Enumeration(int expectedDiskCount, int expectedTagsCount) {
Query s3Query = QueryTask.Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_NAME, TEST_BUCKET_NAME).build();
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(s3Query).addOption(QueryOption.EXPAND_CONTENT).build();
Operation s3QueryOp = QueryUtils.createQueryTaskOperation(this.host, queryTask, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
Operation s3QueryResponse = this.host.waitForResponse(s3QueryOp);
QueryTask response = s3QueryResponse.getBody(QueryTask.class);
if (expectedDiskCount > 0) {
DiskState diskState = Utils.fromJson(response.results.documents.get(response.results.documentLinks.get(0)), DiskState.class);
assertEquals(expectedTagsCount, diskState.tagLinks.size());
}
DiskState diskState = Utils.fromJson(response.results.documents.get(response.results.documentLinks.get(0)), DiskState.class);
assertEquals(expectedDiskCount, diskState.endpointLinks.size());
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState in project photon-model by vmware.
the class TestAWSEnumerationTask method testTagEnumeration.
@Test
public void testTagEnumeration() throws Throwable {
if (this.isMock) {
return;
}
setUpTestVolume(this.host, this.client, this.awsTestContext, this.isMock);
this.snapshotId = (String) this.awsTestContext.get(TestAWSSetupUtils.SNAPSHOT_KEY);
this.ebsBlockDevice = new EbsBlockDevice().withSnapshotId(this.snapshotId);
this.blockDeviceMapping = new BlockDeviceMapping().withDeviceName(BLOCK_DEVICE_NAME).withEbs(this.ebsBlockDevice);
this.diskId = (String) this.awsTestContext.get(TestAWSSetupUtils.DISK_KEY);
this.host.log("Running test: " + this.currentTestName.getMethodName());
// VM tags
Tag tag1 = new Tag(VM_TAG_KEY_1, VM_TAG_VALUE_1);
Tag tag2 = new Tag(VM_TAG_KEY_2, VM_TAG_VALUE_2);
Tag tag3 = new Tag(VM_TAG_KEY_3, VM_TAG_VALUE_3);
List<Tag> vmTags = Arrays.asList(tag1, tag2, tag3);
// SG tag
List<Tag> sgTags = new ArrayList<>();
sgTags.add(new Tag(INITIAL_SG_TAG, INITIAL_SG_TAG));
// Network tag
List<Tag> networkTags = new ArrayList<>();
networkTags.add(new Tag(INITIAL_VPC_TAG, INITIAL_VPC_TAG));
// Subnet tag
List<Tag> subnetTags = new ArrayList<>();
subnetTags.add(new Tag(INITIAL_SUBNET_TAG, INITIAL_SUBNET_TAG));
// Disk tag
List<Tag> diskTags = new ArrayList<>();
diskTags.add(new Tag(INITIAL_DISK_TAG, INITIAL_DISK_TAG));
try {
String linuxVMId1 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
this.instancesToCleanUp.add(linuxVMId1);
waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
// Tag the first VM with a name and add some additional tags
tagResourcesWithName(this.client, VM_NAME, linuxVMId1);
List<Tag> linuxVMId1Tags = Arrays.asList(tag1, tag2);
// tag vm, default SG, VPC, Subnet and Disk
tagResources(this.client, linuxVMId1Tags, linuxVMId1);
tagResources(this.client, sgTags, this.securityGroupId);
tagResources(this.client, networkTags, this.vpcId);
tagResources(this.client, subnetTags, this.subnetId);
tagResources(this.client, diskTags, this.diskId);
enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
String linuxVMId2 = provisionAWSEBSVMWithEC2Client(this.host, this.client, EC2_LINUX_AMI, this.subnetId, this.securityGroupId, this.blockDeviceMapping);
this.instancesToCleanUp.add(linuxVMId2);
waitForProvisioningToComplete(this.instancesToCleanUp, this.host, this.client, ZERO);
// Name the second VM and add some tags
tagResourcesWithName(this.client, VM_UPDATED_NAME, linuxVMId2);
List<Tag> linuxVMId2Tags = Arrays.asList(tag2, tag3);
tagResources(this.client, linuxVMId2Tags, linuxVMId2);
// Un-tag the resources
unTagResources(this.client, sgTags, this.securityGroupId);
unTagResources(this.client, networkTags, this.vpcId);
unTagResources(this.client, subnetTags, this.subnetId);
unTagResources(this.client, diskTags, this.diskId);
// re-init tag arrays
sgTags = new ArrayList<>();
networkTags = new ArrayList<>();
subnetTags = new ArrayList<>();
diskTags = new ArrayList<>();
// new key-value set remotely should result in a new tag state created locally
// and a new tag link added to the SecurityGroupState, NetworkState, SubnetState and
// DiskState
sgTags.add(new Tag(SECONDARY_SG_TAG, SECONDARY_SG_TAG));
networkTags.add(new Tag(SECONDARY_VPC_TAG, SECONDARY_VPC_TAG));
subnetTags.add(new Tag(SECONDARY_SUBNET_TAG, SECONDARY_SUBNET_TAG));
diskTags.add(new Tag(SECONDARY_DISK_TAG, SECONDARY_DISK_TAG));
// tag again default SG, VPC, Subnet and Disk
tagResources(this.client, diskTags, this.diskId);
tagResources(this.client, sgTags, this.securityGroupId);
tagResources(this.client, networkTags, this.vpcId);
tagResources(this.client, subnetTags, this.subnetId);
enumerateResources(this.host, this.computeHost, this.endpointState, this.isMock, TEST_CASE_INITIAL);
validateComputeName(linuxVMId1, VM_NAME);
validateComputeName(linuxVMId2, VM_UPDATED_NAME);
// Validate tag states number
int allTagsNumber = vmTags.size() + sgTags.size() + networkTags.size() + subnetTags.size() + diskTags.size();
queryDocumentsAndAssertExpectedCount(this.host, allTagsNumber, TagService.FACTORY_LINK, false);
ServiceDocumentQueryResult serviceDocumentQueryResult = queryAllFactoryResources(this.host, TagService.FACTORY_LINK);
Map<String, TagState> tagsMap = new HashMap<>();
for (Entry<String, Object> entry : serviceDocumentQueryResult.documents.entrySet()) {
tagsMap.put(entry.getKey(), Utils.fromJson(entry.getValue(), TagState.class));
}
// validate security group tags
Map<String, SecurityGroupState> allSecurityGroupStatesMap = ProvisioningUtils.<SecurityGroupState>getResourceStates(this.host, SecurityGroupService.FACTORY_LINK, SecurityGroupState.class);
SecurityGroupState defaultSgState = allSecurityGroupStatesMap.get(this.securityGroupId);
// ensure one link is deleted and one new is added to the sg state. One additional
// link is an internal tag.
assertNotNull(defaultSgState.tagLinks);
assertEquals("Wrong number of security-group tag links found.", 1 + internalTagsCount1, defaultSgState.tagLinks.size());
// validate vpc tags
Map<String, NetworkState> allNetworkStatesMap = ProvisioningUtils.<NetworkState>getResourceStates(this.host, NetworkService.FACTORY_LINK, NetworkState.class);
NetworkState defaultNetworkState = allNetworkStatesMap.get(this.vpcId);
// ensure one link is deleted and one new is added to the network state. One additional
// link is an internal tag.
assertEquals("Wrong number of network tag links found.", 1 + internalTagsCount1, defaultNetworkState.tagLinks.size());
// validate subnet tags
Map<String, SubnetState> allSubnetStatesMap = ProvisioningUtils.<SubnetState>getResourceStates(this.host, SubnetService.FACTORY_LINK, SubnetState.class);
SubnetState defaultSubnetState = allSubnetStatesMap.get(this.subnetId);
// ensure one link is deleted and one new is added to the subnet state. One additional
// link is an internal tag.
assertEquals("Wrong number of subnet tag links found.", 1 + internalTagsCount1, defaultSubnetState.tagLinks.size());
// validate disk tags
Map<String, DiskState> allDiskStatesMap = ProvisioningUtils.<DiskState>getResourceStates(this.host, DiskService.FACTORY_LINK, DiskState.class);
DiskState defaultDiskState = allDiskStatesMap.get(this.diskId);
// ensure one link is deleted and one new is added to the disk state
assertEquals("Wrong number of disk tag links found.", 1 + internalTagsCount1, defaultDiskState.tagLinks.size());
// ensure EBS disk has an internal type tag set
assertTrue(defaultDiskState.tagLinks.contains(TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.ebs_block.toString(), false, this.endpointState.tenantLinks).documentSelfLink));
// validate vm tags
Map<Tag, String> vmTagLinks = new HashMap<>();
for (Tag tag : vmTags) {
for (TagState tagState : tagsMap.values()) {
if (tagState.key.equals(tag.getKey())) {
vmTagLinks.put(tag, tagState.documentSelfLink);
}
}
}
ComputeState linuxVMId1ComputeState = getComputeByAWSId(this.host, linuxVMId1);
// compute has 2 remote tags + 1 local tag
assertEquals(linuxVMId1Tags.size() + internalTagsCount1, linuxVMId1ComputeState.tagLinks.size());
for (Tag tag : linuxVMId1Tags) {
assertTrue(linuxVMId1ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
}
ComputeState linuxVMId2ComputeState = getComputeByAWSId(this.host, linuxVMId2);
assertEquals(linuxVMId2Tags.size() + internalTagsCount1, linuxVMId2ComputeState.tagLinks.size());
for (Tag tag : linuxVMId2Tags) {
assertTrue(linuxVMId2ComputeState.tagLinks.contains(vmTagLinks.get(tag)));
}
} catch (Throwable t) {
this.host.log("Exception occurred during test execution: %s", t.getMessage());
if (t instanceof AssertionError) {
fail("Assert exception occurred during test execution: " + t.getMessage());
}
} finally {
// un-tag default SG
unTagResources(this.client, sgTags, this.securityGroupId);
// un-tag default VPC
unTagResources(this.client, networkTags, this.vpcId);
// un-tag default Subnet
unTagResources(this.client, subnetTags, this.subnetId);
// un-tag default Disk
unTagResources(this.client, diskTags, this.diskId);
tearDownTestDisk(this.client, this.host, this.awsTestContext, this.isMock);
}
}
use of com.vmware.photon.controller.model.resources.DiskService.DiskState 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.DiskService.DiskState in project photon-model by vmware.
the class AWSDiskService method updateDiskState.
/**
* Update photon-model disk state with the properties of ebs volume.
*/
private void updateDiskState(Volume volume, AWSDiskContext context, AwsDiskStage next) {
DiskState diskState = context.disk;
diskState.id = volume.getVolumeId();
if (volume.getCreateTime() != null) {
diskState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(volume.getCreateTime().getTime());
}
diskState.status = DiskService.DiskStatus.AVAILABLE;
diskState.origin = DiskService.DiskOrigin.DEPLOYED;
diskState.encrypted = volume.getEncrypted();
// calculate disk name, default to volume-id if 'Name' tag is not present
if (diskState.name == null) {
if (volume.getTags() == null) {
diskState.name = volume.getVolumeId();
} else {
diskState.name = volume.getTags().stream().filter(tag -> tag.getKey().equals(AWS_TAG_NAME)).map(tag -> tag.getValue()).findFirst().orElse(volume.getVolumeId());
}
}
diskState.zoneId = volume.getAvailabilityZone();
sendRequest(Operation.createPatch(createInventoryUri(this.getHost(), context.diskRequest.resourceLink())).setBody(diskState).setCompletion((o, e) -> {
if (e != null) {
handleStages(context, e);
return;
}
handleStages(context, next);
}));
}
Aggregations