Search in sources :

Example 1 with VpcCidrBlockAssociation

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();
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Vpc(com.amazonaws.services.ec2.model.Vpc) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 2 with VpcCidrBlockAssociation

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));
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Vpc(com.amazonaws.services.ec2.model.Vpc) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) VpcCidrBlockAssociation(com.amazonaws.services.ec2.model.VpcCidrBlockAssociation) GroupNetwork(com.sequenceiq.cloudbreak.cloud.model.GroupNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Test(org.junit.Test)

Example 3 with VpcCidrBlockAssociation

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));
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) VpcCidrBlockAssociation(com.amazonaws.services.ec2.model.VpcCidrBlockAssociation) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Test(org.junit.Test)

Example 4 with VpcCidrBlockAssociation

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;
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) VpcCidrBlockAssociation(com.amazonaws.services.ec2.model.VpcCidrBlockAssociation) Vpc(com.amazonaws.services.ec2.model.Vpc) ArrayList(java.util.ArrayList)

Example 5 with VpcCidrBlockAssociation

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);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) SubnetFilterStrategyType(com.sequenceiq.cloudbreak.cloud.aws.common.subnetselector.SubnetFilterStrategyType) NetworkDeletionRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkDeletionRequest) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) AwsCloudFormationErrorMessageProvider(com.sequenceiq.cloudbreak.cloud.aws.util.AwsCloudFormationErrorMessageProvider) LoggerFactory(org.slf4j.LoggerFactory) VpcCidrBlockAssociation(com.amazonaws.services.ec2.model.VpcCidrBlockAssociation) OnFailure(com.amazonaws.services.cloudformation.model.OnFailure) EnvironmentCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.EnvironmentCancellationCheck) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) Map(java.util.Map) BadRequestException(javax.ws.rs.BadRequestException) Waiter(com.amazonaws.waiters.Waiter) AwsConstants(com.sequenceiq.cloudbreak.cloud.aws.common.AwsConstants) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) Platform(com.sequenceiq.cloudbreak.cloud.model.Platform) Set(java.util.Set) Retry(com.sequenceiq.cloudbreak.service.Retry) CAPABILITY_IAM(com.amazonaws.services.cloudformation.model.Capability.CAPABILITY_IAM) Collectors(java.util.stream.Collectors) List(java.util.List) Tag(com.amazonaws.services.cloudformation.model.Tag) SubnetFilterStrategy(com.sequenceiq.cloudbreak.cloud.aws.common.subnetselector.SubnetFilterStrategy) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) SubnetSelectionParameters(com.sequenceiq.cloudbreak.cloud.model.SubnetSelectionParameters) Variant(com.sequenceiq.cloudbreak.cloud.model.Variant) DefaultNetworkConnector(com.sequenceiq.cloudbreak.cloud.DefaultNetworkConnector) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Service(org.springframework.stereotype.Service) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Network(com.sequenceiq.cloudbreak.cloud.model.Network) DeleteStackRequest(com.amazonaws.services.cloudformation.model.DeleteStackRequest) CloudSubnet(com.sequenceiq.cloudbreak.cloud.model.CloudSubnet) Logger(org.slf4j.Logger) SubnetSelectionResult(com.sequenceiq.cloudbreak.cloud.model.SubnetSelectionResult) Vpc(com.amazonaws.services.ec2.model.Vpc) ResourceStatus(com.amazonaws.services.cloudformation.model.ResourceStatus) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) WaiterRunner.run(com.sequenceiq.cloudbreak.cloud.aws.scheduler.WaiterRunner.run) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) AwsSubnetRequestProvider(com.sequenceiq.cloudbreak.cloud.aws.common.AwsSubnetRequestProvider) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) ArrayList(java.util.ArrayList) Vpc(com.amazonaws.services.ec2.model.Vpc) BadRequestException(javax.ws.rs.BadRequestException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Aggregations

DescribeVpcsRequest (com.amazonaws.services.ec2.model.DescribeVpcsRequest)4 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)4 Vpc (com.amazonaws.services.ec2.model.Vpc)4 VpcCidrBlockAssociation (com.amazonaws.services.ec2.model.VpcCidrBlockAssociation)4 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)4 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)4 Network (com.sequenceiq.cloudbreak.cloud.model.Network)3 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)2 NetworkCidr (com.sequenceiq.cloudbreak.cloud.network.NetworkCidr)2 Test (org.junit.Test)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 CAPABILITY_IAM (com.amazonaws.services.cloudformation.model.Capability.CAPABILITY_IAM)1 CreateStackRequest (com.amazonaws.services.cloudformation.model.CreateStackRequest)1 DeleteStackRequest (com.amazonaws.services.cloudformation.model.DeleteStackRequest)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 OnFailure (com.amazonaws.services.cloudformation.model.OnFailure)1 ResourceStatus (com.amazonaws.services.cloudformation.model.ResourceStatus)1 Tag (com.amazonaws.services.cloudformation.model.Tag)1