Search in sources :

Example 11 with AwsLoadBalancer

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

the class CloudFormationTemplateBuilderTest method buildTestWithSingleLoadBalancerBeforeUpdate.

@Test
public void buildTestWithSingleLoadBalancerBeforeUpdate() {
    // GIVEN
    AwsLoadBalancer awsLoadBalancer = setupLoadBalancer(AwsLoadBalancerScheme.INTERNAL, 443, false);
    // 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(List.of(awsLoadBalancer));
    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\"").doesNotContain("\"ListenerPort443Internal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::Listener\"");
    assert templateString.contains("\"Targets\" : [{ \"Id\" : \"instance1-443\" },{ \"Id\" : \"instance2-443\" }]}}") || templateString.contains("\"Targets\" : [{ \"Id\" : \"instance2-443\" },{ \"Id\" : \"instance1-443\" }]}}");
}
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 12 with AwsLoadBalancer

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

the class AwsLaunchServiceLoadBalancerTest method setupAwsLoadBalancers.

private List<AwsLoadBalancer> setupAwsLoadBalancers() {
    AwsLoadBalancer privateLb = new AwsLoadBalancer(AwsLoadBalancerScheme.INTERNAL);
    privateLb.getOrCreateListener(PORT, PORT);
    AwsLoadBalancer publicLb = new AwsLoadBalancer(AwsLoadBalancerScheme.INTERNET_FACING);
    publicLb.getOrCreateListener(PORT, PORT);
    return List.of(privateLb, publicLb);
}
Also used : AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)

Example 13 with AwsLoadBalancer

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

the class AwsLaunchServiceLoadBalancerTest method setupMocksForUpdate.

private void setupMocksForUpdate(AwsNetworkView awsNetworkView, Network network, List<CloudResource> instances, Set<LoadBalancerType> types) {
    List<CloudLoadBalancer> loadBalancers = new ArrayList<>();
    List<AwsLoadBalancer> awsLoadBalancers = new ArrayList<>();
    for (LoadBalancerType type : types) {
        loadBalancers.add(createCloudLoadBalancer(type));
        AwsLoadBalancerScheme scheme = AwsLoadBalancerScheme.INTERNET_FACING;
        if (LoadBalancerType.PRIVATE.equals(type)) {
            scheme = AwsLoadBalancerScheme.INTERNAL;
        }
        AwsLoadBalancer awsLoadBalancer = new AwsLoadBalancer(scheme);
        awsLoadBalancer.getOrCreateListener(PORT, PORT);
        awsLoadBalancers.add(awsLoadBalancer);
    }
    when(cfStackUtil.getCfStackName(any())).thenReturn(STACK_NAME);
    when(awsClient.createCloudFormationClient(any(), anyString())).thenReturn(cfClient);
    when(cfClient.updateStack(any())).thenReturn(null);
    when(cfClient.listStackResources(any())).thenReturn(result);
    when(cloudFormationTemplateBuilder.build(any(ModelContext.class))).thenReturn("{}");
    when(awsStackRequestHelper.createUpdateStackRequest(any(), any(), anyString(), anyString())).thenReturn(null);
    when(cfClient.waiters()).thenReturn(waiters);
    when(waiters.stackUpdateComplete()).thenReturn(updateWaiter);
    when(ac.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getId()).thenReturn(1L);
    when(cloudContext.getLocation()).thenReturn(location);
    when(location.getRegion()).thenReturn(region);
    when(region.value()).thenReturn("region");
    when(cloudStack.getLoadBalancers()).thenReturn(loadBalancers);
    when(cloudStack.getNetwork()).thenReturn(network);
    when(awsModelService.buildDefaultModelContext(any(), any(), any())).thenReturn(new ModelContext());
    when(awsLoadBalancerCommonService.getAwsLoadBalancers(eq(loadBalancers), any(), any())).thenReturn(awsLoadBalancers);
    PowerMockito.mockStatic(AwsPageCollector.class);
    PowerMockito.when(AwsPageCollector.getAllRouteTables(any(), any())).thenReturn(List.of());
}
Also used : ModelContext(com.sequenceiq.cloudbreak.cloud.aws.common.resource.ModelContext) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) ArrayList(java.util.ArrayList) 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)

Example 14 with AwsLoadBalancer

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

the class AwsLoadBalancerCommonService method getAwsLoadBalancers.

public List<AwsLoadBalancer> getAwsLoadBalancers(List<CloudLoadBalancer> cloudLoadBalancers, Map<String, List<String>> instanceIdsByGroupName, AwsNetworkView awsNetworkView) {
    LOGGER.debug("Converting internal load balancer model to AWS cloud provider model.");
    List<AwsLoadBalancer> awsLoadBalancers = new ArrayList<>();
    for (CloudLoadBalancer cloudLoadBalancer : cloudLoadBalancers) {
        LOGGER.debug("Found load balancer model of type {}", cloudLoadBalancer.getType());
        AwsLoadBalancer loadBalancer = convertLoadBalancer(cloudLoadBalancer, instanceIdsByGroupName, awsNetworkView, awsLoadBalancers);
        if (loadBalancer != null && !awsLoadBalancers.contains(loadBalancer)) {
            awsLoadBalancers.add(loadBalancer);
        }
    }
    Set<String> requestedTypes = cloudLoadBalancers.stream().map(lb -> lb.getType().name()).collect(Collectors.toSet());
    Set<String> awsTypes = awsLoadBalancers.stream().map(lb -> AwsLoadBalancerScheme.INTERNAL.awsScheme().equals(lb.getAwsScheme()) ? "PRIVATE" : "PUBLIC").collect(Collectors.toSet());
    if (!requestedTypes.equals(awsTypes)) {
        throw new CloudConnectorException(String.format("Can not create all requested AWS load balancers. " + "Types requested: [%s]; type to be created: [%s]", requestedTypes, awsTypes));
    }
    return awsLoadBalancers;
}
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) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) ArrayList(java.util.ArrayList) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)

Example 15 with AwsLoadBalancer

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

the class AwsLoadBalancerCommonService method setupLoadBalancer.

private void setupLoadBalancer(CloudLoadBalancer cloudLoadBalancer, Map<String, List<String>> instanceIdsByGroupName, AwsLoadBalancer awsLoadBalancer) {
    LOGGER.debug("Configuring target instances for listeners.");
    for (Map.Entry<TargetGroupPortPair, Set<Group>> entry : cloudLoadBalancer.getPortToTargetGroupMapping().entrySet()) {
        AwsListener listener = awsLoadBalancer.getOrCreateListener(entry.getKey().getTrafficPort(), entry.getKey().getHealthCheckPort());
        Set<String> instanceIds = entry.getValue().stream().flatMap(tg -> instanceIdsByGroupName.get(tg.getName()).stream()).collect(Collectors.toSet());
        LOGGER.debug(String.format("Adding instances %s to listener %s", instanceIds, listener.getName()));
        listener.addInstancesToTargetGroup(instanceIds);
    }
}
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) Set(java.util.Set) HashSet(java.util.HashSet) AwsListener(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsListener) Map(java.util.Map) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair)

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