@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); }
@Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AzureClient azureClient = azureClientService.getClient(networkRequest.getCloudCredential()); String region = networkRequest.getRegion().value(); List<SubnetRequest> subnetRequests = azureSubnetRequestProvider.provide( region, Lists.newArrayList(networkRequest.getPublicSubnets()), Lists.newArrayList(networkRequest.getPrivateSubnets()), networkRequest.isPrivateSubnetEnabled()); Deployment templateDeployment; ResourceGroup resourceGroup; try { resourceGroup = getOrCreateResourceGroup(azureClient, networkRequest); String template = azureNetworkTemplateBuilder.build(networkRequest, subnetRequests, resourceGroup.name()); String parametersMapAsString = new Json(Map.of()).getValue(); templateDeployment = azureClient.createTemplateDeployment(resourceGroup.name(), networkRequest.getStackName(), template, parametersMapAsString); } catch (CloudException e) { LOGGER.info("Provisioning error, cloud exception happened: ", e); if (e.body() != null && e.body().details() != null) { String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", ")); throw new CloudConnectorException(String.format("Network provisioning failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details)); } else { throw new CloudConnectorException(String.format("Network provisioning failed: '%s', please go to Azure Portal for detailed message", e)); } } catch (Exception e) { LOGGER.warn("Provisioning error:", e); throw new CloudConnectorException(String.format("Error in provisioning network %s: %s", networkRequest.getStackName(), e.getMessage())); } Map<String, Map> outputMap = (HashMap) templateDeployment.outputs(); String networkName = cropId((String) outputMap.get(NETWORK_ID_KEY).get("value")); Set<CreatedSubnet> subnets = createSubnets( subnetRequests, outputMap, region); return new CreatedCloudNetwork(networkRequest.getStackName(), networkName, subnets, createProperties(resourceGroup.name(), networkRequest.getStackName())); }
AzureNetworkConnector implements NetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AzureClient azureClient = azureClientService.getClient(networkRequest.getCloudCredential()); String region = networkRequest.getRegion().value(); List<SubnetRequest> subnetRequests = azureSubnetRequestProvider.provide( region, Lists.newArrayList(networkRequest.getPublicSubnets()), Lists.newArrayList(networkRequest.getPrivateSubnets()), networkRequest.isPrivateSubnetEnabled()); Deployment templateDeployment; ResourceGroup resourceGroup; try { resourceGroup = getOrCreateResourceGroup(azureClient, networkRequest); String template = azureNetworkTemplateBuilder.build(networkRequest, subnetRequests, resourceGroup.name()); String parametersMapAsString = new Json(Map.of()).getValue(); templateDeployment = azureClient.createTemplateDeployment(resourceGroup.name(), networkRequest.getStackName(), template, parametersMapAsString); } catch (CloudException e) { LOGGER.info("Provisioning error, cloud exception happened: ", e); if (e.body() != null && e.body().details() != null) { String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", ")); throw new CloudConnectorException(String.format("Network provisioning failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details)); } else { throw new CloudConnectorException(String.format("Network provisioning failed: '%s', please go to Azure Portal for detailed message", e)); } } catch (Exception e) { LOGGER.warn("Provisioning error:", e); throw new CloudConnectorException(String.format("Error in provisioning network %s: %s", networkRequest.getStackName(), e.getMessage())); } Map<String, Map> outputMap = (HashMap) templateDeployment.outputs(); String networkName = cropId((String) outputMap.get(NETWORK_ID_KEY).get("value")); Set<CreatedSubnet> subnets = createSubnets( subnetRequests, outputMap, region); return new CreatedCloudNetwork(networkRequest.getStackName(), networkName, subnets, createProperties(resourceGroup.name(), networkRequest.getStackName())); } }
AzureNetworkConnector implements NetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AzureClient azureClient = azureClientService.getClient(networkRequest.getCloudCredential()); String region = networkRequest.getRegion().value(); List<SubnetRequest> subnetRequests = azureSubnetRequestProvider.provide( region, Lists.newArrayList(networkRequest.getPublicSubnets()), Lists.newArrayList(networkRequest.getPrivateSubnets()), networkRequest.isPrivateSubnetEnabled()); Deployment templateDeployment; ResourceGroup resourceGroup; try { resourceGroup = getOrCreateResourceGroup(azureClient, networkRequest); String template = azureNetworkTemplateBuilder.build(networkRequest, subnetRequests, resourceGroup.name()); String parametersMapAsString = new Json(Map.of()).getValue(); templateDeployment = azureClient.createTemplateDeployment(resourceGroup.name(), networkRequest.getStackName(), template, parametersMapAsString); } catch (CloudException e) { LOGGER.info("Provisioning error, cloud exception happened: ", e); if (e.body() != null && e.body().details() != null) { String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", ")); throw new CloudConnectorException(String.format("Network provisioning failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details)); } else { throw new CloudConnectorException(String.format("Network provisioning failed: '%s', please go to Azure Portal for detailed message", e)); } } catch (Exception e) { LOGGER.warn("Provisioning error:", e); throw new CloudConnectorException(String.format("Error in provisioning network %s: %s", networkRequest.getStackName(), e.getMessage())); } Map<String, Map> outputMap = (HashMap) templateDeployment.outputs(); String networkName = cropId((String) outputMap.get(NETWORK_ID_KEY).get("value")); Set<CreatedSubnet> subnets = createSubnets( subnetRequests, outputMap, region); return new CreatedCloudNetwork(networkRequest.getStackName(), networkName, subnets, createProperties(resourceGroup.name(), networkRequest.getStackName())); }  }
AzureNetworkConnector implements NetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AzureClient azureClient = azureClientService.getClient(networkRequest.getCloudCredential()); String region = networkRequest.getRegion().value(); List<SubnetRequest> subnetRequests = azureSubnetRequestProvider.provide( region, Lists.newArrayList(networkRequest.getPublicSubnets()), Lists.newArrayList(networkRequest.getPrivateSubnets()), networkRequest.isPrivateSubnetEnabled()); Deployment templateDeployment; ResourceGroup resourceGroup; try { resourceGroup = getOrCreateResourceGroup(azureClient, networkRequest); String template = azureNetworkTemplateBuilder.build(networkRequest, subnetRequests, resourceGroup.name()); String parametersMapAsString = new Json(Map.of()).getValue(); templateDeployment = azureClient.createTemplateDeployment(resourceGroup.name(), networkRequest.getStackName(), template, parametersMapAsString); } catch (CloudException e) { LOGGER.info("Provisioning error, cloud exception happened: ", e); if (e.body() != null && e.body().details() != null) { String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", ")); throw new CloudConnectorException(String.format("Network provisioning failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details)); } else { throw new CloudConnectorException(String.format("Network provisioning failed: '%s', please go to Azure Portal for detailed message", e)); } } catch (Exception e) { LOGGER.warn("Provisioning error:", e); throw new CloudConnectorException(String.format("Error in provisioning network %s: %s", networkRequest.getStackName(), e.getMessage())); } Map<String, Map> outputMap = (HashMap) templateDeployment.outputs(); String networkName = cropId((String) outputMap.get(NETWORK_ID_KEY).get("value")); Set<CreatedSubnet> subnets = createSubnets( subnetRequests, outputMap, region); return new CreatedCloudNetwork(networkRequest.getStackName(), networkName, subnets, createProperties(resourceGroup.name(), networkRequest.getStackName())); }  @Override CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest); @Override void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest); @Override NetworkCidr getNetworkCidr(Network network, CloudCredential credential); @Override SubnetSelectionResult chooseSubnets(Collection<CloudSubnet> subnetMetas, SubnetSelectionParameters subnetSelectionParameters); @Override Platform platform(); @Override Variant variant(); }
AzureNetworkConnector implements NetworkConnector { @Override public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) { AzureClient azureClient = azureClientService.getClient(networkRequest.getCloudCredential()); String region = networkRequest.getRegion().value(); List<SubnetRequest> subnetRequests = azureSubnetRequestProvider.provide( region, Lists.newArrayList(networkRequest.getPublicSubnets()), Lists.newArrayList(networkRequest.getPrivateSubnets()), networkRequest.isPrivateSubnetEnabled()); Deployment templateDeployment; ResourceGroup resourceGroup; try { resourceGroup = getOrCreateResourceGroup(azureClient, networkRequest); String template = azureNetworkTemplateBuilder.build(networkRequest, subnetRequests, resourceGroup.name()); String parametersMapAsString = new Json(Map.of()).getValue(); templateDeployment = azureClient.createTemplateDeployment(resourceGroup.name(), networkRequest.getStackName(), template, parametersMapAsString); } catch (CloudException e) { LOGGER.info("Provisioning error, cloud exception happened: ", e); if (e.body() != null && e.body().details() != null) { String details = e.body().details().stream().map(CloudError::message).collect(Collectors.joining(", ")); throw new CloudConnectorException(String.format("Network provisioning failed, status code %s, error message: %s, details: %s", e.body().code(), e.body().message(), details)); } else { throw new CloudConnectorException(String.format("Network provisioning failed: '%s', please go to Azure Portal for detailed message", e)); } } catch (Exception e) { LOGGER.warn("Provisioning error:", e); throw new CloudConnectorException(String.format("Error in provisioning network %s: %s", networkRequest.getStackName(), e.getMessage())); } Map<String, Map> outputMap = (HashMap) templateDeployment.outputs(); String networkName = cropId((String) outputMap.get(NETWORK_ID_KEY).get("value")); Set<CreatedSubnet> subnets = createSubnets( subnetRequests, outputMap, region); return new CreatedCloudNetwork(networkRequest.getStackName(), networkName, subnets, createProperties(resourceGroup.name(), networkRequest.getStackName())); }  @Override CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest); @Override void deleteNetworkWithSubnets(NetworkDeletionRequest networkDeletionRequest); @Override NetworkCidr getNetworkCidr(Network network, CloudCredential credential); @Override SubnetSelectionResult chooseSubnets(Collection<CloudSubnet> subnetMetas, SubnetSelectionParameters subnetSelectionParameters); @Override Platform platform(); @Override Variant variant();  }