Search in sources :

Example 1 with CreatedSubnet

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

the class AzureNetworkConnector method createNetworkWithSubnets.

@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) {
        throw azureUtils.convertToCloudConnectorException(e, "Network template deployment provisioning");
    } 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()));
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) HashMap(java.util.HashMap) Deployment(com.microsoft.azure.management.resources.Deployment) Json(com.sequenceiq.cloudbreak.common.json.Json) CloudException(com.microsoft.azure.CloudException) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) BadRequestException(javax.ws.rs.BadRequestException) CloudException(com.microsoft.azure.CloudException) AzureClient(com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient) CreatedCloudNetwork(com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) HashMap(java.util.HashMap) Map(java.util.Map) ResourceGroup(com.microsoft.azure.management.resources.ResourceGroup)

Example 2 with CreatedSubnet

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

the class AzureNetworkConnector method createSubnets.

private Set<CreatedSubnet> createSubnets(List<SubnetRequest> subnetRequests, Map<String, Map> outputMap, String region) {
    Set<CreatedSubnet> createdSubnets = new HashSet<>();
    for (SubnetRequest subnetRequest : subnetRequests) {
        if (outputMap.containsKey(SUBNET_ID_KEY + subnetRequest.getIndex())) {
            CreatedSubnet createdSubnet = new CreatedSubnet();
            createdSubnet.setSubnetId(cropId((String) outputMap.get(SUBNET_ID_KEY + subnetRequest.getIndex()).get("value")));
            if (!Strings.isNullOrEmpty(subnetRequest.getPrivateSubnetCidr())) {
                createdSubnet.setCidr(subnetRequest.getPrivateSubnetCidr());
            } else {
                createdSubnet.setCidr(subnetRequest.getPublicSubnetCidr());
            }
            createdSubnet.setAvailabilityZone(region);
            createdSubnet.setType(subnetRequest.getType());
            createdSubnets.add(createdSubnet);
        } else {
            throw new CloudConnectorException("Subnet could not be found in the Azure deployment output.");
        }
    }
    return createdSubnets;
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet) HashSet(java.util.HashSet)

Example 3 with CreatedSubnet

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

the class GcpCloudSubnetProvider method provide.

public List<CreatedSubnet> provide(NetworkCreationRequest request, List<String> subnetCidrs) throws IOException {
    Compute compute = gcpComputeFactory.buildCompute(request.getCloudCredential());
    String projectId = gcpStackUtil.getProjectId(request.getCloudCredential());
    List<String> az = getAvailabilityZones(compute, projectId, request.getRegion());
    List<CreatedSubnet> subnets = new ArrayList<>(subnetCidrs.size());
    for (int i = 0; i < subnetCidrs.size(); i++) {
        CreatedSubnet createdSubnet = new CreatedSubnet();
        createdSubnet.setCidr(subnetCidrs.get(i));
        if (i < az.size()) {
            createdSubnet.setAvailabilityZone(az.get(i));
        } else {
            createdSubnet.setAvailabilityZone(az.get(az.size() - 1));
        }
        subnets.add(createdSubnet);
    }
    return subnets;
}
Also used : Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)

Example 4 with CreatedSubnet

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

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

the class GcpNetworkConnectorTest method createdSubnet.

private CreatedSubnet createdSubnet() {
    CreatedSubnet createdSubnet = new CreatedSubnet();
    createdSubnet.setCidr("0.0.0.0/0");
    createdSubnet.setSubnetId("subnet-1");
    createdSubnet.setPublicSubnet(true);
    createdSubnet.setIgwAvailable(true);
    createdSubnet.setMapPublicIpOnLaunch(true);
    createdSubnet.setType(SubnetType.DWX);
    return createdSubnet;
}
Also used : CreatedSubnet(com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)

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