use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest 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();
}
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest in project cloudbreak by hortonworks.
the class AwsResourceConnector method isMapPublicOnLaunch.
private boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
boolean mapPublicIpOnLaunch = true;
if (awsNetworkView.isExistingVPC() && awsNetworkView.isExistingSubnet()) {
DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
describeSubnetsRequest.setSubnetIds(awsNetworkView.getSubnetList());
DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
if (!describeSubnetsResult.getSubnets().isEmpty()) {
mapPublicIpOnLaunch = describeSubnetsResult.getSubnets().get(0).isMapPublicIpOnLaunch();
}
}
return mapPublicIpOnLaunch;
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest in project cloudbreak by hortonworks.
the class AwsResourceConnector method getExistingSubnetCidr.
private List<String> getExistingSubnetCidr(AuthenticatedContext ac, CloudStack stack) {
AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonEC2Client ec2Client = awsClient.createAccess(new AwsCredentialView(ac.getCloudCredential()), region);
DescribeSubnetsRequest subnetsRequest = new DescribeSubnetsRequest().withSubnetIds(awsNetworkView.getSubnetList());
List<Subnet> subnets = ec2Client.describeSubnets(subnetsRequest).getSubnets();
if (subnets.isEmpty()) {
throw new CloudConnectorException("The specified subnet does not exist (maybe it's in a different region).");
}
List<String> cidrs = Lists.newArrayList();
for (Subnet subnet : subnets) {
cidrs.add(subnet.getCidrBlock());
}
return cidrs;
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest in project cloudbreak by hortonworks.
the class AwsSetup method validateExistingSubnet.
private void validateExistingSubnet(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
if (awsNetworkView.isExistingSubnet()) {
DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
describeSubnetsRequest.withSubnetIds(awsNetworkView.getSubnetList());
DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
if (describeSubnetsResult.getSubnets().size() < awsNetworkView.getSubnetList().size()) {
throw new CloudConnectorException(String.format(SUBNET_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet()));
} else {
for (Subnet subnet : describeSubnetsResult.getSubnets()) {
String vpcId = subnet.getVpcId();
if (vpcId != null && !vpcId.equals(awsNetworkView.getExistingVPC())) {
throw new CloudConnectorException(String.format(SUBNETVPC_DOES_NOT_EXIST_MSG, awsNetworkView.getExistingSubnet(), awsNetworkView.getExistingVPC()));
}
}
}
}
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest in project herd by FINRAOS.
the class MockEc2OperationsImpl method describeSubnets.
/**
* In-memory implementation of describeSubnets. Returns the subnets from the pre-configured subnets. The method can be ordered to throw an
* AmazonServiceException with a specified error code by specifying a subnet ID with prefix "throw." followed by a string indicating the error code to
* throw. The exception thrown in this manner will always set the status code to 500.
*/
@Override
public DescribeSubnetsResult describeSubnets(AmazonEC2Client ec2Client, DescribeSubnetsRequest describeSubnetsRequest) {
List<Subnet> subnets = new ArrayList<>();
List<String> requestedSubnetIds = describeSubnetsRequest.getSubnetIds();
// add all subnets if request is empty (this is AWS behavior)
if (requestedSubnetIds.isEmpty()) {
requestedSubnetIds.addAll(mockSubnets.keySet());
}
for (String requestedSubnetId : requestedSubnetIds) {
MockSubnet mockSubnet = mockSubnets.get(requestedSubnetId);
// Throw exception if any of the subnet ID do not exist
if (mockSubnet == null) {
AmazonServiceException amazonServiceException;
if (requestedSubnetId.startsWith("throw.")) {
String errorCode = requestedSubnetId.substring("throw.".length());
amazonServiceException = new AmazonServiceException(errorCode);
amazonServiceException.setErrorCode(errorCode);
amazonServiceException.setStatusCode(500);
} else {
amazonServiceException = new AmazonServiceException("The subnet ID '" + requestedSubnetId + "' does not exist");
amazonServiceException.setErrorCode(Ec2DaoImpl.ERROR_CODE_SUBNET_ID_NOT_FOUND);
amazonServiceException.setStatusCode(400);
}
throw amazonServiceException;
}
subnets.add(mockSubnet.toAwsObject());
}
DescribeSubnetsResult describeSubnetsResult = new DescribeSubnetsResult();
describeSubnetsResult.setSubnets(subnets);
return describeSubnetsResult;
}
Aggregations