boto_ec2: support network_interface_name when associating an eip addr

This commit is contained in:
Quan Tong Anh 2015-11-13 14:14:54 +07:00
parent 147cae12b0
commit 07b9309617

View File

@ -53,6 +53,7 @@ from distutils.version import LooseVersion as _LooseVersion # pylint: disable=i
# Import Salt libs # Import Salt libs
import salt.utils.compat import salt.utils.compat
import salt.utils.exactly_one
import salt.ext.six as six import salt.ext.six as six
from salt.exceptions import SaltInvocationError, CommandExecutionError from salt.exceptions import SaltInvocationError, CommandExecutionError
@ -243,7 +244,7 @@ def release_eip_address(public_ip=None, allocation_id=None, region=None, key=Non
.. versionadded:: Boron .. versionadded:: Boron
''' '''
if not _exactly_one((public_ip, allocation_id)): if not salt.utils.exactly_one((public_ip, allocation_id)):
raise SaltInvocationError('Exactly one (but not both) of \'public_ip\' ' raise SaltInvocationError('Exactly one (but not both) of \'public_ip\' '
'or \'allocation_id\' must be provided') 'or \'allocation_id\' must be provided')
@ -257,8 +258,10 @@ def release_eip_address(public_ip=None, allocation_id=None, region=None, key=Non
def associate_eip_address(instance_id=None, instance_name=None, public_ip=None, def associate_eip_address(instance_id=None, instance_name=None, public_ip=None,
allocation_id=None, network_interface_id=None, private_ip_address=None, allocation_id=None, network_interface_id=None,
allow_reassociation=False, region=None, key=None, keyid=None, profile=None): network_interface_name=None, private_ip_address=None,
allow_reassociation=False, region=None, key=None,
keyid=None, profile=None):
''' '''
Associate an Elastic IP address with a currently running instance or a network interface. Associate an Elastic IP address with a currently running instance or a network interface.
This requires exactly one of either 'public_ip' or 'allocation_id', depending This requires exactly one of either 'public_ip' or 'allocation_id', depending
@ -273,7 +276,9 @@ def associate_eip_address(instance_id=None, instance_name=None, public_ip=None,
allocation_id allocation_id
(string) Allocation ID for a VPC-based EIP. (string) Allocation ID for a VPC-based EIP.
network_interface_id network_interface_id
(string) ID of the network interface to associate the EIP with (string) - ID of the network interface to associate the EIP with
network_interface_name
(string) - Name of the network interface to associate the EIP with
private_ip_address private_ip_address
(string) The primary or secondary private IP address to associate with the Elastic IP address. (string) The primary or secondary private IP address to associate with the Elastic IP address.
allow_reassociation allow_reassociation
@ -290,21 +295,38 @@ def associate_eip_address(instance_id=None, instance_name=None, public_ip=None,
.. versionadded:: Boron .. versionadded:: Boron
''' '''
if not network_interface_id: if not salt.utils.exactly_one((instance_id, instance_name,
if not _exactly_one((instance_id, instance_name)): network_interface_id,
raise SaltInvocationError('Exactly one (but not both) of \'instance_id\' ' network_interface_name)):
'or \'instance_name\' must be provided or a network_interface_id') raise SaltInvocationError("Exactly one of 'instance_id', "
"'instance_name', 'network_interface_id', "
"'network_interface_name' must be provided")
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile) conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if instance_name: if instance_name:
try: try:
instance_id = get_id(name=instance_name, region=region, key=key, keyid=keyid, profile=profile) instance_id = get_id(name=instance_name, region=region, key=key,
keyid=keyid, profile=profile)
except boto.exception.BotoServerError as e: except boto.exception.BotoServerError as e:
log.error(e) log.error(e)
return False return False
if not instance_id: if not instance_id:
log.error("Given instance_name cannot be mapped to an instance_id") log.error("Given instance_name '{0}' cannot be mapped to an "
"instance_id".format(instance_name))
return False
if network_interface_name:
try:
network_interface_id = get_network_interface_id(
network_interface_name, region=region, key=key, keyid=keyid,
profile=profile)
except boto.exception.BotoServerError as e:
log.error(e)
return False
if not network_interface_id:
log.error("Given network_interface_name '{0}' cannot be mapped to "
"an network_interface_id".format(network_interface_name))
return False return False
try: try: