Fix assigning public ip to instances. Closes #729.

This commit is contained in:
Steve Pulec 2016-10-12 22:02:06 -04:00
parent 094b10cea1
commit 27f1bece9b
3 changed files with 34 additions and 0 deletions

View File

@ -549,6 +549,10 @@ class Instance(BotoInstance, TaggedEC2Resource):
self.attach_eni(use_nic, device_index)
def set_ip(self, ip_address):
# Should we be creating a new ENI?
self.nics[0].public_ip = ip_address
def attach_eni(self, eni, device_index):
device_index = int(device_index)
self.nics[device_index] = eni
@ -2663,6 +2667,7 @@ class ElasticAddressBackend(object):
eip.eni.public_ip = eip.public_ip
if eip.domain == "vpc":
eip.association_id = random_eip_association_id()
instance.set_ip(eip.public_ip)
return eip

View File

@ -207,6 +207,7 @@ EC2_RUN_INSTANCES = """<RunInstancesResponse xmlns="http://ec2.amazonaws.com/doc
</instanceState>
<privateDnsName>{{ instance.private_dns }}</privateDnsName>
<publicDnsName>{{ instance.public_dns }}</publicDnsName>
<dnsName>{{ instance.public_dns }}</dnsName>
<reason/>
<keyName>{{ instance.key_name }}</keyName>
<amiLaunchIndex>0</amiLaunchIndex>
@ -335,6 +336,7 @@ EC2_DESCRIBE_INSTANCES = """<DescribeInstancesResponse xmlns="http://ec2.amazona
</instanceState>
<privateDnsName>{{ instance.private_dns }}</privateDnsName>
<publicDnsName>{{ instance.public_dns }}</publicDnsName>
<dnsName>{{ instance.public_dns }}</dnsName>
<reason>{{ instance._reason }}</reason>
<keyName>{{ instance.key_name }}</keyName>
<amiLaunchIndex>0</amiLaunchIndex>

View File

@ -4,6 +4,7 @@ import tests.backport_assert_raises
from nose.tools import assert_raises
import boto
import boto3
from boto.exception import EC2ResponseError
import six
@ -110,6 +111,32 @@ def test_eip_associate_vpc():
instance.terminate()
@mock_ec2
def test_eip_boto3_vpc_association():
"""Associate EIP to VPC instance in a new subnet with boto3"""
session = boto3.session.Session(region_name='us-west-1')
service = session.resource('ec2')
client = session.client('ec2')
vpc_res = client.create_vpc(CidrBlock='10.0.0.0/24')
subnet_res = client.create_subnet(
VpcId=vpc_res['Vpc']['VpcId'], CidrBlock='10.0.0.0/24')
instance = service.create_instances(**{
'InstanceType': 't2.micro',
'ImageId': 'ami-test',
'MinCount': 1,
'MaxCount': 1,
'SubnetId': subnet_res['Subnet']['SubnetId']
})[0]
allocation_id = client.allocate_address(Domain='vpc')['AllocationId']
association_id = client.associate_address(
InstanceId=instance.id,
AllocationId=allocation_id,
AllowReassociation=False)
instance.load()
instance.public_ip_address.should_not.be.none
instance.public_dns_name.should_not.be.none
@mock_ec2
def test_eip_associate_network_interface():
"""Associate/Disassociate EIP to NIC"""