use of com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult in project cloudbreak by hortonworks.
the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenLoadBalancerTargetGroupCouldNotBeCreated.
@Test
void testLaunchLoadBalancerResourcesWhenLoadBalancerTargetGroupCouldNotBeCreated() {
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(loadBalancingClient.createTargetGroup(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(1)).notifyAllocation(cloudResourceArgumentCaptor.capture(), any());
assertEquals(ResourceType.ELASTIC_LOAD_BALANCER, cloudResourceArgumentCaptor.getValue().getType());
}
use of com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult 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));
}
use of com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult in project cloudbreak by hortonworks.
the class AwsNativeLoadBalancerLaunchServiceTest method testLaunchLoadBalancerResourcesWhenListenerAlreadyExistsAndLoadBalancerTargetsCouldNotBeRegistered.
@Test
void testLaunchLoadBalancerResourcesWhenListenerAlreadyExistsAndLoadBalancerTargetsCouldNotBeRegistered() {
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());
AmazonServiceException amazonServiceException = new AmazonServiceException(DUPLICATE_LISTENER_ERROR_CODE);
amazonServiceException.setErrorCode(DUPLICATE_LISTENER_ERROR_CODE);
when(loadBalancingClient.registerListener(any())).thenThrow(amazonServiceException);
Listener listener = new Listener().withListenerArn("aListenerArn");
DescribeListenersResult describeListenersResult = new DescribeListenersResult().withListeners(listener);
when(loadBalancingClient.describeListeners(any())).thenReturn(describeListenersResult);
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())));
}
use of com.amazonaws.services.elasticloadbalancingv2.model.CreateLoadBalancerResult in project cloudbreak by hortonworks.
the class AwsNativeLoadBalancerLaunchService method createLoadBalancer.
private void createLoadBalancer(ResourceCreationContext context, AwsLoadBalancer awsLoadBalancer) {
CloudResource loadBalancerResource;
Optional<CloudResource> existingLoadBalancer = persistenceRetriever.retrieveFirstByTypeAndStatusForStack(ResourceType.ELASTIC_LOAD_BALANCER, CommonStatus.CREATED, context.getStackId());
String loadBalancerArn;
if (existingLoadBalancer.isPresent()) {
loadBalancerResource = existingLoadBalancer.get();
loadBalancerArn = loadBalancerResource.getReference();
LOGGER.info("Elastic load balancer resource has already been created for stack proceeding forward with existing resource '{}'", loadBalancerArn);
} else {
AwsLoadBalancerScheme scheme = awsLoadBalancer.getScheme();
Set<String> subnetIds = awsLoadBalancer.getSubnetIds();
String loadBalancerName = resourceNameService.resourceName(ResourceType.ELASTIC_LOAD_BALANCER, context.getStackName(), scheme.resourceName());
LOGGER.info("Creating load balancer with name '{}', subnet ids: '{}' and scheme: '{}'", loadBalancerName, String.join(",", subnetIds), scheme);
CreateLoadBalancerRequest request = new CreateLoadBalancerRequest().withName(loadBalancerName).withSubnets(subnetIds).withScheme(scheme.awsScheme()).withType(LoadBalancerTypeEnum.Network).withIpAddressType(IpAddressType.Ipv4).withTags(context.getTags());
CreateLoadBalancerResult loadBalancerResult = createOrGetLoadBalancer(context, request);
loadBalancerArn = loadBalancerResult.getLoadBalancers().stream().findFirst().orElseThrow().getLoadBalancerArn();
context.setLoadBalancerArn(loadBalancerArn);
loadBalancerResource = new CloudResource.Builder().name(loadBalancerName).type(ResourceType.ELASTIC_LOAD_BALANCER).reference(loadBalancerArn).status(CommonStatus.CREATED).build();
context.getPersistenceNotifier().notifyAllocation(loadBalancerResource, context.getCloudContext());
}
context.setLoadBalancerArn(loadBalancerArn);
CloudResourceStatus cloudResourceStatus = new CloudResourceStatus(loadBalancerResource, ResourceStatus.CREATED);
context.addResourceStatus(cloudResourceStatus);
}
Aggregations