use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class AwsUpscaleServiceTest method upscaleWithLoadBalancers.
@Test
void upscaleWithLoadBalancers() {
AmazonAutoScalingClient amazonAutoScalingClient = mock(AmazonAutoScalingClient.class);
AmazonCloudFormationClient amazonCloudFormationClient = mock(AmazonCloudFormationClient.class);
when(amazonAutoScalingClient.describeAutoScalingGroups(any(DescribeAutoScalingGroupsRequest.class))).thenReturn(new DescribeAutoScalingGroupsResult().withAutoScalingGroups(newAutoScalingGroup("masterASG", List.of("i-master1", "i-master2")), newAutoScalingGroup("workerASG", List.of("i-worker1", "i-worker2", "i-worker3"))));
when(awsClient.createAutoScalingClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonAutoScalingClient);
when(awsClient.createCloudFormationClient(any(AwsCredentialView.class), anyString())).thenReturn(amazonCloudFormationClient);
when(awsClient.createEc2Client(any(), any())).thenReturn(mock(AmazonEc2Client.class));
when(cfStackUtil.getAutoscalingGroupName(any(AuthenticatedContext.class), any(AmazonCloudFormationClient.class), eq("worker"))).thenReturn("workerASG");
CloudContext cloudContext = CloudContext.Builder.builder().withId(1L).withName("teststack").withCrn("crn").withPlatform("AWS").withVariant("AWS").withLocation(Location.location(Region.region("eu-west-1"), AvailabilityZone.availabilityZone("eu-west-1a"))).withAccountId("1").build();
AuthenticatedContext authenticatedContext = new AuthenticatedContext(cloudContext, new CloudCredential());
List<CloudResource> allInstances = List.of(newInstanceResource("worker1", "worker", "i-worker1"), newInstanceResource("worker2", "worker", "i-worker2"), newInstanceResource("worker3", "worker", "i-worker3"), newInstanceResource("worker4", "worker", "i-worker4"), newInstanceResource("worker5", "worker", "i-worker5"));
when(cfStackUtil.getInstanceCloudResources(eq(authenticatedContext), eq(amazonCloudFormationClient), eq(amazonAutoScalingClient), anyList())).thenReturn(allInstances);
doNothing().when(cfStackUtil).addLoadBalancerTargets(any(), any(), any());
InstanceAuthentication instanceAuthentication = new InstanceAuthentication("sshkey", "", "cloudbreak");
List<Group> groups = new ArrayList<>();
List<CloudLoadBalancer> loadBalancers = List.of(new CloudLoadBalancer(LoadBalancerType.PRIVATE), new CloudLoadBalancer(LoadBalancerType.PUBLIC));
Group master = getMasterGroup(instanceAuthentication);
groups.add(master);
Group worker = getWorkerGroup(instanceAuthentication);
groups.add(worker);
Map<String, String> tags = new HashMap<>();
tags.put("owner", "cbuser");
tags.put("created", "yesterday");
CloudStack cloudStack = new CloudStack(groups, getNetwork(), null, emptyMap(), tags, null, instanceAuthentication, instanceAuthentication.getLoginUserName(), instanceAuthentication.getPublicKey(), null, loadBalancers);
List<CloudResource> cloudResourceList = Collections.emptyList();
AdjustmentTypeWithThreshold adjustmentTypeWithThreshold = new AdjustmentTypeWithThreshold(AdjustmentType.EXACT, 0L);
awsUpscaleService.upscale(authenticatedContext, cloudStack, cloudResourceList, adjustmentTypeWithThreshold);
verify(cfStackUtil, times(2)).addLoadBalancerTargets(any(), any(), any());
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class AwsLoadBalancerCommonService method getAwsLoadBalancers.
public List<AwsLoadBalancer> getAwsLoadBalancers(List<CloudLoadBalancer> cloudLoadBalancers, Map<String, List<String>> instanceIdsByGroupName, AwsNetworkView awsNetworkView) {
LOGGER.debug("Converting internal load balancer model to AWS cloud provider model.");
List<AwsLoadBalancer> awsLoadBalancers = new ArrayList<>();
for (CloudLoadBalancer cloudLoadBalancer : cloudLoadBalancers) {
LOGGER.debug("Found load balancer model of type {}", cloudLoadBalancer.getType());
AwsLoadBalancer loadBalancer = convertLoadBalancer(cloudLoadBalancer, instanceIdsByGroupName, awsNetworkView, awsLoadBalancers);
if (loadBalancer != null && !awsLoadBalancers.contains(loadBalancer)) {
awsLoadBalancers.add(loadBalancer);
}
}
Set<String> requestedTypes = cloudLoadBalancers.stream().map(lb -> lb.getType().name()).collect(Collectors.toSet());
Set<String> awsTypes = awsLoadBalancers.stream().map(lb -> AwsLoadBalancerScheme.INTERNAL.awsScheme().equals(lb.getAwsScheme()) ? "PRIVATE" : "PUBLIC").collect(Collectors.toSet());
if (!requestedTypes.equals(awsTypes)) {
throw new CloudConnectorException(String.format("Can not create all requested AWS load balancers. " + "Types requested: [%s]; type to be created: [%s]", requestedTypes, awsTypes));
}
return awsLoadBalancers;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class AwsLoadBalancerCommonService method setupLoadBalancer.
private void setupLoadBalancer(CloudLoadBalancer cloudLoadBalancer, Map<String, List<String>> instanceIdsByGroupName, AwsLoadBalancer awsLoadBalancer) {
LOGGER.debug("Configuring target instances for listeners.");
for (Map.Entry<TargetGroupPortPair, Set<Group>> entry : cloudLoadBalancer.getPortToTargetGroupMapping().entrySet()) {
AwsListener listener = awsLoadBalancer.getOrCreateListener(entry.getKey().getTrafficPort(), entry.getKey().getHealthCheckPort());
Set<String> instanceIds = entry.getValue().stream().flatMap(tg -> instanceIdsByGroupName.get(tg.getName()).stream()).collect(Collectors.toSet());
LOGGER.debug(String.format("Adding instances %s to listener %s", instanceIds, listener.getName()));
listener.addInstancesToTargetGroup(instanceIds);
}
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class AwsLaunchServiceLoadBalancerTest method createCloudLoadBalancer.
private CloudLoadBalancer createCloudLoadBalancer(LoadBalancerType type) {
Group group = new Group(INSTANCE_NAME, GATEWAY, List.of(), null, null, null, null, null, null, 100, null, createGroupNetwork(), emptyMap());
CloudLoadBalancer cloudLoadBalancer = new CloudLoadBalancer(type);
cloudLoadBalancer.addPortToTargetGroupMapping(new TargetGroupPortPair(PORT, PORT), Set.of(group));
return cloudLoadBalancer;
}
use of com.sequenceiq.cloudbreak.cloud.model.CloudLoadBalancer in project cloudbreak by hortonworks.
the class AwsLoadBalancerLaunchService method updateCloudformationWithLoadBalancers.
public List<CloudResourceStatus> updateCloudformationWithLoadBalancers(AuthenticatedContext ac, CloudStack stack, PersistenceNotifier resourceNotifier, ModelContext modelContext) {
List<CloudResourceStatus> statuses = new ArrayList<>();
List<CloudLoadBalancer> cloudLoadBalancers = stack.getLoadBalancers();
String cFStackName = cfStackUtil.getCfStackName(ac);
if (!cloudLoadBalancers.isEmpty()) {
LOGGER.debug("Creating load balancers of types " + cloudLoadBalancers.stream().map(CloudLoadBalancer::getType).map(LoadBalancerType::name).collect(Collectors.joining(",")));
AwsCredentialView credentialView = new AwsCredentialView(ac.getCloudCredential());
String regionName = ac.getCloudContext().getLocation().getRegion().value();
AmazonCloudFormationClient cfRetryClient = awsClient.createCloudFormationClient(credentialView, regionName);
AmazonAutoScalingClient amazonASClient = awsClient.createAutoScalingClient(credentialView, regionName);
List<CloudResource> instances = cfStackUtil.getInstanceCloudResources(ac, cfRetryClient, amazonASClient, stack.getGroups());
Network network = stack.getNetwork();
AwsNetworkView awsNetworkView = new AwsNetworkView(network);
if (modelContext == null) {
LOGGER.debug("Creating default model context");
modelContext = awsModelService.buildDefaultModelContext(ac, stack, resourceNotifier);
}
Map<String, List<String>> instanceIdsByGroupName = instances.stream().collect(Collectors.groupingBy(CloudResource::getGroup, mapping(CloudResource::getInstanceId, toList())));
List<AwsLoadBalancer> awsLoadBalancers = loadBalancerCommonService.getAwsLoadBalancers(cloudLoadBalancers, instanceIdsByGroupName, awsNetworkView);
modelContext.withLoadBalancers(awsLoadBalancers);
LOGGER.debug("Starting CloudFormation update to create load balancer and target groups.");
ListStackResourcesResult result;
if (checkForLoadBalancerAndTargetGroupResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
LOGGER.debug("Load balancer and target group resources already exist, skipping creation");
result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
} else {
result = updateCloudFormationStack(ac, stack, modelContext);
}
setLoadBalancerMetadata(awsLoadBalancers, result);
LOGGER.debug("Starting CloudFormation update to create listeners.");
if (checkForListenerResources(cfRetryClient, cFStackName, awsLoadBalancers)) {
LOGGER.debug("Listener resources already exist, skipping creation");
result = cfRetryClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
} else {
result = updateCloudFormationStack(ac, stack, modelContext);
}
ListStackResourcesResult finalResult = result;
awsLoadBalancers.forEach(lb -> statuses.add(createLoadBalancerStatus(ac, lb, finalResult)));
} else {
LOGGER.debug("No load balancers in stack");
}
return statuses;
}
Aggregations