From 4ec748542f91c727147390685adf641a29a4e9e4 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Tue, 6 Dec 2022 02:50:03 +0530 Subject: [PATCH] EC2: AllocateAddress fallback domain (#5729) --- moto/ec2/exceptions.py | 7 ----- moto/ec2/models/elastic_ip_addresses.py | 3 +-- moto/ec2/responses/elastic_ip_addresses.py | 2 +- tests/test_ec2/test_elastic_ip_addresses.py | 30 ++++++++++----------- 4 files changed, 16 insertions(+), 26 deletions(-) diff --git a/moto/ec2/exceptions.py b/moto/ec2/exceptions.py index a0938f6e0..bac72ceff 100644 --- a/moto/ec2/exceptions.py +++ b/moto/ec2/exceptions.py @@ -333,13 +333,6 @@ class VolumeInUseError(EC2ClientError): ) -class InvalidDomainError(EC2ClientError): - def __init__(self, domain): - super().__init__( - "InvalidParameterValue", f"Invalid value '{domain}' for domain." - ) - - class InvalidAddressError(EC2ClientError): def __init__(self, ip): super().__init__("InvalidAddress.NotFound", f"Address '{ip}' not found.") diff --git a/moto/ec2/models/elastic_ip_addresses.py b/moto/ec2/models/elastic_ip_addresses.py index e1dbc7f2e..1e1d31ad2 100644 --- a/moto/ec2/models/elastic_ip_addresses.py +++ b/moto/ec2/models/elastic_ip_addresses.py @@ -2,7 +2,6 @@ from moto.core import CloudFormationModel from .core import TaggedEC2Resource from ..exceptions import ( FilterNotImplementedError, - InvalidDomainError, InvalidAddressError, InvalidAllocationIdError, ResourceAlreadyAssociatedError, @@ -113,7 +112,7 @@ class ElasticAddressBackend: def allocate_address(self, domain, address=None, tags=None): if domain not in ["standard", "vpc"]: - raise InvalidDomainError(domain) + domain = "vpc" if address: address = ElasticAddress(self, domain=domain, address=address, tags=tags) else: diff --git a/moto/ec2/responses/elastic_ip_addresses.py b/moto/ec2/responses/elastic_ip_addresses.py index eea5e2f6e..2deba6be8 100644 --- a/moto/ec2/responses/elastic_ip_addresses.py +++ b/moto/ec2/responses/elastic_ip_addresses.py @@ -4,7 +4,7 @@ from ._base_response import EC2BaseResponse class ElasticIPAddresses(EC2BaseResponse): def allocate_address(self): - domain = self._get_param("Domain", if_none="standard") + domain = self._get_param("Domain", if_none=None) reallocate_address = self._get_param("Address", if_none=None) tags = self._get_multi_param("TagSpecification") tags = add_tag_specification(tags) diff --git a/tests/test_ec2/test_elastic_ip_addresses.py b/tests/test_ec2/test_elastic_ip_addresses.py index 585f08ac3..0677d3a86 100644 --- a/tests/test_ec2/test_elastic_ip_addresses.py +++ b/tests/test_ec2/test_elastic_ip_addresses.py @@ -24,7 +24,7 @@ def test_eip_allocate_classic(): "An error occurred (DryRunOperation) when calling the AllocateAddress operation: Request would have succeeded, but DryRun flag is set" ) - standard = client.allocate_address() + standard = client.allocate_address(Domain="standard") standard.should.have.key("PublicIp") standard.should.have.key("Domain").equal("standard") @@ -78,12 +78,22 @@ def test_eip_allocate_vpc(): vpc.should.have.key("AllocationId") vpc.should.have.key("Domain").equal("vpc") + # Ensure that correct fallback is used for the optional attribute `Domain` contains an empty or invalid value + vpc2 = client.allocate_address(Domain="") + vpc3 = client.allocate_address(Domain="xyz") + + vpc2.should.have.key("Domain").equal("vpc") + vpc3.should.have.key("Domain").equal("vpc") + allocation_id = vpc["AllocationId"] + allocation_id2 = vpc["AllocationId"] + allocation_id3 = vpc["AllocationId"] all_addresses = client.describe_addresses()["Addresses"] - [a["AllocationId"] for a in all_addresses if "AllocationId" in a].should.contain( - allocation_id - ) + allocation_ids = [a["AllocationId"] for a in all_addresses if "AllocationId" in a] + allocation_ids.should.contain(allocation_id) + allocation_ids.should.contain(allocation_id2) + allocation_ids.should.contain(allocation_id3) vpc = ec2.VpcAddress(allocation_id) vpc.release() @@ -104,18 +114,6 @@ def test_specific_eip_allocate_vpc(): vpc["PublicIp"].should.be.equal("127.38.43.222") -@mock_ec2 -def test_eip_allocate_invalid_domain(): - """Allocate EIP invalid domain""" - client = boto3.client("ec2", region_name="us-east-1") - - with pytest.raises(ClientError) as ex: - client.allocate_address(Domain="bogus") - ex.value.response["ResponseMetadata"]["HTTPStatusCode"].should.equal(400) - ex.value.response["ResponseMetadata"]["RequestId"].shouldnt.equal(None) - ex.value.response["Error"]["Code"].should.equal("InvalidParameterValue") - - @mock_ec2 def test_eip_associate_classic(): """Associate/Disassociate EIP to classic instance"""