Because of bugs in stub_out(), if we use staticmethods combined with the db_api context managers, we'll break those methods when the stubs are removed. There is a fix in the queue, but it's going to be weeks before we can use it. This adds staticmethod proxy jumps to work around the problem like we're doing for the flavor code right now. That keeps the tests and other code the way we want it to look, and we can move the meat back into the staticmethods once the bug is fixed. The following patch will be stub_out-ing relevant bits, so fix this first to keep that diff smaller. Change-Id: I2015ac25500f7f6960f4916a4249bb711b157b9b
214 lines
7.3 KiB
Python
214 lines
7.3 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 sqlalchemy.orm import joinedload
|
|
|
|
from nova.db.sqlalchemy import api as db_api
|
|
from nova.db.sqlalchemy import models
|
|
from nova import exception
|
|
from nova import objects
|
|
from nova.objects import base
|
|
from nova.objects import fields
|
|
|
|
|
|
@db_api.main_context_manager.writer
|
|
def _create_rp_in_db(context, updates):
|
|
db_rp = models.ResourceProvider()
|
|
db_rp.update(updates)
|
|
context.session.add(db_rp)
|
|
return db_rp
|
|
|
|
|
|
@db_api.main_context_manager.reader
|
|
def _get_rp_by_uuid_from_db(context, uuid):
|
|
result = context.session.query(models.ResourceProvider).filter_by(
|
|
uuid=uuid).first()
|
|
if not result:
|
|
raise exception.NotFound()
|
|
return result
|
|
|
|
|
|
@base.NovaObjectRegistry.register
|
|
class ResourceProvider(base.NovaObject):
|
|
# Version 1.0: Initial version
|
|
VERSION = '1.0'
|
|
|
|
fields = {
|
|
'id': fields.IntegerField(read_only=True),
|
|
'uuid': fields.UUIDField(nullable=False),
|
|
}
|
|
|
|
@base.remotable
|
|
def create(self):
|
|
if 'id' in self:
|
|
raise exception.ObjectActionError(action='create',
|
|
reason='already created')
|
|
if 'uuid' not in self:
|
|
raise exception.ObjectActionError(action='create',
|
|
reason='uuid is required')
|
|
updates = self.obj_get_changes()
|
|
db_rp = self._create_in_db(self._context, updates)
|
|
self._from_db_object(self._context, self, db_rp)
|
|
|
|
@base.remotable_classmethod
|
|
def get_by_uuid(cls, context, uuid):
|
|
db_resource_provider = cls._get_by_uuid_from_db(context, uuid)
|
|
return cls._from_db_object(context, cls(), db_resource_provider)
|
|
|
|
@staticmethod
|
|
def _create_in_db(context, updates):
|
|
return _create_rp_in_db(context, updates)
|
|
|
|
@staticmethod
|
|
def _from_db_object(context, resource_provider, db_resource_provider):
|
|
for field in resource_provider.fields:
|
|
setattr(resource_provider, field, db_resource_provider[field])
|
|
resource_provider._context = context
|
|
resource_provider.obj_reset_changes()
|
|
return resource_provider
|
|
|
|
@staticmethod
|
|
def _get_by_uuid_from_db(context, uuid):
|
|
return _get_rp_by_uuid_from_db(context, uuid)
|
|
|
|
|
|
class _HasAResourceProvider(base.NovaObject):
|
|
"""Code shared between Inventory and Allocation
|
|
|
|
Both contain a ResourceProvider.
|
|
"""
|
|
|
|
@staticmethod
|
|
def _make_db(updates):
|
|
try:
|
|
resource_provider = updates.pop('resource_provider')
|
|
updates['resource_provider_id'] = resource_provider.id
|
|
except (KeyError, NotImplementedError):
|
|
raise exception.ObjectActionError(
|
|
action='create',
|
|
reason='resource_provider required')
|
|
try:
|
|
resource_class = updates.pop('resource_class')
|
|
except KeyError:
|
|
raise exception.ObjectActionError(
|
|
action='create',
|
|
reason='resource_class required')
|
|
updates['resource_class_id'] = fields.ResourceClass.index(
|
|
resource_class)
|
|
return updates
|
|
|
|
@staticmethod
|
|
def _from_db_object(context, target, source):
|
|
for field in target.fields:
|
|
if field not in ('resource_provider', 'resource_class'):
|
|
setattr(target, field, source[field])
|
|
|
|
if 'resource_class' not in target:
|
|
target.resource_class = (
|
|
target.fields['resource_class'].from_index(
|
|
source['resource_class_id']))
|
|
if ('resource_provider' not in target and
|
|
'resource_provider' in source):
|
|
target.resource_provider = ResourceProvider()
|
|
ResourceProvider._from_db_object(
|
|
context,
|
|
target.resource_provider,
|
|
source['resource_provider'])
|
|
|
|
target._context = context
|
|
target.obj_reset_changes()
|
|
return target
|
|
|
|
|
|
@db_api.main_context_manager.writer
|
|
def _create_inventory_in_db(context, updates):
|
|
db_inventory = models.Inventory()
|
|
db_inventory.update(updates)
|
|
context.session.add(db_inventory)
|
|
return db_inventory
|
|
|
|
|
|
@db_api.main_context_manager.writer
|
|
def _update_inventory_in_db(context, id_, updates):
|
|
result = context.session.query(
|
|
models.Inventory).filter_by(id=id_).update(updates)
|
|
if not result:
|
|
raise exception.NotFound()
|
|
|
|
|
|
@base.NovaObjectRegistry.register
|
|
class Inventory(_HasAResourceProvider):
|
|
# Version 1.0: Initial version
|
|
VERSION = '1.0'
|
|
|
|
fields = {
|
|
'id': fields.IntegerField(read_only=True),
|
|
'resource_provider': fields.ObjectField('ResourceProvider'),
|
|
'resource_class': fields.ResourceClassField(read_only=True),
|
|
'total': fields.IntegerField(),
|
|
'reserved': fields.IntegerField(),
|
|
'min_unit': fields.IntegerField(),
|
|
'max_unit': fields.IntegerField(),
|
|
'step_size': fields.IntegerField(),
|
|
'allocation_ratio': fields.FloatField(),
|
|
}
|
|
|
|
@base.remotable
|
|
def create(self):
|
|
if 'id' in self:
|
|
raise exception.ObjectActionError(action='create',
|
|
reason='already created')
|
|
updates = self._make_db(self.obj_get_changes())
|
|
db_inventory = self._create_in_db(self._context, updates)
|
|
self._from_db_object(self._context, self, db_inventory)
|
|
|
|
@base.remotable
|
|
def save(self):
|
|
if 'id' not in self:
|
|
raise exception.ObjectActionError(action='save',
|
|
reason='not created')
|
|
updates = self.obj_get_changes()
|
|
updates.pop('id', None)
|
|
self._update_in_db(self._context, self.id, updates)
|
|
|
|
@staticmethod
|
|
def _create_in_db(context, updates):
|
|
return _create_inventory_in_db(context, updates)
|
|
|
|
@staticmethod
|
|
def _update_in_db(context, id_, updates):
|
|
return _update_inventory_in_db(context, id_, updates)
|
|
|
|
|
|
@base.NovaObjectRegistry.register
|
|
class InventoryList(base.ObjectListBase, base.NovaObject):
|
|
# Version 1.0: Initial Version
|
|
VERSION = '1.0'
|
|
|
|
fields = {
|
|
'objects': fields.ListOfObjectsField('Inventory'),
|
|
}
|
|
|
|
@staticmethod
|
|
@db_api.main_context_manager.reader
|
|
def _get_all_by_resource_provider(context, rp_uuid):
|
|
return context.session.query(models.Inventory).\
|
|
options(joinedload('resource_provider')).\
|
|
filter(models.ResourceProvider.uuid == rp_uuid).all()
|
|
|
|
@base.remotable_classmethod
|
|
def get_all_by_resource_provider_uuid(cls, context, rp_uuid):
|
|
db_inventory_list = cls._get_all_by_resource_provider(context,
|
|
rp_uuid)
|
|
return base.obj_make_list(context, cls(context), objects.Inventory,
|
|
db_inventory_list)
|