Search in sources :

Example 16 with AwsLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method buildTestWithMultipleLoadBalancers.

@Test
public void buildTestWithMultipleLoadBalancers() {
    // GIVEN
    List<AwsLoadBalancer> awsLoadBalancers = List.of(setupLoadBalancer(AwsLoadBalancerScheme.INTERNAL, 443, true), setupLoadBalancer(AwsLoadBalancerScheme.INTERNET_FACING, 888, true));
    // WHEN
    modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(true).withExistingIGW(true).withExistingSubnetCidr(singletonList(existingSubnetCidr)).withExistinVpcCidr(List.of(existingSubnetCidr)).mapPublicIpOnLaunch(true).withEnableInstanceProfile(true).withInstanceProfileAvailable(true).withOutboundInternetTraffic(OutboundInternetTraffic.ENABLED).withTemplate(awsCloudFormationTemplate).withLoadBalancers(awsLoadBalancers);
    String templateString = cloudFormationTemplateBuilder.build(modelContext);
    // THEN
    Assertions.assertThat(templateString).matches(JsonUtil::isValid, "Invalid JSON: " + templateString).contains("\"LoadBalancerInternal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::LoadBalancer\"").contains("\"Scheme\" : \"internal\"").contains("\"TargetGroupPort443Internal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::TargetGroup\"").contains("\"ListenerPort443Internal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::Listener\"").contains("\"LoadBalancerExternal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::LoadBalancer\"").contains("\"Scheme\" : \"internet-facing\"").contains("\"TargetGroupPort888External\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::TargetGroup\"").contains("\"ListenerPort888External\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::Listener\"");
    assert templateString.contains("\"Targets\" : [{ \"Id\" : \"instance1-443\" },{ \"Id\" : \"instance2-443\" }]}}") || templateString.contains("\"Targets\" : [{ \"Id\" : \"instance2-443\" },{ \"Id\" : \"instance1-443\" }]}}");
    assert templateString.contains("\"Targets\" : [{ \"Id\" : \"instance1-888\" },{ \"Id\" : \"instance2-888\" }]}}") || templateString.contains("\"Targets\" : [{ \"Id\" : \"instance2-888\" },{ \"Id\" : \"instance1-888\" }]}}");
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) Matchers.containsString(org.hamcrest.Matchers.containsString) JsonUtil(com.sequenceiq.cloudbreak.common.json.JsonUtil) Test(org.junit.jupiter.api.Test)

Example 17 with AwsLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.

the class CloudFormationTemplateBuilderTest method setupLoadBalancer.

private AwsLoadBalancer setupLoadBalancer(AwsLoadBalancerScheme scheme, int port, boolean setArn) {
    AwsLoadBalancer loadBalancer = new AwsLoadBalancer(scheme);
    loadBalancer.addSubnets(Set.of("subnet1"));
    AwsListener listener = loadBalancer.getOrCreateListener(port, 8443);
    listener.addInstancesToTargetGroup(Set.of("instance1-" + port, "instance2-" + port));
    if (setArn) {
        listener.getTargetGroup().setArn("arn://targetgroup");
        loadBalancer.setArn("arn://loadbalancer");
        loadBalancer.validateListenerConfigIsSet();
    }
    return loadBalancer;
}
Also used : AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)

Example 18 with AwsLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.

the class AwsLaunchServiceLoadBalancerTest method testSetLoadBalancerMetadata.

@Test
public void testSetLoadBalancerMetadata() {
    AwsLoadBalancer loadBalancer = new AwsLoadBalancer(AwsLoadBalancerScheme.INTERNAL);
    loadBalancer.getOrCreateListener(PORT, PORT);
    when(result.getStackResourceSummaries()).thenReturn(createFullSummaries(Set.of(LoadBalancerType.PRIVATE)));
    underTest.setLoadBalancerMetadata(List.of(loadBalancer), result);
    assertEquals(LOAD_BALANCER_ARN, loadBalancer.getArn());
    AwsListener listener = loadBalancer.getListeners().iterator().next();
    assert listener.areTargetGroupArnsSet();
    AwsTargetGroup targetGroup = listener.getTargetGroup();
    assertEquals(TARGET_GROUP_ARN, targetGroup.getArn());
}
Also used : AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with AwsLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.

the class AwsLoadBalancerLaunchService method checkForListenerResources.

