Skip to content

Commit 86e1908

Browse files
EC2: Resource name DNS options for subnets (#154)
1 parent e9db968 commit 86e1908

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

moto/ec2/models/subnets.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ def __init__(
9797
self.map_public_ip_on_launch = map_public_ip_on_launch
9898
self.assign_ipv6_address_on_creation = ipv6_native
9999
self.ipv6_cidr_block_associations: dict[str, dict[str, Any]] = {}
100+
self.private_dns_name_options_on_launch = {
101+
"HostnameType": "ip-name",
102+
"EnableResourceNameDnsARecord": False,
103+
"EnableResourceNameDnsAAAARecord": False,
104+
}
100105
if ipv6_cidr_block:
101106
self.attach_ipv6_cidr_block_associations(ipv6_cidr_block)
102107

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

633638
def modify_subnet_attribute(
634-
self, subnet_id: str, attr_name: str, attr_value: str
639+
self, subnet_id: str, attr_name: str, attr_value: bool
635640
) -> None:
636641
subnet = self.get_subnet(subnet_id)
637642
if attr_name in ("map_public_ip_on_launch", "assign_ipv6_address_on_creation"):
638643
setattr(subnet, attr_name, attr_value)
644+
elif attr_name == "enable_resource_name_dns_a_record_on_launch":
645+
subnet.private_dns_name_options_on_launch[
646+
"EnableResourceNameDnsARecord"
647+
] = attr_value
639648
else:
640649
raise InvalidParameterValueError(attr_name)
641650

moto/ec2/responses/subnets.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ def describe_subnets(self) -> ActionResult:
6767
def modify_subnet_attribute(self) -> ActionResult:
6868
subnet_id = self._get_param("SubnetId")
6969

70-
for attribute in ("MapPublicIpOnLaunch", "AssignIpv6AddressOnCreation"):
70+
for attribute in (
71+
"MapPublicIpOnLaunch",
72+
"AssignIpv6AddressOnCreation",
73+
"EnableResourceNameDnsARecordOnLaunch",
74+
):
7175
if self._get_param(f"{attribute}.Value") is not None:
7276
attr_name = camelcase_to_underscores(attribute)
7377
attr_value = self._get_param(f"{attribute}.Value")

tests/test_ec2/test_subnets.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,43 @@ def test_create_ipv6native_subnet(account_id, ec2_client=None, vpc_id=None):
10171017
ec2_client.delete_subnet(SubnetId=subnet["SubnetId"])
10181018

10191019

1020+
@pytest.mark.aws_verified
1021+
@ec2_aws_verified(create_vpc=True)
1022+
def test_private_dns_name_options(ec2_client=None, vpc_id=None):
1023+
subnet = None
1024+
try:
1025+
subnet = ec2_client.create_subnet(
1026+
VpcId=vpc_id,
1027+
CidrBlock="10.0.0.0/24",
1028+
)["Subnet"]
1029+
1030+
assert "PrivateDnsNameOptionsOnLaunch" in subnet
1031+
assert subnet["PrivateDnsNameOptionsOnLaunch"]["HostnameType"] == "ip-name"
1032+
assert (
1033+
subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsARecord"]
1034+
is False
1035+
)
1036+
assert (
1037+
subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsAAAARecord"]
1038+
is False
1039+
)
1040+
1041+
ec2_client.modify_subnet_attribute(
1042+
SubnetId=subnet["SubnetId"],
1043+
EnableResourceNameDnsARecordOnLaunch={"Value": True},
1044+
)
1045+
subnet = ec2_client.describe_subnets(SubnetIds=[subnet["SubnetId"]])["Subnets"][
1046+
0
1047+
]
1048+
assert (
1049+
subnet["PrivateDnsNameOptionsOnLaunch"]["EnableResourceNameDnsARecord"]
1050+
is True
1051+
)
1052+
finally:
1053+
if subnet:
1054+
ec2_client.delete_subnet(SubnetId=subnet["SubnetId"])
1055+
1056+
10201057
@mock_aws
10211058
def test_create_subnet_cidr_reservations() -> None:
10221059
ec2 = boto3.client("ec2", region_name="us-west-1")

0 commit comments

Comments
 (0)