Search in sources :

Example 6 with TargetGroupPortPair

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

the class CloudFormationStackUtil method addLoadBalancerTargets.

public void addLoadBalancerTargets(AuthenticatedContext ac, CloudLoadBalancer loadBalancer, List<CloudResource> resourcesToAdd) {
    String region = ac.getCloudContext().getLocation().getRegion().value();
    AmazonElasticLoadBalancingClient amazonElbClient = awsClient.createElasticLoadBalancingClient(new AwsCredentialView(ac.getCloudCredential()), region);
    for (Map.Entry<TargetGroupPortPair, Set<Group>> entry : loadBalancer.getPortToTargetGroupMapping().entrySet()) {
        // Get a list of the new instances in the target groups
        Set<String> updatedInstanceIds = getInstanceIdsForGroups(resourcesToAdd, entry.getValue());
        // Find target group ARN
        AwsLoadBalancerScheme scheme = loadBalancerTypeConverter.convert(loadBalancer.getType());
        String targetGroupArn = getResourceArnByLogicalId(ac, AwsTargetGroup.getTargetGroupName(entry.getKey().getTrafficPort(), scheme), region);
        // Use ARN to fetch a list of current targets
        DescribeTargetHealthResult targetHealthResult = amazonElbClient.describeTargetHealth(new DescribeTargetHealthRequest().withTargetGroupArn(targetGroupArn));
        List<TargetDescription> targetDescriptions = targetHealthResult.getTargetHealthDescriptions().stream().map(TargetHealthDescription::getTarget).collect(Collectors.toList());
        Set<String> alreadyRegisteredInstanceIds = targetDescriptions.stream().map(TargetDescription::getId).collect(Collectors.toSet());
        // Remove any targets that have already been registered from the list being processed
        updatedInstanceIds.removeAll(alreadyRegisteredInstanceIds);
        // Register any new instances
        if (!updatedInstanceIds.isEmpty()) {
            List<TargetDescription> targetsToAdd = updatedInstanceIds.stream().map(instanceId -> new TargetDescription().withId(instanceId)).collect(Collectors.toList());
            RegisterTargetsResult registerTargetsResult = amazonElbClient.registerTargets(new RegisterTargetsRequest().withTargetGroupArn(targetGroupArn).withTargets(targetsToAdd));
        }
    }
}
Also used : RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersRequest) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) TargetHealthDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetHealthDescription) AmazonCloudFormationClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonCloudFormationClient) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) Splitter(com.google.common.base.Splitter) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) DescribeFileSystemsResult(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) Collection(java.util.Collection) Output(com.amazonaws.services.cloudformation.model.Output) Set(java.util.Set) Retry(com.sequenceiq.cloudbreak.service.Retry) Collectors(java.util.stream.Collectors) Backoff(org.springframework.retry.annotation.Backoff) Objects(java.util.Objects) List(java.util.List) AmazonAutoScalingClient(com.sequenceiq.cloudbreak.cloud.aws.client.AmazonAutoScalingClient) Group(com.sequenceiq.cloudbreak.cloud.model.Group) PRIVATE_ID(com.sequenceiq.cloudbreak.cloud.model.CloudResource.PRIVATE_ID) LoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.LoadBalancerTypeConverter) DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) INSTANCE_TYPE(com.sequenceiq.cloudbreak.cloud.model.CloudResource.INSTANCE_TYPE) DescribeStacksRequest(com.amazonaws.services.cloudformation.model.DescribeStacksRequest) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) AmazonEfsClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonEfsClient) Supplier(java.util.function.Supplier) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) Value(org.springframework.beans.factory.annotation.Value) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) Service(org.springframework.stereotype.Service) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Retryable(org.springframework.retry.annotation.Retryable) DescribeStackResourceRequest(com.amazonaws.services.cloudformation.model.DescribeStackResourceRequest) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) ResourceType(com.sequenceiq.common.api.type.ResourceType) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) FileSystemDescription(com.amazonaws.services.elasticfilesystem.model.FileSystemDescription) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) DescribeFileSystemsRequest(com.amazonaws.services.elasticfilesystem.model.DescribeFileSystemsRequest) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) SdkClientException(com.amazonaws.SdkClientException) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) Instance(com.amazonaws.services.autoscaling.model.Instance) DescribeStackResourceResult(com.amazonaws.services.cloudformation.model.DescribeStackResourceResult) Collections(java.util.Collections) DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) Set(java.util.Set) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) AwsCredentialView(com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) Map(java.util.Map) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)

