def update(self, req, id, body): context = req.environ['karbor.context'] LOG.info("Update quotas with id: %s", id, context=context) if not uuidutils.is_uuid_like(id): msg = _("Invalid project id provided.") raise exc.HTTPBadRequest(explanation=msg) context.can(quota_policy.UPDATE_POLICY) project_id = id bad_keys = [] for key, value in body.get('quota', {}).items(): if (key not in QUOTAS and key not in NON_QUOTA_KEYS): bad_keys.append(key) continue if key not in NON_QUOTA_KEYS and value: try: value = int(value) except (ValueError, TypeError): msg = _("Quota '%(value)s' for %(key)s should be " "integer.") % {'value': value, 'key': key} LOG.warning(msg) raise exc.HTTPBadRequest(explanation=msg) for key in body['quota'].keys(): if key in QUOTAS: value = int(body['quota'][key]) self._validate_quota_limit(value) try: db.quota_update(context, project_id, key, value) except exception.ProjectQuotaNotFound: db.quota_create(context, project_id, key, value) except exception.AdminRequired: raise exc.HTTPForbidden() LOG.info("Update quotas successfully.", resource={'id': project_id}) return self._view_builder.detail_list( req, self._get_quotas(context, id))
def test_quota_update(self): quota = db.quota_create(self.ctxt, self.project_id, self.resource, self.limit) db.quota_update(self.ctxt, quota.project_id, quota.resource, 20) quota = db.quota_get(self.ctxt, self.project_id, self.resource) self.assertEqual(20, quota.hard_limit)
def test_quota_get(self): quota = db.quota_create(self.ctxt, self.project_id, self.resource, self.limit) self._assertEqualObjects( quota, db.quota_get(self.ctxt, quota.project_id, quota.resource)) db.quota_destroy(self.ctxt, self.project_id, self.resource)
def test_quota_destroy(self): quota = db.quota_create(self.ctxt, self.project_id, self.resource, self.limit) db.quota_destroy(self.ctxt, quota.project_id, quota.resource) self.assertRaises(exception.ProjectQuotaNotFound, db.quota_get, self.ctxt, quota.project_id, quota.resource)
def test_quota_create(self): quota = db.quota_create(self.ctxt, self.project_id, self.resource, self.limit) self.assertEqual("volume_backups", quota.resource) db.quota_destroy(self.ctxt, self.project_id, self.resource)