from sqlalchemy import ( Column, String, BigInteger, ) from models.base import Base, BaseMixin class Credential(Base, BaseMixin): __tablename__ = "credential" cluster_id = Column(String(64), nullable=False, unique=True) host = Column(String(128), nullable=False) port = Column(String(8), nullable=False) username = Column(String(32), nullable=False) passwd = Column(String(64), nullable=False) @classmethod def upsert(cls, s, cluster_id, host, port, username, passwd): item = cls.get_by_cluster_id(s, cluster_id) if item: item.host = host item.port = port item.username = username item.passwd = passwd else: item = cls(cluster_id=cluster_id, host=host, port=port, username=username, passwd=passwd) s.add(item) s.flush() return item @classmethod def get_by_cluster_id(cls, s, cluster_id): return s.query(cls).filter(cls.cluster_id == cluster_id).first()