nova/nova/objects/tag.py
Chris Dent def4b17934 Use nova.db.api directly
nova/db/__init__.py was importing * from nova.db.api. This meant that
any time any code anywhere within the nova.db package was imported
then nova.db.api was too, leading to a cascade of imports that may
not have been desired. Also, in general, code in __init__.py is a pain.

Therefore, this change adjusts code that so that either:

* nova.db.api is used directly
* nova.db.api is imported as 'db'

In either case, the functionality remains the same.

The primary goal of this change was to make it possible to import the
model files without having to import the db api. Moving the model files
to a different place in the directory hierarchy was considered, but
given that "code in __init__.py is a pain" this mode was chosen.

This looks like a very large change, but it is essentially adjusting
package names, many in mocks.

Change-Id: Ic1fd7c87ceda05eeb96735da2a415ef37060bb1a
2018-07-10 14:56:27 +00:00

77 lines
2.5 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.
from nova.db import api as db
from nova import objects
from nova.objects import base
from nova.objects import fields
MAX_TAG_LENGTH = 60
@base.NovaObjectRegistry.register
class Tag(base.NovaObject):
# Version 1.0: Initial version
# Version 1.1: Added method exists()
VERSION = '1.1'
fields = {
'resource_id': fields.StringField(),
'tag': fields.StringField(),
}
@staticmethod
def _from_db_object(context, tag, db_tag):
for key in tag.fields:
setattr(tag, key, db_tag[key])
tag.obj_reset_changes()
tag._context = context
return tag
@base.remotable
def create(self):
db_tag = db.instance_tag_add(self._context, self.resource_id, self.tag)
self._from_db_object(self._context, self, db_tag)
@base.remotable_classmethod
def destroy(cls, context, resource_id, name):
db.instance_tag_delete(context, resource_id, name)
@base.remotable_classmethod
def exists(cls, context, resource_id, name):
return db.instance_tag_exists(context, resource_id, name)
@base.NovaObjectRegistry.register
class TagList(base.ObjectListBase, base.NovaObject):
# Version 1.0: Initial version
# Version 1.1: Tag <= version 1.1
VERSION = '1.1'
fields = {
'objects': fields.ListOfObjectsField('Tag'),
}
@base.remotable_classmethod
def get_by_resource_id(cls, context, resource_id):
db_tags = db.instance_tag_get_by_instance_uuid(context, resource_id)
return base.obj_make_list(context, cls(), objects.Tag, db_tags)
@base.remotable_classmethod
def create(cls, context, resource_id, tags):
db_tags = db.instance_tag_set(context, resource_id, tags)
return base.obj_make_list(context, cls(), objects.Tag, db_tags)
@base.remotable_classmethod
def destroy(cls, context, resource_id):
db.instance_tag_delete_all(context, resource_id)