Extract getting instance's AZ into a helper module.
An instance's availability zone logic may used by many modules, so move it from extend API into helper module `nova.availability_zones`. Also add some tests for the new method. part of bp: different-availability-zone-filter Change-Id: I5916a0b09d5dddec338e8e36503953720dfd6bcd
This commit is contained in:
parent
5dba32a23e
commit
488fcb4ad3
|
@ -21,34 +21,16 @@ from nova.api.openstack import extensions
|
|||
from nova.api.openstack import wsgi
|
||||
from nova.api.openstack import xmlutil
|
||||
from nova import availability_zones
|
||||
from nova.openstack.common import memorycache
|
||||
|
||||
# NOTE(vish): azs don't change that often, so cache them for an hour to
|
||||
# avoid hitting the db multiple times on every request.
|
||||
AZ_CACHE_SECONDS = 60 * 60
|
||||
authorize = extensions.soft_extension_authorizer('compute',
|
||||
'extended_availability_zone')
|
||||
|
||||
|
||||
class ExtendedAZController(wsgi.Controller):
|
||||
def __init__(self):
|
||||
self.mc = memorycache.get_client()
|
||||
|
||||
def _get_host_az(self, context, instance):
|
||||
host = str(instance.get('host'))
|
||||
if not host:
|
||||
return None
|
||||
cache_key = "azcache-%s" % host
|
||||
az = self.mc.get(cache_key)
|
||||
if not az:
|
||||
elevated = context.elevated()
|
||||
az = availability_zones.get_host_availability_zone(elevated, host)
|
||||
self.mc.set(cache_key, az, AZ_CACHE_SECONDS)
|
||||
return az
|
||||
|
||||
def _extend_server(self, context, server, instance):
|
||||
key = "%s:availability_zone" % Extended_availability_zone.alias
|
||||
server[key] = self._get_host_az(context, instance)
|
||||
server[key] = availability_zones.get_instance_availability_zone(
|
||||
context, instance)
|
||||
|
||||
@wsgi.extends
|
||||
def show(self, req, resp_obj, id):
|
||||
|
|
|
@ -18,6 +18,12 @@
|
|||
from oslo.config import cfg
|
||||
|
||||
from nova import db
|
||||
from nova.openstack.common import memorycache
|
||||
|
||||
# NOTE(vish): azs don't change that often, so cache them for an hour to
|
||||
# avoid hitting the db multiple times on every request.
|
||||
AZ_CACHE_SECONDS = 60 * 60
|
||||
MC = memorycache.get_client()
|
||||
|
||||
availability_zone_opts = [
|
||||
cfg.StrOpt('internal_service_availability_zone',
|
||||
|
@ -81,3 +87,18 @@ def get_availability_zones(context):
|
|||
if zone not in not_available_zones:
|
||||
not_available_zones.append(zone)
|
||||
return (available_zones, not_available_zones)
|
||||
|
||||
|
||||
def get_instance_availability_zone(context, instance):
|
||||
"""Return availability zone of specified instance."""
|
||||
host = str(instance.get('host'))
|
||||
if not host:
|
||||
return None
|
||||
|
||||
cache_key = "azcache-%s" % host
|
||||
az = MC.get(cache_key)
|
||||
if not az:
|
||||
elevated = context.elevated()
|
||||
az = get_host_availability_zone(elevated, host)
|
||||
MC.set(cache_key, az, AZ_CACHE_SECONDS)
|
||||
return az
|
||||
|
|
|
@ -25,6 +25,7 @@ from nova import availability_zones as az
|
|||
from nova import context
|
||||
from nova import db
|
||||
from nova import test
|
||||
from nova.tests.api.openstack import fakes
|
||||
|
||||
CONF = cfg.CONF
|
||||
CONF.import_opt('internal_service_availability_zone',
|
||||
|
@ -155,3 +156,23 @@ class AvailabilityZoneTestCases(test.TestCase):
|
|||
|
||||
self.assertEquals(zones, ['nova-test', 'nova-test2'])
|
||||
self.assertEquals(not_zones, ['nova-test3', 'nova'])
|
||||
|
||||
def test_get_instance_availability_zone_default_value(self):
|
||||
"""Test get right availability zone by given an instance."""
|
||||
fake_inst_id = 162
|
||||
fake_inst = fakes.stub_instance(fake_inst_id, host=self.host)
|
||||
|
||||
self.assertEqual(self.default_az,
|
||||
az.get_instance_availability_zone(self.context, fake_inst))
|
||||
|
||||
def test_get_instance_availability_zone_from_aggregate(self):
|
||||
"""Test get availability zone from aggregate by given an instance."""
|
||||
host = 'host170'
|
||||
service = self._create_service_with_topic('compute', host)
|
||||
self._add_to_aggregate(service, self.agg)
|
||||
|
||||
fake_inst_id = 174
|
||||
fake_inst = fakes.stub_instance(fake_inst_id, host=host)
|
||||
|
||||
self.assertEqual(self.availability_zone,
|
||||
az.get_instance_availability_zone(self.context, fake_inst))
|
||||
|
|
Loading…
Reference in New Issue