From c7c72dd469e747a7c4ef8402b9b6c458a5ca14fe Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 13 Jun 2023 12:26:28 +1000 Subject: [PATCH] Importing machinery --- main.go | 17 +---------------- mariadb.go | 11 ++++++----- mongo.go | 11 ++++++----- opensearch.go | 3 ++- persistentstorage.go | 6 +++--- postgres.go | 11 ++++++----- 6 files changed, 24 insertions(+), 35 deletions(-) diff --git a/main.go b/main.go index 2aba581..d1ecd64 100644 --- a/main.go +++ b/main.go @@ -3,16 +3,10 @@ package main import ( "bytes" "fmt" + "github.com/gorilla/mux" "log" "net/http" - "os" "strings" - - "github.com/gorilla/mux" -) - -var ( - localCheck = os.Getenv("LAGOON_ENVIRONMENT") ) type funcType func() map[string]string @@ -66,12 +60,3 @@ func cleanRoute(basePath string) (string, string) { lagoonService := strings.ToUpper(replaceHyphen) return localService, lagoonService } - -// getEnv get key environment variable if exist otherwise return defalutValue -func getEnv(key, defaultValue string) string { - value := os.Getenv(key) - if len(value) == 0 { - return defaultValue - } - return value -} diff --git a/mariadb.go b/mariadb.go index 29c531d..47989e9 100644 --- a/mariadb.go +++ b/mariadb.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "fmt" + machineryEnvVars "github.com/uselagoon/machinery/utils/variables" "log" "net/http" "os" @@ -19,11 +20,11 @@ var ( func mariadbHandler(w http.ResponseWriter, r *http.Request) { mariadbPath := r.URL.Path localService, lagoonService := cleanRoute(mariadbPath) - mariadbUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") - mariadbPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") - mariadbHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) - mariadbPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306") - mariadbDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") + mariadbUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") + mariadbPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") + mariadbHost := machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) + mariadbPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306") + mariadbDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") mariadbConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mariadbUser, mariadbPassword, mariadbHost, mariadbPort, mariadbDatabase) log.Print(fmt.Sprintf("Using %s as the connstring", mariadbConnectionStr)) diff --git a/mongo.go b/mongo.go index 099cb66..70fccee 100644 --- a/mongo.go +++ b/mongo.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + machineryEnvVars "github.com/uselagoon/machinery/utils/variables" "log" "net/http" "os" @@ -25,11 +26,11 @@ var ( func mongoHandler(w http.ResponseWriter, r *http.Request) { mongoPath := r.URL.Path localService, lagoonService := cleanRoute(mongoPath) - mongoUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") - mongoPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") - mongoHost = getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) - mongoPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "27017") - mongoDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") + mongoUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") + mongoPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") + mongoHost = machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) + mongoPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "27017") + mongoDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") if mongoHost != localService { mongoConnectionStr = fmt.Sprintf("mongodb://%s:%s@%s:%s/%s", mongoUser, mongoPassword, mongoHost, mongoPort, mongoDatabase) diff --git a/opensearch.go b/opensearch.go index 8aae8d3..35cb47b 100644 --- a/opensearch.go +++ b/opensearch.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "encoding/json" "fmt" + machineryEnvVars "github.com/uselagoon/machinery/utils/variables" "log" "net/http" "os" @@ -17,7 +18,7 @@ import ( ) var ( - opensearchHost = os.Getenv("OPENSEARCH_HOST") + opensearchHost = machineryEnvVars.GetEnv("OPENSEARCH_HOST", "opensearch-2") opensearchConnectionStr = fmt.Sprintf("http://%s:9200", opensearchHost) ) diff --git a/persistentstorage.go b/persistentstorage.go index 86c4184..ceb4a8e 100644 --- a/persistentstorage.go +++ b/persistentstorage.go @@ -2,12 +2,12 @@ package main import ( "fmt" + machineryEnvVars "github.com/uselagoon/machinery/utils/variables" "log" "net/http" + "os" "strconv" "strings" - - "os" ) func persistentStorageHandler(w http.ResponseWriter, r *http.Request) { @@ -17,7 +17,7 @@ func persistentStorageHandler(w http.ResponseWriter, r *http.Request) { } func persistentStorageConnector(route string) string { - if route != os.Getenv("STORAGE_LOCATION") { + if route != machineryEnvVars.GetEnv("STORAGE_LOCATION", "") { return "Storage location is not defined - ensure format matches '/storage?path=[path]'" } path := route + "/storage.txt" diff --git a/postgres.go b/postgres.go index ba24ab0..45ff44b 100644 --- a/postgres.go +++ b/postgres.go @@ -3,6 +3,7 @@ package main import ( "database/sql" "fmt" + machineryEnvVars "github.com/uselagoon/machinery/utils/variables" "log" "net/http" "os" @@ -20,11 +21,11 @@ var ( func postgresHandler(w http.ResponseWriter, r *http.Request) { postgresPath := r.URL.Path localService, lagoonService := cleanRoute(postgresPath) - postgresUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") - postgresPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") - postgresHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) - postgresPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "5432") - postgresDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") + postgresUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") + postgresPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") + postgresHost := machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) + postgresPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "5432") + postgresDatabase := machineryEnvVars.GetEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") postgresConnectionStr = fmt.Sprintf("user=%s password=%s dbname=%s sslmode=%s host=%s port=%s", postgresUser, postgresPassword, postgresDatabase, postgresSSL, postgresHost, postgresPort) log.Print(fmt.Sprintf("Using %s as the connstring", postgresConnectionStr))