Search in sources :

Example 1 with ForwardingRule

use of com.google.api.services.compute.model.ForwardingRule in project cloudbreak by hortonworks.

the class GcpMetadataCollector method collectLoadBalancer.

@Override
public List<CloudLoadBalancerMetadata> collectLoadBalancer(AuthenticatedContext ac, List<LoadBalancerType> loadBalancerTypes, List<CloudResource> resources) {
    CloudCredential credential = ac.getCloudCredential();
    Compute compute = gcpComputeFactory.buildCompute(credential);
    String projectId = gcpStackUtil.getProjectId(credential);
    String region = ac.getCloudContext().getLocation().getRegion().getRegionName();
    List<CloudLoadBalancerMetadata> results = new ArrayList<>();
    Set<String> names = resources.stream().filter(resource -> resource.getType().equals(ResourceType.GCP_FORWARDING_RULE)).map(CloudResource::getName).collect(Collectors.toSet());
    try {
        ForwardingRuleList forwardingRuleList = compute.forwardingRules().list(projectId, region).execute();
        if (forwardingRuleList.getWarning() != null) {
            LOGGER.warn("Warning fetching GCP loadbalancer metadata, {}", forwardingRuleList.getWarning().getMessage());
        }
        for (ForwardingRule item : forwardingRuleList.getItems()) {
            LoadBalancerType itemType = gcpLoadBalancerTypeConverter.getScheme(item.getLoadBalancingScheme()).getCbType();
            if (names.contains(item.getName()) && loadBalancerTypes.contains(itemType)) {
                Map<String, Object> params = getParams(compute, projectId, item);
                CloudLoadBalancerMetadata loadBalancerMetadata = new CloudLoadBalancerMetadata.Builder().withType(itemType).withIp(item.getIPAddress()).withName(item.getName()).withParameters(params).build();
                results.add(loadBalancerMetadata);
            }
        }
    } catch (RuntimeException | IOException e) {
        LOGGER.error("Couldn't collect GCP LB metadata for {} ", projectId, e);
    }
    // no-op
    return results;
}
Also used : ForwardingRule(com.google.api.services.compute.model.ForwardingRule) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CloudLoadBalancerMetadata(com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata) LoadBalancerType(com.sequenceiq.common.api.type.LoadBalancerType) ForwardingRuleList(com.google.api.services.compute.model.ForwardingRuleList) Compute(com.google.api.services.compute.Compute)

Example 2 with ForwardingRule

use of com.google.api.services.compute.model.ForwardingRule in project cloudbreak by hortonworks.

the class GCPLoadBalancerMetadataCollectorTest method testNullPortInformation.

@Test
public void testNullPortInformation() {
    List<CloudResource> resources = new ArrayList<>();
    resources.add(createCloudResource(FORWARDING_RULE_NAME_1, ResourceType.GCP_FORWARDING_RULE));
    ForwardingRule publicFowardingRule = createPublicFowardingRule();
    publicFowardingRule.setPorts(null);
    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(LoadBalancerType.PUBLIC, result.get(0).getType());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getName());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertNull(result.get(0).getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(8080)));
    assertNull(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 3 with ForwardingRule

use of com.google.api.services.compute.model.ForwardingRule in project cloudbreak by hortonworks.

the class GCPLoadBalancerMetadataCollectorTest method createPrivateFowardingRule.

private ForwardingRule createPrivateFowardingRule() {
    ForwardingRule forwardingRule = new ForwardingRule();
    forwardingRule.setLoadBalancingScheme("INTERNAL");
    forwardingRule.setName(FORWARDING_RULE_NAME_2);
    forwardingRule.setIPAddress(PRIVATE_IP);
    forwardingRule.setPorts(List.of(PORT2));
    forwardingRule.setBackendService(BACKEND_SERVICE_NAME);
    return forwardingRule;
}
Also used : ForwardingRule(com.google.api.services.compute.model.ForwardingRule)

Example 4 with ForwardingRule

use of com.google.api.services.compute.model.ForwardingRule in project cloudbreak by hortonworks.

the class GCPLoadBalancerMetadataCollectorTest method testCollectLoadBalancerWithNoIps.

@Test
public void testCollectLoadBalancerWithNoIps() {
    List<CloudResource> resources = new ArrayList<>();
    resources.add(createCloudResource(FORWARDING_RULE_NAME_1, ResourceType.GCP_FORWARDING_RULE));
    ForwardingRule forwardingRule = new ForwardingRule();
    forwardingRule.setLoadBalancingScheme("EXTERNAL");
    forwardingRule.setName(FORWARDING_RULE_NAME_1);
    when(forwardingRuleListResponse.getItems()).thenReturn(List.of(forwardingRule));
    List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PUBLIC), resources);
    assertEquals(1, result.size());
    assertEquals(null, result.get(0).getIp());
    assertEquals(LoadBalancerType.PUBLIC, result.get(0).getType());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getName());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertNull(result.get(0).getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(8080)));
    assertNull(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 5 with ForwardingRule

use of com.google.api.services.compute.model.ForwardingRule in project cloudbreak by hortonworks.

the class GCPLoadBalancerMetadataCollectorTest method testCollectSinglePublicLoadBalancerWithMultipleForwardingRules.

@Test
public void testCollectSinglePublicLoadBalancerWithMultipleForwardingRules() {
    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 = createPublicFowardingRule();
    publicFowardingRule2.setIPAddress(PUBLIC_IP2);
    publicFowardingRule2.setName(FORWARDING_RULE_NAME_2);
    when(forwardingRuleListResponse.getItems()).thenReturn(List.of(publicFowardingRule, publicFowardingRule2));
    List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PUBLIC), resources);
    assertEquals(2, result.size());
    assertEquals(PUBLIC_IP, result.get(0).getIp());
    assertEquals(PUBLIC_IP2, result.get(1).getIp());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getName());
    assertEquals(FORWARDING_RULE_NAME_1, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertEquals(FORWARDING_RULE_NAME_2, result.get(1).getName());
    assertEquals(FORWARDING_RULE_NAME_2, result.get(1).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)));
    assertEquals(BACKEND_SERVICE_NAME, result.get(1).getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(8080)));
    assertEquals(INSTANCE_GROUP_NAME, result.get(1).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)

Aggregations

ForwardingRule (com.google.api.services.compute.model.ForwardingRule)14 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)11 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)9 CloudLoadBalancerMetadata (com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata)8 Compute (com.google.api.services.compute.Compute)2 ForwardingRuleList (com.google.api.services.compute.model.ForwardingRuleList)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 LoadBalancerType (com.sequenceiq.common.api.type.LoadBalancerType)2 IOException (java.io.IOException)2 List (java.util.List)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Backend (com.google.api.services.compute.model.Backend)1 BackendService (com.google.api.services.compute.model.BackendService)1 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 GcpComputeFactory (com.sequenceiq.cloudbreak.cloud.gcp.client.GcpComputeFactory)1 GcpLoadBalancerTypeConverter (com.sequenceiq.cloudbreak.cloud.gcp.loadbalancer.GcpLoadBalancerTypeConverter)1 GcpStackUtil (com.sequenceiq.cloudbreak.cloud.gcp.util.GcpStackUtil)1 GcpLoadBalancerMetadataView (com.sequenceiq.cloudbreak.cloud.gcp.view.GcpLoadBalancerMetadataView)1