Skip to content

Commit

Permalink
added support for the TargetGroupARNs property in Elastigroups (#536)
Browse files Browse the repository at this point in the history
- adds support for the `TargetGroupARNs` property. When present, they will be set as the [Elastigroup's `loadBalancers`](https://api.spotinst.com/spotinst-api/elastigroup/amazon-web-services/create/).
  • Loading branch information
lmineiro authored and jmcs committed Sep 18, 2018
1 parent 98bcf0f commit 72d43e3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 8 deletions.
24 changes: 16 additions & 8 deletions senza/components/elastigroup.py
Expand Up @@ -351,17 +351,25 @@ def extract_load_balancer_name(configuration, elastigroup_config: dict):
if "ElasticLoadBalancerV2" in configuration:
health_check_type = "TARGET_GROUP"
load_balancer_refs = configuration.pop("ElasticLoadBalancerV2")
if isinstance(load_balancer_refs, str):
load_balancers.append({
"arn": {"Ref": load_balancer_refs + 'TargetGroup'},
"type": "TARGET_GROUP"
})
elif isinstance(load_balancer_refs, list):
for load_balancer_ref in load_balancer_refs:
custom_target_groups = configuration.pop("TargetGroupARNs", None)
if custom_target_groups:
for custom_target_group in custom_target_groups:
load_balancers.append({
"arn": custom_target_group,
"type": "TARGET_GROUP"
})
else:
if isinstance(load_balancer_refs, str):
load_balancers.append({
"arn": {"Ref": load_balancer_ref + "TargetGroup"},
"arn": {"Ref": load_balancer_refs + 'TargetGroup'},
"type": "TARGET_GROUP"
})
elif isinstance(load_balancer_refs, list):
for load_balancer_ref in load_balancer_refs:
load_balancers.append({
"arn": {"Ref": load_balancer_ref + "TargetGroup"},
"type": "TARGET_GROUP"
})

if len(load_balancers) > 0:
launch_spec_config["loadBalancersConfig"] = {"loadBalancers": load_balancers}
Expand Down
60 changes: 60 additions & 0 deletions tests/test_elastigroup.py
Expand Up @@ -508,6 +508,66 @@ def test_load_balancers():
assert test_case["expected_config"] == got


def test_multiple_target_groups():
test_cases = [
{ # multiple target groups in raw ARN form, ignore ALB TG
"input": {"ElasticLoadBalancerV2": "foo", "TargetGroupARNs": ["bar", "baz"]},
"given_config": {},
"expected_config": {"compute": {"launchSpecification": {
"loadBalancersConfig": {
"loadBalancers": [
{"arn": "bar", "type": "TARGET_GROUP"},
{"arn": "baz", "type": "TARGET_GROUP"},
],
},
"healthCheckType": "TARGET_GROUP",
"healthCheckGracePeriod": 300,
}}},
},
{ # multiple target groups with Ref, ignore ALB TG
"input": {"ElasticLoadBalancerV2": "foo", "TargetGroupARNs": [{"Ref": "bar"}, {"Ref": "baz"}]},
"given_config": {},
"expected_config": {"compute": {"launchSpecification": {
"loadBalancersConfig": {
"loadBalancers": [
{"arn": {"Ref": "bar"}, "type": "TARGET_GROUP"},
{"arn": {"Ref": "baz"}, "type": "TARGET_GROUP"},
],
},
"healthCheckType": "TARGET_GROUP",
"healthCheckGracePeriod": 300,
}}},
},
{ # ignore Taupage target groups, leave Elatigroup untouched
"input": {"ElasticLoadBalancerV2": "foo", "TargetGroupARNs": [{"Ref": "bar"}, {"Ref": "baz"}]},
"given_config": {"compute": {
"launchSpecification": {
"loadBalancersConfig": {
"loadBalancers": [
{"arn": "givenTargetGroup", "type": "TARGET_GROUP"},
],
},
"healthCheckType": "TARGET_GROUP",
"healthCheckGracePeriod": 300,
}
}},
"expected_config": {"compute": {"launchSpecification": {
"loadBalancersConfig": {
"loadBalancers": [
{"arn": "givenTargetGroup", "type": "TARGET_GROUP"},
],
},
"healthCheckType": "TARGET_GROUP",
"healthCheckGracePeriod": 300,
}}},
},
]
for test_case in test_cases:
got = test_case["given_config"]
extract_load_balancer_name(test_case["input"], got)
assert test_case["expected_config"] == got


def test_public_ips():
test_cases = [
{ # default behavior - no public IPs, leave untouched
Expand Down

0 comments on commit 72d43e3

Please sign in to comment.