use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.
the class CloudFormationTemplateBuilderTest method buildTestWithMultipleLoadBalancers.
@Test
public void buildTestWithMultipleLoadBalancers() {
// GIVEN
List<AwsLoadBalancer> awsLoadBalancers = List.of(setupLoadBalancer(AwsLoadBalancerScheme.INTERNAL, 443, true), setupLoadBalancer(AwsLoadBalancerScheme.INTERNET_FACING, 888, true));
// WHEN
modelContext = new ModelContext().withAuthenticatedContext(authenticatedContext).withStack(cloudStack).withExistingVpc(true).withExistingIGW(true).withExistingSubnetCidr(singletonList(existingSubnetCidr)).withExistinVpcCidr(List.of(existingSubnetCidr)).mapPublicIpOnLaunch(true).withEnableInstanceProfile(true).withInstanceProfileAvailable(true).withOutboundInternetTraffic(OutboundInternetTraffic.ENABLED).withTemplate(awsCloudFormationTemplate).withLoadBalancers(awsLoadBalancers);
String templateString = cloudFormationTemplateBuilder.build(modelContext);
// THEN
Assertions.assertThat(templateString).matches(JsonUtil::isValid, "Invalid JSON: " + templateString).contains("\"LoadBalancerInternal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::LoadBalancer\"").contains("\"Scheme\" : \"internal\"").contains("\"TargetGroupPort443Internal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::TargetGroup\"").contains("\"ListenerPort443Internal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::Listener\"").contains("\"LoadBalancerExternal\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::LoadBalancer\"").contains("\"Scheme\" : \"internet-facing\"").contains("\"TargetGroupPort888External\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::TargetGroup\"").contains("\"ListenerPort888External\" : {\"Type\" : \"AWS::ElasticLoadBalancingV2::Listener\"");
assert templateString.contains("\"Targets\" : [{ \"Id\" : \"instance1-443\" },{ \"Id\" : \"instance2-443\" }]}}") || templateString.contains("\"Targets\" : [{ \"Id\" : \"instance2-443\" },{ \"Id\" : \"instance1-443\" }]}}");
assert templateString.contains("\"Targets\" : [{ \"Id\" : \"instance1-888\" },{ \"Id\" : \"instance2-888\" }]}}") || templateString.contains("\"Targets\" : [{ \"Id\" : \"instance2-888\" },{ \"Id\" : \"instance1-888\" }]}}");
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.
the class CloudFormationTemplateBuilderTest method setupLoadBalancer.
private AwsLoadBalancer setupLoadBalancer(AwsLoadBalancerScheme scheme, int port, boolean setArn) {
AwsLoadBalancer loadBalancer = new AwsLoadBalancer(scheme);
loadBalancer.addSubnets(Set.of("subnet1"));
AwsListener listener = loadBalancer.getOrCreateListener(port, 8443);
listener.addInstancesToTargetGroup(Set.of("instance1-" + port, "instance2-" + port));
if (setArn) {
listener.getTargetGroup().setArn("arn://targetgroup");
loadBalancer.setArn("arn://loadbalancer");
loadBalancer.validateListenerConfigIsSet();
}
return loadBalancer;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.
the class AwsLaunchServiceLoadBalancerTest method testSetLoadBalancerMetadata.
@Test
public void testSetLoadBalancerMetadata() {
AwsLoadBalancer loadBalancer = new AwsLoadBalancer(AwsLoadBalancerScheme.INTERNAL);
loadBalancer.getOrCreateListener(PORT, PORT);
when(result.getStackResourceSummaries()).thenReturn(createFullSummaries(Set.of(LoadBalancerType.PRIVATE)));
underTest.setLoadBalancerMetadata(List.of(loadBalancer), result);
assertEquals(LOAD_BALANCER_ARN, loadBalancer.getArn());
AwsListener listener = loadBalancer.getListeners().iterator().next();
assert listener.areTargetGroupArnsSet();
AwsTargetGroup targetGroup = listener.getTargetGroup();
assertEquals(TARGET_GROUP_ARN, targetGroup.getArn());
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer in project cloudbreak by hortonworks.
the class AwsLoadBalancerLaunchService method checkForListenerResources.
@VisibleForTesting
boolean checkForListenerResources(AmazonCloudFormationClient cfClient, String cFStackName, List<AwsLoadBalancer> awsLoadBalancers) {
ListStackResourcesResult result = cfClient.listStackResources(awsStackRequestHelper.createListStackResourcesRequest(cFStackName));
boolean resourcesFound = true;
List<StackResourceSummary> summaries = result.getStackResourceSummaries();
for (AwsLoadBalancer loadBalancer : awsLoadBalancers) {
for (AwsListener listener : loadBalancer.getListeners()) {
LOGGER.debug("Checking to see if listener resource {} already exists", listener.getName());
resourcesFound = resourcesFound && summaries.stream().anyMatch(stackResourceSummary -> listener.getName().equals(stackResourceSummary.getLogicalResourceId()));
}
}
return resourcesFound;
}
use of com.sequenceiq.cloudbreak.cloud.aws.common.loadbalancer.AwsLoadBalancer 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