def list( self, bento_name=None, offset=None, limit=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) # 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() result = [ _bento_orm_obj_to_pb(bento_obj) for bento_obj in query_result if not bento_obj.deleted ] return result
def add(self, bento_name, bento_version, uri, uri_type): with create_session(self.sess_maker) as sess: bento_obj = Bento() bento_obj.name = bento_name bento_obj.version = bento_version bento_obj.uri = uri bento_obj.uri_type = BentoUri.StorageType.Name(uri_type) return sess.add(bento_obj)
def update_deployment(self, name, namespace): with create_session(self.sess_maker) as sess: try: deployment_obj = (sess.query(Deployment).filter_by( name=name, namespace=namespace).one()) yield deployment_obj except NoResultFound: yield None
def get(self, name, namespace): with create_session(self.sess_maker) as sess: try: deployment_obj = (sess.query(Deployment).filter_by( name=name, namespace=namespace).one()) except NoResultFound: return None return deployment_orm_obj_to_pb(deployment_obj)
def delete(self, name, namespace): with create_session(self.sess_maker) as sess: try: deployment = (sess.query(Deployment).filter_by( name=name, namespace=namespace).one()) return sess.delete(deployment) except NoResultFound: raise YataiDeploymentException( "Deployment '%s' in namespace: '%s' is not found" % name, namespace)
def add(self, deployment_pb): with create_session() as session: deployment_obj = Deployment( name=deployment_pb.name, namespace=deployment_pb.namespace, spec=MessageToDict(deployment_pb.spec), labels=MessageToDict(deployment_pb.labels), annotation=MessageToDict(deployment_pb.labels), ) session.add(deployment_obj)
def insert_or_update(self, deployment_pb): with create_session(self.sess_maker) as sess: try: deployment_obj = (sess.query(Deployment).filter_by( name=deployment_pb.name, namespace=deployment_pb.namespace).one()) if deployment_obj: # updating deployment record in db deployment_pb_to_orm_obj(deployment_pb, deployment_obj) except NoResultFound: sess.add(deployment_pb_to_orm_obj(deployment_pb))
def get(self, bento_name, bento_version): with create_session(self.sess_maker) as sess: try: bento_obj = (sess.query(Bento).filter_by( name=bento_name, version=bento_version).one()) if bento_obj.deleted: # bento has been marked as deleted return None return _bento_orm_obj_to_pb(bento_obj) except NoResultFound: return None
def update_bento_service_metadata(self, bento_name, bento_version, bento_service_metadata_pb): with create_session(self.sess_maker) as sess: try: bento_obj = (sess.query(Bento).filter_by(name=bento_name, version=bento_version, deleted=False).one()) bento_obj.bento_service_metadata = ProtoMessageToDict( bento_service_metadata_pb) except NoResultFound: raise YataiRepositoryException( "Bento %s:%s is not found in repository" % bento_name, bento_version)
def list(self, bento_name=None, offset=None, limit=None, filter_str=None): with create_session(self.sess_maker) as sess: query = sess.query(Bento) if limit: query.limit(limit) if offset: query.offset(offset) if filter_str: query.filter(Bento.name.contains(filter_str)) if bento_name: query.filter_by(name=bento_name) return query.all()
def _get_latest(self, bento_name): with create_session(self.sess_maker) as sess: query = ( sess.query(Bento) .filter_by(name=bento_name, deleted=False) .order_by(desc(Bento.created_at)) .limit(1) ) query_result = query.all() if len(query_result) == 1: return _bento_orm_obj_to_pb(query_result[0]) else: return None
def dangerously_delete(self, bento_name, bento_version): with create_session(self.sess_maker) as sess: try: bento_obj = (sess.query(Bento).filter_by( name=bento_name, version=bento_version).one()) if bento_obj.deleted: raise YataiRepositoryException( "Bento {}:{} has already been deleted".format( bento_name, bento_version)) bento_obj.deleted = True except NoResultFound: raise YataiRepositoryException( "Bento %s:%s is not found in repository" % bento_name, bento_version)
def update_upload_status(self, bento_name, bento_version, upload_status_pb): with create_session(self.sess_maker) as sess: try: bento_obj = (sess.query(Bento).filter_by(name=bento_name, version=bento_version, deleted=False).one()) # TODO: # if bento_obj.upload_status and bento_obj.upload_status.updated_at > # upload_status_pb.updated_at, update should be ignored bento_obj.upload_status = ProtoMessageToDict(upload_status_pb) except NoResultFound: raise YataiRepositoryException( "Bento %s:%s is not found in repository" % bento_name, bento_version)
def list( self, namespace, operator=None, labels_query=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 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'].contains(operator_name)) if labels_query: # We only handle key=value query at the moment, the more advanced query # such as `in` or `notin` are not handled. labels_list = labels_query.split(',') for label in labels_list: if '=' not in label: raise BadInput( 'Invalid label format. Please present query in ' 'key=value format') label_key, label_value = label.split('=') query = query.filter( Deployment.labels[label_key].contains(label_value)) # 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() return list(map(_deployment_orm_obj_to_pb, query_result))
def list(self, namespace, filter_str=None, labels=None, offset=None, limit=None): with create_session(self.sess_maker) as sess: query = sess.query(Deployment) if namespace != ALL_NAMESPACE_TAG: # else query all namespaces query.filter_by(namespace=namespace) if limit: query.limit(limit) if offset: query.offset(offset) if filter_str: query.filter(Deployment.name.contains(filter_str)) if labels: raise NotImplementedError( "Listing by labels is not yet implemented") return query.all()
def list( self, namespace, operator=None, labels=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 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'].contains(operator_name) ) if labels: raise NotImplementedError("Listing by labels is not yet implemented") # 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() return list(map(_deployment_orm_obj_to_pb, query_result))
def insert(self, deployment_pb): with create_session(self.sess_maker) as sess: deployment_obj = deployment_pb_to_orm_obj(deployment_pb) return sess.add(deployment_obj)
def delete(self, name, namespace): with create_session(self.sess_maker) as sess: deployment = (sess.query(Deployment).filter_by( name=name, namespace=namespace).one()) return sess.delete(deployment)
def get(self, name): with create_session() as session: return session.query(Deployment).filter_by(name=name).first()