OVO for Tag

This patch introduces and integrates Oslo-Versioned Object for
Tag Model class.

Change-Id: I0179a3616689f39d93c337c730283e21a01677cf
Partially-Implements: blueprint adopt-oslo-versioned-objects-for-db
This commit is contained in:
Aradhana Singh 2016-08-22 16:53:44 -05:00
parent a94223a685
commit 7fa7e1b915
5 changed files with 85 additions and 29 deletions

32
neutron/objects/tag.py Normal file
View File

@ -0,0 +1,32 @@
# 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.
from oslo_versionedobjects import base as obj_base
from oslo_versionedobjects import fields as obj_fields
from neutron.db.models import tag as tag_model
from neutron.objects import base
@obj_base.VersionedObjectRegistry.register
class Tag(base.NeutronDbObject):
# Version 1.0: Initial version
VERSION = '1.0'
db_model = tag_model.Tag
fields = {
'tag': obj_fields.StringField(),
'standard_attr_id': obj_fields.IntegerField()
}
primary_keys = ['tag', 'standard_attr_id']

View File

@ -24,11 +24,12 @@ from neutron.api.v2 import attributes
from neutron.db import api as db_api
from neutron.db import common_db_mixin
from neutron.db.models import l3 as l3_model
from neutron.db.models import tag as tag_model
from neutron.db import models_v2
from neutron.db import tag_db as tag_methods
from neutron.extensions import l3 as l3_ext
from neutron.extensions import tag as tag_ext
from neutron.objects import exceptions as obj_exc
from neutron.objects import tag as tag_obj
resource_model_map = {
@ -86,13 +87,14 @@ class TagPlugin(common_db_mixin.CommonDbMixin, tag_ext.TagPluginBase):
old_tags = {tag_db.tag for tag_db in res.standard_attr.tags}
tags_added = new_tags - old_tags
tags_removed = old_tags - new_tags
for tag_db in res.standard_attr.tags:
if tag_db.tag in tags_removed:
context.session.delete(tag_db)
tag_obj.Tag.delete_objects(context, tag=[
tag_db.tag
for tag_db in res.standard_attr.tags
if tag_db.tag in tags_removed
])
for tag in tags_added:
tag_db = tag_model.Tag(standard_attr_id=res.standard_attr_id,
tag=tag)
context.session.add(tag_db)
tag_obj.Tag(context, standard_attr_id=res.standard_attr_id,
tag=tag).create()
return body
@log_helpers.log_method_call
@ -101,32 +103,23 @@ class TagPlugin(common_db_mixin.CommonDbMixin, tag_ext.TagPluginBase):
if any(tag == tag_db.tag for tag_db in res.standard_attr.tags):
return
try:
with db_api.context_manager.writer.using(context):
tag_db = tag_model.Tag(standard_attr_id=res.standard_attr_id,
tag=tag)
context.session.add(tag_db)
except db_exc.DBDuplicateEntry:
tag_obj.Tag(context, standard_attr_id=res.standard_attr_id,
tag=tag).create()
except obj_exc.NeutronDbObjectDuplicateEntry:
pass
@log_helpers.log_method_call
def delete_tags(self, context, resource, resource_id):
res = self._get_resource(context, resource, resource_id)
with db_api.context_manager.writer.using(context):
query = context.session.query(tag_model.Tag)
for t in query.filter_by(standard_attr_id=res.standard_attr_id):
context.session.delete(t)
tag_obj.Tag.delete_objects(context,
standard_attr_id=res.standard_attr_id)
@log_helpers.log_method_call
def delete_tag(self, context, resource, resource_id, tag):
res = self._get_resource(context, resource, resource_id)
with db_api.context_manager.writer.using(context):
query = context.session.query(tag_model.Tag)
query = query.filter_by(tag=tag,
standard_attr_id=res.standard_attr_id)
try:
context.session.delete(query.one())
except exc.NoResultFound:
raise tag_ext.TagNotFound(tag=tag)
if not tag_obj.Tag.delete_objects(context,
tag=tag, standard_attr_id=res.standard_attr_id):
raise tag_ext.TagNotFound(tag=tag)
def __new__(cls, *args, **kwargs):
inst = super(TagPlugin, cls).__new__(cls, *args, **kwargs)

View File

@ -21,10 +21,10 @@ from neutron_lib.plugins import directory
from oslo_utils import timeutils
from neutron.db import db_base_plugin_v2
from neutron.db.models import tag as tag_model
from neutron.db import models_v2
from neutron.extensions import timestamp
from neutron import manager
from neutron.objects import tag as tag_obj
from neutron.tests.unit.db import test_db_base_plugin_v2
@ -254,10 +254,8 @@ class TimeStampDBMixinTestCase(TimeStampChangedsinceTestCase):
def _save_tag(self, tags, standard_attr_id):
ctx = context.get_admin_context()
for tag in tags:
with ctx.session.begin(subtransactions=True):
tag_db = tag_model.Tag(standard_attr_id=standard_attr_id,
tag=tag)
ctx.session.add(tag_db)
tag_obj.Tag(ctx, standard_attr_id=standard_attr_id,
tag=tag).create()
def test_update_timpestamp(self):
network_id = "foo_network_id"

View File

@ -76,6 +76,7 @@ object_data = {
'SubnetPoolPrefix': '1.0-13c15144135eb869faa4a76dc3ee3b6c',
'SubnetServiceType': '1.0-05ae4cdb2a9026a697b143926a1add8c',
'SubPort': '1.0-72c8471068db1f0491b5480fe49b52bb',
'Tag': '1.0-1a0d20379920ffa3cebfd3e016d2f7a0',
'Trunk': '1.1-aa3922b39e37fbb89886c2ee8715cf49',
'VlanAllocation': '1.0-72636c1b7d5c8eef987bd09666e64f3e',
'VxlanAllocation': '1.0-934638cd32d00f81d6fbf93c8eb5755a',

View File

@ -0,0 +1,32 @@
# 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.
from neutron.objects import tag
from neutron.tests.unit.objects import test_base as obj_test_base
from neutron.tests.unit import testlib_api
class TagIfaceObjectTestCase(obj_test_base.BaseObjectIfaceTestCase):
_test_class = tag.Tag
class TagDbObjectTestCase(obj_test_base.BaseDbObjectTestCase,
testlib_api.SqlTestCase):
_test_class = tag.Tag
def setUp(self):
super(TagDbObjectTestCase, self).setUp()
self._create_test_standard_attribute()
self.update_obj_fields(
{'standard_attr_id': self._standard_attribute['id']})