From 6831c422840cebc2d142a13872e3aaa466d803f5 Mon Sep 17 00:00:00 2001 From: Florian Weyandt <40235661+florianweyandt1337@users.noreply.github.com> Date: Fri, 15 Sep 2023 09:39:17 +0200 Subject: [PATCH] Elbv2.create rule links target group to lb (#6452) * Link loadbalancer arn to target group when creating a forward rule so target groups can be found by loadbalancer arn * add test for describe_target_groups by loadbalancer arn when creating listener rule after listener creation * Linting --------- Co-authored-by: Bert Blommers --- moto/elbv2/models.py | 7 +++++++ tests/test_elbv2/test_elbv2_listener_rules.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/moto/elbv2/models.py b/moto/elbv2/models.py index 0a45bd9e9..9f0e53e2d 100644 --- a/moto/elbv2/models.py +++ b/moto/elbv2/models.py @@ -822,6 +822,13 @@ class ELBv2Backend(BaseBackend): raise PriorityInUseError() self._validate_actions(fake_actions) + for action in fake_actions: + if action.type == "forward": + found_arns = self._get_target_group_arns_from(action_data=action.data) + for arn in found_arns: + target_group = self.target_groups[arn] + target_group.load_balancer_arns.append(listener.load_balancer_arn) + arn = listener_arn.replace(":listener/", ":listener-rule/") arn += f"/{mock_random.get_random_hex(16)}" diff --git a/tests/test_elbv2/test_elbv2_listener_rules.py b/tests/test_elbv2/test_elbv2_listener_rules.py index 56555d1f0..6c2b7a224 100644 --- a/tests/test_elbv2/test_elbv2_listener_rules.py +++ b/tests/test_elbv2/test_elbv2_listener_rules.py @@ -518,3 +518,10 @@ def test_create_rule_action_forward_target_group(): assert rule["Priority"] == "99" assert rule["Conditions"] == [] assert rule["Actions"][0] == action + + # assert describe_target_group by loadbalancer_arn response + load_balancer_arn = conn.describe_listeners(ListenerArns=[http_listener_arn])[ + "Listeners" + ][0]["LoadBalancerArn"] + response = conn.describe_target_groups(LoadBalancerArn=load_balancer_arn) + assert len(response.get("TargetGroups")) == 1