Search in sources :

Example 1 with NetworkCreationRequest

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

the class GcpCloudSubnetProviderTest method testProvideCloudSubnetsWhen2Subnet2AZIsProvidedShouldCreate2NewSubnetWithDifferentAzShouldBeProvided.

@Test
public void testProvideCloudSubnetsWhen2Subnet2AZIsProvidedShouldCreate2NewSubnetWithDifferentAzShouldBeProvided() 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", "euwest1/euwest1b"));
    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("euwest1b")).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 2 with NetworkCreationRequest

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

the class GcpNetworkConnectorTest method testCreateNetworkWithSubnetsWhenCreationGoesFine.

@Test
public void testCreateNetworkWithSubnetsWhenCreationGoesFine() throws Exception {
    NetworkCreationRequest networkCreationRequest = new NetworkCreationRequest.Builder().withNetworkCidr("16.0.0.0/16").withAccountId("account-id").withCreatorCrn("creator-crn").withVariant("GCP").withEnvCrn("env-crn").withEnvName("super-env").withPrivateSubnetEnabled(true).withTags(new HashMap<>()).withCreatorCrn("creator-crn").withRegion(Region.region("us-west-1")).withPublicSubnets(new HashSet<>()).withPrivateSubnets(new HashSet<>()).build();
    GcpContext gcpContext = mock(GcpContext.class);
    PollTask pollTask = mock(PollTask.class);
    CloudResource cloudResource = mock(CloudResource.class);
    when(cloudResource.getName()).thenReturn("network");
    when(contextBuilders.contextInit(any(CloudContext.class), any(AuthenticatedContext.class), any(Network.class), any(), anyBoolean())).thenReturn(gcpContext);
    when(gcpNetworkResourceBuilder.create(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class))).thenReturn(cloudResource);
    when(gcpNetworkResourceBuilder.build(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class), any(Security.class), any(CloudResource.class))).thenReturn(cloudResource);
    when(statusCheckFactory.newPollResourceTask(any(ResourceChecker.class), any(AuthenticatedContext.class), anyList(), any(ResourceBuilderContext.class), anyBoolean())).thenReturn(pollTask);
    when(syncPollingScheduler.schedule(any(PollTask.class))).thenReturn(new ArrayList());
    when(gcpCloudSubnetProvider.provide(any(NetworkCreationRequest.class), anyList())).thenReturn(List.of(createdSubnet()));
    when(gcpSubnetResourceBuilder.create(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class))).thenReturn(cloudResource);
    when(gcpSubnetResourceBuilder.build(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class), any(Security.class), any(CloudResource.class))).thenReturn(cloudResource);
    CreatedCloudNetwork networkWithSubnets = underTest.createNetworkWithSubnets(networkCreationRequest);
    Assert.assertEquals(1, networkWithSubnets.getSubnets().size());
}
Also used : HashMap(java.util.HashMap) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Security(com.sequenceiq.cloudbreak.cloud.model.Security) ResourceChecker(com.sequenceiq.cloudbreak.cloud.template.ResourceChecker) PollTask(com.sequenceiq.cloudbreak.cloud.task.PollTask) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with NetworkCreationRequest

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

the class GcpNetworkConnectorTest method testCreateNetworkWithSubnetsWhenCreationThrowGoogleJsonResponseExceptionShouldThrowGcpResourceException.

@Test
public void testCreateNetworkWithSubnetsWhenCreationThrowGoogleJsonResponseExceptionShouldThrowGcpResourceException() throws Exception {
    NetworkCreationRequest networkCreationRequest = new NetworkCreationRequest.Builder().withNetworkCidr("16.0.0.0/16").withAccountId("account-id").withCreatorCrn("creator-crn").withVariant("GCP").withEnvCrn("env-crn").withEnvName("super-env").withPrivateSubnetEnabled(true).withTags(new HashMap<>()).withCreatorCrn("creator-crn").withRegion(Region.region("us-west-1")).withPublicSubnets(new HashSet<>()).withPrivateSubnets(new HashSet<>()).build();
    GcpContext gcpContext = mock(GcpContext.class);
    PollTask pollTask = mock(PollTask.class);
    CloudResource cloudResource = mock(CloudResource.class);
    when(cloudResource.getName()).thenReturn("network");
    when(contextBuilders.contextInit(any(CloudContext.class), any(AuthenticatedContext.class), any(Network.class), any(), anyBoolean())).thenReturn(gcpContext);
    GoogleJsonResponseException googleJsonResponseException = mock(GoogleJsonResponseException.class);
    GoogleJsonError googleJsonError = mock(GoogleJsonError.class);
    when(googleJsonResponseException.getDetails()).thenReturn(googleJsonError);
    when(googleJsonError.getMessage()).thenReturn("google-error");
    when(gcpNetworkResourceBuilder.create(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class))).thenReturn(cloudResource);
    when(gcpNetworkResourceBuilder.build(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class), any(Security.class), any(CloudResource.class))).thenReturn(cloudResource);
    when(statusCheckFactory.newPollResourceTask(any(ResourceChecker.class), any(AuthenticatedContext.class), anyList(), any(ResourceBuilderContext.class), anyBoolean())).thenReturn(pollTask);
    when(syncPollingScheduler.schedule(any(PollTask.class))).thenReturn(new ArrayList());
    when(gcpCloudSubnetProvider.provide(any(NetworkCreationRequest.class), anyList())).thenThrow(googleJsonResponseException);
    when(gcpSubnetResourceBuilder.create(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class))).thenReturn(cloudResource);
    when(gcpSubnetResourceBuilder.build(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class), any(Security.class), any(CloudResource.class))).thenReturn(cloudResource);
    when(gcpContext.getProjectId()).thenReturn("id");
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.createNetworkWithSubnets(networkCreationRequest));
    Assert.assertEquals("google-error: [ resourceType: GCP_NETWORK,  resourceName: super-env ]", gcpResourceException.getMessage());
    verify(gcpStackUtil, times(0)).getMissingServiceAccountKeyError(any(TokenResponseException.class), anyString());
}
Also used : HashMap(java.util.HashMap) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Security(com.sequenceiq.cloudbreak.cloud.model.Security) ResourceChecker(com.sequenceiq.cloudbreak.cloud.template.ResourceChecker) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) PollTask(com.sequenceiq.cloudbreak.cloud.task.PollTask) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with NetworkCreationRequest

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

