use of software.amazon.awssdk.services.ec2.model.Tag 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 software.amazon.awssdk.services.ec2.model.Tag in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method updateTagLinks.
private DeferredResult<AWSNetworkStateCreationContext> updateTagLinks(AWSNetworkStateCreationContext context) {
if ((context.awsVpcs == null || context.awsVpcs.isEmpty()) && (context.awsSubnets == null || context.awsSubnets.isEmpty())) {
logFine(() -> "No local vpcs or subnets to be updated so there are no tags to update.");
return DeferredResult.completed(context);
} else {
List<DeferredResult<Set<String>>> updateNetworkSubnetTagLinksOps = new ArrayList<>();
// update tag links for the existing NetworkStates
for (String vpcId : context.awsVpcs.keySet()) {
if (!context.localNetworkStateMap.containsKey(vpcId)) {
// this is not a network to update
continue;
}
Vpc vpc = context.awsVpcs.get(vpcId);
NetworkState existingNetworkState = context.localNetworkStateMap.get(vpcId);
Map<String, String> remoteTags = new HashMap<>();
for (Tag awsVpcTag : vpc.getTags()) {
if (!awsVpcTag.getKey().equals(AWSConstants.AWS_TAG_NAME)) {
remoteTags.put(awsVpcTag.getKey(), awsVpcTag.getValue());
}
}
updateNetworkSubnetTagLinksOps.add(updateLocalTagStates(this, existingNetworkState, remoteTags, null));
}
// update tag links for the existing SubnetStates
for (String subnetId : context.awsSubnets.keySet()) {
if (!context.localSubnetStateMap.containsKey(subnetId)) {
// this is not a subnet to update
continue;
}
Subnet subnet = context.awsSubnets.get(subnetId);
SubnetState existingSubnetState = context.localSubnetStateMap.get(subnetId);
Map<String, String> remoteTags = new HashMap<>();
for (Tag awsSubnetTag : subnet.getTags()) {
if (!awsSubnetTag.getKey().equals(AWSConstants.AWS_TAG_NAME)) {
remoteTags.put(awsSubnetTag.getKey(), awsSubnetTag.getValue());
}
}
updateNetworkSubnetTagLinksOps.add(updateLocalTagStates(this, existingSubnetState, remoteTags, null));
}
return DeferredResult.allOf(updateNetworkSubnetTagLinksOps).thenApply(ignore -> context);
}
}
use of software.amazon.awssdk.services.ec2.model.Tag in project photon-model by vmware.
the class AWSNetworkClient method createNameTagAsync.
public DeferredResult<Void> createNameTagAsync(String resourceId, String name) {
Tag nameTag = new Tag().withKey(AWS_TAG_NAME).withValue(name);
CreateTagsRequest request = new CreateTagsRequest().withResources(resourceId).withTags(nameTag);
String message = "Name tag AWS resource with id [" + resourceId + "] with name [" + name + "].";
AWSDeferredResultAsyncHandler<CreateTagsRequest, CreateTagsResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createTagsAsync(request, handler);
return handler.toDeferredResult().thenApply(result -> (Void) null);
}
use of software.amazon.awssdk.services.ec2.model.Tag in project photon-model by vmware.
the class AWSEnumerationUtils method mapInstanceToComputeState.
/**
* Maps the instance discovered on AWS to a local compute state that will be persisted.
*/
public static ComputeState mapInstanceToComputeState(ServiceHost host, Instance instance, String parentComputeLink, String placementComputeLink, String resourcePoolLink, String existingEndpointLink, Set<String> endpointLinks, String computeDescriptionLink, Set<URI> parentCDStatsAdapterReferences, Set<String> internalTagLinks, String regionId, String zoneId, List<String> tenantLinks, List<Tag> createdExternalTags, Boolean isNewState, List<String> diskLinks) {
ComputeState computeState = new ComputeState();
computeState.id = instance.getInstanceId();
computeState.name = instance.getInstanceId();
computeState.parentLink = parentComputeLink;
computeState.computeHostLink = parentComputeLink;
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AWS;
computeState.regionId = regionId;
computeState.zoneId = zoneId;
computeState.instanceType = instance.getInstanceType();
computeState.instanceAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_INSTANCE_ADAPTER);
computeState.enumerationAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_ENUMERATION_CREATION_ADAPTER);
computeState.statsAdapterReference = AdapterUriUtil.buildAdapterUri(host, AWSUriPaths.AWS_STATS_ADAPTER);
computeState.statsAdapterReferences = parentCDStatsAdapterReferences;
computeState.resourcePoolLink = resourcePoolLink;
if (computeState.endpointLinks == null) {
computeState.endpointLinks = new HashSet<>();
}
computeState.endpointLinks.addAll(endpointLinks);
// assign existing one, if exists
if (existingEndpointLink != null) {
computeState.endpointLink = existingEndpointLink;
} else {
computeState.endpointLink = endpointLinks.iterator().next();
}
// Compute descriptions are looked up by the instanceType in the local list of CDs.
computeState.descriptionLink = computeDescriptionLink;
computeState.hostName = instance.getPublicDnsName();
// TODO VSYM-375 for adding disk information
computeState.address = instance.getPublicIpAddress();
computeState.powerState = AWSUtils.mapToPowerState(instance.getState());
computeState.customProperties = new HashMap<>();
computeState.customProperties.put(CUSTOM_OS_TYPE, getNormalizedOSType(instance));
computeState.customProperties.put(SOURCE_TASK_LINK, ResourceEnumerationTaskService.FACTORY_LINK);
computeState.customProperties.put(ComputeProperties.PLACEMENT_LINK, placementComputeLink);
// Network State. Create one network state mapping to each VPC that is discovered during
// enumeration.
computeState.customProperties.put(AWS_VPC_ID, instance.getVpcId());
computeState.tagLinks = new HashSet<>();
// PATCH to update tagLinks of existing disks.
if (!instance.getTags().isEmpty() && isNewState) {
// we have already made sure that the tags exist and we can build their links ourselves
computeState.tagLinks = instance.getTags().stream().filter(t -> !AWSConstants.AWS_TAG_NAME.equals(t.getKey()) && createdExternalTags.contains(t)).map(t -> newTagState(t.getKey(), t.getValue(), true, tenantLinks)).map(TagFactoryService::generateSelfLink).collect(Collectors.toSet());
if (computeState.tagLinks != null && computeState.tagLinks.contains(null)) {
host.log(Level.SEVERE, "Null tag link inserted in new ComputeState for instance ID: %s", instance.getInstanceId());
host.log(Level.SEVERE, "Removing null tag link from new ComputeState");
computeState.tagLinks.remove(null);
}
}
// The name of the compute state is the value of the AWS_TAG_NAME tag
String nameTag = getTagValue(instance.getTags(), AWS_TAG_NAME);
if (nameTag != null && !nameTag.equals(EMPTY_STRING)) {
computeState.name = nameTag;
}
// append internal tagLinks to any existing ones
if (internalTagLinks != null) {
computeState.tagLinks.addAll(internalTagLinks);
}
if (instance.getLaunchTime() != null) {
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
}
if (diskLinks != null && !diskLinks.isEmpty()) {
computeState.diskLinks = new ArrayList<>();
computeState.diskLinks.addAll(diskLinks);
}
computeState.tenantLinks = tenantLinks;
return computeState;
}
use of software.amazon.awssdk.services.ec2.model.Tag in project photon-model by vmware.
the class TestAWSSetupUtils method createOrGetSubnet.
/**
* Creates a Subnet if not exist and return the Subnet id.
*/
public static Subnet createOrGetSubnet(AmazonEC2AsyncClient client, String subnetCidr, String vpcId, String zoneId) {
List<Filter> filters = new ArrayList<>();
Filter vpcFilter = new Filter();
vpcFilter.withName(VPC_KEY);
vpcFilter.withValues(vpcId);
filters.add(vpcFilter);
if (zoneId != null) {
Filter azFilter = new Filter();
azFilter.withName(AVAILABILITY_ZONE_FILTER);
azFilter.withValues(zoneId);
filters.add(azFilter);
}
DescribeSubnetsResult result = client.describeSubnets(new DescribeSubnetsRequest().withFilters(filters));
List<Subnet> defaultSubnets = new ArrayList<>();
result.getSubnets().stream().forEach(subnet -> {
subnet.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(TAG_KEY_FOR_TEST_RESOURCES) && tag.getValue().equalsIgnoreCase(AWS_DEFAULT_SUBNET_NAME)).forEach(tag -> defaultSubnets.add(subnet));
});
if (defaultSubnets != null && !defaultSubnets.isEmpty()) {
return defaultSubnets.get(0);
} else {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId);
if (zoneId != null) {
req.withAvailabilityZone(zoneId);
}
CreateSubnetResult res = client.createSubnet(req);
String subnetId = res.getSubnet().getSubnetId();
tagResources(client, Arrays.asList(subnetId), TAG_KEY_FOR_TEST_RESOURCES, AWS_DEFAULT_SUBNET_NAME);
return res.getSubnet();
}
}
Aggregations