use of com.amazonaws.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 com.amazonaws.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 com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkClient method createSubnetAsync.
/**
* Async create the subnet and return it.
*/
public DeferredResult<Subnet> createSubnetAsync(String subnetCidr, String vpcId, String zoneId) {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId).withAvailabilityZone(zoneId);
String message = "Create AWS Subnet with CIDR [" + subnetCidr + "] for vpc id [" + vpcId + "].";
AWSDeferredResultAsyncHandler<CreateSubnetRequest, CreateSubnetResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createSubnetAsync(req, handler);
return handler.toDeferredResult().thenApply(CreateSubnetResult::getSubnet);
}
use of com.amazonaws.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 com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkClient method createRouteTable.
/**
* Create a route table
*/
public DeferredResult<String> createRouteTable(String vpcId) {
CreateRouteTableRequest req = new CreateRouteTableRequest().withVpcId(vpcId);
String message = "Create AWS Route Table on VPC [" + vpcId + "].";
AWSDeferredResultAsyncHandler<CreateRouteTableRequest, CreateRouteTableResult> handler = new AWSDeferredResultAsyncHandler<>(this.service, message);
this.client.createRouteTableAsync(req, handler);
return handler.toDeferredResult().thenApply(CreateRouteTableResult::getRouteTable).thenApply(RouteTable::getRouteTableId);
}
Aggregations