@VisibleForTesting
boolean checkForListenerResources(AmazonCloudFormationClient cfClient, String cFStackName, List<AwsLoadBalancer> awsLoadBalancers) {
    ListStackResourcesResult result = cfClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
    boolean resourcesFound = true;
    List<StackResourceSummary> summaries = result.getStackResourceSummaries();
    for (AwsLoadBalancer loadBalancer : awsLoadBalancers) {
        for (AwsListener listener : loadBalancer.getListeners()) {
            LOGGER.debug("Checking to see if listener resource {} already exists", listener.getName());
            resourcesFound = resourcesFound && summaries.stream().anyMatch(stackResourceSummary -> listener.getName().equals(stackResourceSummary.getLogicalResourceId()));
        }
    }
    return resourcesFound;
}
Also used : ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) StackResourceSummary(com.amazonaws.services.cloudformation.model.StackResourceSummary) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 20 with AwsLoadBalancer

use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.

the class AwsLoadBalancerLaunchService method updateCloudformationWithLoadBalancers.

public List<CloudResourceStatus> updateCloudformationWithLoadBalancers(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, ModelContext modelContext) {
    List<CloudResourceStatus> statuses = new ArrayList<>();
    List<CloudLoadBalancer> cloudLoadBalancers = stack.getLoadBalancers();
    String cFStackName = cfStackUtil.getCfStackName(ac);
    if (!cloudLoadBalancers.isEmpty()) {
        LOGGER.debug("Creating load balancers of types " + cloudLoadBalancers.stream().map(CloudLoadBalancer::getType).map(LoadBalancerType::name).collect(Collectors.joining(",")));
        AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
        String regionName = ac.getCloudContext().getLocation().getRegion().value();
        AmazonCloudFormationClient cfRetryClient = awsClient.createCloudFormationClient(credentialView, regionName);
        AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
        List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfRetryClient, amazonASClient, stack.getGroups());
        Network network = stack.getNetwork();
        AwsNetworkView awsNetworkView = new AwsNetworkView(network);
        if (modelContext == null) {
            LOGGER.debug("Creating default model context");
            modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
        }
        Map<String, List<String>> instanceIdsByGroupName = instances.stream().collect(Collectors.groupingBy(CloudResource::getGroup, mapping(CloudResource::getInstanceId, toList())));
        List<AwsLoadBalancer> awsLoadBalancers = loadBalancerCommonService.getAwsLoadBalancers(cloudLoadBalancers, instanceIdsByGroupName, awsNetworkView);
        modelContext.withLoadBalancers(awsLoadBalancers);
        LOGGER.debug("Starting CloudFormation update to create load balancer and target groups.");
        ListStackResourcesResult result;
        if (checkForLoadBalancerAndTargetGroupResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
            LOGGER.debug("Load balancer and target group resources already exist, skipping creation");
            result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
        } else {
            result = updateCloudFormationStack(ac, stack, modelContext);
        }
        setLoadBalancerMetadata(awsLoadBalancers, result);
        LOGGER.debug("Starting CloudFormation update to create listeners.");
        if (checkForListenerResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
            LOGGER.debug("Listener resources already exist, skipping creation");
            result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
        } else {
            result = updateCloudFormationStack(ac, stack, modelContext);
        }
        ListStackResourcesResult finalResult = result;
        awsLoadBalancers.forEach(lb -> statuses.add(createLoadBalancerStatus(ac, lb, finalResult)));
    } else {
        LOGGER.debug("No load balancers in stack");
    }
    return statuses;
}
Also used : ListStackResourcesResult(com.amazonaws.services.cloudformation.model.ListStackResourcesResult) ArrayList(java.util.ArrayList) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) AwsNetworkView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Network(com.sequenceiq.cloudbreak.cloud.model.Network) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient)

Aggregations

AwsLoadBalancer (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)23 AwsListener (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener)14 ArrayList (java.util.ArrayList)8 AwsTargetGroup (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup)7 AwsNetworkView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsNetworkView)7 List (java.util.List)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 AwsLoadBalancerScheme (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)6 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)6 CloudLoadBalancer (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer)6 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Test (org.junit.jupiter.api.Test)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 Service (org.springframework.stereotype.Service)6 ModelContext (com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext)5 ListStackResourcesResult (com.amazonaws.services.cloudformation.model.ListStackResourcesResult)4 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4