RPC callbacks: add hook to register additional resources

This change adds a hook so that additional classes
for OVObjects can be registered beyond the base classes.

The intent is to allow stadium project to register classes
for the OVObjects they use.

Change-Id: Icb6a73aeca0286725c78a3bf403e1df895a34d4e
Needed-By: Ie0f1c9f3f2bd1cf317b921ee1c1691c2816d8832
This commit is contained in:
Thomas Morin 2017-10-16 17:26:19 +02:00
parent b11b5c5c43
commit 23a3cc84dd
2 changed files with 34 additions and 0 deletions

View File

@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from neutron._i18n import _
from neutron.objects.logapi import logging_resource as log_object
from neutron.objects import network
from neutron.objects import ports
@ -61,6 +62,17 @@ def get_resource_type(resource_cls):
return resource_cls.obj_name()
def register_resource_class(resource_cls):
resource_type = get_resource_type(resource_cls)
if not resource_type:
msg = _("cannot find resource type for %s class") % resource_cls
raise ValueError(msg)
if resource_type not in _TYPE_TO_CLS_MAP:
_TYPE_TO_CLS_MAP[resource_type] = resource_cls
if resource_type not in LOCAL_RESOURCE_VERSIONS:
LOCAL_RESOURCE_VERSIONS[resource_type] = resource_cls.VERSION
def is_valid_resource_type(resource_type):
return resource_type in _TYPE_TO_CLS_MAP

View File

@ -10,6 +10,8 @@
# License for the specific language governing permissions and limitations
# under the License.
from oslo_versionedobjects import base as obj_base
from neutron.api.rpc.callbacks import resources
from neutron.objects.qos import policy
from neutron.tests import base
@ -52,3 +54,23 @@ class GetResourceClsTestCase(base.BaseTestCase):
def test_unknown_type(self):
self.assertIsNone(resources.get_resource_cls('unknown-resource-type'))
class RegisterResourceClass(base.BaseTestCase):
def test_register_resource_class(self):
class DummyOVO(obj_base.VersionedObject):
pass
self.assertFalse(
resources.is_valid_resource_type('DummyOVO'))
resources.register_resource_class(DummyOVO)
self.assertTrue(
resources.is_valid_resource_type('DummyOVO'))
def test_register_bogus_resource_class(self):
class DummyOVO(object):
pass
self.assertRaises(ValueError,
resources.register_resource_class, DummyOVO)