From 3cf5df041d05749c503528072bc7aec327567457 Mon Sep 17 00:00:00 2001 From: Boden R Date: Wed, 15 Nov 2017 10:28:00 -0700 Subject: [PATCH] use agent api def from neutron-lib The agent extension's API definition was moved into neutron-lib with commit Iae4746aa2e3f68a7120696a12a6ae717115dbc31 This patch consumes it by removing the rehomed code and using the APIExtensionDescriptor for the parent class of the extension. NeutronLibImpact Change-Id: I18aebec1e2b5b361d9b96192d4a59e5424c6a80b --- neutron/db/agents_db.py | 14 +-- neutron/db/agentschedulers_db.py | 4 +- neutron/extensions/agent.py | 94 ++----------------- neutron/extensions/dhcpagentscheduler.py | 4 +- neutron/extensions/l3agentscheduler.py | 4 +- .../unit/extensions/test_availability_zone.py | 12 --- 6 files changed, 22 insertions(+), 110 deletions(-) diff --git a/neutron/db/agents_db.py b/neutron/db/agents_db.py index ddac55b48ef..84d61c78098 100644 --- a/neutron/db/agents_db.py +++ b/neutron/db/agents_db.py @@ -18,11 +18,13 @@ import datetime from eventlet import greenthread from neutron_lib.agent import constants as agent_consts from neutron_lib.api import converters +from neutron_lib.api.definitions import agent as agent_apidef from neutron_lib.callbacks import events from neutron_lib.callbacks import registry from neutron_lib.callbacks import resources from neutron_lib import constants from neutron_lib import context +from neutron_lib.exceptions import agent as agent_exc from neutron_lib.exceptions import availability_zone as az_exc from neutron_lib.plugins import directory from oslo_config import cfg @@ -122,7 +124,7 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): def _get_agent(self, context, id): agent = agent_obj.Agent.get_object(context, id=id) if not agent: - raise ext_agent.AgentNotFound(id=id) + raise agent_exc.AgentNotFound(id=id) return agent @db_api.retry_if_session_inactive() @@ -186,8 +188,8 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): return load def _make_agent_dict(self, agent, fields=None): - attr = ext_agent.RESOURCE_ATTRIBUTE_MAP.get( - ext_agent.RESOURCE_NAME + 's') + attr = agent_apidef.RESOURCE_ATTRIBUTE_MAP.get( + agent_apidef.COLLECTION_NAME) res = dict((k, agent[k]) for k in attr if k not in ['alive', 'configurations']) res['alive'] = not utils.is_agent_down( @@ -266,10 +268,10 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): agent_type=agent_type, host=host) if not agent_objs: - raise ext_agent.AgentNotFoundByTypeHost(agent_type=agent_type, + raise agent_exc.AgentNotFoundByTypeHost(agent_type=agent_type, host=host) if len(agent_objs) > 1: - raise ext_agent.MultipleAgentFoundByTypeHost( + raise agent_exc.MultipleAgentFoundByTypeHost( agent_type=agent_type, host=host) return agent_objs[0] @@ -343,7 +345,7 @@ class AgentDbMixin(ext_agent.AgentPluginBase, AgentAvailabilityZoneMixin): agent.update_fields(res) agent.update() event_type = events.AFTER_UPDATE - except ext_agent.AgentNotFoundByTypeHost: + except agent_exc.AgentNotFoundByTypeHost: greenthread.sleep(0) res['created_at'] = current_time res['started_at'] = current_time diff --git a/neutron/db/agentschedulers_db.py b/neutron/db/agentschedulers_db.py index a47369a5091..73859b24580 100644 --- a/neutron/db/agentschedulers_db.py +++ b/neutron/db/agentschedulers_db.py @@ -19,6 +19,7 @@ import time from neutron_lib import constants from neutron_lib import context as ncontext +from neutron_lib.exceptions import agent as agent_exc from oslo_config import cfg from oslo_log import log as logging import oslo_messaging @@ -30,7 +31,6 @@ from neutron.common import utils from neutron.conf.agent.database import agentschedulers_db from neutron.db import agents_db from neutron.db.availability_zone import network as network_az -from neutron.extensions import agent as ext_agent from neutron.extensions import dhcpagentscheduler from neutron.objects import network from neutron import worker as neutron_worker @@ -432,7 +432,7 @@ class DhcpAgentSchedulerDbMixin(dhcpagentscheduler try: agent = self._get_agent_by_type_and_host( context, constants.AGENT_TYPE_DHCP, host) - except ext_agent.AgentNotFoundByTypeHost: + except agent_exc.AgentNotFoundByTypeHost: LOG.debug("DHCP Agent not found on host %s", host) return [] diff --git a/neutron/extensions/agent.py b/neutron/extensions/agent.py index df7c55483c8..31e33940901 100644 --- a/neutron/extensions/agent.py +++ b/neutron/extensions/agent.py @@ -15,113 +15,35 @@ import abc -from neutron_lib.api import converters +from neutron_lib.api.definitions import agent as apidef from neutron_lib.api import extensions as api_extensions -from neutron_lib.db import constants as db_const from neutron_lib import exceptions from neutron_lib.plugins import directory import six -from neutron._i18n import _ from neutron.api import extensions from neutron.api.v2 import base -# Attribute Map -RESOURCE_NAME = 'agent' -RESOURCE_ATTRIBUTE_MAP = { - RESOURCE_NAME + 's': { - 'id': {'allow_post': False, 'allow_put': False, - 'validate': {'type:uuid': None}, - 'is_visible': True}, - 'agent_type': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'binary': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'topic': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'host': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'admin_state_up': {'allow_post': False, 'allow_put': True, - 'convert_to': converters.convert_to_boolean, - 'is_visible': True}, - 'created_at': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'started_at': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'heartbeat_timestamp': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'alive': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'configurations': {'allow_post': False, 'allow_put': False, - 'is_visible': True}, - 'description': { - 'allow_post': False, 'allow_put': True, - 'is_visible': True, - 'validate': { - 'type:string_or_none': db_const.DESCRIPTION_FIELD_SIZE}}, - }, -} - - -class AgentNotFound(exceptions.NotFound): - message = _("Agent %(id)s could not be found") - - -class AgentNotFoundByTypeHost(exceptions.NotFound): - message = _("Agent with agent_type=%(agent_type)s and host=%(host)s " - "could not be found") - - -class MultipleAgentFoundByTypeHost(exceptions.Conflict): - message = _("Multiple agents with agent_type=%(agent_type)s and " - "host=%(host)s found") - - -class Agent(api_extensions.ExtensionDescriptor): +class Agent(api_extensions.APIExtensionDescriptor): """Agent management extension.""" - @classmethod - def get_name(cls): - return "agent" - - @classmethod - def get_alias(cls): - return "agent" - - @classmethod - def get_description(cls): - return "The agent management extension." - - @classmethod - def get_updated(cls): - return "2013-02-03T10:00:00-00:00" + api_definition = apidef @classmethod def get_resources(cls): """Returns Ext Resources.""" plugin = directory.get_plugin() - params = RESOURCE_ATTRIBUTE_MAP.get(RESOURCE_NAME + 's') - controller = base.create_resource(RESOURCE_NAME + 's', - RESOURCE_NAME, - plugin, params - ) + params = apidef.RESOURCE_ATTRIBUTE_MAP.get(apidef.COLLECTION_NAME) + controller = base.create_resource(apidef.COLLECTION_NAME, + apidef.RESOURCE_NAME, + plugin, params) - ex = extensions.ResourceExtension(RESOURCE_NAME + 's', + ex = extensions.ResourceExtension(apidef.COLLECTION_NAME, controller) return [ex] - def update_attributes_map(self, attributes): - super(Agent, self).update_attributes_map( - attributes, extension_attrs_map=RESOURCE_ATTRIBUTE_MAP) - - def get_extended_resources(self, version): - if version == "2.0": - return RESOURCE_ATTRIBUTE_MAP - else: - return {} - @six.add_metaclass(abc.ABCMeta) class AgentPluginBase(object): diff --git a/neutron/extensions/dhcpagentscheduler.py b/neutron/extensions/dhcpagentscheduler.py index 13652f9903a..8548cfd4612 100644 --- a/neutron/extensions/dhcpagentscheduler.py +++ b/neutron/extensions/dhcpagentscheduler.py @@ -19,6 +19,7 @@ from neutron_lib.api import extensions as api_extensions from neutron_lib.api import faults from neutron_lib import constants from neutron_lib import exceptions +from neutron_lib.exceptions import agent as agent_exc from neutron_lib.plugins import directory import six @@ -26,7 +27,6 @@ from neutron._i18n import _ from neutron.api import extensions from neutron.api.v2 import resource from neutron.common import rpc as n_rpc -from neutron.extensions import agent from neutron import policy from neutron import wsgi @@ -123,7 +123,7 @@ class Dhcpagentscheduler(api_extensions.ExtensionDescriptor): return {} -class InvalidDHCPAgent(agent.AgentNotFound): +class InvalidDHCPAgent(agent_exc.AgentNotFound): message = _("Agent %(id)s is not a valid DHCP Agent or has been disabled") diff --git a/neutron/extensions/l3agentscheduler.py b/neutron/extensions/l3agentscheduler.py index ef7b90a6f5b..d976f426a2d 100644 --- a/neutron/extensions/l3agentscheduler.py +++ b/neutron/extensions/l3agentscheduler.py @@ -19,6 +19,7 @@ from neutron_lib.api import extensions as api_extensions from neutron_lib.api import faults from neutron_lib import constants from neutron_lib import exceptions +from neutron_lib.exceptions import agent as agent_exc from neutron_lib.plugins import constants as plugin_constants from neutron_lib.plugins import directory from oslo_log import log as logging @@ -29,7 +30,6 @@ from neutron._i18n import _ from neutron.api import extensions from neutron.api.v2 import resource from neutron.common import rpc as n_rpc -from neutron.extensions import agent from neutron import policy from neutron import wsgi @@ -149,7 +149,7 @@ class L3agentscheduler(api_extensions.ExtensionDescriptor): return {} -class InvalidL3Agent(agent.AgentNotFound): +class InvalidL3Agent(agent_exc.AgentNotFound): message = _("Agent %(id)s is not a L3 Agent or has been disabled") diff --git a/neutron/tests/unit/extensions/test_availability_zone.py b/neutron/tests/unit/extensions/test_availability_zone.py index 9b5ce9ed23f..90cb4ff2dac 100644 --- a/neutron/tests/unit/extensions/test_availability_zone.py +++ b/neutron/tests/unit/extensions/test_availability_zone.py @@ -11,8 +11,6 @@ # License for the specific language governing permissions and limitations # under the License. -import copy - from neutron_lib.api.definitions import availability_zone as az_def from neutron_lib import context from neutron_lib.exceptions import availability_zone as az_exc @@ -55,16 +53,11 @@ class AZTestCommon(test_db_base_plugin_v2.NeutronDbPluginV2TestCase): class TestAZAgentCase(AZTestCommon): def setUp(self): - self._agent_backup = copy.deepcopy(agent.RESOURCE_ATTRIBUTE_MAP) - self.addCleanup(self._restore) plugin = ('neutron.tests.unit.extensions.' 'test_availability_zone.AZTestPlugin') ext_mgr = AZExtensionManager() super(TestAZAgentCase, self).setUp(plugin=plugin, ext_mgr=ext_mgr) - def _restore(self): - agent.RESOURCE_ATTRIBUTE_MAP = self._agent_backup - def test_list_availability_zones(self): self._register_azs() helpers.set_agent_admin_state(self.agent3['id'], admin_state_up=False) @@ -103,14 +96,9 @@ class TestAZAgentCase(AZTestCommon): class TestAZNetworkCase(AZTestCommon): def setUp(self): - self._agent_backup = copy.deepcopy(agent.RESOURCE_ATTRIBUTE_MAP) - self.addCleanup(self._restore) ext_mgr = AZExtensionManager() super(TestAZNetworkCase, self).setUp(plugin='ml2', ext_mgr=ext_mgr) - def _restore(self): - agent.RESOURCE_ATTRIBUTE_MAP = self._agent_backup - def test_availability_zones_in_create_response(self): with self.network() as net: self.assertIn('availability_zone_hints', net['network'])