Example 7 with TargetGroupPortPair

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

the class AwsLoadBalancerCommonServiceTest method createCloudLoadBalancer.

private CloudLoadBalancer createCloudLoadBalancer(LoadBalancerType type, List<String> instanceGroupNetworkSubnetIds) {
    Group group = new Group(INSTANCE_NAME, GATEWAY, List.of(), null, null, null, null, null, null, 100, null, createGroupNetwork(instanceGroupNetworkSubnetIds), emptyMap());
    CloudLoadBalancer cloudLoadBalancer = new CloudLoadBalancer(type);
    cloudLoadBalancer.addPortToTargetGroupMapping(new TargetGroupPortPair(PORT, PORT), Set.of(group));
    return cloudLoadBalancer;
}
Also used : AwsTargetGroup(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsTargetGroup) Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair)

Example 8 with TargetGroupPortPair

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

the class AzureLoadBalancerModelBuilderTest method testGetModel.

@Test
void testGetModel() {
    CloudStack mockCloudStack = mock(CloudStack.class);
    Group targetGroup = new Group(INSTANCE_GROUP_NAME, InstanceGroupType.GATEWAY, List.of(new CloudInstance(INSTANCE_NAME, null, null, "subnet-1", "az1")), null, null, null, null, null, 64, null, createGroupNetwork(), emptyMap());
    CloudLoadBalancer cloudLoadBalancer = new CloudLoadBalancer(LoadBalancerType.PRIVATE);
    cloudLoadBalancer.addPortToTargetGroupMapping(new TargetGroupPortPair(443, 443), Set.of(targetGroup));
    when(mockCloudStack.getLoadBalancers()).thenReturn(List.of(cloudLoadBalancer));
    underTest = new AzureLoadBalancerModelBuilder(mockCloudStack, STACK_NAME);
    Map<String, Object> result = underTest.buildModel();
    assertTrue(result.containsKey(LOAD_BALANCERS_KEY));
    assertTrue(result.get(LOAD_BALANCERS_KEY) instanceof List);
    List<AzureLoadBalancer> loadBalancers = (List<AzureLoadBalancer>) result.get(LOAD_BALANCERS_KEY);
    assertEquals(1, loadBalancers.size());
    AzureLoadBalancer lb = loadBalancers.get(0);
    assertEquals(LOAD_BALANCER_STACK_NAME, lb.getName());
    assertEquals(LoadBalancerType.PRIVATE, lb.getType());
    assertTrue(result.containsKey(LOAD_BALANCER_MAPPING_KEY));
    assertTrue(result.get(LOAD_BALANCER_MAPPING_KEY) instanceof Map);
    Map<String, List<AzureLoadBalancer>> mapping = (Map<String, List<AzureLoadBalancer>>) result.get(LOAD_BALANCER_MAPPING_KEY);
    List<AzureLoadBalancer> mappingList = mapping.get(INSTANCE_GROUP_NAME);
    assertEquals(1, mappingList.size());
    assertEquals(LOAD_BALANCER_STACK_NAME, mappingList.get(0).getName());
}
Also used : Group(com.sequenceiq.cloudbreak.cloud.model.Group) CloudInstance(com.sequenceiq.cloudbreak.cloud.model.CloudInstance) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) List(java.util.List) AzureLoadBalancer(com.sequenceiq.cloudbreak.cloud.azure.loadbalancer.AzureLoadBalancer) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 9 with TargetGroupPortPair

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

the class GcpBackendServiceResourceBuilderTest method testCreateWhereEverythingGoesFine.

