use of com.amazonaws.services.ec2.model.VpcCidrBlockAssociation in project cloudbreak by hortonworks.
the class AwsNetworkService method getVpcCidrs.
public List<String> getVpcCidrs(AuthenticatedContext ac, AwsNetworkView awsNetworkView) {
if (awsNetworkView.isExistingVPC()) {
String region = ac.getCloudContext().getLocation().getRegion().value();
AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(ac.getCloudCredential()), region);
DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc());
Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
List<String> cidrBlockAssociationSet = vpc.getCidrBlockAssociationSet().stream().map(VpcCidrBlockAssociation::getCidrBlock).collect(Collectors.toList());
LOGGER.info("VPC associated CIDR blocks: [{}]", cidrBlockAssociationSet);
return cidrBlockAssociationSet;
} else {
return Collections.emptyList();
}
}
use of com.amazonaws.services.ec2.model.VpcCidrBlockAssociation in project cloudbreak by hortonworks.
the class AwsNetworkServiceTest method testGetVpcCidrs.
@Test
public void testGetVpcCidrs() {
AwsNetworkView awsNetworkView = new AwsNetworkView(new Network(new Subnet(null), Map.of("vpcId", "vpc-123")));
String cidr1 = "1.2.3.0/24";
String cidr2 = "10.0.0.0/8";
AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
CloudContext cloudContext = mock(CloudContext.class);
AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(Location.location(Region.region("eu-west1")));
when(awsClient.createEc2Client(any(AwsCredentialView.class), anyString())).thenReturn(ec2Client);
when(ec2Client.describeVpcs(any(DescribeVpcsRequest.class))).thenReturn(new DescribeVpcsResult().withVpcs(new Vpc().withCidrBlockAssociationSet(new VpcCidrBlockAssociation().withCidrBlock(cidr1), new VpcCidrBlockAssociation().withCidrBlock(cidr2))));
List<String> vpcCidrs = underTest.getVpcCidrs(authenticatedContext, awsNetworkView);
assertTrue(vpcCidrs.contains(cidr1));
assertTrue(vpcCidrs.contains(cidr2));
}
use of com.amazonaws.services.ec2.model.VpcCidrBlockAssociation in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method testGetNetworkCidrWithDuplicatedCidr.
@Test
public void testGetNetworkCidrWithDuplicatedCidr() {
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, cidrBlock);
describeVpcsResult.getVpcs().get(0).getCidrBlockAssociationSet().add(new VpcCidrBlockAssociation().withCidrBlock(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());
assertEquals(1, result.getCidrs().size());
assertEquals(cidrBlock, result.getCidrs().get(0));
}
use of com.amazonaws.services.ec2.model.VpcCidrBlockAssociation in project cloudbreak by hortonworks.
the class AwsNetworkConnectorTest method describeVpcsResult.
private DescribeVpcsResult describeVpcsResult(String... cidrBlocks) {
DescribeVpcsResult describeVpcsResult = new DescribeVpcsResult();
List<Vpc> vpcs = new ArrayList<>();
for (String block : cidrBlocks) {
Vpc vpc = new Vpc();
vpc.setCidrBlock(block);
VpcCidrBlockAssociation vpcCidrBlockAssociation = new VpcCidrBlockAssociation();
vpcCidrBlockAssociation.setCidrBlock(block);
vpc.getCidrBlockAssociationSet().add(vpcCidrBlockAssociation);
vpcs.add(vpc);
}
describeVpcsResult.withVpcs(vpcs);
return describeVpcsResult;
}
use of com.amazonaws.services.ec2.model.VpcCidrBlockAssociation 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