def get_collections_by_request(request_id=None, workload_id=None, session=None): """ Get collections of a request. :param request_id: the request id. :param workload_id: The workload_id of the request. :param session: The database session in use. :returns: dict of {'transform_id': []} """ request_id = orm_requests.get_request_id(request_id, workload_id, session=session) transform_ids = orm_transforms.get_transform_ids(request_id, session=session) collections = orm_collections.get_collections_by_transform_ids( transform_ids=transform_ids, session=session) rets = {} for collection in collections: if request_id not in rets: rets[request_id] = {} transform_id = collection['transform_id'] if transform_id not in rets[request_id]: rets[request_id][transform_id] = [] rets[request_id][transform_id].append(collection) return rets
def get_collections(scope, name, request_id=None, workload_id=None, session=None): """ Get collections by scope, name, request_id and workload id. :param scope: scope of the collection. :param name: name the the collection. :param request_id: the request id. :param workload_id: The workload_id of the request. :param session: The database session in use. :returns: dict of collections """ if scope is None and name is None: return get_collections_by_request(request_id=request_id, workload_id=workload_id, session=session) if request_id is None and workload_id is not None: request_id = orm_requests.get_request_id(request_id, workload_id, session=session) transform_ids = None if request_id: transform_ids = orm_transforms.get_transform_ids(request_id, session=session) collections = orm_collections.get_collections(scope=scope, name=name, transform_ids=transform_ids, session=session) rets = {} for collection in collections: if request_id not in rets: rets[request_id] = {} transform_id = collection['transform_id'] if transform_id not in rets[request_id]: rets[request_id][transform_id] = [] rets[request_id][transform_id].append(collection) return rets
def get_collections_by_request_transform_id(request_id=None, transform_id=None, session=None): """ Get collections by request_id and transform id or raise a NoObject exception. :param request_id: The request id related to this collection. :param transform_id: The transform id related to this collection. :param session: The database session in use. :raises NoObject: If no collections are founded. :returns: list of Collections. """ if request_id is not None: request_id = orm_requests.get_request_id(request_id=request_id, session=session) transform_ids = orm_transforms.get_transform_ids( request_id, transform_id=transform_id, session=session) else: if transform_id is None: transform_ids = [] else: transform_ids = [transform_id] if transform_ids: collections = orm_collections.get_collections_by_transform_ids( transform_ids=transform_ids, session=session) else: collections = [] rets = {} for collection in collections: if request_id not in rets: rets[request_id] = {} transform_id = collection['transform_id'] if transform_id not in rets[request_id]: rets[request_id][transform_id] = [] rets[request_id][transform_id].append(collection) return rets
def register_output_contents(coll_scope, coll_name, contents, request_id=None, workload_id=None, relation_type=CollectionRelationType.Output, session=None): """ register contents with collection scope, collection name, request id, workload id and contents. :param coll_scope: scope of the collection. :param coll_name: name the the collection. :param request_id: the request id. :param workload_id: The workload_id of the request. :param contents: list of contents [{'scope': <scope>, 'name': <name>, 'min_id': min_id, 'max_id': max_id, 'status': <status>, 'path': <path>}]. :param session: The database session in use. """ if (request_id is None and workload_id is None) or coll_scope is None or coll_name is None: msg = "Only one of (request_id, workload_id) can be None. All other parameters should not be None: " msg += "request_id=%s, workload_id=%s, coll_scope=%s, coll_name=%s" % ( request_id, workload_id, coll_scope, coll_name) raise exceptions.WrongParameterException(msg) if request_id is None and workload_id is not None: request_id = orm_requests.get_request_id(request_id, workload_id, session=session) coll_id = orm_collections.get_collection_id_by_scope_name(coll_scope, coll_name, request_id, relation_type, session=session) parameters = [] for content in contents: if 'status' not in content or content['status'] is None: raise exceptions.WrongParameterException( "Content status is required and should not be None: %s" % content) if content['status'] in [ ContentStatus.Available, ContentStatus.Available.value ]: content_keys = [ 'scope', 'name', 'min_id', 'max_id', 'status', 'path' ] else: content_keys = ['scope', 'name', 'min_id', 'max_id', 'status'] parameter = {} for key in content_keys: if content[key] is None: raise exceptions.WrongParameterException( "Content %s should not be None" % key) parameter[key] = content[key] if isinstance(parameter['status'], ContentStatus): parameter['status'] = parameter['status'].value parameter['coll_id'] = coll_id parameters.append(parameter) orm_contents.update_contents(parameters, session=session)