Search in sources :

Example 26 with CloudLoadBalancerMetadata

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

the class LoadBalancerConfigConverterTest method testConvertGcpTargetGroup.

@Test
public void testConvertGcpTargetGroup() {
    CloudLoadBalancerMetadata cloudLoadBalancerMetadata = new CloudLoadBalancerMetadata.Builder().withParameters(creatGcpParams(1)).build();
    TargetGroup targetGroup = new TargetGroup();
    targetGroup.setType(TargetGroupType.KNOX);
    TargetGroupPortPair portPair = new TargetGroupPortPair(PORT1, PORT2);
    when(loadBalancerConfigService.getTargetGroupPortPairs(eq(targetGroup))).thenReturn(Set.of(portPair));
    TargetGroupConfigDbWrapper targetGroupConfigDbWrapper = underTest.convertTargetGroup(GCP, cloudLoadBalancerMetadata, targetGroup);
    assertNotNull(targetGroupConfigDbWrapper.getGcpConfig());
    GcpTargetGroupConfigDb gcpTargetGroupConfigDb = targetGroupConfigDbWrapper.getGcpConfig();
    assertEquals(1, gcpTargetGroupConfigDb.getPortMapping().size());
    assertEquals(PORT1, gcpTargetGroupConfigDb.getPortMapping().keySet().iterator().next());
    GcpLoadBalancerNamesDb gcpLoadBalancerNamesDb = gcpTargetGroupConfigDb.getPortMapping().get(PORT1);
    assertEquals(BACKEND_SERVICE_NAME, gcpLoadBalancerNamesDb.getBackendServiceName());
    assertEquals(INSTANCE_GROUP_NAME, gcpLoadBalancerNamesDb.getInstanceGroupName());
}
Also used : GcpLoadBalancerNamesDb(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.gcp.GcpLoadBalancerNamesDb) TargetGroupConfigDbWrapper(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroupConfigDbWrapper) TargetGroup(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroup) GcpTargetGroupConfigDb(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.gcp.GcpTargetGroupConfigDb) TargetGroupPortPair(com.sequenceiq.cloudbreak.cloud.model.TargetGroupPortPair) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) Test(org.junit.Test)

Example 27 with CloudLoadBalancerMetadata

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

the class GCPLoadBalancerMetadataCollectorTest method testCollectSinglePublicLoadBalancer.

@Test
public void testCollectSinglePublicLoadBalancer() {
    List<CloudResource> resources = new ArrayList<>();
    resources.add(createCloudResource(FORWARDING_RULE_NAME_1, ResourceType.GCP_FORWARDING_RULE));
    ForwardingRule publicFowardingRule = createPublicFowardingRule();
    when(forwardingRuleListResponse.getItems()).thenReturn(List.of(publicFowardingRule));
    List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PUBLIC), resources);
    assertEquals(1, result.size());
    assertEquals(PUBLIC_IP, result.get(0).getIp());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getName());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertEquals(LoadBalancerType.PUBLIC, result.get(0).getType());
    assertEquals(BACKEND_SERVICE_NAME, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(8080)));
    assertEquals(INSTANCE_GROUP_NAME, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.getInstanceGroupParam(8080)));
}
Also used : ForwardingRule(com.google.api.services.compute.model.ForwardingRule) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) Test(org.junit.jupiter.api.Test)

Example 28 with CloudLoadBalancerMetadata

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

the class GCPLoadBalancerMetadataCollectorTest method testCollectPublicAndPrivateLoadBalancer.

