nova/nova/objects/virt_device_metadata.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

141 lines
3.7 KiB
Python

# Copyright (C) 2016, Red Hat, Inc.
#
# 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_serialization import jsonutils
from oslo_utils import versionutils
from nova.db import api as db
from nova.objects import base
from nova.objects import fields
@base.NovaObjectRegistry.register
class DeviceBus(base.NovaObject):
VERSION = '1.0'
@base.NovaObjectRegistry.register
class PCIDeviceBus(DeviceBus):
VERSION = '1.0'
fields = {
'address': fields.PCIAddressField(),
}
@base.NovaObjectRegistry.register
class USBDeviceBus(DeviceBus):
VERSION = '1.0'
fields = {
'address': fields.USBAddressField(),
}
@base.NovaObjectRegistry.register
class SCSIDeviceBus(DeviceBus):
VERSION = '1.0'
fields = {
'address': fields.SCSIAddressField(),
}
@base.NovaObjectRegistry.register
class IDEDeviceBus(DeviceBus):
VERSION = '1.0'
fields = {
'address': fields.IDEAddressField(),
}
@base.NovaObjectRegistry.register
class XenDeviceBus(DeviceBus):
VERSION = '1.0'
fields = {
'address': fields.XenAddressField(),
}
@base.NovaObjectRegistry.register
class DeviceMetadata(base.NovaObject):
VERSION = '1.0'
fields = {
'bus': fields.ObjectField("DeviceBus", subclasses=True),
'tags': fields.ListOfStringsField(),
}
@base.NovaObjectRegistry.register
class NetworkInterfaceMetadata(DeviceMetadata):
# Version 1.0: Initial version
# Version 1.1: Add vlans field
# Version 1.2: Add vf_trusted field
VERSION = '1.2'
fields = {
'mac': fields.MACAddressField(),
'vlan': fields.IntegerField(),
'vf_trusted': fields.BooleanField(default=False),
}
def obj_make_compatible(self, primitive, target_version):
target_version = versionutils.convert_version_to_tuple(target_version)
if target_version < (1, 1) and 'vlan' in primitive:
del primitive['vlan']
if target_version < (1, 2) and 'vf_trusted' in primitive:
del primitive['vf_trusted']
@base.NovaObjectRegistry.register
class DiskMetadata(DeviceMetadata):
VERSION = '1.0'
fields = {
'serial': fields.StringField(nullable=True),
'path': fields.StringField(nullable=True),
}
@base.NovaObjectRegistry.register
class InstanceDeviceMetadata(base.NovaObject):
VERSION = '1.0'
fields = {
'devices': fields.ListOfObjectsField('DeviceMetadata',
subclasses=True),
}
@classmethod
def obj_from_db(cls, context, db_dev_meta):
primitive = jsonutils.loads(db_dev_meta)
device_metadata = cls.obj_from_primitive(primitive)
return device_metadata
@base.remotable_classmethod
def get_by_instance_uuid(cls, context, instance_uuid):
db_extra = db.instance_extra_get_by_instance_uuid(
context, instance_uuid, columns=['device_metadata'])
if not db_extra or db_extra['device_metadata'] is None:
return None
primitive = jsonutils.loads(db_extra['device_metadata'])
device_metadata = cls.obj_from_primitive(primitive)
return device_metadata
def _to_json(self):
return jsonutils.dumps(self.obj_to_primitive())