Skip to content

Commit

Permalink
ELBv2: fix parity in describe_target_group (#6812)
Browse files Browse the repository at this point in the history
  • Loading branch information
macnev2013 committed Sep 15, 2023
1 parent c6b3e5a commit 971e432
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 11 deletions.
4 changes: 1 addition & 3 deletions moto/elbv2/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def __init__(self) -> None:

class TargetGroupNotFoundError(ELBClientError):
def __init__(self) -> None:
super().__init__(
"TargetGroupNotFound", "The specified target group does not exist."
)
super().__init__("TargetGroupNotFound", "One or more target groups not found")


class TooManyTagsError(ELBClientError):
Expand Down
21 changes: 17 additions & 4 deletions moto/elbv2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1377,20 +1377,33 @@ def describe_target_groups(
target_group_arns: List[str],
names: Optional[List[str]],
) -> Iterable[FakeTargetGroup]:

args = sum(bool(arg) for arg in [load_balancer_arn, target_group_arns, names])

if args > 1:
raise ValidationError(
"Target group names, target group ARNs, and a load balancer ARN cannot be specified at the same time"
)

if load_balancer_arn:
if load_balancer_arn not in self.load_balancers:
raise LoadBalancerNotFoundError()
return [
target_groups = [
tg
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:
return [self.target_groups[arn] for arn in target_group_arns]
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:
matched = []
for name in names:
Expand All @@ -1401,9 +1414,9 @@ def describe_target_groups(
if not found:
raise TargetGroupNotFoundError()
matched.append(found)
return matched
return sorted(matched, key=lambda tg: tg.name)

return self.target_groups.values()
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
1 change: 1 addition & 0 deletions moto/elbv2/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ def remove_listener_certificates(self) -> str:
{% if target_group.vpc_id %}
<VpcId>{{ target_group.vpc_id }}</VpcId>
{% endif %}
<IpAddressType>{{ target_group.ip_address_type }}</IpAddressType>
<HealthCheckProtocol>{{ target_group.healthcheck_protocol }}</HealthCheckProtocol>
{% if target_group.healthcheck_port %}<HealthCheckPort>{{ target_group.healthcheck_port }}</HealthCheckPort>{% endif %}
<HealthCheckPath>{{ target_group.healthcheck_path or '' }}</HealthCheckPath>
Expand Down
87 changes: 83 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,17 +455,23 @@ 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
@mock_ec2
def test_describe_target_groups_no_arguments():
def test_describe_target_groups():
elbv2 = boto3.client("elbv2", region_name="us-east-1")

response, vpc, _, _, _, conn = create_load_balancer()

lb_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
assert "LoadBalancerArn" in response["LoadBalancers"][0]

conn.create_target_group(
groups = conn.describe_target_groups()["TargetGroups"]
assert len(groups) == 0

response = conn.create_target_group(
Name="a-target",
Protocol="HTTP",
Port=8080,
Expand All @@ -479,11 +485,84 @@ def test_describe_target_groups_no_arguments():
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
assert groups[0]["Matcher"] == {"HttpCode": "201"}

response = elbv2.create_target_group(
Name="c-target",
Protocol="HTTP",
Port=8081,
VpcId=vpc.id,
)
arn_c = response["TargetGroups"][0]["TargetGroupArn"]
groups = conn.describe_target_groups()["TargetGroups"]
assert len(groups) == 2
assert groups[0]["TargetGroupName"] == "a-target"
assert groups[1]["TargetGroupName"] == "c-target"

response = elbv2.create_target_group(
Name="b-target",
Protocol="HTTP",
Port=8082,
VpcId=vpc.id,
)
arn_b = response["TargetGroups"][0]["TargetGroupArn"]
groups = conn.describe_target_groups()["TargetGroups"]
assert len(groups) == 3
assert groups[0]["TargetGroupName"] == "a-target"
assert groups[1]["TargetGroupName"] == "b-target"
assert groups[2]["TargetGroupName"] == "c-target"

groups = conn.describe_target_groups(Names=["a-target"])["TargetGroups"]
assert len(groups) == 1
assert groups[0]["TargetGroupName"] == "a-target"

groups = conn.describe_target_groups(Names=["a-target", "b-target"])["TargetGroups"]
assert len(groups) == 2
assert groups[0]["TargetGroupName"] == "a-target"
assert groups[1]["TargetGroupName"] == "b-target"

groups = conn.describe_target_groups(TargetGroupArns=[arn_b])["TargetGroups"]
assert len(groups) == 1
assert groups[0]["TargetGroupName"] == "b-target"

groups = conn.describe_target_groups(TargetGroupArns=[arn_b, arn_c])["TargetGroups"]
assert len(groups) == 2
assert groups[0]["TargetGroupName"] == "b-target"
assert groups[1]["TargetGroupName"] == "c-target"

groups = conn.describe_target_groups(LoadBalancerArn=lb_arn)["TargetGroups"]
assert len(groups) == 1
assert groups[0]["TargetGroupName"] == "a-target"

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"
assert groups[1]["TargetGroupName"] == "d-target"


@mock_elbv2
@mock_ec2
Expand Down

0 comments on commit 971e432

Please sign in to comment.