Merge "placement: adds ResourceClass.save()"
This commit is contained in:
commit
474c2ef282
@ -2138,6 +2138,10 @@ class ResourceClassCannotDeleteStandard(Invalid):
|
||||
msg_fmt = _("Cannot delete standard resource class %(resource_class)s.")
|
||||
|
||||
|
||||
class ResourceClassCannotUpdateStandard(Invalid):
|
||||
msg_fmt = _("Cannot update standard resource class %(resource_class)s.")
|
||||
|
||||
|
||||
class InvalidInventory(Invalid):
|
||||
msg_fmt = _("Inventory for '%(resource_class)s' on "
|
||||
"resource provider '%(resource_provider)s' invalid.")
|
||||
|
@ -1245,6 +1245,32 @@ class ResourceClass(base.NovaObject):
|
||||
if not res:
|
||||
raise exception.NotFound()
|
||||
|
||||
def save(self):
|
||||
if 'id' not in self:
|
||||
raise exception.ObjectActionError(action='save',
|
||||
reason='ID attribute not found')
|
||||
updates = self.obj_get_changes()
|
||||
# Never update any standard resource class, since the standard resource
|
||||
# classes don't even exist in the database table anyway.
|
||||
_ensure_rc_cache(self._context)
|
||||
standards = _RC_CACHE.get_standards()
|
||||
if self.id in (rc['id'] for rc in standards):
|
||||
raise exception.ResourceClassCannotUpdateStandard(
|
||||
resource_class=self.name)
|
||||
self._save(self._context, self.id, self.name, updates)
|
||||
_RC_CACHE.clear()
|
||||
|
||||
@staticmethod
|
||||
@db_api.api_context_manager.writer
|
||||
def _save(context, id, name, updates):
|
||||
db_rc = context.session.query(models.ResourceClass).filter_by(
|
||||
id=id).first()
|
||||
db_rc.update(updates)
|
||||
try:
|
||||
db_rc.save(context.session)
|
||||
except db_exc.DBDuplicateEntry:
|
||||
raise exception.ResourceClassExists(resource_class=name)
|
||||
|
||||
|
||||
@base.NovaObjectRegistry.register
|
||||
class ResourceClassList(base.ObjectListBase, base.NovaObject):
|
||||
|
@ -1222,3 +1222,38 @@ class ResourceClassTestCase(ResourceProviderBaseCase):
|
||||
rc_list = objects.ResourceClassList.get_all(self.context)
|
||||
rc_ids = (r.id for r in rc_list)
|
||||
self.assertNotIn(rc.id, rc_ids)
|
||||
|
||||
def test_save_fail_no_id(self):
|
||||
rc = objects.ResourceClass(
|
||||
self.context,
|
||||
name='CUSTOM_IRON_NFV',
|
||||
)
|
||||
self.assertRaises(exception.ObjectActionError, rc.save)
|
||||
|
||||
def test_save_fail_standard(self):
|
||||
rc = objects.ResourceClass.get_by_name(
|
||||
self.context,
|
||||
'VCPU',
|
||||
)
|
||||
self.assertRaises(exception.ResourceClassCannotUpdateStandard,
|
||||
rc.save)
|
||||
|
||||
def test_save(self):
|
||||
rc = objects.ResourceClass(
|
||||
self.context,
|
||||
name='CUSTOM_IRON_NFV',
|
||||
)
|
||||
rc.create()
|
||||
|
||||
rc = objects.ResourceClass.get_by_name(
|
||||
self.context,
|
||||
'CUSTOM_IRON_NFV',
|
||||
)
|
||||
rc.name = 'CUSTOM_IRON_SILVER'
|
||||
rc.save()
|
||||
|
||||
# Verify rc cache was purged of the old entry
|
||||
self.assertRaises(exception.NotFound,
|
||||
objects.ResourceClass.get_by_name,
|
||||
self.context,
|
||||
'CUSTOM_IRON_NFV')
|
||||
|
Loading…
x
Reference in New Issue
Block a user