Search in sources :

Example 11 with CreatedSubnet

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

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

the class AwsEnvironmentNetworkConverterTest method createCreatedSubnets.

private Set<CreatedSubnet> createCreatedSubnets() {
    CreatedSubnet createdSubnet1 = new CreatedSubnet();
    createdSubnet1.setSubnetId(SUBNET_1);
    createdSubnet1.setAvailabilityZone(AZ_1);
    createdSubnet1.setCidr(SUBNET_CIDR_1);
    createdSubnet1.setPublicSubnet(true);
    CreatedSubnet createdSubnet2 = new CreatedSubnet();
    createdSubnet2.setSubnetId(SUBNET_2);
    createdSubnet2.setAvailabilityZone(AZ_2);
    createdSubnet2.setCidr(SUBNET_CIDR_2);
    createdSubnet2.setPublicSubnet(true);
    CreatedSubnet createdSubnet3 = new CreatedSubnet();
    createdSubnet3.setSubnetId(SUBNET_3);
    createdSubnet3.setAvailabilityZone(AZ_3);
    createdSubnet3.setCidr(SUBNET_CIDR_3);
    createdSubnet3.setPublicSubnet(true);
    return Set.of(createdSubnet1, createdSubnet2, createdSubnet3);
}
Also used : CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)

Example 13 with CreatedSubnet

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

the class AwsEnvironmentNetworkConverterTest method testSetProviderSpecificNetworkShouldPopulateTheExistingNetworkWithTheNewNetworkData.

@Test
void testSetProviderSpecificNetworkShouldPopulateTheExistingNetworkWithTheNewNetworkData() {
    BaseNetwork awsNetwork = new AwsNetwork();
    Set<CreatedSubnet> createdSubnets = createCreatedSubnets();
    CreatedCloudNetwork createdCloudNetwork = new CreatedCloudNetwork("network-1", VPC_ID, createdSubnets);
    AwsNetwork actual = (AwsNetwork) underTest.setCreatedCloudNetwork(awsNetwork, createdCloudNetwork);
    assertEquals(createdCloudNetwork.getStackName(), actual.getName());
    assertEquals(VPC_ID, actual.getVpcId());
    assertTrue(SUBNET_IDS.containsAll(actual.getSubnetMetas().keySet()));
    assertEquals(SUBNET_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getId());
    assertEquals(SUBNET_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getName());
    assertEquals(AZ_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_1, awsNetwork.getSubnetMetas().get(SUBNET_1).getCidr());
    assertFalse(awsNetwork.getSubnetMetas().get(SUBNET_1).isPrivateSubnet());
    assertEquals(SUBNET_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getId());
    assertEquals(SUBNET_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getName());
    assertEquals(AZ_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_2, awsNetwork.getSubnetMetas().get(SUBNET_2).getCidr());
    assertFalse(awsNetwork.getSubnetMetas().get(SUBNET_2).isPrivateSubnet());
    assertEquals(SUBNET_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getId());
    assertEquals(SUBNET_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getName());
    assertEquals(AZ_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_3, awsNetwork.getSubnetMetas().get(SUBNET_3).getCidr());
    assertFalse(awsNetwork.getSubnetMetas().get(SUBNET_3).isPrivateSubnet());
}
Also used : BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) Test(org.junit.jupiter.api.Test)

Example 14 with CreatedSubnet

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

the class GcpCloudSubnetProviderTest method testProvideCloudSubnetsWhen2Subnet1AZIsProvidedShouldCreate2NewSubnetWith1AzShouldBeProvided.

@Test
public void testProvideCloudSubnetsWhen2Subnet1AZIsProvidedShouldCreate2NewSubnetWith1AzShouldBeProvided() throws IOException {
    NetworkCreationRequest request = new NetworkCreationRequest.Builder().withEnvName("envName").withEnvId(1L).withNetworkCidr("10.0.0.0/16").withRegion(Region.region("euwest1")).withPrivateSubnetEnabled(true).withCloudCredential(mock(CloudCredential.class)).withEndpointType(PrivateEndpointType.USE_VPC_ENDPOINT).build();
    Compute compute = mock(Compute.class);
    Compute.Regions regions = mock(Compute.Regions.class);
    Compute.Regions.List regionsList = mock(Compute.Regions.List.class);
    RegionList regionListObject = mock(RegionList.class);
    com.google.api.services.compute.model.Region regionObject = new com.google.api.services.compute.model.Region();
    regionObject.setName("euwest1");
    regionObject.setZones(List.of("euwest1/euwest1a"));
    when(gcpComputeFactory.buildCompute(any(CloudCredential.class))).thenReturn(compute);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(compute.regions()).thenReturn(regions);
    when(regions.list(anyString())).thenReturn(regionsList);
    when(regionsList.execute()).thenReturn(regionListObject);
    when(regionListObject.getItems()).thenReturn(List.of(regionObject));
    List<CreatedSubnet> provide = underTest.provide(request, List.of("10.0.0.0/16", "10.0.0.1/16"));
    Assert.assertEquals(2, provide.size());
    Assert.assertTrue(provide.stream().map(e -> e.getAvailabilityZone()).filter(e -> e.equals("euwest1a")).findFirst().isPresent());
    Assert.assertTrue(provide.stream().map(e -> e.getAvailabilityZone()).filter(e -> e.equals("euwest1a")).findFirst().isPresent());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) PrivateEndpointType(com.sequenceiq.common.model.PrivateEndpointType) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Test(org.junit.jupiter.api.Test) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) GcpComputeFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpComputeFactory) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) RegionList(com.google.api.services.compute.model.RegionList) Assert(org.junit.Assert) Compute(com.google.api.services.compute.Compute) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) Compute(com.google.api.services.compute.Compute) Region(com.sequenceiq.cloudbreak.cloud.model.Region) RegionList(com.google.api.services.compute.model.RegionList) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) Test(org.junit.jupiter.api.Test)