@Test
public void testCollectPublicAndPrivateLoadBalancer() {
    List<CloudResource> resources = new ArrayList<>();
    resources.add(createCloudResource(FORWARDING_RULE_NAME_1, ResourceType.GCP_FORWARDING_RULE));
    resources.add(createCloudResource(FORWARDING_RULE_NAME_2, ResourceType.GCP_FORWARDING_RULE));
    ForwardingRule publicFowardingRule = createPublicFowardingRule();
    ForwardingRule publicFowardingRule2 = createPrivateFowardingRule();
    when(forwardingRuleListResponse.getItems()).thenReturn(List.of(publicFowardingRule, publicFowardingRule2));
    List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PUBLIC, LoadBalancerType.PRIVATE), resources);
    assertEquals(2, result.size());
    Optional<CloudLoadBalancerMetadata> publicResult = result.stream().filter(p -> p.getType().equals(LoadBalancerType.PUBLIC)).findFirst();
    assertTrue(publicResult.isPresent());
    assertEquals(FORWARDING_RULE_NAME_1, publicResult.get().getName());
    assertEquals(FORWARDING_RULE_NAME_1, publicResult.get().getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertEquals(PUBLIC_IP, publicResult.get().getIp());
    assertEquals(BACKEND_SERVICE_NAME, publicResult.get().getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(8080)));
    assertEquals(INSTANCE_GROUP_NAME, publicResult.get().getStringParameter(GcpLoadBalancerMetadataView.getInstanceGroupParam(8080)));
    Optional<CloudLoadBalancerMetadata> privateResult = result.stream().filter(p -> p.getType().equals(LoadBalancerType.PRIVATE)).findFirst();
    assertTrue(privateResult.isPresent());
    assertEquals(FORWARDING_RULE_NAME_2, privateResult.get().getName());
    assertEquals(FORWARDING_RULE_NAME_2, privateResult.get().getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertEquals(PRIVATE_IP, privateResult.get().getIp());
    assertEquals(BACKEND_SERVICE_NAME, privateResult.get().getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(443)));
    assertEquals(INSTANCE_GROUP_NAME, privateResult.get().getStringParameter(GcpLoadBalancerMetadataView.getInstanceGroupParam(443)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) GcpLoadBalancerTypeConverter(com.sequenceiq.cloudbreak.cloud.gcp.loadbalancer.GcpLoadBalancerTypeConverter) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Region(com.sequenceiq.cloudbreak.cloud.model.Region) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mock(org.mockito.Mock) Backend(com.google.api.services.compute.model.Backend) ForwardingRuleList(com.google.api.services.compute.model.ForwardingRuleList) BackendService(com.google.api.services.compute.model.BackendService) Mockito.lenient(org.mockito.Mockito.lenient) ArrayList(java.util.ArrayList) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) AvailabilityZone(com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) GcpLoadBalancerMetadataView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpLoadBalancerMetadataView) GcpComputeFactory(com.sequenceiq.cloudbreak.cloud.gcp.client.GcpComputeFactory) Location(com.sequenceiq.cloudbreak.cloud.model.Location) GcpStackUtil(com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil) CommonStatus(com.sequenceiq.common.api.type.CommonStatus) InjectMocks(org.mockito.InjectMocks) ResourceType(com.sequenceiq.common.api.type.ResourceType) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) ForwardingRule(com.google.api.services.compute.model.ForwardingRule) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) Test(org.junit.jupiter.api.Test) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Optional(java.util.Optional) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) Collections(java.util.Collections) Compute(com.google.api.services.compute.Compute) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ForwardingRule(com.google.api.services.compute.model.ForwardingRule) ArrayList(java.util.ArrayList) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) Test(org.junit.jupiter.api.Test)

Example 29 with CloudLoadBalancerMetadata

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

the class MetadataSetupServiceTest method saveLoadBalancerMetadata.

