def list( self, namespace, operator=None, label_selectors=None, offset=None, limit=None, order_by=ListDeploymentsRequest.created_at, ascending_order=False, ): with create_session(self.sess_maker) as sess: query = sess.query(Deployment) order_by = ListDeploymentsRequest.SORTABLE_COLUMN.Name(order_by) order_by_field = getattr(Deployment, order_by) order_by_action = (order_by_field if ascending_order else desc(order_by_field)) query = query.order_by(order_by_action) if label_selectors.match_labels or label_selectors.match_expressions: deployment_ids = filter_label_query(sess, RESOURCE_TYPE.deployment, label_selectors) query.filter(Deployment.id.in_(deployment_ids)) if namespace != ALL_NAMESPACE_TAG: # else query all namespaces query = query.filter_by(namespace=namespace) if operator: operator_name = DeploymentSpec.DeploymentOperator.Name( operator) query = query.filter( Deployment.spec['operator'].as_string().contains( operator_name)) # We are not defaulting limit to 200 in the signature, # because protobuf will pass 0 as value limit = limit or 200 # Limit and offset need to be called after order_by filter/filter_by is # called query = query.limit(limit) if offset: query = query.offset(offset) query_result = query.all() deployment_ids = [ deployment_obj.id for deployment_obj in query_result ] labels = list_labels(sess, RESOURCE_TYPE.deployment, deployment_ids) return [ _deployment_orm_obj_to_pb(deployment_obj, labels.get(str(deployment_obj.id))) for deployment_obj in query_result ]
def list( self, bento_name=None, offset=None, limit=None, label_selectors=None, order_by=ListBentoRequest.created_at, ascending_order=False, ): with create_session(self.sess_maker) as sess: query = sess.query(Bento) order_by = ListBentoRequest.SORTABLE_COLUMN.Name(order_by) order_by_field = getattr(Bento, order_by) order_by_action = ( order_by_field if ascending_order else desc(order_by_field) ) query = query.order_by(order_by_action) if bento_name: # filter_by apply filtering criterion to a copy of the query query = query.filter_by(name=bento_name) query = query.filter_by(deleted=False) if label_selectors is not None and ( label_selectors.match_labels or label_selectors.match_expressions ): bento_ids = filter_label_query( sess, RESOURCE_TYPE.bento, label_selectors ) query = query.filter(Bento.id.in_(bento_ids)) # We are not defaulting limit to 200 in the signature, # because protobuf will pass 0 as value limit = limit or DEFAULT_LIST_LIMIT # Limit and offset need to be called after order_by filter/filter_by is # called query = query.limit(limit) if offset: query = query.offset(offset) query_result = query.all() bento_ids = [bento_obj.id for bento_obj in query_result] labels = list_labels(sess, RESOURCE_TYPE.bento, bento_ids) result = [ _bento_orm_obj_to_pb(bento_obj, labels.get(str(bento_obj.id))) for bento_obj in query_result ] return result