Skip to content

Commit

Permalink
fixed minor bug
Browse files Browse the repository at this point in the history
  • Loading branch information
macnev2013 committed Sep 15, 2023
1 parent 40d684e commit 2216c4a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
29 changes: 16 additions & 13 deletions moto/elbv2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,8 +1378,6 @@ def describe_target_groups(
"Target group names, target group ARNs, and a load balancer ARN cannot be specified at the same time"
)

target_groups = []

if load_balancer_arn:
if load_balancer_arn not in self.load_balancers:
raise LoadBalancerNotFoundError()
Expand All @@ -1388,25 +1386,30 @@ def describe_target_groups(
for tg in self.target_groups.values()
if load_balancer_arn in tg.load_balancer_arns
]
if target_groups is None or len(target_groups) == 0:
raise TargetGroupNotFoundError()
return sorted(target_groups, key=lambda tg: tg.name)

if target_group_arns:
try:
target_groups = [self.target_groups[arn] for arn in target_group_arns]
return sorted(target_groups, key=lambda tg: tg.name)
except KeyError:
raise TargetGroupNotFoundError()

if names:
target_groups = [
next((tg for tg in self.target_groups.values() if tg.name == name))
for name in names
]
if None in target_groups:
raise TargetGroupNotFoundError()

if len(target_groups) == 0:
target_groups = list(self.target_groups.values())

return sorted(target_groups, key=lambda tg: tg.name)
matched = []
for name in names:
found = None
for target_group in self.target_groups.values():
if target_group.name == name:
found = target_group
if not found:
raise TargetGroupNotFoundError()
matched.append(found)
return sorted(matched, key=lambda tg: tg.name)

return sorted(self.target_groups.values(), key=lambda tg: tg.name)

def describe_listeners(
self, load_balancer_arn: Optional[str], listener_arns: List[str]
Expand Down
23 changes: 19 additions & 4 deletions tests/test_elbv2/test_elbv2_target_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def test_create_target_group_and_listeners():
http_listener_arn = listener["ListenerArn"]

response = conn.describe_target_groups(
LoadBalancerArn=load_balancer_arn, Names=["a-target"]
LoadBalancerArn=load_balancer_arn,
)
assert len(response["TargetGroups"]) == 1

Expand Down Expand Up @@ -455,7 +455,7 @@ def test_describe_invalid_target_group():
conn.describe_target_groups(Names=["invalid"])
err = exc.value.response["Error"]
assert err["Code"] == "TargetGroupNotFound"
assert err["Message"] == "The specified target group does not exist."
assert err["Message"] == "One or more target groups not found"


@mock_elbv2
Expand All @@ -471,7 +471,7 @@ def test_describe_target_groups():
groups = conn.describe_target_groups()["TargetGroups"]
assert len(groups) == 0

conn.create_target_group(
response = conn.create_target_group(
Name="a-target",
Protocol="HTTP",
Port=8080,
Expand All @@ -485,6 +485,14 @@ def test_describe_target_groups():
UnhealthyThresholdCount=2,
Matcher={"HttpCode": "201"},
)
arn_a = response["TargetGroups"][0]["TargetGroupArn"]

conn.create_listener(
LoadBalancerArn=lb_arn,
Protocol="HTTP",
Port=80,
DefaultActions=[{"Type": "forward", "TargetGroupArn": arn_a}],
)

groups = conn.describe_target_groups()["TargetGroups"]
assert len(groups) == 1
Expand Down Expand Up @@ -537,12 +545,19 @@ def test_describe_target_groups():
assert len(groups) == 1
assert groups[0]["TargetGroupName"] == "a-target"

conn.create_target_group(
response = conn.create_target_group(
Name="d-target",
Protocol="HTTP",
Port=8082,
VpcId=vpc.id,
)
arn_d = response["TargetGroups"][0]["TargetGroupArn"]
conn.create_listener(
LoadBalancerArn=lb_arn,
Protocol="HTTP",
Port=80,
DefaultActions=[{"Type": "forward", "TargetGroupArn": arn_d}],
)
groups = conn.describe_target_groups(LoadBalancerArn=lb_arn)["TargetGroups"]
assert len(groups) == 2
assert groups[0]["TargetGroupName"] == "a-target"
Expand Down

0 comments on commit 2216c4a

Please sign in to comment.