use of com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSUtils method getDefaultVPC.
/**
* Gets the default VPC
*/
public static Vpc getDefaultVPC(AWSInstanceContext aws) {
DescribeVpcsResult result = aws.amazonEC2Client.describeVpcs();
List<Vpc> vpcs = result.getVpcs();
for (Vpc vpc : vpcs) {
if (vpc.isDefault()) {
return vpc;
}
}
return null;
}
use of com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSUtils method createSecurityGroupOnDefaultVPC.
// method create a security group in the VPC from custom properties or the default VPC
private static String createSecurityGroupOnDefaultVPC(AWSInstanceContext aws) {
String vpcId = null;
// get the subnet cidr (if any)
String subnetCidr = null;
// in case subnet will be obtained from the default vpc, the security group should
// as well be created there
Vpc defaultVPC = getDefaultVPC(aws);
if (defaultVPC != null) {
vpcId = defaultVPC.getVpcId();
subnetCidr = defaultVPC.getCidrBlock();
}
// no subnet or no vpc is not an option...
if (subnetCidr == null || vpcId == null) {
throw new AmazonServiceException("default VPC not found");
}
return new AWSSecurityGroupClient(aws.amazonEC2Client).createDefaultSecurityGroupWithDefaultRules(defaultVPC);
}
use of com.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method getMainRouteTableInformation.
/**
* Gets the main route table information associated with a VPC that is being mapped to a network
* state in the system. *
*/
private void getMainRouteTableInformation(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
DescribeRouteTablesRequest routeTablesRequest = new DescribeRouteTablesRequest();
List<String> vpcList = new ArrayList<>(context.vpcs.keySet());
// build filter list
List<Filter> filters = new ArrayList<>();
filters.add(new Filter(AWS_FILTER_VPC_ID, vpcList));
filters.add(AWSUtils.getFilter(AWS_MAIN_ROUTE_ASSOCIATION, "true"));
AWSMainRouteTableAsyncHandler asyncHandler = new AWSMainRouteTableAsyncHandler(next, context);
context.amazonEC2Client.describeRouteTablesAsync(routeTablesRequest, asyncHandler);
}
use of com.amazonaws.services.ec2.model.Vpc 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.amazonaws.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkClient method createVPC.
/**
* Creates the VPC and returns the VPC id
*/
public String createVPC(String subnetCidr) {
CreateVpcRequest req = new CreateVpcRequest().withCidrBlock(subnetCidr);
CreateVpcResult vpc = this.client.createVpc(req);
return vpc.getVpc().getVpcId();
}
Aggregations