Skip to content

Commit

Permalink
ELBv2: added validation for create_target_group() and set_ip_address_…
Browse files Browse the repository at this point in the history
…type() (#6817)
  • Loading branch information
macnev2013 authored Sep 15, 2023
1 parent b7cedf6 commit fb9023a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
14 changes: 11 additions & 3 deletions moto/elbv2/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,14 @@ def create_target_group(self, name: str, **kwargs: Any) -> FakeTargetGroup:
param = "VPC ID" if param == "vpc_id" else param.lower()
raise ValidationError(f"A {param} must be specified")

if target_type == "lambda":
for param in ["protocol", "port", "vpc_id"]:
if kwargs.get(param) is not None:
param = "VPC ID" if param == "vpc_id" else param.capitalize()
raise ValidationError(
f"{param} cannot be specified for target groups with target type 'lambda'"
)

if kwargs.get("vpc_id"):
from moto.ec2.exceptions import InvalidVPCIdError

Expand Down Expand Up @@ -1568,10 +1576,10 @@ def set_rule_priorities(
return modified_rules

def set_ip_address_type(self, arn: str, ip_type: str) -> None:
if ip_type not in ("internal", "dualstack"):
if ip_type not in ("ipv4", "dualstack"):
raise RESTError(
"InvalidParameterValue",
"IpAddressType must be either internal | dualstack",
"ValidationError",
f"1 validation error detected: Value '{ip_type}' at 'ipAddressType' failed to satisfy constraint: Member must satisfy enum value set: [ipv4, dualstack]",
)

balancer = self.load_balancers.get(arn)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_elbv2/test_elbv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,11 @@ def test_set_ip_address_type():

client.set_ip_address_type(LoadBalancerArn=arn, IpAddressType="dualstack")

with pytest.raises(ClientError) as ex:
client.set_ip_address_type(LoadBalancerArn=arn, IpAddressType="internal")
err = ex.value.response["Error"]
assert err["Code"] == "ValidationError"


@mock_elbv2
@mock_ec2
Expand Down
33 changes: 33 additions & 0 deletions tests/test_elbv2/test_elbv2_target_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,3 +895,36 @@ def test_create_target_group_validation_error():
err = ex.value.response["Error"]
assert err["Code"] == "ValidationError"
assert err["Message"] == "Health check interval must be greater than the timeout."

with pytest.raises(ClientError) as ex:
elbv2.create_target_group(Name="a-target", TargetType="lambda", Port=8080)
err = ex.value.response["Error"]
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "Port cannot be specified for target groups with target type 'lambda'"
)

with pytest.raises(ClientError) as ex:
elbv2.create_target_group(
Name="a-target", TargetType="lambda", VpcId="non-existing"
)
err = ex.value.response["Error"]
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "VPC ID cannot be specified for target groups with target type 'lambda'"
)

with pytest.raises(ClientError) as ex:
elbv2.create_target_group(
Name="a-target",
TargetType="lambda",
Protocol="HTTP",
)
err = ex.value.response["Error"]
assert err["Code"] == "ValidationError"
assert (
err["Message"]
== "Protocol cannot be specified for target groups with target type 'lambda'"
)

0 comments on commit fb9023a

Please sign in to comment.