Skip to content

Commit

Permalink
cmr-9165: add aurora lib with connection config
Browse files Browse the repository at this point in the history
  • Loading branch information
zimzoom committed Jun 22, 2023
1 parent 7f9d4ab commit e3ace18
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
Empty file added aurora-postgres-lib/README.md
Empty file.
66 changes: 66 additions & 0 deletions aurora-postgres-lib/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
(defproject nasa-cmr/cmr-aurora-postgres-lib "0.1.0-SNAPSHOT"
:description "Contains utilities for connecting to and manipulating data in Aurora PostgreSQL"
:url "https://github.com/nasa/Common-Metadata-Repository/tree/master/aurora-postgres-lib"
;; Dynamically include extra repositories in the project definition if configured.
:dependencies [[nasa-cmr/cmr-common-lib "0.1.1-SNAPSHOT"]
[org.clojure/clojure "1.10.0"]
[org.clojure/java.jdbc "0.4.2"]
[org.postgresql/postgresql "42.6.0"]
[software.amazon.jdbc/aws-advanced-jdbc-wrapper "2.1.2"]]
:plugins [[lein-shell "0.5.0"]
[test2junit "1.3.3"]]
:jvm-opts ^:replace ["-server"
"-Dclojure.compiler.direct-linking=true"]
:profiles {:security {:plugins [[com.livingsocial/lein-dependency-check "1.1.1"]]
:dependency-check {:output-format [:all]
:suppression-file "resources/security/suppression.xml"}}
:dev {:exclusions [[org.clojure/tools.nrepl]]
:dependencies [[org.clojars.gjahad/debug-repl "0.3.3"]
[org.clojure/tools.namespace "0.2.11"]
[org.clojure/tools.nrepl "0.2.13"]]
:jvm-opts ^:replace ["-server"]
:source-paths ["src" "dev" "test"]}
:static {}
;; This profile is used for linting and static analysis. To run for this
;; project, use `lein lint` from inside the project directory. To run for
;; all projects at the same time, use the same command but from the top-
;; level directory.
:lint {:source-paths ^:replace ["src"]
:test-paths ^:replace []
:plugins [[jonase/eastwood "0.2.5"]
[lein-ancient "0.6.15"]
[lein-bikeshed "0.5.0"]
[lein-kibit "0.1.6"]]}
;; The following profile is overriden on the build server or in the user's
;; ~/.lein/profiles.clj file.
:internal-repos {}
:kaocha {:dependencies [[lambdaisland/kaocha "1.0.732"]
[lambdaisland/kaocha-cloverage "1.0.75"]
[lambdaisland/kaocha-junit-xml "0.0.76"]]}}
:aliases {;; Alias to test2junit for consistency with lein-test-out
"test-out" ["test2junit"]

;; Kaocha test aliases
;; refer to tests.edn for test configuration
"kaocha" ["with-profile" "+kaocha" "run" "-m" "kaocha.runner"]
"itest" ["kaocha" "--focus" ":integration"]
"utest" ["kaocha" "--focus" ":unit"]
"ci-test" ["kaocha" "--profile" ":ci"]
"ci-itest" ["itest" "--profile" ":ci"]
"ci-utest" ["utest" "--profile" ":ci"]

;; Linting aliases
"kibit" ["do"
["with-profile" "lint" "shell" "echo" "== Kibit =="]
["with-profile" "lint" "kibit"]]
;; Eastwood needs special handling with libs that include oracle
;; drivers in the deps, in particulear:
;; java.lang.ClassNotFoundException: oracle.dms.console.DMSConsole
"eastwood" ["with-profile" "lint" "eastwood"
"{:namespaces [:source-paths] :exclude-namespaces [cmr.oracle.connection]}"]
"bikeshed" ["with-profile" "lint" "bikeshed" "--max-line-length=100"]
"check-deps" ["with-profile" "lint" "ancient" ":all"]
"check-sec" ["with-profile" "security" "dependency-check"]
"lint" ["do" ["check"] ["kibit"] ["eastwood"]]
;; Placeholder for future docs and enabler of top-level alias
"generate-static" ["with-profile" "static" "shell" "echo"]})
32 changes: 32 additions & 0 deletions aurora-postgres-lib/src/cmr/aurora/config.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns cmr.aurora.config
"Contains functions for retrieving Aurora Postgres connection configuration from environment variables"
(:require [cmr.common.config :as cfg :refer [defconfig]]))

(defconfig aurora-cluster
"Aurora cluster name"
{:default "cmr-aurora-cluster"})

(defconfig aurora-db-name
"Aurora database name"
{:default "metadata_db"})

(defconfig db-url-primary
"Primary db url (for writes and reads)"
{:default (str "jdbc:aws-wrapper:postgresql://localhost:5432/" aurora-db-name)})

(defconfig db-url-secondary
"Secondary db url (for reads only)
Note that for local development this is the same as db-url-primary since there is no local Aurora cluster mock-up."
{:default (str "jdbc:aws-wrapper:postgresql://localhost:5432/" aurora-db-name)})

(defconfig aurora-db-user
"Aurora database user"
{:default "admin"})

(defconfig aurora-db-password
"Aurora database password"
{:default "admin"})

(defconfig aurora-toggle
"Three-way toggle for Aurora functionality. 'aurora-off' uses only Oracle, 'aurora-on' uses both Oracle and Aurora, and 'aurora-only' uses only Aurora"
{:default "aurora-off"})
64 changes: 64 additions & 0 deletions aurora-postgres-lib/src/cmr/aurora/connection.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
(ns cmr.aurora.connection
"Contains functions for interacting with the Aurora DB cluster."
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[clj-time.coerce :as cr]
[cmr.common.log :refer [debug error info trace warn]]
[cmr.common.util :as util]
[cmr.aurora.config :as aurora-config]
[cmr.common.log :refer (debug info warn error)]
[cmr.common.services.errors :as errors]
[cmr.common.services.health-helper :as hh])
(:import
java.sql.DriverManager
java.util.Properties))

(defn db-properties
[username password]
(doto (Properties.)
;; Configuring connection properties for the underlying JDBC driver.
(.setProperty "user" username)
(.setProperty "password" password)
(.setProperty "loginTimeout" "100")
;; Configuring connection properties for the Aurora JDBC Wrapper.
(.setProperty "wrapperPlugins" "failover,efm")
(.setProperty "wrapperLogUnclosedConnections" "true")))

(defn execute-query
[sql-query]
(with-open [conn (DriverManager/getConnection connection-string (db-properties
aurora-config/aurora-db-user
aurora-config/aurora-db-password))
stmt (.createStatement conn)
res (.executeQuery stmt sql-query)]
;; return query result
(.next res)))

(defn save-concept
"Saves a concept to Aurora Postgres"
[concept])

(defn get-concept
"Gets a concept from Aurora Postgres"
([provider concept-type concept-id]))

(defn get-concepts
"Gets a group of concepts from Aurora Postgres"
[provider concept-type concept-id-revision-id-tuples]
())

(defn get-concepts-small-table
"Gets a group of concepts from Aurora Postgres using provider-id, concept-id, revision-id tuples"
[concept-type provider-concept-revision-tuples]
())

(defn delete-concept
"Deletes a concept from Aurora Postgres"
[provider concept-type concept-id revision-id]
())

(defn delete-concepts
"Deletes multiple concepts from Aurora Postgres"
[provider concept-type concept-id-revision-id-tuples]
())

0 comments on commit e3ace18

Please sign in to comment.