Search in sources :

Example 1 with TargetGroup

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup in project titus-control-plane by Netflix.

the class AwsLoadBalancerConnector method isValid.

@Override
public Completable isValid(String loadBalancerId) {
    final DescribeTargetGroupsRequest request = new DescribeTargetGroupsRequest().withTargetGroupArns(loadBalancerId);
    long startTime = registry.clock().wallTime();
    Single<DescribeTargetGroupsResult> resultSingle = AwsObservableExt.asyncActionSingle(supplier -> getClient(loadBalancerId).describeTargetGroupsAsync(request, supplier.handler()));
    return resultSingle.observeOn(scheduler).doOnError(throwable -> {
        connectorMetrics.failure(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.DescribeTargetGroups, throwable, startTime);
    }).flatMapObservable(result -> {
        connectorMetrics.success(AwsLoadBalancerConnectorMetrics.AwsLoadBalancerMethods.DescribeTargetGroups, startTime);
        return Observable.from(result.getTargetGroups());
    }).flatMap(targetGroup -> {
        if (targetGroup.getTargetType().equals(AWS_IP_TARGET_TYPE)) {
            return Observable.empty();
        }
        return Observable.error(CloudConnectorException.invalidArgument(String.format("Target group %s is NOT of required type %s", targetGroup.getTargetGroupArn(), AWS_IP_TARGET_TYPE)));
    }).toCompletable();
}
Also used : DescribeTargetHealthRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthRequest) Completable(rx.Completable) LoadBalancerConnector(com.netflix.titus.api.connector.cloud.LoadBalancerConnector) DeregisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DeregisterTargetsRequest) RegisterTargetsRequest(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsRequest) CollectionsExt(com.netflix.titus.common.util.CollectionsExt) ProxyType(com.netflix.titus.common.util.guice.ProxyType) LoggerFactory(org.slf4j.LoggerFactory) Singleton(javax.inject.Singleton) Observable(rx.Observable) Inject(javax.inject.Inject) TargetGroupNotFoundException(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroupNotFoundException) Single(rx.Single) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) CloudConnectorException(com.netflix.titus.api.connector.cloud.CloudConnectorException) Schedulers(rx.schedulers.Schedulers) AmazonElasticLoadBalancingAsync(com.amazonaws.services.elasticloadbalancingv2.AmazonElasticLoadBalancingAsync) TargetDescription(com.amazonaws.services.elasticloadbalancingv2.model.TargetDescription) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) Logger(org.slf4j.Logger) DescribeTargetHealthResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetHealthResult) AmazonClientProvider(com.netflix.titus.ext.aws.AmazonClientProvider) Set(java.util.Set) LoadBalancer(com.netflix.titus.api.connector.cloud.LoadBalancer) AwsObservableExt(com.netflix.titus.ext.aws.AwsObservableExt) Scheduler(rx.Scheduler) Collectors(java.util.stream.Collectors) Registry(com.netflix.spectator.api.Registry) ProxyConfiguration(com.netflix.titus.common.util.guice.annotation.ProxyConfiguration) Arn(com.amazonaws.arn.Arn) Collections(java.util.Collections) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult)

Example 2 with TargetGroup

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenTargetGroupAlreadyExistsWithNameAndLoadBalancerListenerCouldNotBeCreated.

@Test
void testLaunchLoadBalancerResourcesWhenTargetGroupAlreadyExistsWithNameAndLoadBalancerListenerCouldNotBeCreated() {
    CloudStack stack = getCloudStack();
    when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
    CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
    when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    AmazonServiceException amazonServiceException = new AmazonServiceException(DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE);
    amazonServiceException.setErrorCode(DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE);
    when(loadBalancingClient.createTargetGroup(any())).thenThrow(amazonServiceException);
    TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
    DescribeTargetGroupsResult describeTargetGroupsResult = new DescribeTargetGroupsResult().withTargetGroups(targetGroup);
    when(loadBalancingClient.describeTargetGroup(any())).thenReturn(describeTargetGroupsResult);
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    when(loadBalancingClient.registerListener(any())).thenThrow(new AmazonElasticLoadBalancingException("something went wrong"));
    Assertions.assertThrows(CloudConnectorException.class, () -> undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true));
    ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(persistenceNotifier, times(2)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collections.emptyList(java.util.Collections.emptyList) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Test(org.junit.jupiter.api.Test) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) NetworkConstants(com.sequenceiq.cloudbreak.common.network.NetworkConstants) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) ArgumentCaptor(org.mockito.ArgumentCaptor) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Network(com.sequenceiq.cloudbreak.cloud.model.Network) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE) Answers(org.mockito.Answers) DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Mockito.times(org.mockito.Mockito.times) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Mockito.when(org.mockito.Mockito.when) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) Assertions(org.junit.jupiter.api.Assertions) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) DUPLICATE_LISTENER_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LISTENER_ERROR_CODE) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) AmazonServiceException(com.amazonaws.AmazonServiceException) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.jupiter.api.Test)

