Search in sources :

Example 1 with AwsNetworkView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView in project cloudbreak by hortonworks.

the class AwsLoadBalancerCommonService method convertLoadBalancer.

@VisibleForTesting
AwsLoadBalancer convertLoadBalancer(CloudLoadBalancer cloudLoadBalancer, Map<String, List<String>> instanceIdsByGroupName, AwsNetworkView awsNetworkView, List<AwsLoadBalancer> awsLoadBalancers) {
    // Check and see if we already have a load balancer whose scheme matches this one.
    AwsLoadBalancer currentLoadBalancer = null;
    LoadBalancerType cloudLbType = cloudLoadBalancer.getType();
    Set<String> subnetIds = selectLoadBalancerSubnetIds(cloudLbType, awsNetworkView, cloudLoadBalancer);
    AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(cloudLbType);
    currentLoadBalancer = awsLoadBalancers.stream().filter(lb -> lb.getScheme() == scheme).findFirst().orElse(new AwsLoadBalancer(scheme));
    currentLoadBalancer.addSubnets(subnetIds);
    setupLoadBalancer(cloudLoadBalancer, instanceIdsByGroupName, currentLoadBalancer);
    return currentLoadBalancer;
}
Also used : Logger(org.slf4j.Logger) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) HashSet(java.util.HashSet) Inject(javax.inject.Inject) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) List(java.util.List) GroupSubnet(com.sequenceiq.cloudbreak.cloud.model.GroupSubnet) Service(org.springframework.stereotype.Service) Group(com.sequenceiq.cloudbreak.cloud.model.Group) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) VisibleForTesting(com.google.common.annotations.VisibleForTesting) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with AwsNetworkView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView in project cloudbreak by hortonworks.

the class AwsNetworkService method findNonOverLappingCIDR.

public String findNonOverLappingCIDR(AuthenticatedContext ac, CloudStack stack) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonEc2Client ec2Client = awsClient.createEc2Client(new AwsCredentialView(ac.getCloudCredential()), region);
    DescribeVpcsRequest vpcRequest = new DescribeVpcsRequest().withVpcIds(awsNetworkView.getExistingVpc());
    Vpc vpc = ec2Client.describeVpcs(vpcRequest).getVpcs().get(0);
    String vpcCidr = vpc.getCidrBlock();
    LOGGER.debug("Subnet cidr is empty, find a non-overlapping subnet for VPC cidr: {}", vpcCidr);
    DescribeSubnetsRequest request = new DescribeSubnetsRequest().withFilters(new Filter("vpc-id", singletonList(awsNetworkView.getExistingVpc())));
    List<Subnet> awsSubnets = ec2Client.describeSubnets(request).getSubnets();
    List<String> subnetCidrs = awsSubnets.stream().map(Subnet::getCidrBlock).collect(Collectors.toList());
    LOGGER.debug("The selected VPCs: {}, has the following subnets: {}", vpc.getVpcId(), String.join(",", subnetCidrs));
    return calculateSubnet(ac.getCloudContext().getName(), vpc, subnetCidrs);
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) DescribeVpcsRequest(com.amazonaws.services.ec2.model.DescribeVpcsRequest) Filter(com.amazonaws.services.ec2.model.Filter) Vpc(com.amazonaws.services.ec2.model.Vpc) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) Subnet(com.amazonaws.services.ec2.model.Subnet) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest)

Example 3 with AwsNetworkView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView in project cloudbreak by hortonworks.

the class AwsSetup method prerequisites.

@Override
public void prerequisites(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier persistenceNotifier) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String region = ac.getCloudContext().getLocation().getRegion().value();
    verifySpotInstances(stack);
    if (awsNetworkView.isExistingVPC()) {
        try {
            AmazonEc2Client amazonEC2Client = new AuthenticatedContextView(ac).getAmazonEC2Client();
            validateExistingIGW(awsNetworkView, amazonEC2Client);
            validateExistingSubnet(awsNetworkView, amazonEC2Client);
        } catch (AmazonServiceException e) {
            throw new CloudConnectorException(e.getErrorMessage());
        } catch (AmazonClientException e) {
            throw new CloudConnectorException(e.getMessage());
        }
    }
    validateRegionAndZone(ac.getCloudCredential(), ac.getCloudContext().getLocation());
    validateExistingKeyPair(stack.getInstanceAuthentication(), credentialView, region, ac);
    LOGGER.debug("setup has been executed");
}
Also used : AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) AmazonClientException(com.amazonaws.AmazonClientException) AuthenticatedContextView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AuthenticatedContextView) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)

Example 4 with AwsNetworkView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView in project cloudbreak by hortonworks.

the class AwsStackRequestHelper method getStackParameters.

