def _add_inventory_to_provider(conn, rp, inv_list, to_add): """Inserts new inventory records for the supplied resource provider. :param conn: DB connection to use. :param rp: Resource provider to add inventory to. :param inv_list: InventoryList object :param to_add: set() containing resource class IDs to search inv_list for adding to resource provider. """ for rc_id in to_add: rc_str = _RC_CACHE.string_from_id(rc_id) inv_record = inv_list.find(rc_str) if inv_record.capacity <= 0: raise exception.InvalidInventoryCapacity( resource_class=rc_str, resource_provider=rp.uuid) ins_stmt = _INV_TBL.insert().values( resource_provider_id=rp.id, resource_class_id=rc_id, total=inv_record.total, reserved=inv_record.reserved, min_unit=inv_record.min_unit, max_unit=inv_record.max_unit, step_size=inv_record.step_size, allocation_ratio=inv_record.allocation_ratio) conn.execute(ins_stmt)
def _update_inventory_for_provider(conn, rp, inv_list, to_update): """Updates existing inventory records for the supplied resource provider. :param conn: DB connection to use. :param rp: Resource provider on which to update inventory. :param inv_list: InventoryList object :param to_update: set() containing resource class IDs to search inv_list for updating in resource provider. :returns: A list of (uuid, class) tuples that have exceeded their capacity after this inventory update. """ exceeded = [] for res_class in to_update: inv_record = inv_list.find(res_class) if inv_record.capacity <= 0: raise exception.InvalidInventoryCapacity( resource_class=fields.ResourceClass.from_index(res_class), resource_provider=rp.uuid) allocation_query = sa.select( [func.sum(_ALLOC_TBL.c.used).label('usage')]).\ where(sa.and_( _ALLOC_TBL.c.resource_provider_id == rp.id, _ALLOC_TBL.c.resource_class_id == res_class)) allocations = conn.execute(allocation_query).first() if allocations and allocations['usage'] > inv_record.capacity: exceeded.append( (rp.uuid, fields.ResourceClass.from_index(res_class))) upd_stmt = _INV_TBL.update().where( sa.and_(_INV_TBL.c.resource_provider_id == rp.id, _INV_TBL.c.resource_class_id == res_class)).values( total=inv_record.total, reserved=inv_record.reserved, min_unit=inv_record.min_unit, max_unit=inv_record.max_unit, step_size=inv_record.step_size, allocation_ratio=inv_record.allocation_ratio) res = conn.execute(upd_stmt) if not res.rowcount: raise exception.NotFound( 'No inventory of class %s found for update' % fields.ResourceClass.from_index(res_class)) return exceeded