-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
127 lines (108 loc) · 2.71 KB
/
client.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cloudfunctions
import (
"context"
"fmt"
"strings"
"github.com/kubemq-hub/builder/connector/common"
"github.com/kubemq-io/kubemq-targets/config"
"github.com/kubemq-io/kubemq-targets/pkg/logger"
gf "github.com/kubemq-io/kubemq-targets/targets/gcp/cloudfunctions/functions/apiv1"
"github.com/kubemq-io/kubemq-targets/types"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
functionspb "google.golang.org/genproto/googleapis/cloud/functions/v1"
)
type Client struct {
log *logger.Logger
opts options
client *gf.CloudFunctionsClient
parrantProject string
list []string
// nameFunctions map[string]string
}
func New() *Client {
return &Client{}
}
func (c *Client) Connector() *common.Connector {
return Connector()
}
func (c *Client) Init(ctx context.Context, cfg config.Spec, log *logger.Logger) error {
c.log = log
if c.log == nil {
c.log = logger.NewLogger(cfg.Kind)
}
c.log = logger.NewLogger(cfg.Name)
var err error
c.opts, err = parseOptions(cfg)
if err != nil {
return err
}
b := []byte(c.opts.credentials)
client, err := gf.NewCloudFunctionsClient(ctx, option.WithCredentialsJSON(b))
if err != nil {
return err
}
c.client = client
c.parrantProject = c.opts.parentProject
if c.opts.locationMatch {
it := client.ListFunctions(ctx, &functionspb.ListFunctionsRequest{
Parent: fmt.Sprintf("projects/%s/locations/-", c.opts.parentProject),
})
for {
resp, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return err
}
if resp != nil {
c.list = append(c.list, resp.GetName())
}
}
}
return nil
}
func (c *Client) Do(ctx context.Context, request *types.Request) (*types.Response, error) {
m, err := parseMetadata(request.Metadata, c.opts)
if err != nil {
return nil, err
}
if m.project == "" {
m.project = c.parrantProject
}
name := fmt.Sprintf("projects/%s/locations/%s/functions/%s", m.project, m.location, m.name)
if m.location == "" {
for _, n := range c.list {
if strings.Contains(n, m.name) && strings.Contains(n, m.project) {
m.location = "added from match"
name = n
break
}
}
}
if m.location == "" {
return nil, fmt.Errorf("no location found for function")
}
cfo := &functionspb.CallFunctionRequest{
Name: name,
Data: string(request.Data),
}
res, err := c.client.CallFunction(ctx, cfo)
if err != nil {
return nil, err
}
if res.Error != "" {
return nil, fmt.Errorf(res.Error)
}
return types.NewResponse().
SetMetadataKeyValue("result", res.Result).
SetMetadataKeyValue("execution_id", res.ExecutionId).
SetData([]byte(res.Result)), nil
}
func (c *Client) Stop() error {
if c.client != nil {
return c.client.Close()
}
return nil
}