Example 3 with TargetGroup

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenLoadBalancerTargetsCouldNotBeRegistered.

@Test
void testLaunchLoadBalancerResourcesWhenLoadBalancerTargetsCouldNotBeRegistered() {
    CloudStack stack = getCloudStack();
    when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
    CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
    when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
    CreateTargetGroupResult createTargetGroupResult = new CreateTargetGroupResult().withTargetGroups(List.of(targetGroup));
    when(loadBalancingClient.createTargetGroup(any())).thenReturn(createTargetGroupResult);
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    Listener listener = new Listener().withListenerArn("aListenerArn");
    CreateListenerResult createListenerResult = new CreateListenerResult().withListeners(listener);
    when(loadBalancingClient.registerListener(any())).thenReturn(createListenerResult);
    when(loadBalancingClient.registerTargets(any())).thenThrow(new AmazonElasticLoadBalancingException("something went wrong"));
    Assertions.assertThrows(CloudConnectorException.class, () -> undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true));
    ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(loadBalancingClient, times(1)).registerTargets(any());
    verify(persistenceNotifier, times(3)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_LISTENER.equals(cloudResource.getType())));
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collections.emptyList(java.util.Collections.emptyList) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Test(org.junit.jupiter.api.Test) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) NetworkConstants(com.sequenceiq.cloudbreak.common.network.NetworkConstants) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) ArgumentCaptor(org.mockito.ArgumentCaptor) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Network(com.sequenceiq.cloudbreak.cloud.model.Network) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE) Answers(org.mockito.Answers) DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Mockito.times(org.mockito.Mockito.times) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Mockito.when(org.mockito.Mockito.when) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) Assertions(org.junit.jupiter.api.Assertions) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) DUPLICATE_LISTENER_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LISTENER_ERROR_CODE) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.jupiter.api.Test)

Example 4 with TargetGroup

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenLoadBalancerAndTargetGroupAndListenerResourcesExistAlready.

@Test
void testLaunchLoadBalancerResourcesWhenLoadBalancerAndTargetGroupAndListenerResourcesExistAlready() {
    CloudStack stack = getCloudStack();
    when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
    CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
    when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
    String aLoadBalancerTGName = "aLoadBalancerTGName";
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn(aLoadBalancerTGName);
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
    CreateTargetGroupResult createTargetGroupResult = new CreateTargetGroupResult().withTargetGroups(List.of(targetGroup));
    when(loadBalancingClient.createTargetGroup(any())).thenReturn(createTargetGroupResult);
    CloudResource listenerResource = CloudResource.builder().name(aLoadBalancerTGName).type(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER).reference("aListenerArn").build();
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.of(listenerResource));
    when(loadBalancingClient.registerTargets(any())).thenReturn(new RegisterTargetsResult());
    List<CloudResourceStatus> statuses = undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true);
    ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(persistenceNotifier, times(2)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
    verify(loadBalancingClient, times(1)).registerTargets(any());
    verify(loadBalancingClient, times(0)).registerListener(any());
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
    assertTrue(resourceExistsWithTypeAndStatus(statuses, ResourceType.ELASTIC_LOAD_BALANCER, ResourceStatus.CREATED));
    assertTrue(resourceExistsWithTypeAndStatus(statuses, ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP, ResourceStatus.CREATED));
    assertTrue(resourceExistsWithTypeAndStatus(statuses, ResourceType.ELASTIC_LOAD_BALANCER_LISTENER, ResourceStatus.CREATED));
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collections.emptyList(java.util.Collections.emptyList) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Test(org.junit.jupiter.api.Test) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) NetworkConstants(com.sequenceiq.cloudbreak.common.network.NetworkConstants) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) ArgumentCaptor(org.mockito.ArgumentCaptor) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Network(com.sequenceiq.cloudbreak.cloud.model.Network) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE) Answers(org.mockito.Answers) DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Mockito.times(org.mockito.Mockito.times) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Mockito.when(org.mockito.Mockito.when) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) Assertions(org.junit.jupiter.api.Assertions) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) DUPLICATE_LISTENER_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LISTENER_ERROR_CODE) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Test(org.junit.jupiter.api.Test)

Example 5 with TargetGroup

use of com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup in project cloudbreak by hortonworks.

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenLoadBalancerListenerCouldNotBeCreated.

@Test
void testLaunchLoadBalancerResourcesWhenLoadBalancerListenerCouldNotBeCreated() {
    CloudStack stack = getCloudStack();
    when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER), any(), any())).thenReturn("aLoadBalancerName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    LoadBalancer loadBalancer = new LoadBalancer().withLoadBalancerArn("anARN");
    CreateLoadBalancerResult loadBalancerResult = new CreateLoadBalancerResult().withLoadBalancers(loadBalancer);
    when(loadBalancingClient.registerLoadBalancer(any())).thenReturn(loadBalancerResult);
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    TargetGroup targetGroup = new TargetGroup().withTargetGroupArn("aTargetGroupArn");
    CreateTargetGroupResult createTargetGroupResult = new CreateTargetGroupResult().withTargetGroups(List.of(targetGroup));
    when(loadBalancingClient.createTargetGroup(any())).thenReturn(createTargetGroupResult);
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.empty());
    when(loadBalancingClient.registerListener(any())).thenThrow(new AmazonElasticLoadBalancingException("something went wrong"));
    Assertions.assertThrows(CloudConnectorException.class, () -> undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true));
    ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(persistenceNotifier, times(2)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP.equals(cloudResource.getType())));
}
Also used : AwsResourceNameService(com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) DescribeListenersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Map(java.util.Map) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) ResourceStatus(com.sequenceiq.cloudbreak.cloud.model.ResourceStatus) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) AmazonServiceException(com.amazonaws.AmazonServiceException) Collections.emptyList(java.util.Collections.emptyList) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) Test(org.junit.jupiter.api.Test) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) List(java.util.List) Tag(com.amazonaws.services.elasticloadbalancingv2.model.Tag) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) NetworkConstants(com.sequenceiq.cloudbreak.common.network.NetworkConstants) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) Mock(org.mockito.Mock) AmazonElasticLoadBalancingClient(com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult) ArrayList(java.util.ArrayList) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) PersistenceRetriever(com.sequenceiq.cloudbreak.cloud.notification.PersistenceRetriever) ArgumentCaptor(org.mockito.ArgumentCaptor) AwsLoadBalancerCommonService(com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) AwsLoadBalancerScheme(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme) Network(com.sequenceiq.cloudbreak.cloud.model.Network) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LOAD_BALANCER_NAME_ERROR_CODE) Answers(org.mockito.Answers) DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_TARGET_GROUP_NAME_ERROR_CODE) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Mockito.times(org.mockito.Mockito.times) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Mockito.when(org.mockito.Mockito.when) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) Assertions(org.junit.jupiter.api.Assertions) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) DUPLICATE_LISTENER_ERROR_CODE(com.sequenceiq.cloudbreak.cloud.aws.resource.loadbalancer.AwsNativeLoadBalancerLaunchService.DUPLICATE_LISTENER_ERROR_CODE) AwsTaggingService(com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService) CreateLoadBalancerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) AmazonElasticLoadBalancingException(com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) Test(org.junit.jupiter.api.Test)

Aggregations

CreateTargetGroupResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult)11 DescribeTargetGroupsResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult)11 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)11 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)11 AmazonServiceException (com.amazonaws.AmazonServiceException)10 CreateListenerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult)10 CreateLoadBalancerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult)10 DescribeListenersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult)10 DescribeLoadBalancersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult)10 Tag (com.amazonaws.services.elasticloadbalancingv2.model.Tag)10 AwsTaggingService (com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService)10 AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)10 AwsLoadBalancerCommonService (com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService)10 AwsLoadBalancer (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)10 AwsLoadBalancerScheme (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)10 AwsResourceNameService (com.sequenceiq.cloudbreak.cloud.aws.common.service.AwsResourceNameService)10 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)10 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)10 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)10 ResourceStatus (com.sequenceiq.cloudbreak.cloud.model.ResourceStatus)10