@Test
public void testCreateWhereEverythingGoesFine() {
    when(gcpContext.getName()).thenReturn("name");
    when(cloudLoadBalancer.getType()).thenReturn(LoadBalancerType.PUBLIC);
    Map<TargetGroupPortPair, Set<Group>> targetGroupPortPairSetHashMap = new HashMap<>();
    targetGroupPortPairSetHashMap.put(new TargetGroupPortPair(80, 8080), Collections.emptySet());
    when(cloudLoadBalancer.getPortToTargetGroupMapping()).thenReturn(targetGroupPortPairSetHashMap);
    List<CloudResource> cloudResources = underTest.create(gcpContext, authenticatedContext, cloudLoadBalancer);
    Assertions.assertTrue(cloudResources.get(0).getName().startsWith("name-public-8080"));
    Assertions.assertEquals(1, cloudResources.size());
    Assertions.assertEquals(8080, cloudResources.get(0).getParameter("hcport", Integer.class));
    Assertions.assertEquals(80, cloudResources.get(0).getParameter("trafficport", Integer.class));
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) Test(org.junit.jupiter.api.Test)

Example 10 with TargetGroupPortPair

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

the class GcpBackendServiceResourceBuilderTest method testBuildWithSeperateHCPort.

@Test
public void testBuildWithSeperateHCPort() throws Exception {
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hcport", 8080);
    parameters.put("trafficport", 80);
    CloudResource hcResource = new CloudResource.Builder().type(ResourceType.GCP_HEALTH_CHECK).status(CommonStatus.CREATED).group("master").name("hcsuper").params(parameters).persistent(true).build();
    CloudResource resource = new CloudResource.Builder().type(ResourceType.GCP_BACKEND_SERVICE).status(CommonStatus.CREATED).group("master").name("super").params(parameters).persistent(true).build();
    when(group.getName()).thenReturn("master");
    Compute.RegionBackendServices.Insert insert = mock(Compute.RegionBackendServices.Insert.class);
    Map<TargetGroupPortPair, Set<Group>> targetGroupPortPairSetHashMap = new HashMap<>();
    targetGroupPortPairSetHashMap.put(new TargetGroupPortPair(80, 8080), Set.of(group));
    when(cloudLoadBalancer.getPortToTargetGroupMapping()).thenReturn(targetGroupPortPairSetHashMap);
    when(cloudLoadBalancer.getType()).thenReturn(LoadBalancerType.PRIVATE);
    when(gcpContext.getCompute()).thenReturn(compute);
    when(gcpContext.getProjectId()).thenReturn("id");
    when(gcpContext.getLoadBalancerResources(any())).thenReturn(List.of(hcResource));
    when(gcpContext.getLocation()).thenReturn(location);
    when(location.getAvailabilityZone()).thenReturn(availabilityZone);
    when(availabilityZone.value()).thenReturn("us-west2");
    when(location.getRegion()).thenReturn(region);
    when(region.getRegionName()).thenReturn("us-west2");
    when(compute.regionBackendServices()).thenReturn(regionBackendServices);
    when(regionBackendServices.insert(anyString(), anyString(), any())).thenReturn(insert);
    when(insert.execute()).thenReturn(operation);
    when(operation.getName()).thenReturn("name");
    when(operation.getHttpErrorStatusCode()).thenReturn(null);
    when(gcpLoadBalancerTypeConverter.getScheme(any(CloudLoadBalancer.class))).thenCallRealMethod();
    List<CloudResource> cloudResources = underTest.build(gcpContext, authenticatedContext, Collections.singletonList(resource), cloudLoadBalancer, cloudStack);
    Assert.assertEquals("super", cloudResources.get(0).getName());
    Assertions.assertEquals(8080, cloudResources.get(0).getParameter("hcport", Integer.class));
    Assertions.assertEquals(80, cloudResources.get(0).getParameter("trafficport", Integer.class));
}
Also used : Set(java.util.Set) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudLoadBalancer(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer) RegionBackendServices(com.google.api.services.compute.Compute.RegionBackendServices) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) Test(org.junit.jupiter.api.Test)

Aggregations

TargetGroupPortPair (com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair)36 CloudLoadBalancer (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer)24 Group (com.sequenceiq.cloudbreak.cloud.model.Group)19 ArrayList (java.util.ArrayList)17 HashMap (java.util.HashMap)14 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 TargetGroup (com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroup)11 Set (java.util.Set)11 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)9 MethodSource (org.junit.jupiter.params.provider.MethodSource)9 AzureCredentialView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView)8 AzureStackView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView)8 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)8 GroupNetwork (com.sequenceiq.cloudbreak.cloud.model.GroupNetwork)8 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)8 Network (com.sequenceiq.cloudbreak.cloud.model.Network)8 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)8 List (java.util.List)7