use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class AWSComputeStateCreationAdapterService method createTags.
/**
* POSTs all tags for newly discovered instances. Even if some tags already exist we rely on
* IDEMPOTENT_POST behaviour and POST them again. All tags that got created successfully are
* stored in createdExternalTags list.
*/
private void createTags(AWSComputeStateCreationContext context, AWSComputeStateCreationStage next) {
// Get all tags from the instances to be created
Set<Tag> create = context.request.instancesToBeCreated.stream().flatMap(i -> i.getTags().stream()).collect(Collectors.toSet());
// Put them in a set to remove the duplicates
Set<Tag> allTags = new HashSet<>();
allTags.addAll(create);
// POST each of the tags. If a tag exists it won't be created again. We don't want the name
// tags, so filter them out
List<Operation> operations = new ArrayList<>();
Map<Long, Tag> tagsCreationOperationIdsMap = new ConcurrentHashMap<>();
allTags.stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey())).forEach(t -> {
TagState tagState = newTagState(t.getKey(), t.getValue(), true, context.request.tenantLinks);
Operation op = Operation.createPost(this, TagService.FACTORY_LINK).setBody(tagState);
operations.add(op);
tagsCreationOperationIdsMap.put(op.getId(), t);
});
if (operations.isEmpty()) {
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
} else {
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null && !exs.isEmpty()) {
logSevere(() -> String.format("Error creating %s external tags for compute" + "states: %s", exs.size(), Utils.toString(exs.get(0))));
}
ops.values().stream().filter(operation -> operation.getStatusCode() == Operation.STATUS_CODE_OK || operation.getStatusCode() == Operation.STATUS_CODE_NOT_MODIFIED).forEach(operation -> {
if (tagsCreationOperationIdsMap.containsKey(operation.getId())) {
context.createdExternalTags.add(tagsCreationOperationIdsMap.get(operation.getId()));
}
});
context.creationStage = next;
handleComputeStateCreateOrUpdate(context);
}).sendWith(this);
}
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class TestAWSProvisionTask method testProvision.
// Creates a AWS instance via a provision task.
@Test
public void testProvision() throws Throwable {
initResourcePoolAndComputeHost();
// create a AWS VM compute resoruce
boolean addNonExistingSecurityGroup = true;
this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm1", zoneId, regionId, null, /* tagLinks */
this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
// set placement link
String zoneId = TestAWSSetupUtils.zoneId + avalabilityZoneIdentifier;
ComputeState zoneComputeState = createAWSComputeHost(this.host, this.endpointState, zoneId, regionId, this.isAwsClientMock, this.awsMockEndpointReference, null);
zoneComputeState.id = zoneId;
zoneComputeState = TestUtils.doPatch(this.host, zoneComputeState, ComputeState.class, UriUtils.buildUri(this.host, zoneComputeState.documentSelfLink));
if (this.vmState.customProperties == null) {
this.vmState.customProperties = new HashMap<>();
}
this.vmState.customProperties.put(PLACEMENT_LINK, zoneComputeState.documentSelfLink);
TestUtils.doPatch(this.host, this.vmState, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskState provisionTask = new ProvisionComputeTaskService.ProvisionComputeTaskState();
provisionTask.computeLink = this.vmState.documentSelfLink;
provisionTask.isMockRequest = this.isMock;
provisionTask.taskSubStage = ProvisionComputeTaskState.SubStage.CREATING_HOST;
// Wait for default request timeout in minutes for the machine to be powered ON before
// reporting failure to the parent task.
provisionTask.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.MINUTES.toMicros(AWS_VM_REQUEST_TIMEOUT_MINUTES);
provisionTask.tenantLinks = this.endpointState.tenantLinks;
provisionTask = TestUtils.doPost(this.host, provisionTask, ProvisionComputeTaskState.class, UriUtils.buildUri(this.host, ProvisionComputeTaskService.FACTORY_LINK));
this.host.waitForFinishedTask(ProvisionComputeTaskState.class, provisionTask.documentSelfLink);
// check that the VM has been created
ProvisioningUtils.queryComputeInstances(this.host, 3);
if (!this.isMock) {
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
Instance instance = instances.get(0);
assertTags(Collections.emptySet(), instance, this.vmState.name);
assertVmNetworksConfiguration(instance);
assertStorageConfiguration(this.client, instance, compute);
assertEquals(zoneId, instance.getPlacement().getAvailabilityZone());
}
this.host.setTimeoutSeconds(600);
this.host.waitFor("Error waiting for stats with default collection windows", () -> {
try {
this.host.log(Level.INFO, "Issuing stats request for VM with default collection window.");
issueStatsRequest(this.vmState, null);
} catch (Throwable t) {
return false;
}
return true;
});
// store the network links and disk links for removal check later
List<String> resourcesToDelete = new ArrayList<>();
if (this.vmState.diskLinks != null) {
resourcesToDelete.addAll(this.vmState.diskLinks);
}
if (this.vmState.networkInterfaceLinks != null) {
resourcesToDelete.addAll(this.vmState.networkInterfaceLinks);
}
// delete vm
TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host);
// validates the local documents of network links and disk links have been removed
verifyRemovalOfResourceState(this.host, resourcesToDelete);
// create another AWS VM
List<String> instanceIdList = new ArrayList<>();
Set<TagState> tags = createTags(null, "testProvisionKey1", "testProvisionValue1", "testProvisionKey2", "testProvisionValue2");
Set<String> tagLinks = tags.stream().map(t -> t.documentSelfLink).collect(Collectors.toSet());
addNonExistingSecurityGroup = false;
this.vmState = createAWSVMResource(this.host, this.computeHost, this.endpointState, this.getClass(), this.currentTestName.getMethodName() + "_vm2", TestAWSSetupUtils.zoneId, regionId, tagLinks, this.singleNicSpec, addNonExistingSecurityGroup, this.awsTestContext);
TestAWSSetupUtils.provisionMachine(this.host, this.vmState, this.isMock, instanceIdList);
if (!this.isMock) {
ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
assertTags(tags, instances.get(0), this.vmState.name);
assertVmNetworksConfiguration(instances.get(0));
assertStorageConfiguration(this.client, instances.get(0), compute);
// reach out to AWS and get the current state
TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
}
// delete just the local representation of the resource
TestAWSSetupUtils.deleteVMs(this.vmState.documentSelfLink, this.isMock, this.host, true);
if (!this.isMock) {
try {
TestAWSSetupUtils.getBaseLineInstanceCount(this.host, this.client, null);
} finally {
TestAWSSetupUtils.deleteVMsUsingEC2Client(this.client, this.host, instanceIdList);
deleteSecurityGroupUsingEC2Client(this.client, this.host, this.sgToCleanUp);
}
}
this.vmState = null;
this.sgToCleanUp = null;
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class TestAWSProvisionTask method assertTags.
private void assertTags(Set<TagState> expectedTagStates, Instance instance, String instanceName) {
Set<Tag> expectedTags = expectedTagStates.stream().map(ts -> new Tag(ts.key, ts.value)).collect(Collectors.toSet());
Set<Tag> actualTags = new HashSet<>(instance.getTags());
// account for the name tag
assertEquals(expectedTags.size() + 1, actualTags.size());
assertTrue(actualTags.containsAll(expectedTags));
Tag nameTag = new Tag(AWSConstants.AWS_TAG_NAME, instanceName);
assertTrue(actualTags.contains(nameTag));
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class AzureNetworkEnumerationAdapterService method createNetworkInternalTagStates.
/**
* Create internal tag states for networks.
*/
private void createNetworkInternalTagStates(NetworkEnumContext context, NetworkEnumStages next) {
TagState internalTypeTag = newTagState(PhotonModelConstants.TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE, false, context.parentCompute.tenantLinks);
// operation to create tag "type" for subnets.
Operation.createPost(this, TagService.FACTORY_LINK).setBody(internalTypeTag).setCompletion((o, e) -> {
if (e != null) {
logSevere("Error creating internal type tag for networks %s", e.getMessage());
} else {
context.networkInternalTagsMap.put(PhotonModelConstants.TAG_KEY_TYPE, NETWORK_TAG_TYPE_VALUE);
context.networkInternalTagLinksSet.add(internalTypeTag.documentSelfLink);
}
handleSubStage(context, next);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class ResourceUtilsTest method testExpandTags.
@Test
public void testExpandTags() throws Throwable {
TagState tag1 = new TagState();
tag1.key = "A";
tag1.value = "1";
tag1 = postServiceSynchronously(TagService.FACTORY_LINK, tag1, TagState.class);
TagState tag2 = new TagState();
tag2.key = "A";
tag2.value = "2";
tag2 = postServiceSynchronously(TagService.FACTORY_LINK, tag2, TagState.class);
TagState tag3 = new TagState();
tag3.key = "A";
tag3.value = "3";
tag3 = postServiceSynchronously(TagService.FACTORY_LINK, tag3, TagState.class);
// validate expansion on POST
ComputeState compute = new ComputeState();
compute.descriptionLink = "cdLink";
compute.tagLinks = new HashSet<>();
compute.tagLinks.add(tag1.documentSelfLink);
compute.tagLinks.add(tag2.documentSelfLink);
compute = postServiceSynchronously(ComputeService.FACTORY_LINK, compute, ComputeState.class);
Collection<String> tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
assertEquals(2, tags.size());
assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
// validate tags cannot be modified directly
compute.expandedTags.remove(1);
assertEquals(1, compute.expandedTags.size());
putServiceSynchronously(compute.documentSelfLink, compute);
compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
assertEquals(2, tags.size());
assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2")));
// validate expansion on PUT
compute.tagLinks.remove(tag2.documentSelfLink);
compute.tagLinks.add(tag3.documentSelfLink);
putServiceSynchronously(compute.documentSelfLink, compute);
compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
assertEquals(2, tags.size());
assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n3")));
// validate expansion on PATCH
ComputeState patchState = new ComputeState();
patchState.tagLinks = new HashSet<>();
patchState.tagLinks.add(tag2.documentSelfLink);
compute = patchServiceSynchronously(compute.documentSelfLink, patchState, ComputeState.class);
tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
assertEquals(3, tags.size());
assertTrue(tags.containsAll(Arrays.asList("A\n1", "A\n2", "A\n3")));
// validate expansion through custom PATCH body
Map<String, Collection<Object>> itemsToRemove = new HashMap<>();
itemsToRemove.put(ResourceState.FIELD_NAME_TAG_LINKS, Arrays.asList(tag2.documentSelfLink, tag3.documentSelfLink));
patchServiceSynchronously(compute.documentSelfLink, ServiceStateCollectionUpdateRequest.create(null, itemsToRemove));
compute = getServiceSynchronously(compute.documentSelfLink, ComputeState.class);
tags = compute.expandedTags.stream().map(t -> t.tag).collect(Collectors.toList());
assertEquals(1, tags.size());
assertTrue(tags.containsAll(Arrays.asList("A\n1")));
// validate query (case-insensitive) (Note: only 1 tag can be found with Xenon 1.6.1)
Query tagQuery = Query.Builder.create().addFieldClause(TagInfo.COMPOSITE_FIELD_NAME_TAG, "a*", MatchType.WILDCARD).build();
QueryTask tagQueryTask = QueryTask.Builder.createDirectTask().setQuery(tagQuery).addOption(QueryOption.EXPAND_CONTENT).build();
tagQueryTask = postServiceSynchronously(ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, tagQueryTask, QueryTask.class);
assertEquals(1, tagQueryTask.results.documentLinks.size());
assertEquals(1, tagQueryTask.results.documents.size());
assertEquals(compute.documentSelfLink, tagQueryTask.results.documentLinks.get(0));
ComputeState foundCompute = Utils.fromJson(tagQueryTask.results.documents.values().iterator().next(), ComputeState.class);
assertEquals(1, foundCompute.expandedTags.size());
assertEquals("A\n1", foundCompute.expandedTags.get(0).tag);
}
Aggregations