Fix several cases of passing context to quota-related remotable methods

There were a lot of cases where we pass a context to a remotable method
on the quota object.

Related to blueprint kilo-objects

Change-Id: I34a0754e86857c211093c290fb1215091628bd13
This commit is contained in:
Dan Smith 2015-03-02 12:01:37 -08:00
parent 31d6cfa912
commit 191fda5d6d
6 changed files with 32 additions and 33 deletions

View File

@ -144,13 +144,13 @@ class ServerGroupController(wsgi.Controller):
quotas = None
if self.ext_mgr.is_loaded('os-server-group-quotas'):
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
project_id, user_id = objects.quotas.ids_from_server_group(context,
sg)
try:
# We have to add the quota back to the user that created
# the server group
quotas.reserve(context, project_id=project_id,
quotas.reserve(project_id=project_id,
user_id=user_id, server_groups=-1)
except Exception:
quotas = None
@ -194,9 +194,9 @@ class ServerGroupController(wsgi.Controller):
quotas = None
if self.ext_mgr.is_loaded('os-server-group-quotas'):
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
try:
quotas.reserve(context, project_id=context.project_id,
quotas.reserve(project_id=context.project_id,
user_id=context.user_id, server_groups=1)
except nova.exception.OverQuota:
msg = _("Quota exceeded, too many server groups.")

View File

@ -88,12 +88,12 @@ class ServerGroupController(wsgi.Controller):
except nova.exception.InstanceGroupNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message())
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
project_id, user_id = objects.quotas.ids_from_server_group(context, sg)
try:
# We have to add the quota back to the user that created
# the server group
quotas.reserve(context, project_id=project_id,
quotas.reserve(project_id=project_id,
user_id=user_id, server_groups=-1)
except Exception:
quotas = None
@ -131,9 +131,9 @@ class ServerGroupController(wsgi.Controller):
"""Creates a new server group."""
context = _authorize_context(req)
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
try:
quotas.reserve(context, project_id=context.project_id,
quotas.reserve(project_id=context.project_id,
user_id=context.user_id, server_groups=1)
except nova.exception.OverQuota:
msg = _("Quota exceeded, too many server groups.")

View File

@ -401,7 +401,7 @@ class API(base.Base):
# Check the quota
try:
quotas = objects.Quotas(context)
quotas.reserve(context, instances=max_count,
quotas.reserve(instances=max_count,
cores=req_cores, ram=req_ram)
except exception.OverQuota as exc:
# OK, we exceeded quota; let's figure out why...
@ -1737,8 +1737,7 @@ class API(base.Base):
instance=instance)
quotas = objects.Quotas(context)
quotas.reserve(context,
project_id=project_id,
quotas.reserve(project_id=project_id,
user_id=user_id,
instances=-1,
cores=-instance_vcpus,
@ -2439,14 +2438,14 @@ class API(base.Base):
instance.save(expected_task_state=[None])
except Exception:
with excutils.save_and_reraise_exception():
quotas.rollback(context)
quotas.rollback()
migration.status = 'reverting'
migration.save()
# With cells, the best we can do right now is commit the reservations
# immediately...
if CONF.cells.enable:
quotas.commit(context)
quotas.commit()
self._record_action_start(context, instance,
instance_actions.REVERT_RESIZE)
@ -2476,7 +2475,7 @@ class API(base.Base):
# With cells, the best we can do right now is commit the reservations
# immediately...
if CONF.cells.enable:
quotas.commit(context)
quotas.commit()
self._record_action_start(context, instance,
instance_actions.CONFIRM_RESIZE)
@ -2551,11 +2550,11 @@ class API(base.Base):
quotas can use the correct project_id/user_id.
@return: nova.objects.quotas.Quotas
"""
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
if deltas:
project_id, user_id = quotas_obj.ids_from_instance(context,
instance)
quotas.reserve(context, project_id=project_id, user_id=user_id,
quotas.reserve(project_id=project_id, user_id=user_id,
**deltas)
return quotas
@ -2565,7 +2564,7 @@ class API(base.Base):
"""Special API cell logic for resize."""
# With cells, the best we can do right now is commit the
# reservations immediately...
quotas.commit(context)
quotas.commit()
# NOTE(johannes/comstud): The API cell needs a local migration
# record for later resize_confirm and resize_reverts to deal
# with quotas. We don't need source and/or destination
@ -2655,7 +2654,7 @@ class API(base.Base):
allowed=total_allowed,
resource=resource)
else:
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
instance.task_state = task_states.RESIZE_PREP
instance.progress = 0
@ -3693,7 +3692,7 @@ class AggregateAPI(base.Base):
self.is_safe_to_update_az(context, metadata, hosts=[host_name],
aggregate=aggregate)
aggregate.add_host(context, host_name)
aggregate.add_host(host_name)
self.scheduler_client.update_aggregates(context, [aggregate])
self._update_az_cache_for_host(context, host_name, aggregate.metadata)
# NOTE(jogo): Send message to host to support resource pools
@ -3995,11 +3994,11 @@ class SecurityGroupAPI(base.Base, security_group_base.SecurityGroupBase):
msg = _("Security group is still in use")
self.raise_invalid_group(msg)
quotas = objects.Quotas()
quotas = objects.Quotas(context=context)
quota_project, quota_user = quotas_obj.ids_from_security_group(
context, security_group)
try:
quotas.reserve(context, project_id=quota_project,
quotas.reserve(project_id=quota_project,
user_id=quota_user, security_groups=-1)
except Exception:
LOG.exception(_LE("Failed to update usages deallocating "

View File

@ -850,11 +850,11 @@ class NetworkManager(manager.Manager):
# Check the quota; can't put this in the API because we get
# called into from other places
quotas = self.quotas_cls()
quotas = self.quotas_cls(context=context)
quota_project, quota_user = quotas_obj.ids_from_instance(context,
instance)
try:
quotas.reserve(context, fixed_ips=1, project_id=quota_project,
quotas.reserve(fixed_ips=1, project_id=quota_project,
user_id=quota_user)
cleanup.append(functools.partial(quotas.rollback, context))
except exception.OverQuota as exc:
@ -939,7 +939,7 @@ class NetworkManager(manager.Manager):
self._teardown_network_on_host,
context, network))
quotas.commit(context)
quotas.commit()
if address is None:
# TODO(mriedem): should _setup_network_on_host return the addr?
LOG.debug('Fixed IP is setup on network %s but not returning '
@ -979,11 +979,11 @@ class NetworkManager(manager.Manager):
instance = objects.Instance.get_by_uuid(
context.elevated(read_deleted='yes'), instance_uuid)
quotas = self.quotas_cls()
quotas = self.quotas_cls(context=context)
quota_project, quota_user = quotas_obj.ids_from_instance(context,
instance)
try:
quotas.reserve(context, fixed_ips=-1, project_id=quota_project,
quotas.reserve(fixed_ips=-1, project_id=quota_project,
user_id=quota_user)
except Exception:
LOG.exception(_LE("Failed to update usages deallocating "
@ -1056,14 +1056,14 @@ class NetworkManager(manager.Manager):
except Exception:
with excutils.save_and_reraise_exception():
try:
quotas.rollback(context)
quotas.rollback()
except Exception:
LOG.warning(_LW("Failed to rollback quota for "
"deallocate fixed ip: %s"), address,
instance=instance)
# Commit the reservations
quotas.commit(context)
quotas.commit()
def lease_fixed_ip(self, context, address):
"""Called by dhcp-bridge when ip is leased."""

View File

@ -1250,7 +1250,7 @@ class _ComputeAPIUnitTestMixIn(object):
fake_mig.save().WithSideEffects(_check_mig)
if self.cell_type:
fake_quotas.commit(self.context)
fake_quotas.commit()
self.compute_api._record_action_start(self.context, fake_inst,
'confirmResize')
@ -1318,7 +1318,7 @@ class _ComputeAPIUnitTestMixIn(object):
fake_mig.save().WithSideEffects(_check_mig)
if self.cell_type:
fake_quotas.commit(self.context)
fake_quotas.commit()
self.compute_api._record_action_start(self.context, fake_inst,
'revertResize')
@ -1365,7 +1365,7 @@ class _ComputeAPIUnitTestMixIn(object):
actual=task_states.RESIZE_REVERTING, expected=None)
fake_inst.save(expected_task_state=[None]).AndRaise(exc)
fake_quotas.rollback(self.context)
fake_quotas.rollback()
self.mox.ReplayAll()
self.assertRaises(exception.UnexpectedTaskStateError,
@ -1448,7 +1448,7 @@ class _ComputeAPIUnitTestMixIn(object):
else:
expected_reservations = []
if self.cell_type == 'api':
fake_quotas.commit(self.context)
fake_quotas.commit()
expected_reservations = []
mig = objects.Migration()

View File

@ -2575,7 +2575,7 @@ class CommonNetworkTestCase(test.TestCase):
manager.deallocate_fixed_ip,
self.context, 'fake', 'fake',
instance=fake_inst(uuid='ignoreduuid'))
rollback.assert_called_once_with(self.context)
rollback.assert_called_once_with()
def test_fixed_cidr_out_of_range(self):
manager = network_manager.NetworkManager()