-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
241 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package management | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/weka/go-cloud-lib/connectors" | ||
"github.com/weka/go-cloud-lib/lib/jrpc" | ||
"github.com/weka/go-cloud-lib/lib/weka" | ||
"github.com/weka/go-cloud-lib/logging" | ||
) | ||
|
||
type WekaApiRequest struct { | ||
Method weka.JrpcMethod `json:"method"` | ||
Params map[string]string `json:"params"` | ||
} | ||
|
||
type ManagementRequest struct { | ||
WekaApiRequest | ||
|
||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
BackendPrivateIps []string `json:"backend_private_ips"` | ||
} | ||
|
||
func CallJRPC(ctx context.Context, request ManagementRequest) (json.RawMessage, error) { | ||
logger := logging.LoggerFromCtx(ctx) | ||
logger.Debug().Msg("CallJRPC > Start") | ||
logger.Info().Msgf("CallJRPC > method %s", request.Method) | ||
|
||
var jrpcResponse json.RawMessage | ||
|
||
logger.Debug().Msgf("CallJRPC > Username: %s", request.Username) | ||
jrpcBuilder := func(ip string) *jrpc.BaseClient { | ||
return connectors.NewJrpcClient(ctx, ip, weka.ManagementJrpcPort, request.Username, request.Password) | ||
} | ||
|
||
ips := request.BackendPrivateIps | ||
if len(ips) == 0 { | ||
return nil, fmt.Errorf("CallJRPC - backend private ips are empty") | ||
} | ||
logger.Debug().Msgf("CallJRPC > BackendPrivateIps: %v", ips) | ||
|
||
var params interface{} | ||
if request.Params != nil { | ||
params = request.Params | ||
} else { | ||
params = struct{}{} | ||
} | ||
|
||
jpool := &jrpc.Pool{ | ||
Ips: ips, | ||
Clients: map[string]*jrpc.BaseClient{}, | ||
Active: "", | ||
Builder: jrpcBuilder, | ||
Ctx: ctx, | ||
} | ||
|
||
if err := jpool.Call(request.Method, params, &jrpcResponse); err != nil { | ||
return nil, fmt.Errorf("CallJRPC - call [%s] failed > %w", request.Method, err) | ||
} | ||
return jrpcResponse, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package weka_api | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/rs/zerolog/log" | ||
"github.com/weka/aws-tf/modules/deploy_weka/lambdas/common" | ||
"github.com/weka/aws-tf/modules/deploy_weka/lambdas/functions/management" | ||
"github.com/weka/go-cloud-lib/logging" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
func MakeWekaApiRequest[T any](ctx context.Context, wr *management.WekaApiRequest) (response *T, err error) { | ||
logger := logging.LoggerFromCtx(ctx) | ||
logger.Debug().Msg("MakeWekaApiRequest > Start") | ||
|
||
r, err := invokeManagementLambda[T](ctx, wr) | ||
if err != nil { | ||
return nil, fmt.Errorf("MakeWekaApiRequest > lambda invocation failed: %v", err) | ||
} | ||
return r, nil | ||
} | ||
|
||
func invokeManagementLambda[T any](ctx context.Context, wr *management.WekaApiRequest) (response *T, err error) { | ||
logger := logging.LoggerFromCtx(ctx) | ||
logger.Debug().Msg("invokeManagementLambda > Start") | ||
logger.Info().Msgf("invokeManagementLambda > Params: %s", wr.Params) | ||
|
||
managementLambdaName := os.Getenv("MANAGEMENT_LAMBDA") | ||
if managementLambdaName == "" { | ||
return nil, fmt.Errorf("MANAGEMENT_LAMBDA is not set") | ||
} | ||
if wr.Method == "" { | ||
return nil, fmt.Errorf("weka-api method is not set") | ||
} | ||
|
||
useSecretManagerEndpoint, err := strconv.ParseBool(os.Getenv("USE_SECRETMANAGER_ENDPOINT")) | ||
if err != nil { | ||
log.Warn().Msg("Failed to parse USE_SECRETMANAGER_ENDPOINT, assuming false") | ||
} | ||
var username, password string | ||
if !useSecretManagerEndpoint { | ||
log.Info().Msg("Secret manager endpoint not in use, sending credentials in body") | ||
usernameId := os.Getenv("USERNAME_ID") | ||
deploymentPasswordId := os.Getenv("DEPLOYMENT_PASSWORD_ID") | ||
adminPasswordId := os.Getenv("ADMIN_PASSWORD_ID") | ||
creds, err := common.GetDeploymentOrAdminUsernameAndPassword(usernameId, deploymentPasswordId, adminPasswordId) | ||
if err != nil { | ||
return nil, fmt.Errorf("invokeManagementLambda > GetDeploymentOrAdminUsernameAndPassword: %w", err) | ||
} | ||
|
||
username = creds.Username | ||
password = creds.Password | ||
} | ||
|
||
clusterName := os.Getenv("CLUSTER_NAME") | ||
if clusterName == "" { | ||
return nil, fmt.Errorf("CLUSTER_NAME is not set") | ||
} | ||
ips, err := common.GetBackendsPrivateIps(clusterName, "backend") | ||
if err != nil { | ||
return nil, fmt.Errorf("invokeManagementLambda > GetBackendsPrivateIps: %w", err) | ||
} | ||
log.Info().Msgf("invokeManagementLambda > Backend private IPs: %v", ips) | ||
|
||
logger.Debug().Msgf("invokeManagementLambda > Username: %s", username) | ||
|
||
managementRequest := management.ManagementRequest{ | ||
WekaApiRequest: management.WekaApiRequest{ | ||
Method: wr.Method, | ||
Params: wr.Params, | ||
}, | ||
BackendPrivateIps: ips, | ||
Username: username, | ||
Password: password, // empty string is interpreted as no credentials | ||
} | ||
|
||
response, err = common.InvokeLambdaFunction[T](managementLambdaName, managementRequest) | ||
if err != nil { | ||
wrappedError := fmt.Errorf("invokeManagementLambda >: %w", err) | ||
log.Error().Err(wrappedError).Send() | ||
} | ||
|
||
return response, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.