Example 15 with CreatedSubnet

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

the class AzureEnvironmentNetworkConverterTest method testSetProviderSpecificNetworkShouldPopulateTheExistingNetworkWithTheNewNetworkData.

@Test
void testSetProviderSpecificNetworkShouldPopulateTheExistingNetworkWithTheNewNetworkData() {
    AzureNetwork azureNetwork = new AzureNetwork();
    Set<CreatedSubnet> createdSubnets = createCreatedSubnets();
    Map<String, Object> properties = Map.of("resourceGroupName", RESOURCE_GROUP_NAME);
    CreatedCloudNetwork createdCloudNetwork = new CreatedCloudNetwork("network-1", NETWORK_ID, createdSubnets, properties);
    AzureNetwork actual = (AzureNetwork) underTest.setCreatedCloudNetwork(azureNetwork, createdCloudNetwork);
    assertEquals(createdCloudNetwork.getStackName(), actual.getName());
    assertEquals(NETWORK_ID, actual.getNetworkId());
    assertEquals(RESOURCE_GROUP_NAME, actual.getResourceGroupName());
    assertTrue(SUBNET_IDS.containsAll(actual.getSubnetMetas().keySet()));
    assertEquals(SUBNET_1, actual.getSubnetMetas().get(SUBNET_1).getId());
    assertEquals(SUBNET_1, actual.getSubnetMetas().get(SUBNET_1).getName());
    assertEquals(AZ_1, actual.getSubnetMetas().get(SUBNET_1).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_1, actual.getSubnetMetas().get(SUBNET_1).getCidr());
    assertTrue(actual.getSubnetMetas().get(SUBNET_1).isPrivateSubnet());
    assertEquals(SUBNET_2, actual.getSubnetMetas().get(SUBNET_2).getId());
    assertEquals(SUBNET_2, actual.getSubnetMetas().get(SUBNET_2).getName());
    assertEquals(AZ_2, actual.getSubnetMetas().get(SUBNET_2).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_2, actual.getSubnetMetas().get(SUBNET_2).getCidr());
    assertTrue(actual.getSubnetMetas().get(SUBNET_2).isPrivateSubnet());
    assertEquals(SUBNET_3, actual.getSubnetMetas().get(SUBNET_3).getId());
    assertEquals(SUBNET_3, actual.getSubnetMetas().get(SUBNET_3).getName());
    assertEquals(AZ_3, actual.getSubnetMetas().get(SUBNET_3).getAvailabilityZone());
    assertEquals(SUBNET_CIDR_3, actual.getSubnetMetas().get(SUBNET_3).getCidr());
    assertTrue(actual.getSubnetMetas().get(SUBNET_3).isPrivateSubnet());
}
Also used : CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) AzureNetwork(com.sequenceiq.environment.network.dao.domain.AzureNetwork) Test(org.junit.jupiter.api.Test)

Aggregations

CreatedSubnet (com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)20 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)7 SubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)5 CloudSubnet (com.sequenceiq.cloudbreak.cloud.model.CloudSubnet)4 NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)4 Test (org.junit.jupiter.api.Test)4 Compute (com.google.api.services.compute.Compute)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 CreateStackRequest (com.amazonaws.services.cloudformation.model.CreateStackRequest)2 RegionList (com.google.api.services.compute.model.RegionList)2 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)2 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)2 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 GcpComputeFactory (com.sequenceiq.cloudbreak.cloud.gcp.client.GcpComputeFactory)2 GcpStackUtil (com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 Region (com.sequenceiq.cloudbreak.cloud.model.Region)2 NetworkSubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)2