From f276a553b0f8e827455b9b23653ebbb4736cff0b Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 12 Jan 2023 11:09:55 +1100 Subject: [PATCH] Included mysql in test suite - placeholder images to be updated --- docker-compose.yml | 14 +++++++++ main.go | 13 ++++---- mysql.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 mysql.go diff --git a/docker-compose.yml b/docker-compose.yml index c892b2a..8e169e7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -48,6 +48,20 @@ services: ports: - '27017' + mysql-5-7: + image: chrisgio/testimages:mysql-5.7 # Placeholder image - to be updated + labels: + lagoon.type: mariadb + ports: + - "3306" + + mysql-8-0: + image: chrisgio/testimages:mysql-8.0 # Placeholder image - to be updated + labels: + lagoon.type: mariadb + ports: + - "3306" + opensearch-2: image: uselagoon/opensearch-2:latest environment: diff --git a/main.go b/main.go index b77aa78..64553a0 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ func main() { r.HandleFunc("/{solr:solr-.*}", solrHandler) r.HandleFunc("/{mongo:mongo-.*}", mongoHandler) r.HandleFunc("/{opensearch:opensearch-.*}", opensearchHandler) + r.HandleFunc("/{mysql:mysql-.*}", mysqlHandler) r.HandleFunc("/", handleReq) http.Handle("/", r) log.Fatal(http.ListenAndServe(":3000", nil)) @@ -60,7 +61,7 @@ func connectorKeyValues(values []string) string { func cleanRoute(basePath string) (string, string) { cleanRoute := strings.ReplaceAll(basePath, "/", "") - localService := strings.ReplaceAll(cleanRoute, "10.", "10-") + localService := strings.ReplaceAll(cleanRoute, ".", "-") replaceHyphen := strings.ReplaceAll(localService, "-", "_") lagoonService := strings.ToUpper(replaceHyphen) return localService, lagoonService @@ -68,9 +69,9 @@ func cleanRoute(basePath string) (string, string) { // 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 + value := os.Getenv(key) + if len(value) == 0 { + return defaultValue + } + return value } diff --git a/mysql.go b/mysql.go new file mode 100644 index 0000000..693cd02 --- /dev/null +++ b/mysql.go @@ -0,0 +1,75 @@ +package main + +import ( + "database/sql" + "fmt" + "log" + "net/http" + "os" + "strings" + + _ "github.com/go-sql-driver/mysql" +) + +var ( + mysqlVersion string + mysqlConnectionStr string +) + +func mysqlHandler(w http.ResponseWriter, r *http.Request) { + mysqlPath := r.URL.Path + localService, lagoonService := cleanRoute(mysqlPath) + mysqlUser := getEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") + mysqlPassword := getEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") + mysqlHost := getEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) + mysqlPort := getEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306") + mysqlDatabase := getEnv(fmt.Sprintf("%s_DATABASE", lagoonService), "lagoon") + + mysqlConnectionStr = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPassword, mysqlHost, mysqlPort, mysqlDatabase) + log.Print(fmt.Sprintf("Using %s as the connstring", mysqlConnectionStr)) + + fmt.Fprintf(w, dbConnectorPairs(mysqlConnector(mysqlConnectionStr), mysqlVersion)) +} + +func mysqlConnector(connectionString string) map[string]string { + db, err := sql.Open("mysql", connectionString) + if err != nil { + log.Print(err) + } + + defer db.Close() + + createTable := "CREATE TABLE IF NOT EXISTS env(env_key text, env_value text)" + _, err = db.Exec(createTable) + if err != nil { + log.Print(err) + } + + query := "INSERT INTO env(env_key, env_value) VALUES (?, ?)" + + for _, e := range os.Environ() { + pair := strings.SplitN(e, "=", 2) + _, err := db.Exec(query, pair[0], pair[1]) + if err != nil { + log.Print(err) + } + } + + q := "LAGOON_%" + rows, err := db.Query(`SELECT * FROM env where env_key LIKE ?`, q) + if err != nil { + log.Print(err) + } + + db.QueryRow("SELECT VERSION()").Scan(&mysqlVersion) + + defer rows.Close() + results := make(map[string]string) + for rows.Next() { + var envKey, envValue string + _ = rows.Scan(&envKey, &envValue) + results[envKey] = envValue + } + + return results +}