use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSRemoteCleanup method cleanUpVpc.
/**
* Cleaning all VPC's that are not tagged with a name: enumtest-vpc or a default VPC in US_EAST_1 region
* Deleting a VPC would require its dependencies to be deleted in the following order:
* 1) EC2 Instances
* 2) NAT Gateway
* 3) Internet Gateway
* 4) VPN Gateway
* 5) Network ACL's
* 6) Security Group ( not deleting default SG)
* 7) Subnets
* NOTE: Not deleting RouteTables currently
*/
@Test
public void cleanUpVpc() {
if (this.isMock) {
return;
}
AmazonEC2 usEastEc2Client = this.ec2Clients.get(US_EAST_1_TAG);
DescribeVpcsResult vpcsResult = usEastEc2Client.describeVpcs();
List<Vpc> vpcs = vpcsResult.getVpcs();
List<String> vpcIdsToBeDeleted = new ArrayList<>();
List<String> enumTestVpcIds = new ArrayList<>();
try {
vpcs.stream().forEach(vpc -> {
vpc.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(NAME_TAG_KEY) && this.vpcTagsNotToBeDeleted.contains(tag.getValue().toLowerCase())).forEach(tag -> enumTestVpcIds.add(vpc.getVpcId()));
if (!vpc.getIsDefault()) {
vpcIdsToBeDeleted.add(vpc.getVpcId());
}
});
vpcIdsToBeDeleted.removeAll(enumTestVpcIds);
vpcIdsToBeDeleted.stream().forEach(vpcId -> {
DescribeInstancesRequest instancesRequest = new DescribeInstancesRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeInstancesResult instancesResult = usEastEc2Client.describeInstances(instancesRequest);
deleteAwsEc2instances(vpcIdsToBeDeleted, instancesResult, usEastEc2Client);
deleteNATGateway(vpcId, usEastEc2Client);
deleteNetworkInterfaces(vpcId, usEastEc2Client);
deleteInternetGateways(vpcId, usEastEc2Client);
deleteVirtualPrivateGateways(vpcId, usEastEc2Client);
disassociateAndDeleteNetworkACLs(vpcId, usEastEc2Client);
deleteSecurityGroups(vpcId, usEastEc2Client);
deleteSubnets(vpcId, usEastEc2Client);
DeleteVpcRequest deleteVpcRequest = new DeleteVpcRequest().withVpcId(vpcId);
this.host.log("Terminating stale vpc: %s", vpcId);
usEastEc2Client.deleteVpc(deleteVpcRequest);
});
} catch (Exception e) {
this.host.log(Level.INFO, e.getMessage());
}
}
use of software.amazon.awssdk.services.ec2.model.Vpc 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.Vpc in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method createNetworkStateOperations.
/**
* Create the network state operations for all the VPCs that need to be created or updated in
* the system.
*/
private void createNetworkStateOperations(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
if (context.vpcs.isEmpty()) {
logFine(() -> "No new VPCs have been discovered.");
handleNetworkStateChanges(context, next);
return;
}
final List<Operation> networkOperations = new ArrayList<>();
for (String remoteVPCId : context.vpcs.keySet()) {
NetworkState networkState = context.vpcs.get(remoteVPCId);
final Operation networkStateOp;
if (context.localNetworkStateMap.containsKey(remoteVPCId)) {
// If the local network state already exists for the VPC -> Update it.
networkState.documentSelfLink = context.localNetworkStateMap.get(remoteVPCId).documentSelfLink;
// don't overwrite resourcePoolLink
networkState.resourcePoolLink = null;
if (networkState.tagLinks == null || networkState.tagLinks.isEmpty()) {
setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
} else {
context.networkInternalTagLinksSet.stream().filter(tagLink -> !networkState.tagLinks.contains(tagLink)).map(tagLink -> networkState.tagLinks.add(tagLink)).collect(Collectors.toSet());
}
networkStateOp = createPatchOperation(this, networkState, networkState.documentSelfLink);
} else {
Vpc awsVpc = context.awsVpcs.get(remoteVPCId);
// Add both external and internal tags.
setResourceTags(networkState, awsVpc.getTags());
setTagLinksToResourceState(networkState, context.networkInternalTagsMap, false);
networkStateOp = createPostOperation(this, networkState, NetworkService.FACTORY_LINK);
}
networkOperations.add(networkStateOp);
}
JoinedCompletionHandler joinCompletion = (ops, excs) -> {
if (excs != null) {
Entry<Long, Throwable> excEntry = excs.entrySet().iterator().next();
Throwable exc = excEntry.getValue();
Operation op = ops.get(excEntry.getKey());
logSevere(() -> String.format("Error %s-ing a Network state: %s", op.getAction(), Utils.toString(excs)));
finishWithFailure(context, exc);
return;
}
logFine(() -> "Created/updated all network states.");
ops.values().stream().filter(op -> op.getStatusCode() != Operation.STATUS_CODE_NOT_MODIFIED).forEach(op -> {
NetworkState networkState = op.getBody(NetworkState.class);
context.vpcs.put(networkState.id, networkState);
});
handleNetworkStateChanges(context, next);
};
OperationJoin.create(networkOperations).setCompletion(joinCompletion).sendWith(this);
}
use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkClient method getVPC.
public Vpc getVPC(String vpcId) {
DescribeVpcsRequest req = new DescribeVpcsRequest().withVpcIds(vpcId);
DescribeVpcsResult result = this.client.describeVpcs(req);
List<Vpc> vpcs = result.getVpcs();
if (vpcs != null && vpcs.size() == 1) {
return vpcs.get(0);
}
return null;
}
use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.
the class TestAWSNetworkService method testGetMainRouteTable.
@Test
public void testGetMainRouteTable() throws Throwable {
Vpc defVPC = this.netClient.getDefaultVPC();
assertTrue(defVPC != null);
RouteTable routeTable = this.netClient.getMainRouteTable(defVPC.getVpcId());
assertTrue(routeTable != null);
}
Aggregations