use of software.amazon.awssdk.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 software.amazon.awssdk.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 software.amazon.awssdk.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 software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkClient method getDefaultVPC.
/**
* Get the default VPC - return null if no default specified
*/
public Vpc getDefaultVPC() {
DescribeVpcsRequest req = new DescribeVpcsRequest();
DescribeVpcsResult result = this.client.describeVpcs(req);
List<Vpc> vpcs = result.getVpcs();
for (Vpc vpc : vpcs) {
if (vpc.isDefault()) {
return vpc;
}
}
return null;
}
use of software.amazon.awssdk.services.ec2.model.Vpc in project photon-model by vmware.
the class AWSNetworkUtils method mapVPCToNetworkState.
public static NetworkState mapVPCToNetworkState(Vpc vpc, String regionId, String resourcePoolLink, String endpointLink, String authCredentialsLink, String parentComputeLink, List<String> tenantLinks, URI adapterUri) {
if (vpc == null) {
throw new IllegalArgumentException("Cannot map VPC to network state for null instance");
}
NetworkState networkState = new NetworkState();
networkState.id = vpc.getVpcId();
// calculate vpc name
if (vpc.getTags() == null) {
networkState.name = vpc.getVpcId();
} else {
networkState.name = vpc.getTags().stream().filter(tag -> tag.getKey().equals(AWS_TAG_NAME)).map(tag -> tag.getValue()).findFirst().orElse(vpc.getVpcId());
}
networkState.subnetCIDR = vpc.getCidrBlock();
networkState.regionId = regionId;
networkState.resourcePoolLink = resourcePoolLink;
networkState.endpointLink = endpointLink;
if (networkState.endpointLinks == null) {
networkState.endpointLinks = new HashSet<>();
}
networkState.endpointLinks.add(endpointLink);
networkState.authCredentialsLink = authCredentialsLink;
networkState.instanceAdapterReference = adapterUri;
networkState.tenantLinks = tenantLinks;
networkState.computeHostLink = parentComputeLink;
networkState.customProperties = new HashMap<>();
networkState.customProperties.put("defaultInstance", String.valueOf(vpc.isDefault()));
return networkState;
}
Aggregations