From 71dbf946d9329b59005fbfe4f7fffd4b0807bf84 Mon Sep 17 00:00:00 2001 From: cgoodwin90 Date: Mon, 3 Jun 2024 18:04:11 +1000 Subject: [PATCH] Refactored to utilize the mariadbHandler --- TESTING_dockercompose.md | 8 +++++ docker-compose.yml | 8 ++--- main.go | 2 +- mysql.go | 75 ---------------------------------------- 4 files changed, 13 insertions(+), 80 deletions(-) delete mode 100644 mysql.go diff --git a/TESTING_dockercompose.md b/TESTING_dockercompose.md index 7f7eb95..4090c3b 100644 --- a/TESTING_dockercompose.md +++ b/TESTING_dockercompose.md @@ -45,6 +45,14 @@ docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?servic docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10-11" | grep "SERVICE_HOST=10.11" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mariadb?service=mariadb-10-11" | grep "LAGOON_TEST_VAR=internal-services-test" +# mysql-8-0 should be able to read/write data +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "SERVICE_HOST=8.0.34" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-0" | grep "LAGOON_TEST_VAR=internal-services-test" + +# mysql-8-4 should be able to read/write data +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "SERVICE_HOST=8.4.0" +docker compose exec -T commons sh -c "curl -kL http://go-web:3000/mysql?service=mysql-8-4" | grep "LAGOON_TEST_VAR=internal-services-test" + # postgres-12 should be able to read/write data docker compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-12" | grep "SERVICE_HOST=PostgreSQL 12" docker compose exec -T commons sh -c "curl -kL http://go-web:3000/postgres?service=postgres-12" | grep "LAGOON_TEST_VAR=internal-services-test" diff --git a/docker-compose.yml b/docker-compose.yml index 69a0d12..db3c859 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,15 +57,15 @@ services: ports: - '5432' - mysql-5-7: - image: testlagoon/mysql-5.7:experimental + mysql-8-0: + image: testlagoon/mysql-8.0:experimental labels: lagoon.type: mariadb ports: - "3306" - mysql-8-0: - image: testlagoon/mysql-8.0:experimental + mysql-8-4: + image: testlagoon/mysql-8.4:pr-308 # Placeholder labels: lagoon.type: mariadb ports: diff --git a/main.go b/main.go index 8a50cb7..67ea4d1 100644 --- a/main.go +++ b/main.go @@ -21,7 +21,7 @@ func main() { r.HandleFunc("/mongo", mongoHandler) r.HandleFunc("/opensearch", opensearchHandler) r.HandleFunc("/storage", persistentStorageHandler) - r.HandleFunc("/mysql", mysqlHandler) + r.HandleFunc("/mysql", mariadbHandler) r.HandleFunc("/", handleReq) http.Handle("/", r) diff --git a/mysql.go b/mysql.go deleted file mode 100644 index 014c497..0000000 --- a/mysql.go +++ /dev/null @@ -1,75 +0,0 @@ -package main - -import ( - "database/sql" - "fmt" - _ "github.com/go-sql-driver/mysql" - machineryEnvVars "github.com/uselagoon/machinery/utils/variables" - "log" - "net/http" - "os" - "strings" -) - -var ( - mysqlVersion string - mysqlConnectionStr string -) - -func mysqlHandler(w http.ResponseWriter, r *http.Request) { - service := r.URL.Query().Get("service") - localService, lagoonService := cleanRoute(service) - mysqlUser := machineryEnvVars.GetEnv(fmt.Sprintf("%s_USERNAME", lagoonService), "lagoon") - mysqlPassword := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PASSWORD", lagoonService), "lagoon") - mysqlHost := machineryEnvVars.GetEnv(fmt.Sprintf("%s_HOST", lagoonService), localService) - mysqlPort := machineryEnvVars.GetEnv(fmt.Sprintf("%s_PORT", lagoonService), "3306") - mysqlDatabase := machineryEnvVars.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 -}