SEBA-38 Add DatabaseInfo endpoint

Change-Id: I6892db226673155b779bab8aaf4a9d5337e4d016
diff --git a/VERSION b/VERSION
index e650c01..15a2799 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.2.9
+3.3.0
diff --git a/xos/coreapi/protos/utility.proto b/xos/coreapi/protos/utility.proto
index 2f81055..c351f1f 100644
--- a/xos/coreapi/protos/utility.proto
+++ b/xos/coreapi/protos/utility.proto
@@ -43,6 +43,18 @@
     string arch = 6;
 };
 
+message DatabaseInfo {
+    enum DatabaseStatus {
+        UNKNOWN=0;
+        OPERATIONAL=1;
+        ERROR=2;
+    }
+    string name = 1;
+    string connection = 2;
+    string version = 3;
+    DatabaseStatus status = 4;
+}
+
 message PopulatedServiceInstance {
     option (contentTypeId) = "core.serviceinstance";
     oneof id_present {
@@ -124,5 +136,11 @@
         };
   }
 
+  rpc GetDatabaseInfo(google.protobuf.Empty) returns (DatabaseInfo) {
+        option (googleapi.http) = {
+            get: "/xosapi/v1/dbinfo"
+        };
+  }
+
 
 };
diff --git a/xos/coreapi/xos_utility_api.py b/xos/coreapi/xos_utility_api.py
index 9ab9220..46514a7 100644
--- a/xos/coreapi/xos_utility_api.py
+++ b/xos/coreapi/xos_utility_api.py
@@ -21,6 +21,7 @@
 from xos.exceptions import XOSNotAuthenticated
 from core.models import ServiceInstance
 from django.db.models import F, Q
+from django.db import connection
 import django.apps
 from django.contrib.auth import authenticate as django_authenticate
 import fnmatch
@@ -273,8 +274,8 @@
         ).inc()
         return response
 
-    @translate_exceptions("Utilities", "GetXproto")
-    @track_request_time("Utilities", "GetXproto")
+    @translate_exceptions("Utilities", "GetVersion")
+    @track_request_time("Utilities", "GetVersion")
     def GetVersion(self, request, context):
         res = utility_pb2.VersionInfo()
 
@@ -297,10 +298,47 @@
         res.os = os.uname()[0].lower()
         res.arch = os.uname()[4].lower()
 
-        # TODO(smbaker): res.builTime
-        # TODO(smbaker): res.gitCommit
-
         REQUEST_COUNT.labels(
             "xos-core", "Utilities", "GetVersion", grpc.StatusCode.OK
         ).inc()
         return res
+
+    @translate_exceptions("Utilities", "GetDatabaseInfo")
+    @track_request_time("Utilities", "GetDatabaseInfo")
+    def GetDatabaseInfo(self, request, context):
+        res = utility_pb2.DatabaseInfo()
+
+        res.name = settings.DB["NAME"]
+        res.connection = "%s:%s" % (settings.DB["HOST"], settings.DB["PORT"])
+
+        # Start by assuming the db is operational, then we'll perform some tests
+        # to make sure it's working as we expect.
+        res.status = res.OPERATIONAL
+
+        # TODO(smbaker): Think about whether these are postgres-specific checks and what might happen
+        # if another db is configured.
+
+        try:
+            server_version = connection.cursor().connection.server_version
+            # example: '100003' for postgres 10.3
+            res.version = "%d.%d" % (server_version/10000, server_version % 10000)
+        except Exception:
+            res.version = "Unknown"
+            res.status = res.ERROR
+
+        if res.status == res.OPERATIONAL:
+            # Try performing a simple query that evaluates a constant. This will prove we are talking
+            # to the database.
+            try:
+                cursor = connection.cursor()
+                cursor.execute("select 1")
+                result = cursor.fetchone()
+                assert(len(result) == 1)
+                assert(result[0] == 1)
+            except Exception:
+                res.status = res.ERROR
+
+        REQUEST_COUNT.labels(
+            "xos-core", "Utilities", "GetDatabaseInfo", grpc.StatusCode.OK
+        ).inc()
+        return res
diff --git a/xos/xos/settings.py b/xos/xos/settings.py
index c9ec5f7..dd8e5aa 100644
--- a/xos/xos/settings.py
+++ b/xos/xos/settings.py
@@ -14,10 +14,8 @@
 
 
 from django import VERSION as DJANGO_VERSION
-import socket
 import os
 import warnings
-from urlparse import urlparse
 
 # Initializing xosconfig module
 from xosconfig import Config