Skip to content

Commit

Permalink
WekaApi wip continued (4)
Browse files Browse the repository at this point in the history
call WekaApi from status
  • Loading branch information
rugggger committed Dec 17, 2024
1 parent 866fb0f commit 4bec30a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 38 deletions.
7 changes: 1 addition & 6 deletions cloud-functions/cloud_functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 7 additions & 10 deletions cloud-functions/functions/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions cloud-functions/functions/weka_api/api.go
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 12 additions & 17 deletions cloud-functions/functions/weka_api/weka_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions cloud-functions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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" {
Expand All @@ -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)
Expand Down

0 comments on commit 4bec30a

Please sign in to comment.