neutron/neutron/extensions/availability_zone.py
Armando Migliaccio 17563a802e Adopt neutron-lib plugin directory
Neutron Manager is loaded at the very startup of the neutron
server process and with it plugins are loaded and stored for
lookup purposes as their references are widely used across the
entire neutron codebase.

Rather than holding these references directly in NeutronManager
this patch refactors the code so that these references are held
by a plugin directory.

This allows subprojects and other parts of the Neutron codebase
to use the directory in lieu of the manager. The result is a
leaner, cleaner, and more decoupled code.

Usage pattern [1,2] can be translated to [3,4] respectively.

[1] manager.NeutronManager.get_service_plugins()[FOO]
[2] manager.NeutronManager.get_plugin()
[3] directory.get_plugin(FOO)
[4] directory.get_plugin()

The more entangled part is in the neutron unit tests, where the
use of the manager can be simplified as mocking is typically
replaced by a call to the directory add_plugin() method. This is
safe as each test case gets its own copy of the plugin directory.
That said, unit tests that look more like API tests and that rely on
the entire plugin machinery, need some tweaking to avoid stumbling
into plugin loading failures.

Due to the massive use of the manager, deprecation warnings are
considered impractical as they cause logs to bloat out of proportion.

Follow-up patches that show how to adopt the directory in neutron
subprojects are tagged with topic:plugin-directory.

NeutronLibImpact

Partially-implements: blueprint neutron-lib

Change-Id: I7331e914234c5f0b7abe836604fdd7e4067551cf
2016-11-23 04:45:33 -07:00

140 lines
4.3 KiB
Python

#
# 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.
import abc
from neutron_lib.api import validators
from neutron_lib import exceptions
from neutron_lib.plugins import directory
from oslo_serialization import jsonutils
import six
from neutron._i18n import _
from neutron.api import extensions
from neutron.api.v2 import attributes as attr
from neutron.api.v2 import base
AZ_HINTS_DB_LEN = 255
# resource independent common methods
def convert_az_list_to_string(az_list):
return jsonutils.dumps(az_list)
def convert_az_string_to_list(az_string):
return jsonutils.loads(az_string) if az_string else []
def _validate_availability_zone_hints(data, valid_value=None):
# syntax check only here. existence of az will be checked later.
msg = validators.validate_list_of_unique_strings(data)
if msg:
return msg
az_string = convert_az_list_to_string(data)
if len(az_string) > AZ_HINTS_DB_LEN:
msg = _("Too many availability_zone_hints specified")
raise exceptions.InvalidInput(error_message=msg)
validators.add_validator('availability_zone_hints',
_validate_availability_zone_hints)
# Attribute Map
RESOURCE_NAME = 'availability_zone'
AVAILABILITY_ZONES = 'availability_zones'
AZ_HINTS = 'availability_zone_hints'
# name: name of availability zone (string)
# resource: type of resource: 'network' or 'router'
# state: state of availability zone: 'available' or 'unavailable'
# It means whether users can use the availability zone.
RESOURCE_ATTRIBUTE_MAP = {
AVAILABILITY_ZONES: {
'name': {'is_visible': True},
'resource': {'is_visible': True},
'state': {'is_visible': True}
}
}
EXTENDED_ATTRIBUTES_2_0 = {
'agents': {
RESOURCE_NAME: {'allow_post': False, 'allow_put': False,
'is_visible': True}
}
}
class AvailabilityZoneNotFound(exceptions.NotFound):
message = _("AvailabilityZone %(availability_zone)s could not be found.")
class Availability_zone(extensions.ExtensionDescriptor):
"""Availability zone extension."""
@classmethod
def get_name(cls):
return "Availability Zone"
@classmethod
def get_alias(cls):
return "availability_zone"
@classmethod
def get_description(cls):
return "The availability zone extension."
@classmethod
def get_updated(cls):
return "2015-01-01T10:00:00-00:00"
def get_required_extensions(self):
return ["agent"]
@classmethod
def get_resources(cls):
"""Returns Ext Resources."""
my_plurals = [(key, key[:-1]) for key in RESOURCE_ATTRIBUTE_MAP.keys()]
attr.PLURALS.update(dict(my_plurals))
plugin = directory.get_plugin()
params = RESOURCE_ATTRIBUTE_MAP.get(AVAILABILITY_ZONES)
controller = base.create_resource(AVAILABILITY_ZONES,
RESOURCE_NAME, plugin, params)
ex = extensions.ResourceExtension(AVAILABILITY_ZONES, controller)
return [ex]
def get_extended_resources(self, version):
if version == "2.0":
return dict(list(EXTENDED_ATTRIBUTES_2_0.items()) +
list(RESOURCE_ATTRIBUTE_MAP.items()))
else:
return {}
@six.add_metaclass(abc.ABCMeta)
class AvailabilityZonePluginBase(object):
"""REST API to operate the Availability Zone."""
@abc.abstractmethod
def get_availability_zones(self, context, filters=None, fields=None,
sorts=None, limit=None, marker=None,
page_reverse=False):
"""Return availability zones which a resource belongs to"""
@abc.abstractmethod
def validate_availability_zones(self, context, resource_type,
availability_zones):
"""Verify that the availability zones exist."""