@Test public void testGetNetworkCidrMoreThanOneAssociatedCidrOnOneVpcShouldReturn2Cidr() { String existingVpc = "vpc-1"; String cidrBlock1 = "10.0.0.0/16"; String cidrBlock2 = "10.23.0.0/16"; Network network = new Network(null, Map.of(AwsNetworkView.VPC_ID, existingVpc, "region", "us-west-2")); CloudCredential credential = new CloudCredential(); AmazonEC2Client amazonEC2Client = mock(AmazonEC2Client.class); DescribeVpcsResult describeVpcsResult = describeVpcsResult(cidrBlock1, cidrBlock2); when(awsClient.createAccess(any(AwsCredentialView.class), eq("us-west-2"))).thenReturn(amazonEC2Client); when(amazonEC2Client.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc))).thenReturn(describeVpcsResult); NetworkCidr result = underTest.getNetworkCidr(network, credential); assertEquals(cidrBlock1, result.getCidr()); assertTrue(result.getCidrs().contains(cidrBlock1)); assertTrue(result.getCidrs().contains(cidrBlock2)); }
@Override public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) { AwsCredentialView awsCredentialView = new AwsCredentialView(credential); AmazonEC2Client awsClientAccess = awsClient.createAccess(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION)); AwsNetworkView awsNetworkView = new AwsNetworkView(network); String existingVpc = awsNetworkView.getExistingVpc(); DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc)); List<String> vpcCidrs = new ArrayList<>(); for (Vpc vpc : describeVpcsResult.getVpcs()) { if (vpc.getCidrBlockAssociationSet() != null) { LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId()); List<String> cidrs = vpc.getCidrBlockAssociationSet() .stream() .map(VpcCidrBlockAssociation::getCidrBlock) .collect(Collectors.toList()); LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs); vpcCidrs.addAll(cidrs); } else { LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId()); vpcCidrs.add(vpc.getCidrBlock()); } } if (vpcCidrs.isEmpty()) { throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc); } if (vpcCidrs.size() > 1) { LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0)); } return new NetworkCidr(vpcCidrs.get(0), vpcCidrs); }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) { AwsCredentialView awsCredentialView = new AwsCredentialView(credential); AmazonEC2Client awsClientAccess = awsClient.createAccess(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION)); AwsNetworkView awsNetworkView = new AwsNetworkView(network); String existingVpc = awsNetworkView.getExistingVpc(); DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc)); List<String> vpcCidrs = new ArrayList<>(); for (Vpc vpc : describeVpcsResult.getVpcs()) { if (vpc.getCidrBlockAssociationSet() != null) { LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId()); List<String> cidrs = vpc.getCidrBlockAssociationSet() .stream() .map(VpcCidrBlockAssociation::getCidrBlock) .collect(Collectors.toList()); LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs); vpcCidrs.addAll(cidrs); } else { LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId()); vpcCidrs.add(vpc.getCidrBlock()); } } if (vpcCidrs.isEmpty()) { throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc); } if (vpcCidrs.size() > 1) { LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0)); } return new NetworkCidr(vpcCidrs.get(0), vpcCidrs); } }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) { AwsCredentialView awsCredentialView = new AwsCredentialView(credential); AmazonEC2Client awsClientAccess = awsClient.createAccess(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION)); AwsNetworkView awsNetworkView = new AwsNetworkView(network); String existingVpc = awsNetworkView.getExistingVpc(); DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc)); List<String> vpcCidrs = new ArrayList<>(); for (Vpc vpc : describeVpcsResult.getVpcs()) { if (vpc.getCidrBlockAssociationSet() != null) { LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId()); List<String> cidrs = vpc.getCidrBlockAssociationSet() .stream() .map(VpcCidrBlockAssociation::getCidrBlock) .collect(Collectors.toList()); LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs); vpcCidrs.addAll(cidrs); } else { LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId()); vpcCidrs.add(vpc.getCidrBlock()); } } if (vpcCidrs.isEmpty()) { throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc); } if (vpcCidrs.size() > 1) { LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0)); } return new NetworkCidr(vpcCidrs.get(0), vpcCidrs); }  }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) { AwsCredentialView awsCredentialView = new AwsCredentialView(credential); AmazonEC2Client awsClientAccess = awsClient.createAccess(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION)); AwsNetworkView awsNetworkView = new AwsNetworkView(network); String existingVpc = awsNetworkView.getExistingVpc(); DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc)); List<String> vpcCidrs = new ArrayList<>(); for (Vpc vpc : describeVpcsResult.getVpcs()) { if (vpc.getCidrBlockAssociationSet() != null) { LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId()); List<String> cidrs = vpc.getCidrBlockAssociationSet() .stream() .map(VpcCidrBlockAssociation::getCidrBlock) .collect(Collectors.toList()); LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs); vpcCidrs.addAll(cidrs); } else { LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId()); vpcCidrs.add(vpc.getCidrBlock()); } } if (vpcCidrs.isEmpty()) { throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc); } if (vpcCidrs.size() > 1) { LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0)); } return new NetworkCidr(vpcCidrs.get(0), vpcCidrs); }  @Override CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest); @Override NetworkCidr getNetworkCidr(Network network, CloudCredential credential); @Override SubnetSelectionResult filterSubnets(Collection<CloudSubnet> subnetMetas, SubnetSelectionParameters subnetSelectionParameters); @Override int subnetCountInDifferentAzMin(); @Override int subnetCountInDifferentAzMax(); @Override void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest); @Override Platform platform(); @Override Variant variant(); }
AwsNetworkConnector implements DefaultNetworkConnector { @Override public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) { AwsCredentialView awsCredentialView = new AwsCredentialView(credential); AmazonEC2Client awsClientAccess = awsClient.createAccess(awsCredentialView, network.getStringParameter(AwsNetworkView.REGION)); AwsNetworkView awsNetworkView = new AwsNetworkView(network); String existingVpc = awsNetworkView.getExistingVpc(); DescribeVpcsResult describeVpcsResult = awsClientAccess.describeVpcs(new DescribeVpcsRequest().withVpcIds(existingVpc)); List<String> vpcCidrs = new ArrayList<>(); for (Vpc vpc : describeVpcsResult.getVpcs()) { if (vpc.getCidrBlockAssociationSet() != null) { LOGGER.info("The VPC {} has associated CIDR block so using the CIDR blocks in the VPC.", vpc.getVpcId()); List<String> cidrs = vpc.getCidrBlockAssociationSet() .stream() .map(VpcCidrBlockAssociation::getCidrBlock) .collect(Collectors.toList()); LOGGER.info("The VPC {} CIDRs block are {}.", vpc.getVpcId(), cidrs); vpcCidrs.addAll(cidrs); } else { LOGGER.info("The VPC {} has no associated CIDR block so using the CIDR block in the VPC.", vpc.getVpcId()); vpcCidrs.add(vpc.getCidrBlock()); } } if (vpcCidrs.isEmpty()) { throw new BadRequestException("VPC cidr could not fetch from AWS: " + existingVpc); } if (vpcCidrs.size() > 1) { LOGGER.info("More than one vpc cidrs for VPC {}. We will use the first one: {}", existingVpc, vpcCidrs.get(0)); } return new NetworkCidr(vpcCidrs.get(0), vpcCidrs); }  @Override CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest); @Override NetworkCidr getNetworkCidr(Network network, CloudCredential credential); @Override SubnetSelectionResult filterSubnets(Collection<CloudSubnet> subnetMetas, SubnetSelectionParameters subnetSelectionParameters); @Override int subnetCountInDifferentAzMin(); @Override int subnetCountInDifferentAzMax(); @Override void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest); @Override Platform platform(); @Override Variant variant();  }