[cloud] Support reuse of unallocated Elastic IP's. (#30322)

* Return correct changed status when EIP is reused

When reusing an existing EIP, the changed status
should be False, not True.

* If public_ip is given and it exists, return it

Ensure EIP allocation returns existing public_ip correctly
pull/31048/head
Will Thames 7 years ago committed by Ryan Brown
parent 4f67e855ed
commit c5971047a4

@ -255,9 +255,9 @@ def allocate_address(ec2, domain, reuse_existing_ip_allowed):
unassociated_addresses = [a for a in all_addresses unassociated_addresses = [a for a in all_addresses
if not a.instance_id] if not a.instance_id]
if unassociated_addresses: if unassociated_addresses:
return unassociated_addresses[0] return unassociated_addresses[0], False
return ec2.allocate_address(domain=domain) return ec2.allocate_address(domain=domain), True
def release_address(ec2, address, check_mode): def release_address(ec2, address, check_mode):
@ -305,8 +305,7 @@ def ensure_present(ec2, module, domain, address, private_ip_address, device_id,
if check_mode: if check_mode:
return {'changed': True} return {'changed': True}
address = allocate_address(ec2, domain, reuse_existing_ip_allowed) address, changed = allocate_address(ec2, domain, reuse_existing_ip_allowed)
changed = True
if device_id: if device_id:
# Allocate an IP for instance since no public_ip was provided # Allocate an IP for instance since no public_ip was provided
@ -404,15 +403,18 @@ def main():
if device_id: if device_id:
address = find_address(ec2, public_ip, device_id, isinstance=is_instance) address = find_address(ec2, public_ip, device_id, isinstance=is_instance)
else: else:
address = False address = find_address(ec2, public_ip, None)
if state == 'present': if state == 'present':
if device_id: if device_id:
result = ensure_present(ec2, module, domain, address, private_ip_address, device_id, result = ensure_present(ec2, module, domain, address, private_ip_address, device_id,
reuse_existing_ip_allowed, module.check_mode, isinstance=is_instance) reuse_existing_ip_allowed, module.check_mode, isinstance=is_instance)
else: else:
address = allocate_address(ec2, domain, reuse_existing_ip_allowed) if address:
result = {'changed': True, 'public_ip': address.public_ip, 'allocation_id': address.allocation_id} changed = False
else:
address, changed = allocate_address(ec2, domain, reuse_existing_ip_allowed)
result = {'changed': changed, 'public_ip': address.public_ip, 'allocation_id': address.allocation_id}
else: else:
if device_id: if device_id:
disassociated = ensure_absent(ec2, domain, address, device_id, module.check_mode, isinstance=is_instance) disassociated = ensure_absent(ec2, domain, address, device_id, module.check_mode, isinstance=is_instance)
@ -423,7 +425,6 @@ def main():
else: else:
result = {'changed': disassociated['changed'], 'disassociated': disassociated, 'released': {'changed': False}} result = {'changed': disassociated['changed'], 'disassociated': disassociated, 'released': {'changed': False}}
else: else:
address = find_address(ec2, public_ip, None)
released = release_address(ec2, address, module.check_mode) released = release_address(ec2, address, module.check_mode)
result = {'changed': released['changed'], 'disassociated': {'changed': False}, 'released': released} result = {'changed': released['changed'], 'disassociated': {'changed': False}, 'released': released}

Loading…
Cancel
Save