@Test public void testFindNonOverLappingCIDRWit20Vpc() { InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak"); Group group1 = new Group("group1", InstanceGroupType.CORE, Collections.emptyList(), null, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), ROOT_VOLUME_SIZE, identity); 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(), null); 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[]{(byte) 15})); 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)); when(subnet1.getCidrBlock()).thenReturn("10.0.0.0/24"); when(subnet2.getCidrBlock()).thenReturn("10.0.1.0/24"); when(subnet3.getCidrBlock()).thenReturn("10.0.2.0/24"); when(subnet4.getCidrBlock()).thenReturn("10.0.3.0/24"); String cidr = underTest.findNonOverLappingCIDR(authenticatedContext, cloudStack); Assert.assertEquals("10.0.15.0/24", cidr); }
public String findNonOverLappingCIDR(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); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs)); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); }
AwsNetworkService { public String findNonOverLappingCIDR(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); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs)); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); } }
AwsNetworkService { public String findNonOverLappingCIDR(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); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs)); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); }  }
AwsNetworkService { public String findNonOverLappingCIDR(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); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs)); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); }  List<Group> getGatewayGroups(Collection<Group> groups); List<String> getExistingSubnetCidr(AuthenticatedContext ac, CloudStack stack); boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client); String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack); List<String> getVpcCidrs(AuthenticatedContext ac, CloudStack stack); }
AwsNetworkService { public String findNonOverLappingCIDR(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); DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc()); Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0); String vpcCidr = vpc.getCidrBlock(); LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr); DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc()))); List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets(); List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList()); LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs)); return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs); }  List<Group> getGatewayGroups(Collection<Group> groups); List<String> getExistingSubnetCidr(AuthenticatedContext ac, CloudStack stack); boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client); String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack); List<String> getVpcCidrs(AuthenticatedContext ac, CloudStack stack);  }