@VisibleForTesting
Collection<Parameter> getStackParameters(AuthenticatedContext ac, DatabaseStack stack, boolean deleteProtection) {
    AwsNetworkView awsNetworkView = new AwsNetworkView(stack.getNetwork());
    DatabaseServer databaseServer = stack.getDatabaseServer();
    AwsRdsInstanceView awsRdsInstanceView = new AwsRdsInstanceView(databaseServer);
    AwsRdsDbSubnetGroupView awsRdsDbSubnetGroupView = new AwsRdsDbSubnetGroupView(databaseServer);
    AwsRdsVpcSecurityGroupView awsRdsVpcSecurityGroupView = new AwsRdsVpcSecurityGroupView(databaseServer);
    AwsRdsDbParameterGroupView awsRdsDbParameterGroupView = new AwsRdsDbParameterGroupView(databaseServer);
    List<Parameter> parameters = new ArrayList<>(asList(new Parameter().withParameterKey("DBInstanceClassParameter").withParameterValue(awsRdsInstanceView.getDBInstanceClass()), new Parameter().withParameterKey("DBInstanceIdentifierParameter").withParameterValue(awsRdsInstanceView.getDBInstanceIdentifier()), new Parameter().withParameterKey("DBSubnetGroupNameParameter").withParameterValue(awsRdsDbSubnetGroupView.getDBSubnetGroupName()), new Parameter().withParameterKey("DBSubnetGroupSubnetIdsParameter").withParameterValue(String.join(",", awsNetworkView.getSubnetList())), new Parameter().withParameterKey("EngineParameter").withParameterValue(awsRdsInstanceView.getEngine()), new Parameter().withParameterKey("MasterUsernameParameter").withParameterValue(awsRdsInstanceView.getMasterUsername()), new Parameter().withParameterKey("MasterUserPasswordParameter").withParameterValue(awsRdsInstanceView.getMasterUserPassword()), new Parameter().withParameterKey("DeletionProtectionParameter").withParameterValue(deleteProtection ? "true" : "false")));
    addParameterIfNotNull(parameters, "AllocatedStorageParameter", awsRdsInstanceView.getAllocatedStorage());
    addParameterIfNotNull(parameters, "BackupRetentionPeriodParameter", awsRdsInstanceView.getBackupRetentionPeriod());
    addParameterIfNotNull(parameters, "EngineVersionParameter", awsRdsInstanceView.getEngineVersion());
    addParameterIfNotNull(parameters, "MultiAZParameter", awsRdsInstanceView.getMultiAZ());
    addParameterIfNotNull(parameters, "StorageTypeParameter", awsRdsInstanceView.getStorageType());
    addParameterIfNotNull(parameters, "PortParameter", databaseServer.getPort());
    boolean useSslEnforcement = databaseServer.isUseSslEnforcement();
    addParameterIfNotNull(parameters, "DBParameterGroupNameParameter", useSslEnforcement ? awsRdsDbParameterGroupView.getDBParameterGroupName() : null);
    addParameterIfNotNull(parameters, "DBParameterGroupFamilyParameter", useSslEnforcement ? awsRdsDbParameterGroupView.getDBParameterGroupFamily() : null);
    addParameterIfNotNull(parameters, "SslCertificateIdentifierParameter", useSslEnforcement && awsRdsInstanceView.isSslCertificateIdentifierDefined() ? awsRdsInstanceView.getSslCertificateIdentifier() : null);
    if (awsRdsInstanceView.getVPCSecurityGroups().isEmpty()) {
        // VPC-id and VPC cidr should be filled in
        parameters.addAll(asList(new Parameter().withParameterKey("VPCIdParameter").withParameterValue(String.valueOf(awsNetworkView.getExistingVpc())), new Parameter().withParameterKey("DBSecurityGroupNameParameter").withParameterValue(awsRdsVpcSecurityGroupView.getDBSecurityGroupName())));
    } else {
        parameters.add(new Parameter().withParameterKey("VPCSecurityGroupsParameter").withParameterValue(String.join(",", awsRdsInstanceView.getVPCSecurityGroups())));
    }
    return parameters;
}
Also used : AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AwsRdsDbParameterGroupView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsDbParameterGroupView) ArrayList(java.util.ArrayList) Parameter(com.amazonaws.services.cloudformation.model.Parameter) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AwsRdsInstanceView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsInstanceView) AwsRdsDbSubnetGroupView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsDbSubnetGroupView) AwsRdsVpcSecurityGroupView(com.sequenceiq.cloudbreak.cloud.aws.view.AwsRdsVpcSecurityGroupView) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with AwsNetworkView

use of com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView in project cloudbreak by hortonworks.

the class AwsLaunchService method launch.