@Test
public void saveLoadBalancerMetadata() {
    stack.setName(STACK_NAME);
    stack.setCloudPlatform("DEFAULT");
    stack.setEnvironmentCrn(STACK_CRN);
    LoadBalancer loadBalancer = new LoadBalancer();
    loadBalancer.setStack(stack);
    loadBalancer.setType(LoadBalancerType.PUBLIC);
    Set<LoadBalancer> loadBalancerSet = new HashSet<>();
    loadBalancerSet.add(loadBalancer);
    when(loadBalancerPersistenceService.findByStackId(STACK_ID)).thenReturn(loadBalancerSet);
    when(loadBalancerConfigService.generateLoadBalancerEndpoint(stack)).thenCallRealMethod();
    StackStatus stackStatus = new StackStatus();
    stackStatus.setStatus(Status.AVAILABLE);
    stackStatus.setStack(stack);
    StackIdView stackIdView = new StackIdViewImpl(STACK_ID, STACK_NAME, "no");
    when(stackService.getByEnvironmentCrnAndStackType(STACK_CRN, StackType.DATALAKE)).thenReturn(List.of(stackIdView));
    when(stackStatusService.findFirstByStackIdOrderByCreatedDesc(STACK_ID)).thenReturn(Optional.of(stackStatus));
    when(targetGroupPersistenceService.findByLoadBalancerId(any())).thenReturn(Set.of());
    Iterable<CloudLoadBalancerMetadata> cloudLoadBalancerMetaDataStatuses = getCloudLoadBalancerMetaDataStatuses();
    underTest.saveLoadBalancerMetadata(stack, cloudLoadBalancerMetaDataStatuses);
    verify(loadBalancerPersistenceService).save(loadBalancerCaptor.capture());
    LoadBalancer loadBalancerValue = loadBalancerCaptor.getValue();
    assertThat(loadBalancerValue.getHostedZoneId()).isSameAs(HOSTED_ZONE);
    assertThat(loadBalancerValue.getIp()).isSameAs(PUBLIC_IP);
    assertThat(loadBalancerValue.getDns()).isSameAs(CLOUD_DNS);
    assertThat(loadBalancerValue.getEndpoint()).isEqualTo("STACK_NAME-gateway");
}
Also used : StackIdViewImpl(com.sequenceiq.cloudbreak.service.stack.StackIdViewImpl) StackStatus(com.sequenceiq.cloudbreak.domain.stack.StackStatus) LoadBalancer(com.sequenceiq.cloudbreak.domain.stack.loadbalancer.LoadBalancer) StackIdView(com.sequenceiq.cloudbreak.domain.projection.StackIdView) HashSet(java.util.HashSet) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with CloudLoadBalancerMetadata

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

the class LoadBalancerMetadataHandler method doAccept.

@Override
protected Selectable doAccept(HandlerEvent<LoadBalancerMetadataRequest> event) {
    LoadBalancerMetadataRequest request = event.getData();
    CloudContext cloudContext = request.getCloudContext();
    try {
        LOGGER.info("Fetch cloud load balancer metadata");
        List<CloudLoadBalancerMetadata> loadBalancerStatuses = loadBalancerMetadataService.collectMetadata(cloudContext, request.getCloudCredential(), request.getTypesPresentInStack(), request.getCloudResources());
        Set<CloudLoadBalancerMetadata> failedStatues = loadBalancerStatuses.stream().filter(this::isMissingMetadata).collect(Collectors.toSet());
        if (!failedStatues.isEmpty()) {
            Set<String> names = failedStatues.stream().map(CloudLoadBalancerMetadata::getName).collect(Collectors.toSet());
            throw new CloudbreakException("Creation failed for load balancers: " + names);
        }
        LOGGER.info("Persisting load balancer metadata to the database: {}", loadBalancerStatuses);
        metadataSetupService.saveLoadBalancerMetadata(request.getStack(), loadBalancerStatuses);
        LOGGER.info("Load balancer metadata collection was successful");
        return new LoadBalancerMetadataSuccess(request.getStack());
    } catch (Exception e) {
        LOGGER.warn("Failed to fetch cloud load balancer metadata.", e);
        return new LoadBalancerMetadataFailure(request.getResourceId(), e);
    }
}
Also used : LoadBalancerMetadataFailure(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.LoadBalancerMetadataFailure) LoadBalancerMetadataRequest(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.LoadBalancerMetadataRequest) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) LoadBalancerMetadataSuccess(com.sequenceiq.cloudbreak.reactor.api.event.stack.loadbalancer.LoadBalancerMetadataSuccess)

Aggregations

CloudLoadBalancerMetadata (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata)45 ArrayList (java.util.ArrayList)22 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)20 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)18 Test (org.junit.jupiter.api.Test)17 Test (org.junit.Test)16 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)9 List (java.util.List)9 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)8 Optional (java.util.Optional)8 LoadBalancer (com.amazonaws.services.elasticloadbalancingv2.model.LoadBalancer)7 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.common.view.AwsCredentialView)7 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)7 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)7 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)7 TargetGroup (com.sequenceiq.cloudbreak.domain.stack.loadbalancer.TargetGroup)7 Map (java.util.Map)7 Collectors (java.util.stream.Collectors)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 ForwardingRule (com.google.api.services.compute.model.ForwardingRule)6