use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method createTags.
/**
* Gets the Networks and Subnets tags information and creates TagState for each tag
*/
private void createTags(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
// Collect all tags in a List
List<Tag> allNetworkAndSubnetsTags = context.awsVpcs.values().stream().filter(vpc -> !context.localNetworkStateMap.containsKey(vpc.getVpcId())).flatMap(vpc -> vpc.getTags().stream()).collect(Collectors.toList());
allNetworkAndSubnetsTags.addAll(context.awsSubnets.values().stream().filter(subnet -> !context.localSubnetStateMap.containsKey(subnet.getSubnetId())).flatMap(subnet -> subnet.getTags().stream()).collect(Collectors.toList()));
// 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<>();
allNetworkAndSubnetsTags.stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey())).forEach(t -> {
TagState tagState = newTagState(t.getKey(), t.getValue(), true, context.request.tenantLinks);
Operation createTagOp = Operation.createPost(this, TagService.FACTORY_LINK).setBody(tagState);
operations.add(createTagOp);
tagsCreationOperationIdsMap.put(createTagOp.getId(), t);
});
if (operations.isEmpty()) {
context.networkCreationStage = next;
handleNetworkStateChanges(context);
} else {
OperationJoin.create(operations).setCompletion((ops, exs) -> {
if (exs != null && !exs.isEmpty()) {
this.logWarning("Failure creating external tags for network and subnets: %s", exs.get(0).getMessage());
}
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.networkCreationStage = next;
handleNetworkStateChanges(context);
}).sendWith(this);
}
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method createInternalTypeTags.
/**
* Method to create the internal tags for subnets and VPCs.
*/
private void createInternalTypeTags(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
// Go over the list of internal tags to be created. Find whatever already does not have an
// associated tag state and create an operation for its creation.
List<Operation> joinOperations = new ArrayList<>();
for (String resourceType : internalTagList) {
TagState typeTag = newTagState(TAG_KEY_TYPE, resourceType, false, context.request.tenantLinks);
Operation op = Operation.createPost(this, TagService.FACTORY_LINK).setBody(typeTag);
joinOperations.add(op);
}
OperationJoin.create(joinOperations).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error creating internal tag : %s", ex.getMessage())));
context.networkCreationStage = next;
handleNetworkStateChanges(context);
return;
}
for (String internalTagValue : internalTagList) {
TagState tagState = newTagState(TAG_KEY_TYPE, internalTagValue, false, context.request.tenantLinks);
if (internalTagValue.equalsIgnoreCase(AWSResourceType.ec2_vpc.toString())) {
context.networkInternalTagsMap.put(PhotonModelConstants.TAG_KEY_TYPE, tagState.value);
context.networkInternalTagLinksSet.add(tagState.documentSelfLink);
} else {
context.subnetInternalTagsMap.put(PhotonModelConstants.TAG_KEY_TYPE, tagState.value);
context.subnetInternalTagLinksSet.add(tagState.documentSelfLink);
}
}
context.networkCreationStage = next;
handleNetworkStateChanges(context);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class AWSS3StorageEnumerationAdapterService method createInternalTypeTag.
/**
* Send a POST to create internal type tag for S3. Don't wait for completion, tag will eventually get created
* while we go through the enumeration flow. If tag creation is successful, we set the internalTypeTagCreated
* flag and add the internalTypeTagSelfLink to DiskStates.
*/
private void createInternalTypeTag(S3StorageEnumerationContext aws, S3StorageEnumerationSubStage next) {
TagState internalTypeTagState = TagsUtil.newTagState(TAG_KEY_TYPE, AWSResourceType.s3_bucket.toString(), false, aws.parentCompute.tenantLinks);
Operation.createPost(aws.service, TagService.FACTORY_LINK).setBody(internalTypeTagState).setReferer(aws.service.getUri()).setCompletion((o, e) -> {
// log exception if tag creation fails with anything other than IDEMPOTENT_POST behaviour.
if (e != null) {
logSevere("Error creating internal type tag for S3");
return;
}
aws.internalTypeTagSelfLink = internalTypeTagState.documentSelfLink;
}).sendWith(aws.service);
aws.subStage = next;
handleReceivedEnumerationData(aws);
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class AzureStorageEnumerationAdapterService method createInternalTypeTags.
private void createInternalTypeTags(StorageEnumContext context, StorageEnumStages next) {
TagState blobTypeTag = newTagState(TAG_KEY_TYPE, AzureResourceType.azure_blob.name(), false, context.parentCompute.tenantLinks);
TagState vhdTypeTag = newTagState(TAG_KEY_TYPE, AzureResourceType.azure_vhd.name(), false, context.parentCompute.tenantLinks);
Collection<Operation> opCollection = new ArrayList<>();
Operation blobTypeTagOp = Operation.createPost(getHost(), TagService.FACTORY_LINK).setBody(blobTypeTag);
opCollection.add(blobTypeTagOp);
Operation vhdTypeTagOp = Operation.createPost(getHost(), TagService.FACTORY_LINK).setBody(vhdTypeTag);
opCollection.add(vhdTypeTagOp);
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
} else {
for (Operation taskOp : ops.values()) {
TagState state = taskOp.getBody(TagState.class);
if (EnumUtils.isValidEnum(AzureResourceType.class, state.value)) {
switch(AzureResourceType.valueOf(state.value)) {
case azure_vhd:
context.internalVhdTypeTag = vhdTypeTag.documentSelfLink;
break;
case azure_blob:
context.internalBlobTypeTag = blobTypeTag.documentSelfLink;
break;
default:
break;
}
}
}
}
context.subStage = next;
handleSubStage(context);
}).sendWith(this);
}
use of com.vmware.photon.controller.model.resources.TagService.TagState in project photon-model by vmware.
the class TestAzureLongRunningEnumeration method assertRemoteResources.
/**
* Assert that remote resources are enumerated and exist locally.
*/
private void assertRemoteResources() {
for (int i = 0; i < numOfVMsToTest; i++) {
assertResourceExists(this.host, NetworkService.FACTORY_LINK, nicSpecs.get(i).network.name, true);
for (NicSpec nicSpec : nicSpecs.get(i).nicSpecs) {
assertResourceExists(this.host, SubnetService.FACTORY_LINK, nicSpec.getSubnetSpec().name, true);
}
assertResourceExists(this.host, ResourceGroupService.FACTORY_LINK, azureVMNames.get(i), true);
assertResourceExists(this.host, SecurityGroupService.FACTORY_LINK, AZURE_SECURITY_GROUP_NAME, true);
assertResourceExists(this.host, StorageDescriptionService.FACTORY_LINK, (azureVMNames.get(i) + "sa").replace("-", ""), true);
assertDiskExist(this.host, DiskService.FACTORY_LINK, azureVMNames.get(i) + "-boot-disk", true);
validateDiskInternalTag(this.host);
// Tags
final Map<String, String> expectedTags = new HashMap<>();
expectedTags.put(NETWORK_TAG_KEY_PREFIX + azureVMNames.get(i), NETWORK_TAG_VALUE);
expectedTags.put(VM_TAG_KEY_PREFIX + azureVMNames.get(i), VM_TAG_VALUE);
expectedTags.put(SG_TAG_KEY_PREFIX + azureVMNames.get(i), SG_TAG_VALUE);
final List<String> keysToLowerCase = expectedTags.keySet().stream().map(String::toLowerCase).collect(Collectors.toList());
Query query = Query.Builder.create().addKindFieldClause(TagState.class).addInClause(TagState.FIELD_NAME_KEY, keysToLowerCase).build();
Map<String, Query.Occurance> origin = new HashMap<>();
origin.put(DISCOVERED.toString(), Query.Occurance.MUST_OCCUR);
origin.put(SYSTEM.toString(), Query.Occurance.MUST_NOT_OCCUR);
origin.put(USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR);
Query externalQuery = createOriginTagQuery(Boolean.TRUE, origin);
query.addBooleanClause(externalQuery);
QueryStrategy<TagState> queryLocalTags = new QueryTop<>(this.host, query, TagState.class, null).setMaxResultsLimit(expectedTags.size() + 1);
List<TagState> tagStates = waitToComplete(queryLocalTags.collectDocuments(Collectors.toList()));
assertEquals("TagStates were not discovered.", expectedTags.size(), tagStates.size());
for (TagState tag : tagStates) {
assertEquals(expectedTags.get(tag.key), tag.value);
}
}
}
Aggregations