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:
gtt116
2013-05-08 14:18:21 +00:00
parent 6cf5822dc6
commit 5d5448275f

View File

@@ -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