Search in sources :

Example 1 with NetworkSubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.

the class DefaultSubnetCidrProvider method provide.

@Override
public Cidrs provide(String networkCidr, boolean privateSubnetEnabled) {
    Set<NetworkSubnetRequest> result = new HashSet<>();
    for (int i = 0; i < SUBNETS; i++) {
        String subnet = calculateSubnet(networkCidr, result);
        result.add(new NetworkSubnetRequest(subnet, SubnetType.PUBLIC));
    }
    return cidrs(result, Sets.newHashSet());
}
Also used : NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) HashSet(java.util.HashSet)

Example 2 with NetworkSubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets.

@Test
public void testCreateNetworkWithSubnetsShouldReturnTheNetworkAndSubnets() {
    String networkCidr = "0.0.0.0/16";
    Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    Map<String, String> output = createOutput();
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
    List<SubnetRequest> subnetRequestList = createSubnetRequestList();
    Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());
    when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
    when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
    when(cfStackUtil.getOutputs(NETWORK_ID, cfClient)).thenReturn(output);
    when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);
    CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(creationWaiter, times(1)).run(any());
    verify(cfStackUtil).getOutputs(NETWORK_ID, cfClient);
    verify(awsTaggingService, never()).prepareCloudformationTags(any(), any());
    verify(cfClient, never()).createStack(any(CreateStackRequest.class));
    assertEquals(VPC_ID, actual.getNetworkId());
    assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
Also used : NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) Test(org.junit.Test)

Example 3 with NetworkSubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.

the class AwsNetworkConnectorTest method testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets.

@Test
public void testCreateNewNetworkWithSubnetsShouldCreateTheNetworkAndSubnets() {
    String networkCidr = "0.0.0.0/16";
    Set<NetworkSubnetRequest> subnets = Set.of(new NetworkSubnetRequest("1.1.1.1/8", PUBLIC), new NetworkSubnetRequest("1.1.1.2/8", PUBLIC));
    AmazonServiceException amazonServiceException = new AmazonServiceException("does not exist");
    amazonServiceException.setStatusCode(400);
    AmazonCloudFormationClient cfClient = mock(AmazonCloudFormationClient.class);
    when(cfClient.describeStacks(any(DescribeStacksRequest.class))).thenThrow(amazonServiceException);
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    Map<String, String> output = createOutput();
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
    List<SubnetRequest> subnetRequestList = createSubnetRequestList();
    Set<CreatedSubnet> createdSubnets = Set.of(new CreatedSubnet(), new CreatedSubnet(), new CreatedSubnet());
    when(awsClient.createEc2Client(any(), any())).thenReturn(ec2Client);
    when(awsSubnetRequestProvider.provide(ec2Client, new ArrayList<>(subnets), new ArrayList<>(subnets))).thenReturn(subnetRequestList);
    when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()))).thenReturn(cfClient);
    when(cfClient.waiters()).thenReturn(cfWaiters);
    when(cfWaiters.stackCreateComplete()).thenReturn(creationWaiter);
    when(cfStackUtil.getOutputs(NETWORK_ID, cfClient)).thenReturn(output);
    when(awsCreatedSubnetProvider.provide(output, subnetRequestList, true)).thenReturn(createdSubnets);
    CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);
    verify(awsClient).createCloudFormationClient(any(AwsCredentialView.class), eq(REGION.value()));
    verify(awsNetworkCfTemplateProvider).provide(networkCreationRequest, subnetRequestList);
    verify(creationWaiter, times(1)).run(any());
    verify(awsTaggingService).prepareCloudformationTags(any(), any());
    verify(cfClient).createStack(any(CreateStackRequest.class));
    verify(cfStackUtil).getOutputs(NETWORK_ID, cfClient);
    assertEquals(VPC_ID, actual.getNetworkId());
    assertEquals(NUMBER_OF_SUBNETS, actual.getSubnets().size());
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) CreateStackRequest(com.amazonaws.services.cloudformation.model.CreateStackRequest) Test(org.junit.Test)

Example 4 with NetworkSubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.

the class ExtendedSubnetTypeProvider method updateCidrAndAddToList.

public void updateCidrAndAddToList(int offset, int count, int step, String[] ip, Set<NetworkSubnetRequest> subnetRequests, SubnetType subnetType, String subnetMask) {
    for (int i = 0; i < count; i++) {
        int newIpPart = i * step + offset;
        ip[2] = String.valueOf(newIpPart);
        String cidr = String.join(".", ip) + "/" + subnetMask;
        subnetRequests.add(new NetworkSubnetRequest(cidr, subnetType));
    }
}
Also used : NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)

Example 5 with NetworkSubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest in project cloudbreak by hortonworks.

the class DefaultSubnetCidrProvider method getSubnetCidrInRange.

private String getSubnetCidrInRange(String networkCidr, Iterable<NetworkSubnetRequest> subnetCidrs, int start, int end) {
    SubnetUtils.SubnetInfo vpcInfo = new SubnetUtils(networkCidr).getInfo();
    String lowProbe = incrementIp(vpcInfo.getLowAddress());
    String highProbe = new SubnetUtils(toSubnetCidr(lowProbe)).getInfo().getHighAddress();
    // start from the target subnet
    for (int i = 0; i < start - 1; i++) {
        lowProbe = incrementIp(lowProbe);
        highProbe = incrementIp(highProbe);
    }
    boolean foundProbe = false;
    for (int i = start; i < end; i++) {
        boolean overlapping = false;
        for (NetworkSubnetRequest subnetCidr : subnetCidrs) {
            SubnetUtils.SubnetInfo subnetInfo = new SubnetUtils(subnetCidr.getCidr()).getInfo();
            if (isInRange(lowProbe, subnetInfo) || isInRange(highProbe, subnetInfo)) {
                overlapping = true;
                break;
            }
        }
        if (overlapping) {
            lowProbe = incrementIp(lowProbe);
            highProbe = incrementIp(highProbe);
        } else {
            foundProbe = true;
            break;
        }
    }
    if (foundProbe && isInRange(highProbe, vpcInfo)) {
        String subnet = toSubnetCidr(lowProbe);
        LOGGER.debug("The following subnet cidr found: {} for VPC: {}", subnet, networkCidr);
        return subnet;
    } else {
        return null;
    }
}
Also used : SubnetUtils(org.apache.commons.net.util.SubnetUtils) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)

Aggregations

NetworkSubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)12 SubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)9 Test (org.junit.Test)6 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)5 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)3 NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)3 ArrayList (java.util.ArrayList)3 CreateStackRequest (com.amazonaws.services.cloudformation.model.CreateStackRequest)2 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)2 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)2 CreatedSubnet (com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)2 HashSet (java.util.HashSet)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 DescribeStacksRequest (com.amazonaws.services.cloudformation.model.DescribeStacksRequest)1 Lists (com.google.common.collect.Lists)1 CloudException (com.microsoft.azure.CloudException)1 Deployment (com.microsoft.azure.management.resources.Deployment)1 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 AzureClientService (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClientService)1