From 4bec30a819884244cb06a9ed1f6ed94805e63ae2 Mon Sep 17 00:00:00 2001 From: Yaron Bar Date: Tue, 10 Dec 2024 15:08:51 +0200 Subject: [PATCH] WekaApi wip continued (4) call WekaApi from status --- cloud-functions/cloud_functions_test.go | 7 +---- cloud-functions/functions/status/status.go | 17 +++++------ cloud-functions/functions/weka_api/api.go | 21 ++++++++++++++ .../functions/weka_api/weka_api.go | 29 ++++++++----------- cloud-functions/main.go | 6 +--- 5 files changed, 42 insertions(+), 38 deletions(-) create mode 100644 cloud-functions/functions/weka_api/api.go diff --git a/cloud-functions/cloud_functions_test.go b/cloud-functions/cloud_functions_test.go index 725270b6..7a96c424 100644 --- a/cloud-functions/cloud_functions_test.go +++ b/cloud-functions/cloud_functions_test.go @@ -247,16 +247,11 @@ func Test_resize(t *testing.T) { func Test_status(t *testing.T) { // This will pass only before clusterization, after clusterization it will fail trying to fetch weka status - project := "wekaio-rnd" - zone := "europe-west1-b" bucket := "weka-poc-wekaio-rnd" stateName := "state" - instanceGroup := "weka-poc-instance-group" - usernameId := "projects/896245720241/secrets/weka-poc-username/versions/1" - passwordId := "projects/896245720241/secrets/weka-poc-password/versions/1" ctx := context.TODO() - clusterStatus, err := status.GetClusterStatus(ctx, project, zone, bucket, stateName, instanceGroup, usernameId, passwordId, "") + clusterStatus, err := status.GetClusterStatus(ctx, bucket, stateName) if err != nil { t.Logf("Failed getting status %s", err) } else { diff --git a/cloud-functions/functions/status/status.go b/cloud-functions/functions/status/status.go index cb5eeef3..382d058c 100644 --- a/cloud-functions/functions/status/status.go +++ b/cloud-functions/functions/status/status.go @@ -3,12 +3,12 @@ package status import ( "context" "encoding/json" - + "github.com/weka/gcp-tf/modules/deploy_weka/cloud-functions/functions/weka_api" cloudLibCommon "github.com/weka/go-cloud-lib/common" + "github.com/weka/go-cloud-lib/lib/weka" "github.com/weka/go-cloud-lib/logging" "github.com/weka/gcp-tf/modules/deploy_weka/cloud-functions/common" - "github.com/weka/go-cloud-lib/lib/weka" "github.com/weka/go-cloud-lib/protocol" ) @@ -63,7 +63,7 @@ func GetReports(ctx context.Context, project, zone, bucket, object, instanceGrou return } -func GetClusterStatus(ctx context.Context, project, zone, bucket, object, instanceGroup, usernameId, passwordId, adminPasswordId string) (clusterStatus protocol.ClusterStatus, err error) { +func GetClusterStatus(ctx context.Context, bucket, object string) (clusterStatus protocol.ClusterStatus, err error) { state, err := common.GetClusterState(ctx, bucket, object) if err != nil { return @@ -76,14 +76,11 @@ func GetClusterStatus(ctx context.Context, project, zone, bucket, object, instan return } - jpool, err := common.GetWekaJrpcPool(ctx, project, zone, instanceGroup, usernameId, passwordId, adminPasswordId) - if err != nil { - return + wekaApi := weka_api.WekaApiRequest{ + Method: weka.JrpcStatus, + Params: nil, } - - var rawWekaStatus json.RawMessage - - err = jpool.Call(weka.JrpcStatus, struct{}{}, &rawWekaStatus) + rawWekaStatus, err := weka_api.RunWekaApi(ctx, &wekaApi) if err != nil { return } diff --git a/cloud-functions/functions/weka_api/api.go b/cloud-functions/functions/weka_api/api.go new file mode 100644 index 00000000..e68b5c20 --- /dev/null +++ b/cloud-functions/functions/weka_api/api.go @@ -0,0 +1,21 @@ +package weka_api + +import ( + "github.com/weka/go-cloud-lib/lib/weka" +) + +var validMethods = []weka.JrpcMethod{weka.JrpcStatus} + +type WekaApiRequest struct { + Method weka.JrpcMethod `json:"method"` + Params map[string]string `json:"params"` +} + +func isSupportedMethod(method weka.JrpcMethod) bool { + for _, m := range validMethods { + if method == m { + return true + } + } + return false +} diff --git a/cloud-functions/functions/weka_api/weka_api.go b/cloud-functions/functions/weka_api/weka_api.go index 1aec5978..4ded7dd8 100644 --- a/cloud-functions/functions/weka_api/weka_api.go +++ b/cloud-functions/functions/weka_api/weka_api.go @@ -8,15 +8,9 @@ import ( "github.com/rs/zerolog/log" "github.com/weka/gcp-tf/modules/deploy_weka/cloud-functions/common" - "github.com/weka/go-cloud-lib/lib/weka" ) -type WekaApiRequest struct { - Method string `json:"method"` - Payload map[string]string `json:"payload"` -} - -func RunWekaApi(ctx context.Context, wr *WekaApiRequest) (interface{}, error) { +func RunWekaApi(ctx context.Context, wr *WekaApiRequest) (json.RawMessage, error) { log.Info().Msg("RunWekaApi> ") project := os.Getenv("PROJECT") zone := os.Getenv("ZONE") @@ -25,24 +19,25 @@ func RunWekaApi(ctx context.Context, wr *WekaApiRequest) (interface{}, error) { adminPasswordId := os.Getenv("ADMIN_PASSWORD_ID") deploymentPasswordId := os.Getenv("DEPLOYMENT_PASSWORD_ID") + if !isSupportedMethod(wr.Method) { + return nil, fmt.Errorf("weka api method %s is not supported", wr.Method) + } + jpool, err := common.GetWekaJrpcPool(ctx, project, zone, instanceGroup, usernameId, deploymentPasswordId, adminPasswordId) if err != nil { log.Error().Msgf("failed to get jrpc pool %w", err) return nil, fmt.Errorf("failed to get jrpc pool %w", err) } - log.Info().Msgf("RunWekaApi> ips list %v", jpool.Ips) - - var rawWekaStatus json.RawMessage - var jrpcMethod weka.JrpcMethod - switch wr.Method { - case "status": - jrpcMethod = weka.JrpcStatus - default: - return nil, fmt.Errorf("weka api method %s is not supported", wr.Method) + var params interface{} + if wr.Params != nil { + params = wr.Params + } else { + params = struct{}{} } + var rawWekaStatus json.RawMessage - err = jpool.Call(jrpcMethod, struct{}{}, &rawWekaStatus) + err = jpool.Call(wr.Method, params, &rawWekaStatus) if err != nil { log.Error().Msgf("failed to call jrpc %w", err) return nil, fmt.Errorf("failed to call jrpc %w", err) diff --git a/cloud-functions/main.go b/cloud-functions/main.go index c771645a..d987c032 100644 --- a/cloud-functions/main.go +++ b/cloud-functions/main.go @@ -740,9 +740,6 @@ func Status(w http.ResponseWriter, r *http.Request) { bucket := os.Getenv("BUCKET") stateObject := os.Getenv("STATE_OBJ_NAME") instanceGroup := os.Getenv("INSTANCE_GROUP") - usernameId := os.Getenv("USER_NAME_ID") - adminPasswordId := os.Getenv("ADMIN_PASSWORD_ID") - deploymentPasswordId := os.Getenv("DEPLOYMENT_PASSWORD_ID") nfsStateObject := os.Getenv("NFS_STATE_OBJ_NAME") nfsInstanceGroup := os.Getenv("NFS_INSTANCE_GROUP") @@ -760,7 +757,7 @@ func Status(w http.ResponseWriter, r *http.Request) { var clusterStatus interface{} var err error if requestBody.Type == "" || requestBody.Type == "status" { - clusterStatus, err = status.GetClusterStatus(ctx, project, zone, bucket, stateObject, instanceGroup, usernameId, deploymentPasswordId, adminPasswordId) + clusterStatus, err = status.GetClusterStatus(ctx, bucket, stateObject) } else if requestBody.Type == "progress" && requestBody.Protocol == "" { clusterStatus, err = status.GetReports(ctx, project, zone, bucket, stateObject, instanceGroup) } else if requestBody.Type == "progress" && requestBody.Protocol == "nfs" { @@ -787,7 +784,6 @@ func Status(w http.ResponseWriter, r *http.Request) { } func WekaApi(w http.ResponseWriter, r *http.Request) { - var wekaRequest weka_api.WekaApiRequest if err := json.NewDecoder(r.Body).Decode(&wekaRequest); err != nil { failedDecodingReqBody(w, err)