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;
}
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)));
}
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;
}
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)));
}
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)));
}
Aggregations