Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion moto/ec2/models/subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def __init__(
self.map_public_ip_on_launch = map_public_ip_on_launch
self.assign_ipv6_address_on_creation = ipv6_native
self.ipv6_cidr_block_associations: dict[str, dict[str, Any]] = {}
self.private_dns_name_options_on_launch = {
"HostnameType": "ip-name",
"EnableResourceNameDnsARecord": False,
"EnableResourceNameDnsAAAARecord": False,
}
if ipv6_cidr_block:
self.attach_ipv6_cidr_block_associations(ipv6_cidr_block)

Expand Down Expand Up @@ -631,11 +636,15 @@ def delete_subnet(self, subnet_id: str) -> Subnet:
raise InvalidSubnetIdError(subnet_id)

def modify_subnet_attribute(
self, subnet_id: str, attr_name: str, attr_value: str
self, subnet_id: str, attr_name: str, attr_value: bool
) -> None:
subnet = self.get_subnet(subnet_id)
if attr_name in ("map_public_ip_on_launch", "assign_ipv6_address_on_creation"):
setattr(subnet, attr_name, attr_value)
elif attr_name == "enable_resource_name_dns_a_record_on_launch":
subnet.private_dns_name_options_on_launch[
"EnableResourceNameDnsARecord"
] = attr_value
Comment on lines 637 to +647
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am quite surprised that the type change does not require any additional changes, especially in tests/snapshots. Can you give more context here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ModifySubnetAttribute is used to modify a subnet parameter. This change makes it support a new parameter. It's actually being tested in the newly added test_private_dns_name_options, where it's used to change the default value.

AWS validation in Moto doesn't work like LocalStack -- it does not record the actual API response, just marks the test as runnable against AWS.

else:
raise InvalidParameterValueError(attr_name)

Expand Down
6 changes: 5 additions & 1 deletion moto/ec2/responses/subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ def describe_subnets(self) -> ActionResult:
def modify_subnet_attribute(self) -> ActionResult:
subnet_id = self._get_param("SubnetId")

for attribute in ("MapPublicIpOnLaunch", "AssignIpv6AddressOnCreation"):
for attribute in (
"MapPublicIpOnLaunch",
"AssignIpv6AddressOnCreation",
"EnableResourceNameDnsARecordOnLaunch",
):
if self._get_param(f"{attribute}.Value") is not None:
attr_name = camelcase_to_underscores(attribute)
attr_value = self._get_param(f"{attribute}.Value")
Expand Down
37 changes: 37 additions & 0 deletions tests/test_ec2/test_subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,6 +1017,43 @@ def test_create_ipv6native_subnet(account_id, ec2_client=None, vpc_id=None):
ec2_client.delete_subnet(SubnetId=subnet["SubnetId"])


@pytest.mark.aws_verified
@ec2_aws_verified(create_vpc=True)
def test_private_dns_name_options(ec2_client=None, vpc_id=None):
subnet = None
try:
subnet = ec2_client.create_subnet(
VpcId=vpc_id,
CidrBlock="10.0.0.0/24",
)["Subnet"]

assert "PrivateDnsNameOptionsOnLaunch" in subnet
assert subnet["PrivateDnsNameOptionsOnLaunch"]["HostnameType"] == "ip-name"
assert (
subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsARecord"]
is False
)
assert (
subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsAAAARecord"]
is False
)

ec2_client.modify_subnet_attribute(
SubnetId=subnet["SubnetId"],
EnableResourceNameDnsARecordOnLaunch={"Value": True},
)
subnet = ec2_client.describe_subnets(SubnetIds=[subnet["SubnetId"]])["Subnets"][
0
]
assert (
subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsARecord"]
is True
)
finally:
if subnet:
ec2_client.delete_subnet(SubnetId=subnet["SubnetId"])


@mock_aws
def test_create_subnet_cidr_reservations() -> None:
ec2 = boto3.client("ec2", region_name="us-west-1")
Expand Down