/
CommandableLambdaClient.go
101 lines (96 loc) · 3.03 KB
/
CommandableLambdaClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package clients
import (
"context"
"github.com/aws/aws-sdk-go/service/lambda"
)
// Abstract client that calls commandable AWS Lambda Functions.
//
// Commandable services are generated automatically for ICommandable objects.
// Each command is exposed as action determined by "cmd" parameter.
//
// # Configuration parameters
//
// - connections:
// - discovery_key: (optional) a key to retrieve the connection from IDiscovery
// - region: (optional) AWS region
//
// - credentials:
// - store_key: (optional) a key to retrieve the credentials from ICredentialStore
// - access_id: AWS access/client id
// - access_key: AWS access/client id
//
// - options:
// - connect_timeout: (optional) connection timeout in milliseconds (default: 10 sec)
//
// # References
//
// - \*:logger:\*:\*:1.0 (optional) ILogger components to pass log messages
// - \*:counters:\*:\*:1.0 (optional) ICounters components to pass collected measurements
// - \*:discovery:\*:\*:1.0 (optional) IDiscovery services to resolve connection
// - \*:credential-store:\*:\*:1.0 (optional) Credential stores to resolve credentials
//
// # See LambdaFunction
//
// Example:
//
// type MyLambdaClient struct {
// *CommandableLambdaClient
// }
//
// ...
//
// func (c* MyLambdaClient) GetData(ctx context.Context, id string)(result DataPage[MyData], err error) {
//
// valVal, err := c.callCommand(ctx,
// "get_data",
// map[string]any{ "id": id })
//
// if calErr != nil {
// return nil, calErr
// }
//
// defer timing.EndTiming(ctx, err)
//
// return awsclient.HandleLambdaResponse[cdata.DataPage[MyData]](calValue)
// }
//
// ...
//
// client := NewMyLambdaClient();
// client.Configure(context.Background(), NewConfigParamsFromTuples(
//
// "connection.region", "us-east-1",
// "connection.access_id", "XXXXXXXXXXX",
// "connection.access_key", "XXXXXXXXXXX",
// "connection.arn", "YYYYYYYYYYYYY"
//
// ));
//
// res, err := client.GetData(context.Background(), "123", "1")
// ...
type CommandableLambdaClient struct {
*LambdaClient
name string
}
// Creates a new instance of this client.
// - name a service name.
func NewCommandableLambdaClient(name string) *CommandableLambdaClient {
c := &CommandableLambdaClient{
LambdaClient: NewLambdaClient(),
}
c.name = name
return c
}
// Calls a remote action in AWS Lambda function.
// The name of the action is added as "cmd" parameter
// to the action parameters.
// - ctx context.Context execution context to trace execution through call chain.
// - cmd an action name
// - params command parameters.
// - Return *lambda.InvokeOutput result or error.
func (c *CommandableLambdaClient) CallCommand(ctx context.Context, cmd string, params map[string]any) (result *lambda.InvokeOutput, err error) {
timing := c.Instrument(ctx, c.name+"."+cmd)
callRes, callErr := c.Call(ctx, cmd, params)
timing.EndTiming(ctx, callErr)
return callRes, callErr
}