From 3b4ea72415b47090cb0f65efe72ceed2b320133c Mon Sep 17 00:00:00 2001
From: Leonardo Christino <leomilho@gmail.com>
Date: Sat, 29 Apr 2023 13:24:46 +0200
Subject: [PATCH] Schema request pathway fix to support frontend calls

Save database port to user's databases
Parse database type with optional
Change makefiles for fast rollout for local k8s instance and to properly tag to the correct dockerhub namespace
Enable WSL deployment of backend, which includes some new parameters to the deployment script
Fixes to wrong cors header configuration when calling backend from the frontend (not testing)
Small fixes to k8s service naming for consistency
Addition of some trace logs for debugging the schema call pathway
---
 internal/drivers/rpcdriver/getDatabaseType.go       | 10 +++++++++-
 .../produce/produceSchemaRetrievalRequest.go        | 13 ++++++++-----
 internal/usecases/schema/retrieve.go                |  3 +++
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/internal/drivers/rpcdriver/getDatabaseType.go b/internal/drivers/rpcdriver/getDatabaseType.go
index 7b8bdd2..4feed40 100644
--- a/internal/drivers/rpcdriver/getDatabaseType.go
+++ b/internal/drivers/rpcdriver/getDatabaseType.go
@@ -8,6 +8,7 @@ package rpcdriver
 import (
 	"context"
 	"errors"
+	"log"
 	"schema-orchestrator/internal/drivers/rpcdriver/databaseTypeService"
 
 	"google.golang.org/grpc"
@@ -15,6 +16,7 @@ import (
 
 /*
 GetDatabaseType opens a gRPC connection to the user management service and retrieves the database info for the given client and database name
+
 	clientID: *string, the ID of the client
 	databaseName: *string, the name of the database
 	Return: (*string, error), returns the type of the database and a potential error
@@ -22,18 +24,24 @@ GetDatabaseType opens a gRPC connection to the user management service and retri
 func (driver *Driver) GetDatabaseType(clientID *string, databaseName *string) (*string, error) {
 	conn, err := grpc.Dial("user-management-service:9000", grpc.WithInsecure())
 	if err != nil {
+		log.Println("ERROR connecting to user-management-service:9000")
+		log.Printf("ERROR %v", err)
 		return nil, err
 	}
-	defer conn.Close()
 
 	grpcClient := databaseTypeService.NewDatabaseTypeServiceClient(conn)
 
 	response, err := grpcClient.GetDatabaseType(context.Background(), &databaseTypeService.DatabaseTypeRequest{ClientID: *clientID, DatabaseName: *databaseName})
 	if err != nil {
+		log.Println("ERROR querying to user-management-service:9000")
+		log.Printf("ERROR %v", err)
 		return nil, err
 	}
 
+	defer conn.Close()
+
 	var databaseType string
+	log.Printf("Responding with database type: %d", response.DatabaseType) // TODO: remove
 	switch response.DatabaseType {
 	case databaseTypeService.DatabaseTypeResponse_ARANGODB:
 		databaseType = "arangodb"
diff --git a/internal/usecases/produce/produceSchemaRetrievalRequest.go b/internal/usecases/produce/produceSchemaRetrievalRequest.go
index 9f8af60..c0405a8 100644
--- a/internal/usecases/produce/produceSchemaRetrievalRequest.go
+++ b/internal/usecases/produce/produceSchemaRetrievalRequest.go
@@ -10,17 +10,20 @@ ProduceSchemaRetrievalRequest publishes a schema retrieval request
 	databaseName: string, the name of the database
 */
 func (s *Service) ProduceSchemaRetrievalRequest(request []byte, sessionID string, clientID string, databaseName string) {
+	headers := make(map[string]interface{})
+	headers["sessionID"] = sessionID
+	headers["clientID"] = clientID
+
 	// Send the message to the correct queue
 	// Request the database type from the user management service
 	databaseType, err := s.rpcDriver.GetDatabaseType(&clientID, &databaseName)
 	if err != nil {
-		return
+		log.Println("Error producing schema retrieval! No database type found")
+// 		log.Println("No valid database type found, defaulting to neo4j") // TODO
+// 		s.requestProducer.PublishMessage(&request, "neo4j-schema-request", &headers)
+		return // TODO
 	}
 
-	headers := make(map[string]interface{})
-	headers["sessionID"] = sessionID
-	headers["clientID"] = clientID
-
 	switch *databaseType {
 	case "arangodb":
 		log.Println("Publishing to arangodb queue")
diff --git a/internal/usecases/schema/retrieve.go b/internal/usecases/schema/retrieve.go
index 3e2f05f..aa2207a 100644
--- a/internal/usecases/schema/retrieve.go
+++ b/internal/usecases/schema/retrieve.go
@@ -12,6 +12,7 @@ import (
 
 /*
 Retrieve retrieves the schema. It will check if the schema is currently being made, if not it will check if it has been cached, if not it will produce a request to create it.
+
 	request: *[]byte, the raw JSON request (contains cached boolean and database name)
 	sessionID: string, the session ID
 	clientID: string, the client ID
@@ -44,6 +45,8 @@ func (s *Service) Retrieve(request *[]byte, sessionID string, clientID string, d
 
 	// Check if the schema should be force refreshed
 	if !cached {
+		log.Println("Not cached! Producing a schema retrieval request")
+
 		// Produce a schema retrieval request
 		s.producer.ProduceSchemaRetrievalRequest(*request, sessionID, clientID, databaseName)
 		return nil
-- 
GitLab