Search in sources :

Example 6 with ForwardingRule

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

the class GCPLoadBalancerMetadataCollectorTest method testCollectPrivateLoadBalancer.

@Test
public void testCollectPrivateLoadBalancer() {
    List<CloudResource> resources = new ArrayList<>();
    resources.add(createCloudResource(FORWARDING_RULE_NAME_2, ResourceType.GCP_FORWARDING_RULE));
    ForwardingRule privateForwardingRule = createPrivateFowardingRule();
    when(forwardingRuleListResponse.getItems()).thenReturn(List.of(privateForwardingRule));
    List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PRIVATE), resources);
    assertEquals(1, result.size());
    assertEquals(PRIVATE_IP, result.get(0).getIp());
    assertEquals(LoadBalancerType.PRIVATE, result.get(0).getType());
    assertEquals(FORWARDING_RULE_NAME_2, result.get(0).getName());
    assertEquals(FORWARDING_RULE_NAME_2, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.LOADBALANCER_NAME));
    assertEquals(BACKEND_SERVICE_NAME, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.getBackendServiceParam(443)));
    assertEquals(INSTANCE_GROUP_NAME, result.get(0).getStringParameter(GcpLoadBalancerMetadataView.getInstanceGroupParam(443)));
}
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 7 with ForwardingRule

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

the class GCPLoadBalancerMetadataCollectorTest method testCollectOtherType.

@Test
public void testCollectOtherType() {
    List<CloudResource> resources = new ArrayList<>();
    resources.add(createCloudResource(FORWARDING_RULE_NAME_2, ResourceType.GCP_FORWARDING_RULE));
    ForwardingRule privateForwardingRule = createPrivateFowardingRule();
    when(forwardingRuleListResponse.getItems()).thenReturn(List.of(privateForwardingRule));
    List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PUBLIC), resources);
    assertEquals(0, result.size());
}
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 8 with ForwardingRule

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

the class GcpForwardingRuleResourceBuilder method build.

@Override
public List<CloudResource> build(GcpContext context, AuthenticatedContext auth, List<CloudResource> buildableResources, CloudLoadBalancer loadBalancer, CloudStack cloudStack) throws Exception {
    String projectId = context.getProjectId();
    String regionName = context.getLocation().getRegion().getRegionName();
    Network network = cloudStack.getNetwork();
    List<CloudResource> results = new ArrayList<>();
    GcpLoadBalancerScheme scheme = gcpLoadBalancerTypeConverter.getScheme(loadBalancer);
    List<CloudResource> backendResources = filterResourcesByType(context.getLoadBalancerResources(loadBalancer.getType()), ResourceType.GCP_BACKEND_SERVICE);
    List<CloudResource> ipResources = filterResourcesByType(context.getLoadBalancerResources(loadBalancer.getType()), ResourceType.GCP_RESERVED_IP);
    for (CloudResource buildableResource : buildableResources) {
        LOGGER.debug("Building forwarding rule {} for {}", buildableResource.getName(), projectId);
        Map<String, Object> portMap = buildableResource.getParameters();
        ForwardingRule forwardingRule = new ForwardingRule().setIPProtocol(TCP).setName(buildableResource.getName()).setLoadBalancingScheme(scheme.getGcpType());
        if (portMap.containsKey(TRAFFICPORTS)) {
            List<String> ports = Lists.transform((List<Integer>) portMap.get(TRAFFICPORTS), Functions.toStringFunction());
            forwardingRule.setPorts(ports);
        } else {
            Integer trafficPort = buildableResource.getParameter(TRAFFICPORT, Integer.class);
            forwardingRule.setPorts(List.of(String.valueOf(trafficPort)));
        }
        if (scheme.equals(GcpLoadBalancerScheme.INTERNAL)) {
            String sharedProjectId = gcpStackUtil.getSharedProjectId(network);
            if (StringUtils.isEmpty(sharedProjectId)) {
                forwardingRule.setNetwork(gcpStackUtil.getNetworkUrl(projectId, gcpStackUtil.getCustomNetworkId(network)));
                forwardingRule.setSubnetwork(gcpStackUtil.getSubnetUrl(projectId, regionName, gcpStackUtil.getSubnetId(network)));
            } else {
                forwardingRule.setNetwork(gcpStackUtil.getNetworkUrl(sharedProjectId, gcpStackUtil.getCustomNetworkId(network)));
                forwardingRule.setSubnetwork(gcpStackUtil.getSubnetUrl(sharedProjectId, regionName, gcpStackUtil.getSubnetId(network)));
            }
        }
        Integer hcPort = buildableResource.getParameter(HCPORT, Integer.class);
        Optional<String> backendName = backendResources.stream().filter(backendResource -> hcPort.equals(backendResource.getParameter(HCPORT, Integer.class))).findFirst().map(CloudResource::getName);
        if (!backendName.isPresent()) {
            LOGGER.warn("backend not found for forwarding rule {}, port {}, project {}", buildableResource.getName(), hcPort, projectId);
            continue;
        } else {
            forwardingRule.setBackendService(String.format(GCP_BACKEND_SERVICE_REF_FORMAT, projectId, regionName, backendName.get()));
        }
        if (ipResources.isEmpty()) {
            LOGGER.warn("No reserved IP address for loadbalancer {}-{}, using ephemeral", loadBalancer.getType(), projectId);
        } else {
            forwardingRule.setIPAddress(String.format(GCP_IP_REF_FORMAT, projectId, regionName, ipResources.get(0).getName()));
        }
        forwardingRule.setUnknownKeys(new HashMap<>(gcpLabelUtil.createLabelsFromTags(cloudStack)));
        Compute.ForwardingRules.Insert insert = context.getCompute().forwardingRules().insert(projectId, regionName, forwardingRule);
        results.add(doOperationalRequest(buildableResource, insert));
    }
    return results;
}
Also used : ForwardingRule(com.google.api.services.compute.model.ForwardingRule) ArrayList(java.util.ArrayList) Network(com.sequenceiq.cloudbreak.cloud.model.Network) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 9 with ForwardingRule

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