the class GcpNetworkConnectorTest method testCreateNetworkWithSubnetsWhenCreationThrowIoExceptionShouldThrowGcpResourceException.

@Test
public void testCreateNetworkWithSubnetsWhenCreationThrowIoExceptionShouldThrowGcpResourceException() throws Exception {
    NetworkCreationRequest networkCreationRequest = new NetworkCreationRequest.Builder().withNetworkCidr("16.0.0.0/16").withAccountId("account-id").withCreatorCrn("creator-crn").withVariant("GCP").withEnvCrn("env-crn").withEnvName("super-env").withPrivateSubnetEnabled(true).withTags(new HashMap<>()).withCreatorCrn("creator-crn").withRegion(Region.region("us-west-1")).withPublicSubnets(new HashSet<>()).withPrivateSubnets(new HashSet<>()).build();
    GcpContext gcpContext = mock(GcpContext.class);
    PollTask pollTask = mock(PollTask.class);
    CloudResource cloudResource = mock(CloudResource.class);
    when(cloudResource.getName()).thenReturn("network");
    when(contextBuilders.contextInit(any(CloudContext.class), any(AuthenticatedContext.class), any(Network.class), any(), anyBoolean())).thenReturn(gcpContext);
    IOException ioException = mock(IOException.class);
    when(gcpNetworkResourceBuilder.create(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class))).thenReturn(cloudResource);
    when(gcpNetworkResourceBuilder.build(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class), any(Security.class), any(CloudResource.class))).thenReturn(cloudResource);
    when(statusCheckFactory.newPollResourceTask(any(ResourceChecker.class), any(AuthenticatedContext.class), anyList(), any(ResourceBuilderContext.class), anyBoolean())).thenReturn(pollTask);
    when(syncPollingScheduler.schedule(any(PollTask.class))).thenReturn(new ArrayList());
    when(gcpCloudSubnetProvider.provide(any(NetworkCreationRequest.class), anyList())).thenThrow(ioException);
    when(gcpSubnetResourceBuilder.create(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class))).thenReturn(cloudResource);
    when(gcpSubnetResourceBuilder.build(any(GcpContext.class), any(AuthenticatedContext.class), any(Network.class), any(Security.class), any(CloudResource.class))).thenReturn(cloudResource);
    when(gcpContext.getProjectId()).thenReturn("id");
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.createNetworkWithSubnets(networkCreationRequest));
    Assert.assertEquals("Network creation failed due to IO exception: [ resourceType: GCP_NETWORK,  resourceName: super-env ]", gcpResourceException.getMessage());
    verify(gcpStackUtil, times(0)).getMissingServiceAccountKeyError(any(TokenResponseException.class), anyString());
}
Also used : HashMap(java.util.HashMap) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) ArrayList(java.util.ArrayList) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) IOException(java.io.IOException) Security(com.sequenceiq.cloudbreak.cloud.model.Security) ResourceChecker(com.sequenceiq.cloudbreak.cloud.template.ResourceChecker) PollTask(com.sequenceiq.cloudbreak.cloud.task.PollTask) GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) Network(com.sequenceiq.cloudbreak.cloud.model.Network) ResourceBuilderContext(com.sequenceiq.cloudbreak.cloud.template.context.ResourceBuilderContext) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 5 with NetworkCreationRequest

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

the class EnvironmentNetworkService method createCloudNetwork.

public BaseNetwork createCloudNetwork(EnvironmentDto environment, BaseNetwork baseNetwork) {
    NetworkConnector networkConnector = getNetworkConnector(environment.getCloudPlatform());
    NetworkCreationRequest networkCreationRequest = networkCreationRequestFactory.create(environment);
    EnvironmentNetworkConverter converter = environmentNetworkConverterMap.get(getCloudPlatform(environment));
    CreatedCloudNetwork createdCloudNetwork = networkConnector.createNetworkWithSubnets(networkCreationRequest);
    return converter.setCreatedCloudNetwork(baseNetwork, createdCloudNetwork);
}
Also used : NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) NetworkConnector(com.sequenceiq.cloudbreak.cloud.NetworkConnector) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) EnvironmentNetworkConverter(com.sequenceiq.environment.network.v1.converter.EnvironmentNetworkConverter)

Aggregations

NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)25 Test (org.junit.jupiter.api.Test)16 SubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 Template (freemarker.template.Template)10 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 HashSet (java.util.HashSet)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)6 File (java.io.File)6 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)5 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)5 Network (com.sequenceiq.cloudbreak.cloud.model.Network)5 IOException (java.io.IOException)5 List (java.util.List)5