Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #929/0b97186c backport][stable-4] elbv2 - Fix KeyError when passing two TGs #933

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- elb_application_lb - fix ``KeyError`` when balancing across two Target Groups (https://github.com/ansible-collections/community.aws/issues/1089).
32 changes: 24 additions & 8 deletions plugins/module_utils/elbv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,30 @@

# ForwardConfig may be optional if we've got a single TargetGroupArn entry
def _prune_ForwardConfig(action):
if "ForwardConfig" in action and action['Type'] == 'forward':
if action["ForwardConfig"] == {
'TargetGroupStickinessConfig': {'Enabled': False},
'TargetGroups': [{"TargetGroupArn": action["TargetGroupArn"], "Weight": 1}]}:
newAction = action.copy()
del(newAction["ForwardConfig"])
return newAction
return action
"""
Drops a redundant ForwardConfig where TargetGroupARN has already been set.
(So we can perform comparisons)
"""
if action.get('Type', "") != 'forward':
return action
if "ForwardConfig" not in action:
return action
# Where we have multiple TGs, action['TargetGroupArn'] shouldn't be set
if "TargetGroupArn" not in action:
return action

# This is the same as having TargetGroupArn set
equivalent_action = {
'TargetGroupStickinessConfig': {'Enabled': False},
'TargetGroups': [{"TargetGroupArn": action["TargetGroupArn"], "Weight": 1}],
}
if action["ForwardConfig"] != equivalent_action:
return action

# Remove the redundant ForwardConfig
newAction = action.copy()
del(newAction["ForwardConfig"])
return newAction


# the AWS api won't return the client secret, so we'll have to remove it
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/module_utils/test_elbv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,34 @@
}
]

one_action_two_tg = [
{
"ForwardConfig": {
"TargetGroupStickinessConfig": {"Enabled": False},
"TargetGroups": [
{
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:966509639900:targetgroup/my-tg-58045486/5b231e04f663ae21",
"Weight": 1,
},
{
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:966509639900:targetgroup/my-tg-dadf7b62/be2f50b4041f11ed",
"Weight": 1,
}
],
},
"Type": "forward",
}
]


def test__prune_ForwardConfig():
expectation = {
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:966509639900:targetgroup/my-tg-58045486/5b231e04f663ae21",
"Type": "forward",
}
assert elbv2._prune_ForwardConfig(one_action[0]) == expectation
# https://github.com/ansible-collections/community.aws/issues/1089
assert elbv2._prune_ForwardConfig(one_action_two_tg[0]) == one_action_two_tg[0]


def _prune_secret():
Expand Down