From f1859996020b52a6b9335afea4144bc59f595620 Mon Sep 17 00:00:00 2001 From: Marshall Mamiya <44485531+marshall7m@users.noreply.github.com> Date: Mon, 31 Oct 2022 15:52:28 -0700 Subject: [PATCH] EC2: Add EnableNetworkAddressUsageMetrics to VPC attribute (#5618) --- moto/ec2/responses/vpcs.py | 7 ++++-- tests/test_ec2/test_vpcs.py | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/moto/ec2/responses/vpcs.py b/moto/ec2/responses/vpcs.py index 52b2396cf..649fafd30 100644 --- a/moto/ec2/responses/vpcs.py +++ b/moto/ec2/responses/vpcs.py @@ -137,8 +137,11 @@ class VPCs(EC2BaseResponse): def modify_vpc_attribute(self): vpc_id = self._get_param("VpcId") - - for attribute in ("EnableDnsSupport", "EnableDnsHostnames"): + for attribute in ( + "EnableDnsSupport", + "EnableDnsHostnames", + "EnableNetworkAddressUsageMetrics", + ): if self.querystring.get("%s.Value" % attribute): attr_name = camelcase_to_underscores(attribute) attr_value = self.querystring.get("%s.Value" % attribute)[0] diff --git a/tests/test_ec2/test_vpcs.py b/tests/test_ec2/test_vpcs.py index b9999914b..2dc27528c 100644 --- a/tests/test_ec2/test_vpcs.py +++ b/tests/test_ec2/test_vpcs.py @@ -5,6 +5,7 @@ import boto3 import sure # noqa # pylint: disable=unused-import import random +import sys from moto import mock_ec2, settings from unittest import SkipTest @@ -360,6 +361,10 @@ def test_vpc_get_by_tag_value_subset(): @mock_ec2 def test_default_vpc(): + if sys.version_info < (3, 7): + raise SkipTest( + "Cannot test this in Py3.6; outdated botocore dependencies do not have all regions" + ) ec2 = boto3.resource("ec2", region_name="us-west-1") # Create the default VPC @@ -378,9 +383,19 @@ def test_default_vpc(): attr = response.get("EnableDnsHostnames") attr.get("Value").should.equal(True) + response = default_vpc.describe_attribute( + Attribute="enableNetworkAddressUsageMetrics" + ) + attr = response.get("EnableNetworkAddressUsageMetrics") + attr.get("Value").should.equal(False) + @mock_ec2 def test_non_default_vpc(): + if sys.version_info < (3, 7): + raise SkipTest( + "Cannot test this in Py3.6; outdated botocore dependencies do not have all regions" + ) ec2 = boto3.resource("ec2", region_name="us-west-1") # Create the default VPC - this already exists when backend instantiated! @@ -403,6 +418,10 @@ def test_non_default_vpc(): attr = response.get("EnableDnsHostnames") attr.get("Value").should.equal(False) + response = vpc.describe_attribute(Attribute="enableNetworkAddressUsageMetrics") + attr = response.get("EnableNetworkAddressUsageMetrics") + attr.get("Value").should.equal(False) + # Check Primary CIDR Block Associations cidr_block_association_set = next(iter(vpc.cidr_block_association_set), None) cidr_block_association_set["CidrBlockState"]["State"].should.equal("associated") @@ -491,6 +510,30 @@ def test_vpc_modify_enable_dns_hostnames(): attr.get("Value").should.be.ok +@mock_ec2 +def test_vpc_modify_enable_network_address_usage_metrics(): + if sys.version_info < (3, 7): + raise SkipTest( + "Cannot test this in Py3.6; outdated botocore dependencies do not have all regions" + ) + ec2 = boto3.resource("ec2", region_name="us-west-1") + + # Create the default VPC + ec2.create_vpc(CidrBlock="172.31.0.0/16") + + vpc = ec2.create_vpc(CidrBlock="10.0.0.0/16") + # Test default values for VPC attributes + response = vpc.describe_attribute(Attribute="enableNetworkAddressUsageMetrics") + attr = response.get("EnableNetworkAddressUsageMetrics") + attr.get("Value").shouldnt.be.ok + + vpc.modify_attribute(EnableNetworkAddressUsageMetrics={"Value": True}) + + response = vpc.describe_attribute(Attribute="enableNetworkAddressUsageMetrics") + attr = response.get("EnableNetworkAddressUsageMetrics") + attr.get("Value").should.equal(True) + + @mock_ec2 def test_vpc_associate_dhcp_options(): ec2 = boto3.resource("ec2", region_name="us-west-1")