###################
-def reservation_create(context, uuid, usage, project_id, resource, delta,
- expire):
- """Create a reservation for the given project and resource."""
- return IMPL.reservation_create(context, uuid, usage, project_id,
- resource, delta, expire)
-
-
-def reservation_get(context, uuid):
- """Retrieve a reservation or raise if it does not exist."""
- return IMPL.reservation_get(context, uuid)
-
-
-def reservation_get_all_by_project(context, project_id):
- """Retrieve all reservations associated with a given project."""
- return IMPL.reservation_get_all_by_project(context, project_id)
-
-
-def reservation_destroy(context, uuid):
- """Destroy the reservation or raise if it does not exist."""
- return IMPL.reservation_destroy(context, uuid)
-
-
-###################
-
-
def quota_reserve(context, resources, quotas, deltas, expire,
until_refresh, max_age, project_id=None):
"""Check quotas and create appropriate reservations."""
###################
-@require_context
-def _reservation_get(context, uuid, session=None):
- result = model_query(context, models.Reservation, session=session,
- read_deleted="no").\
- filter_by(uuid=uuid).first()
-
- if not result:
- raise exception.ReservationNotFound(uuid=uuid)
-
- return result
-
-
-@require_context
-def reservation_get(context, uuid):
- return _reservation_get(context, uuid)
-
-
-@require_context
-def reservation_get_all_by_project(context, project_id):
- authorize_project_context(context, project_id)
-
- rows = model_query(context, models.Reservation, read_deleted="no").\
- filter_by(project_id=project_id).all()
-
- result = {'project_id': project_id}
- for row in rows:
- result.setdefault(row.resource, {})
- result[row.resource][row.uuid] = row.delta
-
- return result
-
-
-@require_admin_context
def _reservation_create(context, uuid, usage, project_id, resource, delta,
expire, session=None):
reservation_ref = models.Reservation()
return reservation_ref
-@require_admin_context
-def reservation_create(context, uuid, usage, project_id, resource, delta,
- expire):
- return _reservation_create(context, uuid, usage, project_id, resource,
- delta, expire)
-
-
-@require_admin_context
-def reservation_destroy(context, uuid):
- session = get_session()
- with session.begin():
- reservation_ref = _reservation_get(context, uuid, session=session)
- reservation_ref.delete(session=session)
-
-
###################
'usage': {'id': 1}
}
- def test_reservation_create(self):
- reservation = db.reservation_create(self.ctxt, **self.values)
- self._assertEqualObjects(self.values, reservation, ignored_keys=(
- 'deleted', 'updated_at',
- 'deleted_at', 'id',
- 'created_at', 'usage',
- 'usage_id'))
- self.assertEqual(reservation['usage_id'], self.values['usage']['id'])
-
- def test_reservation_get(self):
- reservation = db.reservation_create(self.ctxt, **self.values)
- reservation_db = db.reservation_get(self.ctxt, self.values['uuid'])
- self._assertEqualObjects(reservation, reservation_db)
-
- def test_reservation_get_nonexistent(self):
- self.assertRaises(exception.ReservationNotFound,
- db.reservation_get,
- self.ctxt,
- 'non-exitent-resevation-uuid')
-
def test_reservation_commit(self):
reservations = _quota_reserve(self.ctxt, 'project1')
expected = {'project_id': 'project1',
self.assertEqual(expected,
db.quota_usage_get_all_by_project(
self.ctxt, 'project1'))
- db.reservation_get(self.ctxt, reservations[0])
db.reservation_commit(self.ctxt, reservations, 'project1')
- self.assertRaises(exception.ReservationNotFound,
- db.reservation_get,
- self.ctxt,
- reservations[0])
expected = {'project_id': 'project1',
'volumes': {'reserved': 0, 'in_use': 1},
'gigabytes': {'reserved': 0, 'in_use': 2},
db.quota_usage_get_all_by_project(
self.ctxt,
'project1'))
- db.reservation_get(self.ctxt, reservations[0])
db.reservation_rollback(self.ctxt, reservations, 'project1')
- self.assertRaises(exception.ReservationNotFound,
- db.reservation_get,
- self.ctxt,
- reservations[0])
expected = {'project_id': 'project1',
'volumes': {'reserved': 0, 'in_use': 0},
'gigabytes': {'reserved': 0, 'in_use': 0},
self.ctxt,
'project1'))
- def test_reservation_get_all_by_project(self):
- reservations = _quota_reserve(self.ctxt, 'project1')
- r1 = db.reservation_get(self.ctxt, reservations[0])
- r2 = db.reservation_get(self.ctxt, reservations[1])
- expected = {'project_id': 'project1',
- r1['resource']: {r1['uuid']: r1['delta']},
- r2['resource']: {r2['uuid']: r2['delta']}}
- self.assertEqual(expected, db.reservation_get_all_by_project(
- self.ctxt, 'project1'))
-
def test_reservation_expire(self):
self.values['expire'] = datetime.datetime.utcnow() + \
datetime.timedelta(days=1)
self.ctxt,
'project1'))
- def test_reservation_destroy(self):
- reservations = _quota_reserve(self.ctxt, 'project1')
- r1 = db.reservation_get(self.ctxt, reservations[0])
- db.reservation_destroy(self.ctxt, reservations[1])
- expected = {'project_id': 'project1',
- r1['resource']: {r1['uuid']: r1['delta']}}
- self.assertEqual(expected, db.reservation_get_all_by_project(
- self.ctxt, 'project1'))
-
class DBAPIQuotaClassTestCase(BaseTest):
def test_quota_reserve(self):
reservations = _quota_reserve(self.ctxt, 'project1')
self.assertEqual(len(reservations), 2)
- res_names = ['gigabytes', 'volumes']
- for uuid in reservations:
- reservation = db.reservation_get(self.ctxt, uuid)
- self.assertIn(reservation.resource, res_names)
- res_names.remove(reservation.resource)
+ quota_usage = db.quota_usage_get_all_by_project(self.ctxt, 'project1')
+ self.assertEqual({'project_id': 'project1',
+ 'gigabytes': {'reserved': 2, 'in_use': 0},
+ 'volumes': {'reserved': 1, 'in_use': 0}},
+ quota_usage)
def test_quota_destroy(self):
db.quota_create(self.ctxt, 'project1', 'resource1', 41)
self.ctxt, 'project1', 'resource1')
def test_quota_destroy_all_by_project(self):
- reservations = _quota_reserve(self.ctxt, 'project1')
+ _quota_reserve(self.ctxt, 'project1')
db.quota_destroy_all_by_project(self.ctxt, 'project1')
self.assertEqual(db.quota_get_all_by_project(self.ctxt, 'project1'),
{'project_id': 'project1'})
self.assertEqual(db.quota_usage_get_all_by_project(self.ctxt,
'project1'),
{'project_id': 'project1'})
- for r in reservations:
- self.assertRaises(exception.ReservationNotFound,
- db.reservation_get,
- self.ctxt,
- r)
def test_quota_usage_get_nonexistent(self):
self.assertRaises(exception.QuotaUsageNotFound,