use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testGetNetworkCidr.
@Test
public void testGetNetworkCidr() {
String existingVpc = "vpc-1";
String cidrBlock = "10.0.0.0/16";
Network network = new Network(null, Map.of(NetworkConstants.VPC_ID, existingVpc, "region", "us-west-2"));
CloudCredential credential = new CloudCredential();
AmazonEc2Client amazonEC2Client = mock(AmazonEc2Client.class);
DescribeVpcsResult describeVpcsResult = describeVpcsResult(cidrBlock);
when(awsClient.createEc2Client(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(cidrBlock, result.getCidr());
}
use of com.sequenceiq.cloudbreak.cloud.network.NetworkCidr in project cloudbreak by hortonworks.
the class AwsNetworkConnector method getNetworkCidr.
@Override
public NetworkCidr getNetworkCidr(Network network, CloudCredential credential) {
AwsCredentialView awsCredentialView = new AwsCredentialView(credential);
AmazonEc2Client awsClientAccess = awsClient.createEc2Client(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).distinct().filter(e -> !vpcCidrs.contains(e)).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);
}
Aggregations