def connect(source): """connect to the DB using properties from the source""" host, dbname = source['addr'].rsplit('/', 1) port = 5432 if ':' in host: host, port = host.rsplit(':', 1) port = int(port) # pyscopg expects port to be numeric try: conn = psycopg2.connect( host=host, port=port, user=source['user'], password=source['password'], dbname=dbname, connect_timeout=CONNECT_TIMEOUT ) cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) except psycopg2.OperationalError, e: if 'authentication failed' in e.message: e = panoply.PanoplyException( "Login failed for user: {}".format(source['user']), retryable=False ) raise e
def connect(source: Dict) -> Connector: """connect to the DB using properties from the source""" # create partial DSN, user & pass still supplied as kwargs # as they're input separately from addr and will take precendence # over any user/pass from addr if 'addr' in source: # kept for backward compatibility dsn = 'postgres://{}'.format(source['addr']) else: dsn = 'postgres://{}:{}/{}'.format(source['host'], source['port'], source['db_name']) try: conn = psycopg2.connect(dsn=dsn, user=source['username'], password=source['password'], connect_timeout=CONNECT_TIMEOUT) cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) except psycopg2.OperationalError as e: if 'authentication failed' in str(e): e = panoply.PanoplyException("Login failed for user: {}".format( source['username']), retryable=False) raise e return Connector(connection=conn, cursor=cur)
def connect(source): """connect to the DB using properties from the source""" # create partial DSN, user & pass still supplied as kwargs # as they're input separately from addr and will take precendence # over any user/pass from addr dsn = 'postgres://%s' % source['addr'] try: conn = psycopg2.connect(dsn=dsn, user=source['user'], password=source['password'], connect_timeout=CONNECT_TIMEOUT) cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) except psycopg2.OperationalError, e: if 'authentication failed' in e.message: e = panoply.PanoplyException("Login failed for user: {}".format( source['user']), retryable=False) raise e
def connect(source, log): '''connect to the DB using properties from the source''' host, dbname = source['addr'].rsplit('/', 1) port = 5432 if ':' in host: host, port = host.rsplit(':', 1) port = int(port) # pyscopg expects port to be numeric try: conn = psycopg2.connect( host=host, port=port, user=source['user'], password=source['password'], dbname=dbname ) cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) except psycopg2.OperationalError, e: log(e) raise panoply.PanoplyException( "Login failed for user: {}".format(source['user']), retryable=False )