Skip to content

Commit

Permalink
EC2: Add default DHCP options set for VPCs (#6901)
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni authored Oct 11, 2023
1 parent 0471cd0 commit 7e891f7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions moto/ec2/models/dhcp_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ def associate_dhcp_options(self, dhcp_options: DHCPOptionsSet, vpc: Any) -> None
dhcp_options.vpc = vpc
vpc.dhcp_options = dhcp_options

def disassociate_dhcp_options(self, vpc: Any) -> None:
if vpc.dhcp_options:
vpc.dhcp_options.vpc = None
vpc.dhcp_options = None

def create_dhcp_options(
self,
domain_name_servers: Optional[List[str]] = None,
Expand Down
7 changes: 5 additions & 2 deletions moto/ec2/responses/dhcp_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ def associate_dhcp_options(self) -> str:
dhcp_opt_id = self._get_param("DhcpOptionsId")
vpc_id = self._get_param("VpcId")

dhcp_opt = self.ec2_backend.describe_dhcp_options([dhcp_opt_id])[0]
vpc = self.ec2_backend.get_vpc(vpc_id)

self.ec2_backend.associate_dhcp_options(dhcp_opt, vpc)
if dhcp_opt_id == "default":
self.ec2_backend.disassociate_dhcp_options(vpc)
else:
dhcp_opt = self.ec2_backend.describe_dhcp_options([dhcp_opt_id])[0]
self.ec2_backend.associate_dhcp_options(dhcp_opt, vpc)

template = self.response_template(ASSOCIATE_DHCP_OPTIONS_RESPONSE)
return template.render()
Expand Down
4 changes: 2 additions & 2 deletions moto/ec2/responses/vpcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def modify_managed_prefix_list(self) -> str:
{% endfor %}
</ipv6CidrBlockAssociationSet>
{% endif %}
<dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-1a2b3c4d2{% endif %}</dhcpOptionsId>
<dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}default{% endif %}</dhcpOptionsId>
<instanceTenancy>{{ vpc.instance_tenancy }}</instanceTenancy>
<ownerId> {{ vpc.owner_id }}</ownerId>
<tagSet>
Expand Down Expand Up @@ -475,7 +475,7 @@ def modify_managed_prefix_list(self) -> str:
{% endfor %}
</ipv6CidrBlockAssociationSet>
{% endif %}
<dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}dopt-7a8b9c2d{% endif %}</dhcpOptionsId>
<dhcpOptionsId>{% if vpc.dhcp_options %}{{ vpc.dhcp_options.id }}{% else %}default{% endif %}</dhcpOptionsId>
<instanceTenancy>{{ vpc.instance_tenancy }}</instanceTenancy>
<isDefault>{{ vpc.is_default }}</isDefault>
<ownerId> {{ vpc.owner_id }}</ownerId>
Expand Down
28 changes: 28 additions & 0 deletions tests/test_ec2/test_dhcp_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ def test_dhcp_options_associate_invalid_vpc_id():
assert ex.value.response["Error"]["Code"] == "InvalidVpcID.NotFound"


@mock_ec2
def test_dhcp_options_disassociation():
"""Ensure that VPCs can be set to the 'default' DHCP options set for disassociation."""
ec2 = boto3.resource("ec2", region_name="us-west-1")
client = boto3.client("ec2", region_name="us-west-1")
dhcp_options = ec2.create_dhcp_options(
DhcpConfigurations=[
{"Key": "domain-name", "Values": [SAMPLE_DOMAIN_NAME]},
{"Key": "domain-name-servers", "Values": SAMPLE_NAME_SERVERS},
]
)
vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16")

# Ensure newly created VPCs without any DHCP options can be disassociated
client.associate_dhcp_options(DhcpOptionsId="default", VpcId=vpc.id)
vpc.reload()
assert vpc.dhcp_options_id == "default"

client.associate_dhcp_options(DhcpOptionsId=dhcp_options.id, VpcId=vpc.id)
vpc.reload()
assert vpc.dhcp_options_id == dhcp_options.id

# Ensure VPCs with already associated DHCP options can be disassociated
client.associate_dhcp_options(DhcpOptionsId="default", VpcId=vpc.id)
vpc.reload()
assert vpc.dhcp_options_id == "default"


@mock_ec2
def test_dhcp_options_delete_with_vpc():
"""Test deletion of dhcp options with vpc"""
Expand Down

0 comments on commit 7e891f7

Please sign in to comment.