Search in sources :

Example 1 with SubnetRequest

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

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

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

the class AwsNetworkCfTemplateProvider method createInterfaceServiceEndpoints.

private List<AwsServiceEndpointView> createInterfaceServiceEndpoints(NetworkCreationRequest networkCreationRequest, List<SubnetRequest> subnets) {
    Map<String, String> endpointNameMappings = interfaceServices.stream().collect(Collectors.toMap(s -> String.format(VPC_INTERFACE_SERVICE_ENDPOINT_NAME_PATTERN, networkCreationRequest.getRegion().value(), s), s -> s));
    List<ServiceDetail> serviceDetails = describeVpcServiceDetails(networkCreationRequest, endpointNameMappings);
    Map<String, SubnetRequest> subnetByZoneMap = createPublicSubnetByZoneMap(subnets);
    List<AwsServiceEndpointView> interfaceServceEndpoints = new ArrayList<>();
    for (ServiceDetail serviceDetail : serviceDetails) {
        List<SubnetRequest> subnetRequests = serviceDetail.getAvailabilityZones().stream().filter(az -> subnetByZoneMap.containsKey(az)).map(az -> subnetByZoneMap.get(az)).collect(Collectors.toList());
        if (!subnetRequests.isEmpty()) {
            interfaceServceEndpoints.add(new AwsServiceEndpointView(endpointNameMappings.get(serviceDetail.getServiceName()), subnetRequests));
        }
    }
    return interfaceServceEndpoints;
}
Also used : TemplateException(freemarker.template.TemplateException) LoggerFactory(org.slf4j.LoggerFactory) NetworkCreationRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Strings(com.google.common.base.Strings) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) USE_VPC_ENDPOINT(com.sequenceiq.common.model.PrivateEndpointType.USE_VPC_ENDPOINT) Map(java.util.Map) Template(freemarker.template.Template) Logger(org.slf4j.Logger) IOException(java.io.IOException) PrivateEndpointType(com.sequenceiq.common.model.PrivateEndpointType) Collectors(java.util.stream.Collectors) ServiceDetail(com.amazonaws.services.ec2.model.ServiceDetail) FreeMarkerTemplateUtils(com.sequenceiq.cloudbreak.util.FreeMarkerTemplateUtils) List(java.util.List) Component(org.springframework.stereotype.Component) AwsServiceEndpointView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsServiceEndpointView) Configuration(freemarker.template.Configuration) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) ArrayList(java.util.ArrayList) ServiceDetail(com.amazonaws.services.ec2.model.ServiceDetail) AwsServiceEndpointView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsServiceEndpointView)

Example 4 with SubnetRequest

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

the class AwsNetworkConnector method createNetworkWithSubnets.

@Override
public CreatedCloudNetwork createNetworkWithSubnets(NetworkCreationRequest networkRequest) {
    AwsCredentialView credentialView = new AwsCredentialView(networkRequest.getCloudCredential());
    AmazonCloudFormationClient cloudFormationClient = awsClient.createCloudFormationClient(credentialView, networkRequest.getRegion().value());
    List<SubnetRequest> subnetRequests = getCloudSubNets(networkRequest);
    String cfStackName = networkRequest.getStackName();
    try {
        cloudFormationClient.describeStacks(new DescribeStacksRequest().withStackName(cfStackName));
        LOGGER.warn("AWS CloudFormation stack for Network with stack name: '{}' already exists. Attaching this one to the network.", cfStackName);
        return getCreatedNetworkWithPolling(networkRequest, credentialView, cloudFormationClient, subnetRequests);
    } catch (AmazonServiceException e) {
        if (networkDoesNotExist(e)) {
            LOGGER.warn("{} occurred during describe AWS CloudFormation stack for Network with stack name: '{}'. " + "Assuming the CF Stack does not exist, so creating a new one. Exception message: {}", e.getClass(), cfStackName, e.getMessage());
            String cloudFormationTemplate = createTemplate(networkRequest, subnetRequests);
            return createNewCfNetworkStack(networkRequest, credentialView, cloudFormationClient, cloudFormationTemplate, subnetRequests);
        } else {
            String region = networkRequest.getRegion().getRegionName();
            String errorReason = awsCloudFormationErrorMessageProvider.getErrorReason(credentialView, region, cfStackName, ResourceStatus.CREATE_FAILED);
            String message = String.format("Failed to create network: %s", errorReason);
            LOGGER.debug(message, e);
            throw new CloudConnectorException(message, e);
        }
    }
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Example 5 with SubnetRequest

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

the class AzureNetworkTemplateBuilderTest method publicSubnetRequest.

public SubnetRequest publicSubnetRequest(String cidr, int index) {
    SubnetRequest subnetRequest = new SubnetRequest();
    subnetRequest.setIndex(index);
    subnetRequest.setPublicSubnetCidr(cidr);
    subnetRequest.setSubnetGroup(index % 3);
    subnetRequest.setAvailabilityZone("az");
    return subnetRequest;
}
Also used : SubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest) NetworkSubnetRequest(com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)

Aggregations

SubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.SubnetRequest)27 NetworkSubnetRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkSubnetRequest)12 NetworkCreationRequest (com.sequenceiq.cloudbreak.cloud.model.network.NetworkCreationRequest)11 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)9 Template (freemarker.template.Template)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 Test (org.junit.Test)6 Test (org.junit.jupiter.api.Test)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)5 CreatedSubnet (com.sequenceiq.cloudbreak.cloud.model.network.CreatedSubnet)5 File (java.io.File)5 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)4 CreatedCloudNetwork (com.sequenceiq.cloudbreak.cloud.model.network.CreatedCloudNetwork)4 ArrayList (java.util.ArrayList)4 AmazonCloudFormationClient (com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)3 HashSet (java.util.HashSet)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2