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());
}
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());
}
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());
}
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());
}
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);
}
Aggregations