Search in sources :

Example 6 with CreatedCloudNetwork

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

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

the class EnvironmentNetworkServiceTest method testCreateNetworkShouldReturnWithANewNetwork.

@Test
void testCreateNetworkShouldReturnWithANewNetwork() {
    EnvironmentDto environmentDto = EnvironmentDto.builder().withCloudPlatform(CLOUD_PLATFORM).withCreator(USER_CRN).build();
    BaseNetwork baseNetwork = new AwsNetwork();
    NetworkCreationRequest networkCreationRequest = new NetworkCreationRequest.Builder().build();
    CreatedCloudNetwork createdCloudNetwork = new CreatedCloudNetwork();
    AwsEnvironmentNetworkConverter networkConverter = Mockito.mock(AwsEnvironmentNetworkConverter.class);
    when(cloudConnector.networkConnector()).thenReturn(networkConnector);
    when(networkCreationRequestFactory.create(environmentDto)).thenReturn(networkCreationRequest);
    when(networkConnector.createNetworkWithSubnets(networkCreationRequest)).thenReturn(createdCloudNetwork);
    when(environmentNetworkConverterMap.get(CloudPlatform.valueOf(CLOUD_PLATFORM))).thenReturn(networkConverter);
    when(networkConverter.setCreatedCloudNetwork(baseNetwork, createdCloudNetwork)).thenReturn(baseNetwork);
    BaseNetwork actual = underTest.createCloudNetwork(environmentDto, baseNetwork);
    verify(cloudConnector).networkConnector();
    verify(cloudPlatformConnectors).get(any(CloudPlatformVariant.class));
    verify(networkCreationRequestFactory).create(environmentDto);
    verify(networkConnector).createNetworkWithSubnets(networkCreationRequest);
    verify(environmentNetworkConverterMap).get(CloudPlatform.valueOf(CLOUD_PLATFORM));
    verify(networkConverter).setCreatedCloudNetwork(baseNetwork, createdCloudNetwork);
    assertEquals(baseNetwork, actual);
}
Also used : AwsEnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.AwsEnvironmentNetworkConverter) BaseNetwork(com.sequenceiq.environment.network.dao.domain.BaseNetwork) EnvironmentDto(com.sequenceiq.environment.environment.dto.EnvironmentDto) AwsNetwork(com.sequenceiq.environment.network.dao.domain.AwsNetwork) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CloudPlatformVariant(com.sequenceiq.cloudbreak.cloud.model.CloudPlatformVariant) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with CreatedCloudNetwork

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

use of com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork 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)

Example 10 with CreatedCloudNetwork

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

the class AzureNetworkConnectorTest method testCreateNetworkWithSubnetsShouldReturnTheNetworkNameAndSubnetName.

@Test
public void testCreateNetworkWithSubnetsShouldReturnTheNetworkNameAndSubnetName() {
    String networkCidr = "0.0.0.0/16";
    Set<NetworkSubnetRequest> subnets = new HashSet<>(Arrays.asList(createSubnetRequest(SUBNET_CIDR_0), createSubnetRequest(SUBNET_CIDR_1)));
    Deployment templateDeployment = mock(Deployment.class);
    ResourceGroup resourceGroup = mock(ResourceGroup.class);
    Map<String, Map> outputs = createOutput();
    ArrayList<SubnetRequest> subnetRequests = Lists.newArrayList(publicSubnetRequest("10.0.1.0/24", 0), publicSubnetRequest("10.0.1.0/24", 1));
    NetworkCreationRequest networkCreationRequest = createNetworkRequest(networkCidr, subnets);
    when(resourceGroup.name()).thenReturn(ENV_NAME);
    when(azureSubnetRequestProvider.provide(anyString(), anyList(), anyList(), anyBoolean())).thenReturn(subnetRequests);
    when(azureUtils.generateResourceGroupNameByNameAndId(anyString(), anyString())).thenReturn(ENV_NAME);
    when(azureClientService.getClient(networkCreationRequest.getCloudCredential())).thenReturn(azureClient);
    when(azureNetworkTemplateBuilder.build(networkCreationRequest, subnetRequests, resourceGroup.name())).thenReturn(TEMPLATE);
    when(azureClient.createTemplateDeployment(ENV_NAME, STACK_NAME, TEMPLATE, PARAMETER)).thenReturn(templateDeployment);
    when(azureClient.createResourceGroup(ENV_NAME, REGION.value(), Collections.emptyMap())).thenReturn(resourceGroup);
    when(resourceGroup.name()).thenReturn(ENV_NAME);
    when(templateDeployment.outputs()).thenReturn(outputs);
    CreatedCloudNetwork actual = underTest.createNetworkWithSubnets(networkCreationRequest);
    assertEquals(ENV_NAME, actual.getNetworkId());
    assertTrue(actual.getSubnets().stream().anyMatch(cloudSubnet -> cloudSubnet.getSubnetId().equals(SUBNET_ID_0)));
    assertTrue(actual.getSubnets().stream().anyMatch(cloudSubnet -> cloudSubnet.getSubnetId().equals(SUBNET_ID_1)));
    assertTrue(actual.getSubnets().size() == 2);
}
Also used : Arrays(java.util.Arrays) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) NetworkDeletionRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkDeletionRequest) NetworkResourcesCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkResourcesCreationRequest) Mockito.doThrow(org.mockito.Mockito.doThrow) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) Location(com.sequenceiq.cloudbreak.cloud.model.Location) BadRequestException(javax.ws.rs.BadRequestException) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) Platform(com.sequenceiq.cloudbreak.cloud.model.Platform) Set(java.util.Set) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Json(com.sequenceiq.cloudbreak.common.json.Json) List(java.util.List) AzureTransientDeploymentService(com.sequenceiq.cloudbreak.cloud.azure.template.AzureTransientDeploymentService) AzureClientService(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClientService) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) NetworkCidr(com.sequenceiq.cloudbreak.cloud.network.NetworkCidr) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SubnetSelectionParameters(com.sequenceiq.cloudbreak.cloud.model.SubnetSelectionParameters) Variant(com.sequenceiq.cloudbreak.cloud.model.Variant) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) AzureNetworkView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureNetworkView) Region(com.sequenceiq.cloudbreak.cloud.model.Region) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) HashMap(java.util.HashMap) Response(retrofit2.Response) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) PUBLIC(com.sequenceiq.cloudbreak.cloud.model.network.SubnetType.PUBLIC) Lists(com.google.common.collect.Lists) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) Network(com.sequenceiq.cloudbreak.cloud.model.Network) ExpectedException(org.junit.rules.ExpectedException) InjectMocks(org.mockito.InjectMocks) Deployment(com.microsoft.azure.management.resources.Deployment) Assert.assertTrue(org.junit.Assert.assertTrue) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) CloudException(com.microsoft.azure.CloudException) AzureSubnetSelectorService(com.sequenceiq.cloudbreak.cloud.azure.subnet.selector.AzureSubnetSelectorService) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) Deployment(com.microsoft.azure.management.resources.Deployment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Map(java.util.Map) HashMap(java.util.HashMap) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)11 CreatedSubnet (com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)7 NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)6 SubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)4 Test (org.junit.jupiter.api.Test)4 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)3 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)3 Network (com.sequenceiq.cloudbreak.cloud.model.Network)3 NetworkSubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)3 CreateStackRequest (com.amazonaws.services.cloudformation.model.CreateStackRequest)2 CloudException (com.microsoft.azure.CloudException)2 Deployment (com.microsoft.azure.management.resources.Deployment)2 ResourceGroup (com.microsoft.azure.management.resources.ResourceGroup)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 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 GcpContext (com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2