def get_segment_allocation_ranges(context, **filters): LOG.info("get_segment_allocation_ranges for tenant %s" % context.tenant_id) if not context.is_admin: raise exceptions.NotAuthorized() sa_ranges = db_api.segment_allocation_range_find( context, scope=db_api.ALL, **filters) return [v._make_segment_allocation_range_dict(m) for m in sa_ranges]
def create_segment_allocation_range(context, sa_range): LOG.info("create_segment_allocation_range for tenant %s" % context.tenant_id) if not context.is_admin: raise exceptions.NotAuthorized() sa_range = sa_range.get("segment_allocation_range") if not sa_range: raise exceptions.BadRequest(resource="segment_allocation_range", msg=("segment_allocation_range not in " "request body.")) # TODO(morgabra) Figure out how to get the api extension to validate this # for us. # parse required fields for k in ["first_id", "last_id", "segment_id", "segment_type"]: sa_range[k] = sa_range.get(k, None) if sa_range[k] is None: raise exceptions.BadRequest( resource="segment_allocation_range", msg=("Missing required key %s in request body." % (k))) # parse optional fields for k in ["do_not_use"]: sa_range[k] = sa_range.get(k, None) # use the segment registry to validate and create/populate the range if not SA_REGISTRY.is_valid_strategy(sa_range["segment_type"]): raise exceptions.BadRequest( resource="segment_allocation_range", msg=("Unknown segment type '%s'" % (k))) strategy = SA_REGISTRY.get_strategy(sa_range["segment_type"]) # Create the new range with context.session.begin(): new_range = strategy.create_range(context, sa_range) # Bulk-populate the range, this could take a while for huge ranges # (millions) so we do this in chunks outside the transaction. That # means we need to rollback the range creation if it fails for # whatever reason (and it will cascade delete any added allocations) try: strategy.populate_range(context, new_range) except Exception: LOG.exception("Failed to populate segment allocation range.") delete_segment_allocation_range(context, new_range["id"]) raise return v._make_segment_allocation_range_dict(new_range)
def get_segment_allocation_range(context, id, fields=None): LOG.info("get_segment_allocation_range %s for tenant %s fields %s" % (id, context.tenant_id, fields)) if not context.is_admin: raise n_exc.NotAuthorized() sa_range = db_api.segment_allocation_range_find(context, id=id, scope=db_api.ONE) if not sa_range: raise q_exc.SegmentAllocationRangeNotFound( segment_allocation_range_id=id) # Count up allocations so we can calculate how many are free. allocs = db_api.segment_allocation_find( context, segment_allocation_range_id=sa_range["id"], deallocated=False).count() return v._make_segment_allocation_range_dict(sa_range, allocations=allocs)
def get_segment_allocation_range(context, id, fields=None): LOG.info("get_segment_allocation_range %s for tenant %s fields %s" % (id, context.tenant_id, fields)) if not context.is_admin: raise exceptions.NotAuthorized() sa_range = db_api.segment_allocation_range_find( context, id=id, scope=db_api.ONE) if not sa_range: raise quark_exceptions.SegmentAllocationRangeNotFound( segment_allocation_range_id=id) # Count up allocations so we can calculate how many are free. allocs = db_api.segment_allocation_find( context, segment_allocation_range_id=sa_range["id"], deallocated=False).count() return v._make_segment_allocation_range_dict( sa_range, allocations=allocs)