use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project cloudbreak by hortonworks.
the class AwsResourceConnectorTest method testFindNonOverLappingCIDRWit20Vpc1Empty2.
@Test
public void testFindNonOverLappingCIDRWit20Vpc1Empty2() {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
Map<String, Object> networkParameters = new HashMap<>();
networkParameters.put("vpcId", "vpc-12345678");
networkParameters.put("internetGatewayId", "igw-12345678");
Network network = new Network(new Subnet(null), networkParameters);
CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
Location location = mock(Location.class);
Vpc vpc = mock(Vpc.class);
DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet5 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet6 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet7 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet8 = mock(com.amazonaws.services.ec2.model.Subnet.class);
DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(location);
when(cloudContext.getName()).thenReturn(new String(new byte[] { 4 }));
when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
when(vpc.getCidrBlock()).thenReturn("10.0.0.0/20");
when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4, subnet5, subnet6, subnet7, subnet8));
when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/23");
when(subnet2.getCidrBlock()).thenReturn("10.0.2.0/24");
when(subnet3.getCidrBlock()).thenReturn("10.0.4.0/23");
when(subnet4.getCidrBlock()).thenReturn("10.0.6.0/23");
when(subnet5.getCidrBlock()).thenReturn("10.0.8.0/23");
when(subnet6.getCidrBlock()).thenReturn("10.0.10.0/23");
when(subnet7.getCidrBlock()).thenReturn("10.0.12.0/23");
when(subnet8.getCidrBlock()).thenReturn("10.0.14.0/23");
String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
Assert.assertEquals("10.0.3.0/24", cidr);
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project cloudbreak by hortonworks.
the class AwsResourceConnectorTest method testFindNonOverLappingCIDRWithNon24Subnets2.
@Test
public void testFindNonOverLappingCIDRWithNon24Subnets2() {
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
Map<String, Object> networkParameters = new HashMap<>();
networkParameters.put("vpcId", "vpc-12345678");
networkParameters.put("internetGatewayId", "igw-12345678");
Network network = new Network(new Subnet(null), networkParameters);
CloudStack cloudStack = new CloudStack(singletonList(group1), network, null, emptyMap(), emptyMap(), null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey());
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
Location location = mock(Location.class);
Vpc vpc = mock(Vpc.class);
DescribeVpcsResult describeVpcsResult = mock(DescribeVpcsResult.class);
AmazonEC2Client ec2Client = mock(AmazonEC2Client.class);
com.amazonaws.services.ec2.model.Subnet subnet1 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet2 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet3 = mock(com.amazonaws.services.ec2.model.Subnet.class);
com.amazonaws.services.ec2.model.Subnet subnet4 = mock(com.amazonaws.services.ec2.model.Subnet.class);
DescribeSubnetsResult subnetsResult = mock(DescribeSubnetsResult.class);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(location);
when(cloudContext.getName()).thenReturn(new String(new byte[] { 76 }));
when(location.getRegion()).thenReturn(Region.region("eu-west-1"));
when(awsClient.createAccess(any(), any())).thenReturn(ec2Client);
when(ec2Client.describeVpcs(any())).thenReturn(describeVpcsResult);
when(describeVpcsResult.getVpcs()).thenReturn(singletonList(vpc));
when(vpc.getCidrBlock()).thenReturn("10.0.0.0/16");
when(ec2Client.describeSubnets(any())).thenReturn(subnetsResult);
when(subnetsResult.getSubnets()).thenReturn(Arrays.asList(subnet1, subnet2, subnet3, subnet4));
when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/20");
when(subnet2.getCidrBlock()).thenReturn("10.0.16.0/20");
when(subnet3.getCidrBlock()).thenReturn("10.0.32.0/20");
when(subnet4.getCidrBlock()).thenReturn("10.0.48.0/20");
String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack);
Assert.assertEquals("10.0.76.0/24", cidr);
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult 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;
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project photon-model by vmware.
the class AWSRemoteCleanup method deleteSubnets.
private void deleteSubnets(String vpcId, AmazonEC2 usEastEc2Client) {
DescribeSubnetsRequest subnetsRequest = new DescribeSubnetsRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeSubnetsResult securityGroupsResult = usEastEc2Client.describeSubnets(subnetsRequest);
securityGroupsResult.getSubnets().forEach(subnet -> {
DeleteSubnetRequest deleteSubnetRequest = new DeleteSubnetRequest().withSubnetId(subnet.getSubnetId());
this.host.log("Terminating stale subnet: %s", subnet.getSubnetId());
usEastEc2Client.deleteSubnet(deleteSubnetRequest);
});
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project photon-model by vmware.
the class AWSSubnetTaskServiceTest method deleteAwsPublicSubnet.
public void deleteAwsPublicSubnet() {
if (this.isMock) {
return;
}
DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest().withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList((String) this.awsTestContext.get(TestAWSSetupUtils.VPC_KEY)))).withFilters(new Filter(AWS_SUBNET_CIDR_FILTER, singletonList(AWS_NON_EXISTING_PUBLIC_SUBNET_CIDR)));
DescribeSubnetsResult subnetResult = this.client.describeSubnets(subnetRequest);
subnetResult.getSubnets().forEach(subnet -> {
DeleteSubnetRequest deleteRequest = new DeleteSubnetRequest(subnet.getSubnetId());
this.client.deleteSubnet(deleteRequest);
});
}
Aggregations