From 12188733b73fe5511692f05f0a1ed586a2fa7777 Mon Sep 17 00:00:00 2001 From: zane Date: Thu, 10 May 2018 23:39:19 -0700 Subject: [PATCH] adding Address reallocate capability for EIP --- moto/ec2/models.py | 15 ++++++++++----- moto/ec2/responses/elastic_ip_addresses.py | 7 ++++++- tests/test_ec2/test_elastic_ip_addresses.py | 11 +++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/moto/ec2/models.py b/moto/ec2/models.py index 31bfb4839..674e0bddb 100755 --- a/moto/ec2/models.py +++ b/moto/ec2/models.py @@ -3159,8 +3159,11 @@ class SpotFleetBackend(object): class ElasticAddress(object): - def __init__(self, domain): - self.public_ip = random_ip() + def __init__(self, domain, address=None): + if address: + self.public_ip = address + else: + self.public_ip = random_ip() self.allocation_id = random_eip_allocation_id() if domain == "vpc" else None self.domain = domain self.instance = None @@ -3222,11 +3225,13 @@ class ElasticAddressBackend(object): self.addresses = [] super(ElasticAddressBackend, self).__init__() - def allocate_address(self, domain): + def allocate_address(self, domain, address=None): if domain not in ['standard', 'vpc']: raise InvalidDomainError(domain) - - address = ElasticAddress(domain) + if address: + address = ElasticAddress(domain, address) + else: + address = ElasticAddress(domain) self.addresses.append(address) return address diff --git a/moto/ec2/responses/elastic_ip_addresses.py b/moto/ec2/responses/elastic_ip_addresses.py index 11c1d9c1f..6e1c9fe38 100644 --- a/moto/ec2/responses/elastic_ip_addresses.py +++ b/moto/ec2/responses/elastic_ip_addresses.py @@ -7,8 +7,13 @@ class ElasticIPAddresses(BaseResponse): def allocate_address(self): domain = self._get_param('Domain', if_none='standard') + reallocate_address = self._get_param('Address', if_none=None) if self.is_not_dryrun('AllocateAddress'): - address = self.ec2_backend.allocate_address(domain) + if reallocate_address: + address = self.ec2_backend.allocate_address( + domain, address=reallocate_address) + else: + address = self.ec2_backend.allocate_address(domain) template = self.response_template(ALLOCATE_ADDRESS_RESPONSE) return template.render(address=address) diff --git a/tests/test_ec2/test_elastic_ip_addresses.py b/tests/test_ec2/test_elastic_ip_addresses.py index 709bdc33b..ca6637b18 100644 --- a/tests/test_ec2/test_elastic_ip_addresses.py +++ b/tests/test_ec2/test_elastic_ip_addresses.py @@ -62,6 +62,17 @@ def test_eip_allocate_vpc(): logging.debug("vpc alloc_id:".format(vpc.allocation_id)) vpc.release() +@mock_ec2 +def test_specific_eip_allocate_vpc(): + """Allocate VPC EIP with specific address""" + service = boto3.resource('ec2', region_name='us-west-1') + client = boto3.client('ec2', region_name='us-west-1') + + vpc = client.allocate_address(Domain="vpc", Address="127.38.43.222") + vpc['Domain'].should.be.equal("vpc") + vpc['PublicIp'].should.be.equal("127.38.43.222") + logging.debug("vpc alloc_id:".format(vpc['AllocationId'])) + @mock_ec2_deprecated def test_eip_allocate_invalid_domain():