From 1b2c121f133b15d0fe3f6ebdb131ac9998bc4b83 Mon Sep 17 00:00:00 2001 From: Phil Day Date: Thu, 4 Apr 2013 18:00:49 +0100 Subject: Allow a floating IP to be associated to a specific fixed IP The current floating IP API extension only accepts the instance ID and the floating address to be assigned. Where the instance is connected to more than one network the behaviour is to associated the floating IP with the first fixed IP of the instances (and to log a warning) This change introduces a new extension which wehn loaded adds a fixed IP address as an optional parameter, allowing the floating IP to be associated with a specific fixed IP. Without this extension, or without the optional parameters, the API behaviour is unchanged. If specified the fixed IP must be associated with the instance. DocImpact Implements blueprint multi-nic-floating-ip-assignment Change-Id: I9241137ad794cdf7f452ed84e9445f0e11fdd44e --- .../compute/contrib/extended_floating_ips.py | 27 +++++++++++++++++++ nova/api/openstack/compute/contrib/floating_ips.py | 30 ++++++++++++++-------- 2 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 nova/api/openstack/compute/contrib/extended_floating_ips.py (limited to 'nova/api') diff --git a/nova/api/openstack/compute/contrib/extended_floating_ips.py b/nova/api/openstack/compute/contrib/extended_floating_ips.py new file mode 100644 index 000000000..06f1fa903 --- /dev/null +++ b/nova/api/openstack/compute/contrib/extended_floating_ips.py @@ -0,0 +1,27 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2012 OpenStack Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License + +from nova.api.openstack import extensions + + +class Extended_floating_ips(extensions.ExtensionDescriptor): + """Adds optional fixed_address to the add floating IP command.""" + + name = "ExtendedFloatingIps" + alias = "os-extended-floating-ips" + namespace = ("http://docs.openstack.org/compute/ext/" + "extended_floating_ips/api/v2") + updated = "2013-04-19T00:00:00+00:00" diff --git a/nova/api/openstack/compute/contrib/floating_ips.py b/nova/api/openstack/compute/contrib/floating_ips.py index bf1246ccb..32cf1eece 100644 --- a/nova/api/openstack/compute/contrib/floating_ips.py +++ b/nova/api/openstack/compute/contrib/floating_ips.py @@ -198,10 +198,11 @@ class FloatingIPController(object): class FloatingIPActionController(wsgi.Controller): - def __init__(self, *args, **kwargs): + def __init__(self, ext_mgr=None, *args, **kwargs): super(FloatingIPActionController, self).__init__(*args, **kwargs) self.compute_api = compute.API() self.network_api = network.API() + self.ext_mgr = ext_mgr @wsgi.action('addFloatingIp') def _add_floating_ip(self, req, id, body): @@ -230,18 +231,27 @@ class FloatingIPActionController(wsgi.Controller): msg = _('No fixed ips associated to instance') raise webob.exc.HTTPBadRequest(explanation=msg) - # TODO(tr3buchet): this will associate the floating IP with the - # first fixed_ip an instance has. This should be - # changed to support specifying a particular fixed_ip if - # multiple exist. - if len(fixed_ips) > 1: - msg = _('multiple fixed_ips exist, using the first: %s') - LOG.warning(msg, fixed_ips[0]['address']) + fixed_address = None + if self.ext_mgr.is_loaded('os-extended-floating-ips'): + if 'fixed_address' in body['addFloatingIp']: + fixed_address = body['addFloatingIp']['fixed_address'] + for fixed in fixed_ips: + if fixed['address'] == fixed_address: + break + else: + msg = _('Specified fixed address not assigned to instance') + raise webob.exc.HTTPBadRequest(explanation=msg) + + if not fixed_address: + fixed_address = fixed_ips[0]['address'] + if len(fixed_ips) > 1: + msg = _('multiple fixed_ips exist, using the first: %s') + LOG.warning(msg, fixed_address) try: self.network_api.associate_floating_ip(context, instance, floating_address=address, - fixed_address=fixed_ips[0]['address']) + fixed_address=fixed_address) except exception.FloatingIpAssociated: msg = _('floating ip is already associated') raise webob.exc.HTTPBadRequest(explanation=msg) @@ -318,6 +328,6 @@ class Floating_ips(extensions.ExtensionDescriptor): return resources def get_controller_extensions(self): - controller = FloatingIPActionController() + controller = FloatingIPActionController(self.ext_mgr) extension = extensions.ControllerExtension(self, 'servers', controller) return [extension] -- cgit