Search in sources :

Example 1 with CreateListenerResult

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

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenTargetGroupResourcesExistAlready.

@Test
void testLaunchLoadBalancerResourcesWhenTargetGroupResourcesExistAlready() {
    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);
    CloudResource targetGroupResource = CloudResource.builder().name("aTargetTG443Master").type(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP).reference("aTargetGroupArn").build();
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), eq(CommonStatus.CREATED), any())).thenReturn(Optional.of(targetGroupResource));
    when(resourceNameService.resourceName(eq(ResourceType.ELASTIC_LOAD_BALANCER_TARGET_GROUP), any(), any(), any())).thenReturn("aLoadBalancerTGName");
    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())).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)).createTargetGroup(any());
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER.equals(cloudResource.getType())));
    assertTrue(cloudResourceArgumentCaptor.getAllValues().stream().anyMatch(cloudResource -> ResourceType.ELASTIC_LOAD_BALANCER_LISTENER.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) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) 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) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) Test(org.junit.jupiter.api.Test)

Example 2 with CreateListenerResult

use of com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult 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 3 with CreateListenerResult

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

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenAllResourcesCouldBeCreated.

@Test
void testLaunchLoadBalancerResourcesWhenAllResourcesCouldBeCreated() {
    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())).thenReturn(new RegisterTargetsResult());
    List<CloudResourceStatus> statuses = undertTest.launchLoadBalancerResources(authenticatedContext, stack, persistenceNotifier, loadBalancingClient, true);
    ArgumentCaptor<CloudResource> cloudResourceArgumentCaptor = ArgumentCaptor.forClass(CloudResource.class);
    verify(persistenceNotifier, times(3)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
    verify(loadBalancingClient, times(1)).registerTargets(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())));
    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) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) LoadBalancer(com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer) AwsLoadBalancer(com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) CloudStack(com.sequenceiq.cloudbreak.cloud.model.CloudStack) RegisterTargetsResult(com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 4 with CreateListenerResult

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

the class AwsNativeLoadBalancerLaunchService method createListener.

private void createListener(ResourceCreationContext context, AwsListener listener) {
    CloudResource listenerResource;
    Optional<CloudResource> existingLoadBalancer = persistenceRetriever.retrieveFirstByTypeAndStatusForStack(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER, CommonStatus.CREATED, context.getStackId());
    if (existingLoadBalancer.isPresent()) {
        listenerResource = existingLoadBalancer.get();
        LOGGER.info("Elastic load balancer listener resource has already been created for stack proceeding forward with existing resource '{}'", listenerResource.getReference());
    } else {
        String loadBalancerArn = context.getLoadBalancerArn();
        String targetGroupArn = context.getTargetGroupArn();
        LOGGER.info("Creating listener for load balancer('{}') on target group('{}')", loadBalancerArn, targetGroupArn);
        Action defaultAction = new Action().withType(ActionTypeEnum.Forward).withOrder(1).withTargetGroupArn(targetGroupArn);
        CreateListenerRequest listenerRequest = new CreateListenerRequest().withLoadBalancerArn(loadBalancerArn).withProtocol(ProtocolEnum.TCP).withPort(listener.getPort()).withDefaultActions(defaultAction).withTags(context.getTags());
        CreateListenerResult registerListenerResult = createOrGetListener(context, listenerRequest);
        String listenerArn = registerListenerResult.getListeners().stream().findFirst().orElseThrow().getListenerArn();
        listenerResource = new CloudResource.Builder().name(context.getTargetGroupName()).type(ResourceType.ELASTIC_LOAD_BALANCER_LISTENER).reference(listenerArn).status(CommonStatus.CREATED).instanceId(loadBalancerArn).build();
        context.getPersistenceNotifier().notifyAllocation(listenerResource, context.getCloudContext());
    }
    CloudResourceStatus cloudResourceStatus = new CloudResourceStatus(listenerResource, ResourceStatus.CREATED);
    context.addResourceStatus(cloudResourceStatus);
}
Also used : Action(com.amazonaws.services.elasticloadbalancingv2.model.Action) CreateListenerRequest(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerRequest) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 5 with CreateListenerResult

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

the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenLoadBalancerResourceExistsAlready.

@Test
void testLaunchLoadBalancerResourcesWhenLoadBalancerResourceExistsAlready() {
    CloudStack stack = getCloudStack();
    when(loadBalancerCommonService.getAwsLoadBalancers(any(), any(), any())).thenReturn(List.of(getAwsLoadBalancer()));
    String aLoadBalancerName = "aLoadBalancerName";
    CloudResource loadBalancerResource = CloudResource.builder().name(aLoadBalancerName).reference("aLoadBalancerArn").type(ResourceType.ELASTIC_LOAD_BALANCER).build();
    when(persistenceRetriever.retrieveFirstByTypeAndStatusForStack(eq(ResourceType.ELASTIC_LOAD_BALANCER), eq(CommonStatus.CREATED), any())).thenReturn(Optional.of(loadBalancerResource));
    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())).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)).registerLoadBalancer(any());
    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())));
    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) CreateTargetGroupResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult) Listener(com.amazonaws.services.elasticloadbalancingv2.model.Listener) CloudResourceStatus(com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus) TargetGroup(com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup) CreateListenerResult(com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult) 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)

Aggregations

CreateListenerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateListenerResult)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 AmazonElasticLoadBalancingException (com.amazonaws.services.elasticloadbalancingv2.model.AmazonElasticLoadBalancingException)4 CreateLoadBalancerResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult)4 CreateTargetGroupResult (com.amazonaws.services.elasticloadbalancingv2.model.CreateTargetGroupResult)4 DescribeListenersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeListenersResult)4 DescribeLoadBalancersResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeLoadBalancersResult)4 DescribeTargetGroupsResult (com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult)4 Listener (com.amazonaws.services.elasticloadbalancingv2.model.Listener)4 LoadBalancer (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer)4 RegisterTargetsResult (com.amazonaws.services.elasticloadbalancingv2.model.RegisterTargetsResult)4 Tag (com.amazonaws.services.elasticloadbalancingv2.model.Tag)4 TargetGroup (com.amazonaws.services.elasticloadbalancingv2.model.TargetGroup)4 AwsTaggingService (com.sequenceiq.cloudbreak.cloud.aws.common.AwsTaggingService)4 AmazonElasticLoadBalancingClient (com.sequenceiq.cloudbreak.cloud.aws.common.client.AmazonElasticLoadBalancingClient)4 AwsLoadBalancerCommonService (com.sequenceiq.cloudbreak.cloud.aws.common.connector.resource.AwsLoadBalancerCommonService)4 AwsLoadBalancer (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer)4 AwsLoadBalancerScheme (com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancerScheme)4