From 0695d0739b216d5b0624a637a8a288f9e28a1643 Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Thu, 7 Jan 2016 15:34:54 +0000
Subject: [PATCH] Task #8899: Propagate username, password, port to pg.connect.
 Also, return dbcreds["port"] as an int

---
 LCS/PyCommon/dbcredentials.py         | 11 ++++++++---
 SAS/OTDB_Services/TreeService.py      |  2 +-
 SAS/OTDB_Services/TreeStatusEvents.py |  2 +-
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/LCS/PyCommon/dbcredentials.py b/LCS/PyCommon/dbcredentials.py
index cb197e26617..5f3400446e4 100644
--- a/LCS/PyCommon/dbcredentials.py
+++ b/LCS/PyCommon/dbcredentials.py
@@ -48,9 +48,9 @@ class DBCredentials:
     # Flavour of database (postgres, mysql, oracle, sqlite)
     "type": "postgres",
 
-    # Connection information
+    # Connection information (port 0 = use default)
     "host": "localhost",
-    "port": "",
+    "port": 0,
 
     # Authentication
     "user": "{USER}".format(**environ),
@@ -81,7 +81,12 @@ class DBCredentials:
       Return credentials for a given database.
     """
     try:
-      return dict(self.config.items(self._section(database)))
+      d = dict(self.config.items(self._section(database)))
+
+      # fix types
+      d["port"] = int(d["port"] or 0)
+
+      return d
     except NoSectionError:
       return self.config.defaults()
 
diff --git a/SAS/OTDB_Services/TreeService.py b/SAS/OTDB_Services/TreeService.py
index 7684e879f48..578d94ffefb 100755
--- a/SAS/OTDB_Services/TreeService.py
+++ b/SAS/OTDB_Services/TreeService.py
@@ -224,7 +224,7 @@ class PostgressMessageHandlerInterface(MessageHandlerInterface):
         self.connected = (self.connection and self.connection.status == 1)
         while not self.connected:
             try:
-                self.connection = pg.connect(user=self.dbcreds["user"], host=self.dbcreds["host"], dbname=self.dbcreds["database"])
+                self.connection = pg.connect(user=self.dbcreds["user"], passwd=self.dbcreds["password"], host=self.dbcreds["host"], port=self.dbcreds["port"] or -1, dbname=self.dbcreds["database"])
                 self.connected = True
                 logger.info("Connected to database %s on host %s" % (self.dbcreds["database"], self.dbcreds["host"]))
             except (TypeError, SyntaxError, pg.InternalError), e:
diff --git a/SAS/OTDB_Services/TreeStatusEvents.py b/SAS/OTDB_Services/TreeStatusEvents.py
index e2b6e5a73cf..32220e3b957 100755
--- a/SAS/OTDB_Services/TreeStatusEvents.py
+++ b/SAS/OTDB_Services/TreeStatusEvents.py
@@ -106,7 +106,7 @@ if __name__ == "__main__":
             while alive and not connected:
                 # Connect to the database
                 try:
-                    otdb_connection = pg.connect(user="postgres", host=dbcreds["host"], dbname=dbcreds["database"])
+                    otdb_connection = pg.connect(user=dbcreds["user"], passwd=dbcreds["password"], host=dbcreds["host"], port=dbcreds["port"] or -1, dbname=dbcreds["database"])
                     connected = True
                     # Get list of allowed tree states
                     allowed_states = {}
-- 
GitLab