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