EC2: AllocateAddress fallback domain (#5729)

This commit is contained in:
Viren Nadkarni 2022-12-06 02:50:03 +05:30 committed by GitHub
parent d359b5d074
commit 4ec748542f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 26 deletions

View File

@ -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.")

View File

@ -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:

View File

@ -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)

View File

@ -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"""