Search in sources :

Example 16 with NetworkCidr

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());
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) 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 17 with NetworkCidr

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);
}
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

NetworkCidr (com.sequenceiq.cloudbreak.cloud.network.NetworkCidr)17 Network (com.sequenceiq.cloudbreak.cloud.model.Network)14 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)9 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)8 BaseNetwork (com.sequenceiq.environment.network.dao.domain.BaseNetwork)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)5 NetworkDto (com.sequenceiq.environment.network.dto.NetworkDto)5 EnvironmentNetworkConverter (com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 DescribeVpcsRequest (com.amazonaws.services.ec2.model.DescribeVpcsRequest)4 DescribeVpcsResult (com.amazonaws.services.ec2.model.DescribeVpcsResult)4 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)4 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)4 Credential (com.sequenceiq.environment.credential.domain.Credential)4 Environment (com.sequenceiq.environment.environment.domain.Environment)4 ArrayList (java.util.ArrayList)4 CloudPlatform (com.sequenceiq.cloudbreak.common.mappable.CloudPlatform)3