public List<CloudResourceStatus> launch(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, AdjustmentTypeWithThreshold adjustmentTypeWithThreshold) throws Exception {
    LOGGER.info("Launch AWS cluster with adjustment and threshold: {}", adjustmentTypeWithThreshold);
    createKeyPair(ac, stack);
    String cFStackName = cfStackUtil.getCfStackName(ac);
    AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
    String regionName = ac.getCloudContext().getLocation().getRegion().value();
    AmazonCloudFormationClient cfClient = awsClient.createCloudFormationClient(credentialView, regionName);
    AmazonEc2Client amazonEC2Client = awsClient.createEc2Client(credentialView, regionName);
    Network network = stack.getNetwork();
    AwsNetworkView awsNetworkView = new AwsNetworkView(network);
    boolean mapPublicIpOnLaunch = awsNetworkService.isMapPublicOnLaunch(awsNetworkView, amazonEC2Client);
    DescribeStacksRequest describeStacksRequest = new DescribeStacksRequest().withStackName(cFStackName);
    ModelContext modelContext = null;
    try {
        cfClient.describeStacks(describeStacksRequest);
        LOGGER.debug("Stack already exists: {}", cFStackName);
    } catch (AmazonServiceException ignored) {
        boolean existingVPC = awsNetworkView.isExistingVPC();
        boolean existingSubnet = awsNetworkView.isExistingSubnet();
        CloudResource cloudFormationStack = new Builder().type(ResourceType.CLOUDFORMATION_STACK).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).name(cFStackName).build();
        resourceNotifier.notifyAllocation(cloudFormationStack, ac.getCloudContext());
        String cidr = network.getSubnet().getCidr();
        String subnet = isNoCIDRProvided(existingVPC, existingSubnet, cidr) ? awsNetworkService.findNonOverLappingCIDR(ac, stack) : cidr;
        modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
        String cfTemplate = cloudFormationTemplateBuilder.build(modelContext);
        LOGGER.debug("CloudFormationTemplate: {}", cfTemplate);
        cfClient.createStack(awsStackRequestHelper.createCreateStackRequest(ac, stack, cFStackName, subnet, cfTemplate));
    }
    LOGGER.debug("CloudFormation stack creation request sent with stack name: '{}' for stack: '{}'", cFStackName, ac.getCloudContext().getId());
    Waiter<DescribeStacksRequest> creationWaiter = cfClient.waiters().stackCreateComplete();
    StackCancellationCheck stackCancellationCheck = new StackCancellationCheck(ac.getCloudContext().getId());
    run(creationWaiter, describeStacksRequest, stackCancellationCheck, String.format("CloudFormation stack %s creation failed.", cFStackName), () -> awsCloudFormationErrorMessageProvider.getErrorReason(ac, cFStackName, ResourceStatus.CREATE_FAILED));
    List<CloudResource> networkResources = saveGeneratedSubnet(ac, stack, cFStackName, cfClient, resourceNotifier);
    suspendAutoscalingGoupsWhenNewInstancesAreReady(ac, stack);
    AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
    List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfClient, amazonASClient, stack.getGroups());
    if (mapPublicIpOnLaunch) {
        associatePublicIpsToGatewayInstances(stack, cFStackName, cfClient, amazonEC2Client, instances);
    }
    awsComputeResourceService.buildComputeResourcesForLaunch(ac, stack, adjustmentTypeWithThreshold, instances, networkResources);
    awsTaggingService.tagRootVolumes(ac, amazonEC2Client, instances, stack.getTags());
    awsCloudWatchService.addCloudWatchAlarmsForSystemFailures(instances, regionName, credentialView);
    return awsResourceConnector.check(ac, instances);
}
Also used : DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) StackCancellationCheck(com.sequenceiq.cloudbreak.cloud.aws.scheduler.StackCancellationCheck) CloudFormationTemplateBuilder(com.sequenceiq.cloudbreak.cloud.aws.CloudFormationTemplateBuilder) Builder(com.sequenceiq.cloudbreak.cloud.model.CloudResource.Builder) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEc2Client(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Aggregations

AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)41 Network (com.sequenceiq.cloudbreak.cloud.model.Network)19 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)19 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)18 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)14 GroupNetwork (com.sequenceiq.cloudbreak.cloud.model.GroupNetwork)13 Test (org.junit.Test)13 StackResourceSummary (com.amazonaws.services.cloudformation.model.StackResourceSummary)11 CloudLoadBalancer (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer)11 ArrayList (java.util.ArrayList)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)11 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)9 List (java.util.List)9 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)8 Test (org.junit.jupiter.api.Test)8 AmazonEc2Client (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEc2Client)7 AmazonServiceException (com.amazonaws.AmazonServiceException)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6