summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Meade <alex.meade@rackspace.com>2011-12-06 15:32:33 -0500
committerAlex Meade <alex.meade@rackspace.com>2011-12-06 15:33:33 -0500
commit3d9f0ed000ebe119cb7e15f957ee85b668086fea (patch)
tree23834447c48f777ba0cf384e4fad9892b2ed41fa
parent882b1e475de1ef71d7d3f0b50a58f91569905a75 (diff)
downloadnova-3d9f0ed000ebe119cb7e15f957ee85b668086fea.tar.gz
nova-3d9f0ed000ebe119cb7e15f957ee85b668086fea.tar.xz
nova-3d9f0ed000ebe119cb7e15f957ee85b668086fea.zip
Update associate_floating_ip to use instance objs
Related to blueprint internal-uuids. Also cleans up some unused variables Change-Id: I8adeceac8f4ab2894c48c1e1c7e1c7eab52e42d0
-rw-r--r--nova/api/ec2/cloud.py3
-rw-r--r--nova/api/openstack/v2/contrib/floating_ips.py3
-rw-r--r--nova/compute/api.py13
-rw-r--r--nova/tests/api/openstack/v2/contrib/test_floating_ips.py3
-rw-r--r--nova/tests/test_compute.py51
5 files changed, 60 insertions, 13 deletions
diff --git a/nova/api/ec2/cloud.py b/nova/api/ec2/cloud.py
index 5eea3269a..cdedac679 100644
--- a/nova/api/ec2/cloud.py
+++ b/nova/api/ec2/cloud.py
@@ -1238,8 +1238,9 @@ class CloudController(object):
LOG.audit(_("Associate address %(public_ip)s to"
" instance %(instance_id)s") % locals(), context=context)
instance_id = ec2utils.ec2_id_to_id(instance_id)
+ instance = self.compute_api.get(context, instance_id)
self.compute_api.associate_floating_ip(context,
- instance_id=instance_id,
+ instance,
address=public_ip)
return {'associateResponse': ["Address associated."]}
diff --git a/nova/api/openstack/v2/contrib/floating_ips.py b/nova/api/openstack/v2/contrib/floating_ips.py
index 072ada1ba..760ed65ab 100644
--- a/nova/api/openstack/v2/contrib/floating_ips.py
+++ b/nova/api/openstack/v2/contrib/floating_ips.py
@@ -168,7 +168,8 @@ class Floating_ips(extensions.ExtensionDescriptor):
raise webob.exc.HTTPBadRequest(explanation=msg)
try:
- self.compute_api.associate_floating_ip(context, instance_id,
+ instance = self.compute_api.get(context, instance_id)
+ self.compute_api.associate_floating_ip(context, instance,
address)
except exception.ApiError, e:
raise webob.exc.HTTPBadRequest(explanation=e.message)
diff --git a/nova/compute/api.py b/nova/compute/api.py
index 787817e31..d3b329685 100644
--- a/nova/compute/api.py
+++ b/nova/compute/api.py
@@ -701,7 +701,6 @@ class API(base.Base):
context.project_id,
security_group_name)
- instance_id = instance['id']
instance_uuid = instance['uuid']
#check if the security group is associated with the server
@@ -730,7 +729,6 @@ class API(base.Base):
context.project_id,
security_group_name)
- instance_id = instance['id']
instance_uuid = instance['uuid']
#check if the security group is associated with the server
@@ -1015,7 +1013,6 @@ class API(base.Base):
return instances
def _get_instances_by_filters(self, context, filters):
- ids = None
if 'ip6' in filters or 'ip' in filters:
res = self.network_api.get_instance_uuids_by_ip_filter(context,
filters)
@@ -1332,7 +1329,6 @@ class API(base.Base):
@scheduler_api.reroute_compute("pause")
def pause(self, context, instance):
"""Pause the given instance."""
- instance_id = instance["id"]
instance_uuid = instance["uuid"]
self.update(context,
instance,
@@ -1343,7 +1339,6 @@ class API(base.Base):
@scheduler_api.reroute_compute("unpause")
def unpause(self, context, instance):
"""Unpause the given instance."""
- instance_id = instance["id"]
instance_uuid = instance["uuid"]
self.update(context,
instance,
@@ -1381,7 +1376,6 @@ class API(base.Base):
@scheduler_api.reroute_compute("suspend")
def suspend(self, context, instance):
"""Suspend the given instance."""
- instance_id = instance["id"]
instance_uuid = instance["uuid"]
self.update(context,
instance,
@@ -1392,7 +1386,6 @@ class API(base.Base):
@scheduler_api.reroute_compute("resume")
def resume(self, context, instance):
"""Resume the given instance."""
- instance_id = instance["id"]
instance_uuid = instance["uuid"]
self.update(context,
instance,
@@ -1536,12 +1529,12 @@ class API(base.Base):
"volume_id": volume_id}})
return instance
- def associate_floating_ip(self, context, instance_id, address):
+ def associate_floating_ip(self, context, instance, address):
"""Makes calls to network_api to associate_floating_ip.
:param address: is a string floating ip address
"""
- instance = self.get(context, instance_id)
+ instance_uuid = instance['uuid']
# TODO(tr3buchet): currently network_info doesn't contain floating IPs
# in its info, if this changes, the next few lines will need to
@@ -1558,7 +1551,7 @@ class API(base.Base):
# support specifying a particular fixed_ip if multiple exist.
if not fixed_ip_addrs:
msg = _("instance |%s| has no fixed_ips. "
- "unable to associate floating ip") % instance_id
+ "unable to associate floating ip") % instance_uuid
raise exception.ApiError(msg)
if len(fixed_ip_addrs) > 1:
LOG.warning(_("multiple fixed_ips exist, using the first: %s"),
diff --git a/nova/tests/api/openstack/v2/contrib/test_floating_ips.py b/nova/tests/api/openstack/v2/contrib/test_floating_ips.py
index a578622fa..0147abe9f 100644
--- a/nova/tests/api/openstack/v2/contrib/test_floating_ips.py
+++ b/nova/tests/api/openstack/v2/contrib/test_floating_ips.py
@@ -16,7 +16,6 @@
from lxml import etree
import webob
-from nova.api.openstack import wsgi
from nova.api.openstack.v2.contrib import floating_ips
from nova import context
from nova import db
@@ -24,6 +23,7 @@ from nova import network
from nova import rpc
from nova import test
from nova.tests.api.openstack import fakes
+from nova import utils
def network_api_get_floating_ip(self, context, id):
@@ -84,6 +84,7 @@ def network_get_instance_nw_info(self, context, instance):
def fake_instance_get(context, instance_id):
return {
"id": 1,
+ "uuid": utils.gen_uuid(),
"name": 'fake',
"user_id": 'fakeuser',
"project_id": '123'}
diff --git a/nova/tests/test_compute.py b/nova/tests/test_compute.py
index bccc590c3..12ce509d9 100644
--- a/nova/tests/test_compute.py
+++ b/nova/tests/test_compute.py
@@ -1627,6 +1627,57 @@ class ComputeAPITestCase(BaseTestCase):
finally:
self.compute.terminate_instance(context, instance['uuid'])
+ def test_associate_floating_ip(self):
+ """Ensure we can associate a floating ip with an instance"""
+ called = {'associate': False}
+
+ def fake_associate_ip_network_api(self, ctxt, floating_address,
+ fixed_address):
+ called['associate'] = True
+
+ nw_info = fake_network.fake_get_instance_nw_info(self.stubs, 1)
+
+ def fake_get_nw_info(self, ctxt, instance):
+ return nw_info
+
+ self.stubs.Set(nova.network.API, 'associate_floating_ip',
+ fake_associate_ip_network_api)
+
+ self.stubs.Set(nova.network.API, 'get_instance_nw_info',
+ fake_get_nw_info)
+
+ instance = self._create_fake_instance()
+ instance_uuid = instance['uuid']
+ address = '0.1.2.3'
+
+ self.compute.run_instance(self.context, instance_uuid)
+ self.compute_api.associate_floating_ip(self.context,
+ instance,
+ address)
+ self.assertTrue(called['associate'])
+ self.compute.terminate_instance(self.context, instance_uuid)
+
+ def test_associate_floating_ip_no_fixed_ip(self):
+ """Should fail if instance has no fixed ip."""
+
+ def fake_get_nw_info(self, ctxt, instance):
+ return []
+
+ self.stubs.Set(nova.network.API, 'get_instance_nw_info',
+ fake_get_nw_info)
+
+ instance = self._create_fake_instance()
+ instance_uuid = instance['uuid']
+ address = '0.1.2.3'
+
+ self.compute.run_instance(self.context, instance_uuid)
+ self.assertRaises(exception.ApiError,
+ self.compute_api.associate_floating_ip,
+ self.context,
+ instance,
+ address)
+ self.compute.terminate_instance(self.context, instance_uuid)
+
def test_get(self):
"""Test get instance"""
self.maxDiff = None