Search in sources :

Example 11 with SubnetRequest

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

the class AwsNetworkCfTemplateProviderTest method testProvideWhenPrivateSubnetCreationEnabled.

@ParameterizedTest
@MethodSource("privateSubnetArguments")
public void testProvideWhenPrivateSubnetCreationEnabled(String expectedTemplate, List<String> gatewayServices, List<String> interfaceServices, DescribeVpcEndpointServicesResult describeVpcEndpointServicesResult) throws IOException, TemplateException {
    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode expectedJson = objectMapper.readTree(new File(expectedTemplate));
    when(freeMarkerTemplateUtils.processTemplateIntoString(any(), any())).thenCallRealMethod();
    AmazonEc2Client ec2Client = mock(AmazonEc2Client.class);
    when(awsClient.createEc2Client(any(), anyString())).thenReturn(ec2Client);
    when(ec2Client.describeVpcEndpointServices()).thenReturn(describeVpcEndpointServicesResult);
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(true, PrivateEndpointType.USE_VPC_ENDPOINT);
    List<SubnetRequest> subnetRequestList = createPrivateAndPublicSubnetRequestList();
    ReflectionTestUtils.setField(underTest, "gatewayServices", gatewayServices);
    ReflectionTestUtils.setField(underTest, "interfaceServices", interfaceServices);
    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) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(freemarker.template.Template) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 12 with SubnetRequest

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

the class AwsNetworkCfTemplateProviderTest method createPrivateAndPublicSubnetRequestList.

private static List<SubnetRequest> createPrivateAndPublicSubnetRequestList() {
    SubnetRequest subnetRequest1 = new SubnetRequest();
    subnetRequest1.setPublicSubnetCidr("2.2.2.2/24");
    subnetRequest1.setIndex(0);
    subnetRequest1.setSubnetGroup(0);
    subnetRequest1.setAvailabilityZone("az1");
    SubnetRequest subnetRequest2 = new SubnetRequest();
    subnetRequest2.setPublicSubnetCidr("2.2.2.2/24");
    subnetRequest2.setIndex(1);
    subnetRequest2.setSubnetGroup(1);
    subnetRequest2.setAvailabilityZone("az2");
    SubnetRequest subnetRequest3 = new SubnetRequest();
    subnetRequest3.setPrivateSubnetCidr("2.2.2.2/24");
    subnetRequest3.setIndex(2);
    subnetRequest3.setSubnetGroup(2);
    subnetRequest3.setAvailabilityZone("az1");
    SubnetRequest subnetRequest4 = new SubnetRequest();
    subnetRequest4.setPrivateSubnetCidr("2.2.2.2/24");
    subnetRequest4.setIndex(3);
    subnetRequest4.setSubnetGroup(3);
    subnetRequest4.setAvailabilityZone("az2");
    return List.of(subnetRequest1, subnetRequest2, subnetRequest3, subnetRequest4);
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)

Example 13 with SubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest 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 14 with SubnetRequest

use of com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest 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 15 with SubnetRequest

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

the class AwsCreatedSubnetProviderTest method publicSubnetRequest.

public SubnetRequest publicSubnetRequest(String cidr, int index) {
    SubnetRequest subnetRequest = new SubnetRequest();
    subnetRequest.setIndex(index);
    subnetRequest.setPublicSubnetCidr(cidr);
    subnetRequest.setSubnetGroup(index % 3);
    subnetRequest.setAvailabilityZone("az");
    return subnetRequest;
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)

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