Search in sources :

Example 21 with SubnetRequest

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

the class AwsSubnetRequestProvider method provide.

public List<SubnetRequest> provide(AmazonEc2Client ec2Client, List<NetworkSubnetRequest> publicSubnets, List<NetworkSubnetRequest> privateSubnets) {
    List<String> az = getAvailabilityZones(ec2Client);
    List<SubnetRequest> subnets = new ArrayList<>();
    int index = 0;
    for (int i = 0; i < publicSubnets.size(); i++) {
        SubnetRequest subnetRequest = new SubnetRequest();
        NetworkSubnetRequest networkSubnetRequest = publicSubnets.get(i);
        subnetRequest.setPublicSubnetCidr(networkSubnetRequest.getCidr());
        subnetRequest.setType(networkSubnetRequest.getType());
        subnetRequest.setAvailabilityZone(az.get(i % az.size()));
        subnetRequest.setSubnetGroup(i % publicSubnets.size());
        subnetRequest.setIndex(index++);
        subnets.add(subnetRequest);
    }
    for (int i = 0; i < privateSubnets.size(); i++) {
        SubnetRequest subnetRequest = new SubnetRequest();
        NetworkSubnetRequest networkSubnetRequest = privateSubnets.get(i);
        subnetRequest.setPrivateSubnetCidr(networkSubnetRequest.getCidr());
        subnetRequest.setType(networkSubnetRequest.getType());
        subnetRequest.setAvailabilityZone(az.get(i % az.size()));
        // we will create 3 public subnet for nat gateways so we need to loadbalance between the public subnets
        subnetRequest.setSubnetGroup(i % publicSubnets.size());
        subnetRequest.setIndex(index++);
        subnets.add(subnetRequest);
    }
    return subnets;
}
Also used : NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) ArrayList(java.util.ArrayList)

Example 22 with SubnetRequest

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

the class AwsNetworkCfTemplateProviderTest method testProvideWhenPrivateSubnetsButNoVpcEndpointsConfigured.

@Test
public void testProvideWhenPrivateSubnetsButNoVpcEndpointsConfigured() throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File("src/test/resources/json/aws-cf-network-privatesubnet-novpcendpoints.json"));
    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    ReflectionTestUtils.setField(underTest, "gatewayServices", List.of());
    ReflectionTestUtils.setField(underTest, "interfaceServices", List.of());
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(true, PrivateEndpointType.USE_VPC_ENDPOINT);
    List<SubnetRequest> subnetRequestList = createPrivateAndPublicSubnetRequestList();
    String actual = underTest.provide(networkCreationRequest, subnetRequestList);
    JsonNode json = objectMapper.readTree(actual);
    assertEquals(expectedJson, json);
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 23 with SubnetRequest

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

the class AwsNetworkCfTemplateProviderTest method testProvideShouldThrowExceptionWhenTemplateProcessHasFailed.

@Test
public void testProvideShouldThrowExceptionWhenTemplateProcessHasFailed() throws IOException, TemplateException {
    when(freeMarkerTemplateUtils.processTemplateIntoString(any(Template.class), anyMap())).thenThrow(TemplateException.class);
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(false, PrivateEndpointType.NONE);
    List<SubnetRequest> subnetRequestList = createPublicSubnetRequestList();
    Assertions.assertThrows(CloudConnectorException.class, () -> underTest.provide(networkCreationRequest, subnetRequestList));
    verify(freeMarkerTemplateUtils).processTemplateIntoString(any(Template.class), anyMap());
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) Template(freemarker.template.Template) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 24 with SubnetRequest

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

the class AwsNetworkConnectorTest method createSubnetRequestList.

private List<SubnetRequest> createSubnetRequestList() {
    SubnetRequest subnetRequest1 = new SubnetRequest();
    subnetRequest1.setPublicSubnetCidr("2.2.2.2/24");
    subnetRequest1.setPrivateSubnetCidr("2.2.2.2/24");
    subnetRequest1.setAvailabilityZone("az1");
    SubnetRequest subnetRequest2 = new SubnetRequest();
    subnetRequest2.setPublicSubnetCidr("2.2.2.2/24");
    subnetRequest2.setPrivateSubnetCidr("2.2.2.2/24");
    subnetRequest2.setAvailabilityZone("az2");
    SubnetRequest subnetRequest3 = new SubnetRequest();
    subnetRequest3.setPublicSubnetCidr("2.2.2.2/24");
    subnetRequest3.setPrivateSubnetCidr("2.2.2.2/24");
    subnetRequest3.setAvailabilityZone("az3");
    return List.of(subnetRequest1, subnetRequest2, subnetRequest3);
}
Also used : NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)

Example 25 with SubnetRequest

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

the class AwsCreatedSubnetProvider method provide.

Set<CreatedSubnet> provide(Map<String, String> output, List<SubnetRequest> subnetRequests, boolean privateSubnetEnabled) {
    Set<CreatedSubnet> subnets = new HashSet<>();
    for (SubnetRequest subnetRequest : subnetRequests) {
        CreatedSubnet createdPublicSubnet = new CreatedSubnet();
        createdPublicSubnet.setSubnetId(getValue(output, "id" + subnetRequest.getIndex()));
        createdPublicSubnet.setAvailabilityZone(subnetRequest.getAvailabilityZone());
        if (!Strings.isNullOrEmpty(subnetRequest.getPrivateSubnetCidr()) && privateSubnetEnabled) {
            createdPublicSubnet.setCidr(subnetRequest.getPrivateSubnetCidr());
            createdPublicSubnet.setPublicSubnet(false);
            createdPublicSubnet.setMapPublicIpOnLaunch(false);
            createdPublicSubnet.setIgwAvailable(false);
        } else {
            createdPublicSubnet.setCidr(subnetRequest.getPublicSubnetCidr());
            createdPublicSubnet.setPublicSubnet(true);
            createdPublicSubnet.setMapPublicIpOnLaunch(true);
            createdPublicSubnet.setIgwAvailable(true);
        }
        createdPublicSubnet.setType(subnetRequest.getType());
        subnets.add(createdPublicSubnet);
    }
    return subnets;
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) HashSet(java.util.HashSet)

Aggregations

SubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)27 NetworkSubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)12 NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)11 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)9 Template (freemarker.template.Template)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 CreatedSubnet (com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)5 File (java.io.File)5 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)4 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)4 ArrayList (java.util.ArrayList)4 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)3 HashSet (java.util.HashSet)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2