ELBv2: fix parity in describe_target_group (#6812)
This commit is contained in:
parent
c6b3e5a370
commit
971e432ad3
@ -35,9 +35,7 @@ class SubnetNotFoundError(ELBClientError):
|
|||||||
|
|
||||||
class TargetGroupNotFoundError(ELBClientError):
|
class TargetGroupNotFoundError(ELBClientError):
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__(
|
super().__init__("TargetGroupNotFound", "One or more target groups not found")
|
||||||
"TargetGroupNotFound", "The specified target group does not exist."
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class TooManyTagsError(ELBClientError):
|
class TooManyTagsError(ELBClientError):
|
||||||
|
@ -1377,20 +1377,33 @@ Member must satisfy regular expression pattern: {expression}"
|
|||||||
target_group_arns: List[str],
|
target_group_arns: List[str],
|
||||||
names: Optional[List[str]],
|
names: Optional[List[str]],
|
||||||
) -> Iterable[FakeTargetGroup]:
|
) -> 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:
|
||||||
if load_balancer_arn not in self.load_balancers:
|
if load_balancer_arn not in self.load_balancers:
|
||||||
raise LoadBalancerNotFoundError()
|
raise LoadBalancerNotFoundError()
|
||||||
return [
|
target_groups = [
|
||||||
tg
|
tg
|
||||||
for tg in self.target_groups.values()
|
for tg in self.target_groups.values()
|
||||||
if load_balancer_arn in tg.load_balancer_arns
|
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:
|
if target_group_arns:
|
||||||
try:
|
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:
|
except KeyError:
|
||||||
raise TargetGroupNotFoundError()
|
raise TargetGroupNotFoundError()
|
||||||
|
|
||||||
if names:
|
if names:
|
||||||
matched = []
|
matched = []
|
||||||
for name in names:
|
for name in names:
|
||||||
@ -1401,9 +1414,9 @@ Member must satisfy regular expression pattern: {expression}"
|
|||||||
if not found:
|
if not found:
|
||||||
raise TargetGroupNotFoundError()
|
raise TargetGroupNotFoundError()
|
||||||
matched.append(found)
|
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(
|
def describe_listeners(
|
||||||
self, load_balancer_arn: Optional[str], listener_arns: List[str]
|
self, load_balancer_arn: Optional[str], listener_arns: List[str]
|
||||||
|
@ -1070,6 +1070,7 @@ DESCRIBE_TARGET_GROUPS_TEMPLATE = """<DescribeTargetGroupsResponse xmlns="http:/
|
|||||||
{% if target_group.vpc_id %}
|
{% if target_group.vpc_id %}
|
||||||
<VpcId>{{ target_group.vpc_id }}</VpcId>
|
<VpcId>{{ target_group.vpc_id }}</VpcId>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
<IpAddressType>{{ target_group.ip_address_type }}</IpAddressType>
|
||||||
<HealthCheckProtocol>{{ target_group.healthcheck_protocol }}</HealthCheckProtocol>
|
<HealthCheckProtocol>{{ target_group.healthcheck_protocol }}</HealthCheckProtocol>
|
||||||
{% if target_group.healthcheck_port %}<HealthCheckPort>{{ target_group.healthcheck_port }}</HealthCheckPort>{% endif %}
|
{% if target_group.healthcheck_port %}<HealthCheckPort>{{ target_group.healthcheck_port }}</HealthCheckPort>{% endif %}
|
||||||
<HealthCheckPath>{{ target_group.healthcheck_path or '' }}</HealthCheckPath>
|
<HealthCheckPath>{{ target_group.healthcheck_path or '' }}</HealthCheckPath>
|
||||||
|
@ -122,7 +122,7 @@ def test_create_target_group_and_listeners():
|
|||||||
http_listener_arn = listener["ListenerArn"]
|
http_listener_arn = listener["ListenerArn"]
|
||||||
|
|
||||||
response = conn.describe_target_groups(
|
response = conn.describe_target_groups(
|
||||||
LoadBalancerArn=load_balancer_arn, Names=["a-target"]
|
LoadBalancerArn=load_balancer_arn,
|
||||||
)
|
)
|
||||||
assert len(response["TargetGroups"]) == 1
|
assert len(response["TargetGroups"]) == 1
|
||||||
|
|
||||||
@ -455,17 +455,23 @@ def test_describe_invalid_target_group():
|
|||||||
conn.describe_target_groups(Names=["invalid"])
|
conn.describe_target_groups(Names=["invalid"])
|
||||||
err = exc.value.response["Error"]
|
err = exc.value.response["Error"]
|
||||||
assert err["Code"] == "TargetGroupNotFound"
|
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_elbv2
|
||||||
@mock_ec2
|
@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()
|
response, vpc, _, _, _, conn = create_load_balancer()
|
||||||
|
|
||||||
|
lb_arn = response["LoadBalancers"][0]["LoadBalancerArn"]
|
||||||
assert "LoadBalancerArn" in response["LoadBalancers"][0]
|
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",
|
Name="a-target",
|
||||||
Protocol="HTTP",
|
Protocol="HTTP",
|
||||||
Port=8080,
|
Port=8080,
|
||||||
@ -479,11 +485,84 @@ def test_describe_target_groups_no_arguments():
|
|||||||
UnhealthyThresholdCount=2,
|
UnhealthyThresholdCount=2,
|
||||||
Matcher={"HttpCode": "201"},
|
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"]
|
groups = conn.describe_target_groups()["TargetGroups"]
|
||||||
assert len(groups) == 1
|
assert len(groups) == 1
|
||||||
assert groups[0]["Matcher"] == {"HttpCode": "201"}
|
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_elbv2
|
||||||
@mock_ec2
|
@mock_ec2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user