use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata in project cloudbreak by hortonworks.
the class MetadataSetupService method saveLoadBalancerMetadata.
public void saveLoadBalancerMetadata(Stack stack, Iterable<CloudLoadBalancerMetadata> cloudLoadBalancerMetadataList) {
try {
LOGGER.info("Save load balancer metadata for stack: {}", stack.getName());
Set<LoadBalancer> allLoadBalancerMetadata = loadBalancerPersistenceService.findByStackId(stack.getId());
for (CloudLoadBalancerMetadata cloudLoadBalancerMetadata : cloudLoadBalancerMetadataList) {
LoadBalancer loadBalancerEntry = createLoadBalancerMetadataIfAbsent(allLoadBalancerMetadata, stack, cloudLoadBalancerMetadata.getType());
loadBalancerEntry.setDns(cloudLoadBalancerMetadata.getCloudDns());
loadBalancerEntry.setHostedZoneId(cloudLoadBalancerMetadata.getHostedZoneId());
loadBalancerEntry.setIp(cloudLoadBalancerMetadata.getIp());
loadBalancerEntry.setType(cloudLoadBalancerMetadata.getType());
String endpoint = loadBalancerConfigService.generateLoadBalancerEndpoint(stack);
List<StackIdView> byEnvironmentCrnAndStackType = stackService.getByEnvironmentCrnAndStackType(stack.getEnvironmentCrn(), StackType.DATALAKE);
List<StackStatus> stoppedDatalakes = byEnvironmentCrnAndStackType.stream().map(s -> stackStatusService.findFirstByStackIdOrderByCreatedDesc(s.getId())).filter(Optional::isPresent).map(Optional::get).filter(status -> status.getStatus().isStopState()).collect(Collectors.toList());
if (!stoppedDatalakes.isEmpty()) {
/* Starts to check for a situation where we are resizing a datalake that did not previously have loadbalancers
so that we can use the same endpoint name for a seamless transistion
*/
LOGGER.info("Using old datalake endpoint name for resized datalake: {}, env: {}", stack.getName(), stack.getEnvironmentCrn());
if (stoppedDatalakes.size() > 1) {
String ids = stoppedDatalakes.stream().map(stackStatus -> stackStatus.getStack().getId()).map(Object::toString).collect(Collectors.joining(","));
LOGGER.warn("more than one datalake found to resize from: {}", ids);
}
Long oldId = stoppedDatalakes.get(0).getStack().getId();
Set<LoadBalancer> oldLoadbalancers = loadBalancerPersistenceService.findByStackId(oldId);
if (oldLoadbalancers.isEmpty()) {
Stack oldStack = stackService.getByIdWithGatewayInTransaction(oldId);
if (stack.getDisplayName().equals(oldStack.getDisplayName())) {
endpoint = oldStack.getPrimaryGatewayInstance().getShortHostname();
}
}
}
LOGGER.info("Saving load balancer endpoint as: {}", endpoint);
loadBalancerEntry.setEndpoint(endpoint);
loadBalancerEntry.setProviderConfig(loadBalancerConfigConverter.convertLoadBalancer(stack.getCloudPlatform(), cloudLoadBalancerMetadata));
loadBalancerPersistenceService.save(loadBalancerEntry);
Set<TargetGroup> targetGroups = targetGroupPersistenceService.findByLoadBalancerId(loadBalancerEntry.getId());
for (TargetGroup targetGroup : targetGroups) {
targetGroup.setProviderConfig(loadBalancerConfigConverter.convertTargetGroup(stack.getCloudPlatform(), cloudLoadBalancerMetadata, targetGroup));
targetGroupPersistenceService.save(targetGroup);
}
}
} catch (Exception ex) {
throw new CloudbreakServiceException("Load balancer metadata collection failed", ex);
}
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata in project cloudbreak by hortonworks.
the class AzureMetadataCollectorTest method testCollectSinglePublicLoadBalancerWithMultipleIps.
@Test
public void testCollectSinglePublicLoadBalancerWithMultipleIps() {
List<CloudResource> resources = new ArrayList<>();
CloudResource cloudResource = createCloudResource();
when(authenticatedContext.getCloudContext()).thenReturn(mockCloudContext);
when(mockCloudContext.getName()).thenReturn(RESOURCE_GROUP_NAME);
when(azureUtils.getTemplateResource(resources)).thenReturn(cloudResource);
when(azureUtils.getStackName(any())).thenReturn(STACK_NAME);
final String loadBalancerName = AzureLoadBalancer.getLoadBalancerName(LoadBalancerType.PUBLIC, STACK_NAME);
when(azureClient.getLoadBalancerIps(RESOURCE_GROUP_NAME, loadBalancerName, LoadBalancerType.PUBLIC)).thenReturn(List.of(PUBLIC_IP, SECOND_PUBLIC_IP));
when(authenticatedContext.getParameter(AzureClient.class)).thenReturn(azureClient);
when(azureLbMetadataCollector.getParameters(any(), anyString(), anyString())).thenReturn(Map.of());
List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PUBLIC), resources);
// We're retrieving only one Public IP address by design
assertEquals(1, result.size());
assertEquals(LoadBalancerType.PUBLIC, result.get(0).getType());
final String resultIpAddress = result.get(0).getIp();
assertTrue(PUBLIC_IP.equals(resultIpAddress) || SECOND_PUBLIC_IP.equals(resultIpAddress));
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata in project cloudbreak by hortonworks.
the class AzureMetadataCollectorTest method testCollectLoadBalancerWithNoIps.
@Test
public void testCollectLoadBalancerWithNoIps() {
List<CloudResource> resources = new ArrayList<>();
CloudResource cloudResource = createCloudResource();
when(authenticatedContext.getCloudContext()).thenReturn(mockCloudContext);
when(mockCloudContext.getName()).thenReturn(RESOURCE_GROUP_NAME);
when(azureUtils.getTemplateResource(resources)).thenReturn(cloudResource);
when(azureUtils.getStackName(any())).thenReturn(STACK_NAME);
when(authenticatedContext.getParameter(AzureClient.class)).thenReturn(azureClient);
List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PRIVATE, LoadBalancerType.PUBLIC), resources);
assertEquals(0, result.size());
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata in project cloudbreak by hortonworks.
the class AzureMetadataCollectorTest method testCollectLoadBalancerSkipsMetadataWhenRuntimeExceptionIsThrown.
@Test
public void testCollectLoadBalancerSkipsMetadataWhenRuntimeExceptionIsThrown() {
List<CloudResource> resources = new ArrayList<>();
CloudResource cloudResource = createCloudResource();
when(authenticatedContext.getCloudContext()).thenReturn(mockCloudContext);
when(mockCloudContext.getName()).thenReturn(RESOURCE_GROUP_NAME);
when(azureUtils.getTemplateResource(resources)).thenReturn(cloudResource);
when(azureUtils.getStackName(any())).thenReturn(STACK_NAME);
final String privateLoadBalancerName = AzureLoadBalancer.getLoadBalancerName(LoadBalancerType.PRIVATE, STACK_NAME);
when(azureClient.getLoadBalancerIps(RESOURCE_GROUP_NAME, privateLoadBalancerName, LoadBalancerType.PRIVATE)).thenThrow(new RuntimeException());
when(authenticatedContext.getParameter(AzureClient.class)).thenReturn(azureClient);
List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PRIVATE), resources);
assertEquals(0, result.size());
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancerMetadata in project cloudbreak by hortonworks.
the class AzureMetadataCollectorTest method testCollectPrivateLoadBalancerWithMultipleIps.
@Test
public void testCollectPrivateLoadBalancerWithMultipleIps() {
List<CloudResource> resources = new ArrayList<>();
CloudResource cloudResource = createCloudResource();
when(authenticatedContext.getCloudContext()).thenReturn(mockCloudContext);
when(mockCloudContext.getName()).thenReturn(RESOURCE_GROUP_NAME);
when(azureUtils.getTemplateResource(resources)).thenReturn(cloudResource);
when(azureUtils.getStackName(any())).thenReturn(STACK_NAME);
final String privateLoadBalancerName = AzureLoadBalancer.getLoadBalancerName(LoadBalancerType.PRIVATE, STACK_NAME);
when(azureClient.getLoadBalancerIps(RESOURCE_GROUP_NAME, privateLoadBalancerName, LoadBalancerType.PRIVATE)).thenReturn(List.of(PRIVATE_IP, "10.23.12.1"));
when(authenticatedContext.getParameter(AzureClient.class)).thenReturn(azureClient);
when(azureLbMetadataCollector.getParameters(any(), anyString(), anyString())).thenReturn(Map.of());
List<CloudLoadBalancerMetadata> result = underTest.collectLoadBalancer(authenticatedContext, List.of(LoadBalancerType.PRIVATE), resources);
assertEquals(1, result.size());
assertEquals(LoadBalancerType.PRIVATE, result.get(0).getType());
assertEquals(PRIVATE_IP, result.get(0).getIp());
}
Aggregations