Allow sub-resources to have standard attributes
Prior to this change a DB model with standard attributes could declare that it was mapping to an API resource, but not declare a mapping to a sub-resources (see bug 1763347). This change allows DB models with standard attributes to advertise that they map API *sub*-resources, and modifies the code that extends DB resources to support these specific declarations. Closes-Bug: 1763347 Needed-By: I77ce46c0f33e2a366076d51ce6586fb3008dc6b1 Change-Id: I7630aab5e4f38d0fba862adc2426d4a7ca04a679
This commit is contained in:
parent
d786643214
commit
5179896e77
|
@ -98,6 +98,22 @@ class HasStandardAttributes(object):
|
||||||
return cls.api_collections
|
return cls.api_collections
|
||||||
raise NotImplementedError("%s must define api_collections" % cls)
|
raise NotImplementedError("%s must define api_collections" % cls)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_api_sub_resources(cls):
|
||||||
|
"""Define the API sub-resources this object will appear under.
|
||||||
|
|
||||||
|
This should return a list of API sub-resources that the object
|
||||||
|
will be exposed under.
|
||||||
|
|
||||||
|
This is used by the standard attr extensions to discover which
|
||||||
|
sub-resources need to be extended with the standard attr fields
|
||||||
|
(e.g. created_at/updated_at/etc).
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
return cls.api_sub_resources
|
||||||
|
except AttributeError:
|
||||||
|
return []
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_collection_resource_map(cls):
|
def get_collection_resource_map(cls):
|
||||||
try:
|
try:
|
||||||
|
@ -173,17 +189,26 @@ class HasStandardAttributes(object):
|
||||||
self.standard_attr.bump_revision()
|
self.standard_attr.bump_revision()
|
||||||
|
|
||||||
|
|
||||||
def get_standard_attr_resource_model_map():
|
def _resource_model_map_helper(rs_map, resource, subclass):
|
||||||
|
if resource in rs_map:
|
||||||
|
raise RuntimeError("Model %(sub)s tried to register for API resource "
|
||||||
|
"%(res)s which conflicts with model %(other)s." %
|
||||||
|
dict(sub=subclass,
|
||||||
|
other=rs_map[resource],
|
||||||
|
res=resource))
|
||||||
|
rs_map[resource] = subclass
|
||||||
|
|
||||||
|
|
||||||
|
def get_standard_attr_resource_model_map(include_resources=True,
|
||||||
|
include_sub_resources=True):
|
||||||
rs_map = {}
|
rs_map = {}
|
||||||
for subclass in HasStandardAttributes.__subclasses__():
|
for subclass in HasStandardAttributes.__subclasses__():
|
||||||
for resource in subclass.get_api_collections():
|
if include_resources:
|
||||||
if resource in rs_map:
|
for resource in subclass.get_api_collections():
|
||||||
raise RuntimeError("Model %(sub)s tried to register for "
|
_resource_model_map_helper(rs_map, resource, subclass)
|
||||||
"API resource %(res)s which conflicts "
|
if include_sub_resources:
|
||||||
"with model %(other)s." %
|
for sub_resource in subclass.get_api_sub_resources():
|
||||||
dict(sub=subclass, other=rs_map[resource],
|
_resource_model_map_helper(rs_map, sub_resource, subclass)
|
||||||
res=resource))
|
|
||||||
rs_map[resource] = subclass
|
|
||||||
return rs_map
|
return rs_map
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
|
|
||||||
from neutron_lib.api import extensions
|
from neutron_lib.api import extensions
|
||||||
|
|
||||||
from neutron.db import standard_attr
|
from neutron.extensions import stdattrs_common
|
||||||
|
|
||||||
|
|
||||||
REVISION = 'revision_number'
|
REVISION = 'revision_number'
|
||||||
|
@ -46,5 +46,4 @@ class Revisions(extensions.ExtensionDescriptor):
|
||||||
def get_extended_resources(self, version):
|
def get_extended_resources(self, version):
|
||||||
if version != "2.0":
|
if version != "2.0":
|
||||||
return {}
|
return {}
|
||||||
rs_map = standard_attr.get_standard_attr_resource_model_map()
|
return stdattrs_common.stdattrs_extended_resources(REVISION_BODY)
|
||||||
return {resource: REVISION_BODY for resource in rs_map}
|
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
from neutron_lib.api import extensions
|
from neutron_lib.api import extensions
|
||||||
from neutron_lib.db import constants as db_const
|
from neutron_lib.db import constants as db_const
|
||||||
|
|
||||||
from neutron.db import standard_attr
|
from neutron.extensions import stdattrs_common
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTION_BODY = {
|
DESCRIPTION_BODY = {
|
||||||
|
@ -51,5 +51,4 @@ class Standardattrdescription(extensions.ExtensionDescriptor):
|
||||||
def get_extended_resources(self, version):
|
def get_extended_resources(self, version):
|
||||||
if version != "2.0":
|
if version != "2.0":
|
||||||
return {}
|
return {}
|
||||||
rs_map = standard_attr.get_standard_attr_resource_model_map()
|
return stdattrs_common.stdattrs_extended_resources(DESCRIPTION_BODY)
|
||||||
return {resource: DESCRIPTION_BODY for resource in rs_map}
|
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
# Copyright (c) 2018 Orange.
|
||||||
|
# All Rights Reserved.
|
||||||
|
#
|
||||||
|
# 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 itertools
|
||||||
|
|
||||||
|
from neutron.db import standard_attr
|
||||||
|
|
||||||
|
|
||||||
|
def stdattrs_extended_resources(attributes):
|
||||||
|
r_map = standard_attr.get_standard_attr_resource_model_map(
|
||||||
|
include_resources=True,
|
||||||
|
include_sub_resources=False)
|
||||||
|
sr_map = standard_attr.get_standard_attr_resource_model_map(
|
||||||
|
include_resources=False,
|
||||||
|
include_sub_resources=True)
|
||||||
|
return dict(itertools.chain(
|
||||||
|
{r: attributes for r in r_map}.items(),
|
||||||
|
{sr: {'parameters': attributes} for sr in sr_map}.items()
|
||||||
|
))
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
from neutron_lib.api import extensions
|
from neutron_lib.api import extensions
|
||||||
|
|
||||||
from neutron.db import standard_attr
|
from neutron.extensions import stdattrs_common
|
||||||
|
|
||||||
|
|
||||||
# Attribute Map
|
# Attribute Map
|
||||||
|
@ -57,5 +57,4 @@ class Timestamp(extensions.ExtensionDescriptor):
|
||||||
def get_extended_resources(self, version):
|
def get_extended_resources(self, version):
|
||||||
if version != "2.0":
|
if version != "2.0":
|
||||||
return {}
|
return {}
|
||||||
rs_map = standard_attr.get_standard_attr_resource_model_map()
|
return stdattrs_common.stdattrs_extended_resources(TIMESTAMP_BODY)
|
||||||
return {resource: TIMESTAMP_BODY for resource in rs_map}
|
|
||||||
|
|
|
@ -42,10 +42,26 @@ class StandardAttrTestCase(base.BaseTestCase):
|
||||||
standard_attr.model_base.HasId,
|
standard_attr.model_base.HasId,
|
||||||
base):
|
base):
|
||||||
api_collections = ['my_resource', 'my_resource2']
|
api_collections = ['my_resource', 'my_resource2']
|
||||||
|
api_sub_resources = ['my_subresource']
|
||||||
|
|
||||||
rs_map = standard_attr.get_standard_attr_resource_model_map()
|
rs_map = standard_attr.get_standard_attr_resource_model_map()
|
||||||
self.assertEqual(MyModel, rs_map['my_resource'])
|
self.assertEqual(MyModel, rs_map['my_resource'])
|
||||||
self.assertEqual(MyModel, rs_map['my_resource2'])
|
self.assertEqual(MyModel, rs_map['my_resource2'])
|
||||||
|
self.assertEqual(MyModel, rs_map['my_subresource'])
|
||||||
|
|
||||||
|
sub_rs_map = standard_attr.get_standard_attr_resource_model_map(
|
||||||
|
include_resources=False,
|
||||||
|
include_sub_resources=True)
|
||||||
|
self.assertNotIn('my_resource', sub_rs_map)
|
||||||
|
self.assertNotIn('my_resource2', sub_rs_map)
|
||||||
|
self.assertEqual(MyModel, sub_rs_map['my_subresource'])
|
||||||
|
|
||||||
|
nosub_rs_map = standard_attr.get_standard_attr_resource_model_map(
|
||||||
|
include_resources=True,
|
||||||
|
include_sub_resources=False)
|
||||||
|
self.assertEqual(MyModel, nosub_rs_map['my_resource'])
|
||||||
|
self.assertEqual(MyModel, nosub_rs_map['my_resource2'])
|
||||||
|
self.assertNotIn('my_subresource', nosub_rs_map)
|
||||||
|
|
||||||
class Dup(standard_attr.HasStandardAttributes,
|
class Dup(standard_attr.HasStandardAttributes,
|
||||||
standard_attr.model_base.HasId,
|
standard_attr.model_base.HasId,
|
||||||
|
|
Loading…
Reference in New Issue