the class GcpForwardingRuleResourceBuilderTest method buildforSharedVPC.

@Test
public void buildforSharedVPC() throws Exception {
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("hcport", 8443);
    parameters.put("trafficports", List.of(443, 11443));
    resource = new CloudResource.Builder().type(ResourceType.GCP_FORWARDING_RULE).status(CommonStatus.CREATED).group("master").name("super").params(parameters).persistent(true).build();
    backendResource = new CloudResource.Builder().type(ResourceType.GCP_BACKEND_SERVICE).status(CommonStatus.CREATED).group("master").name("backendsuper").params(parameters).persistent(true).build();
    mockCalls(LoadBalancerType.PRIVATE);
    when(gcpStackUtil.getSharedProjectId(any())).thenReturn("custom-project");
    List<CloudResource> cloudResources = underTest.build(gcpContext, authenticatedContext, Collections.singletonList(resource), cloudLoadBalancer, cloudStack);
    ForwardingRule arg = forwardingRuleArg.getValue();
    Assert.assertEquals("https://www.googleapis.com/compute/v1/projects/custom-project/global/networks/default-network", arg.getNetwork());
    Assert.assertEquals("https://www.googleapis.com/compute/v1/projects/custom-project/regions/us-west2/subnetworks/default-subnet", arg.getSubnetwork());
    Assert.assertEquals("super", cloudResources.get(0).getName());
    Assertions.assertEquals(8443, cloudResources.get(0).getParameter("hcport", Integer.class));
    List<Integer> ports = (List<Integer>) cloudResources.get(0).getParameters().get("trafficports");
    Assertions.assertTrue(ports.contains(443));
    Assertions.assertTrue(ports.contains(11443));
}
Also used : ForwardingRule(com.google.api.services.compute.model.ForwardingRule) HashMap(java.util.HashMap) List(java.util.List) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Test(org.junit.jupiter.api.Test)

Example 10 with ForwardingRule

use of com.google.api.services.compute.model.ForwardingRule 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)

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