-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.go
147 lines (129 loc) · 3.98 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package elastic
import (
"context"
"fmt"
"github.com/kubemq-hub/builder/connector/common"
"github.com/kubemq-io/kubemq-targets/config"
"github.com/kubemq-io/kubemq-targets/pkg/logger"
"github.com/kubemq-io/kubemq-targets/types"
"github.com/olivere/elastic/v7"
)
type Client struct {
log *logger.Logger
elastic *elastic.Client
opts options
}
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)
}
var err error
c.opts, err = parseOptions(cfg)
if err != nil {
return err
}
var elasticOpts []elastic.ClientOptionFunc
elasticOpts = append(elasticOpts,
elastic.SetURL(c.opts.urls...),
elastic.SetSniff(c.opts.sniff),
elastic.SetBasicAuth(c.opts.username, c.opts.password))
c.elastic, err = elastic.NewClient(elasticOpts...)
if err != nil {
return err
}
_, _, err = c.elastic.Ping(c.opts.urls[0]).Do(ctx)
if err != nil {
return err
}
return nil
}
func (c *Client) Do(ctx context.Context, req *types.Request) (*types.Response, error) {
meta, err := parseMetadata(req.Metadata)
if err != nil {
return nil, err
}
switch meta.method {
case "get":
return c.Get(ctx, meta)
case "set":
return c.Set(ctx, meta, req.Data)
case "delete":
return c.Delete(ctx, meta)
case "index.exists":
return c.IndexExists(ctx, meta)
case "index.create":
return c.IndexCreate(ctx, meta, req.Data)
case "index.delete":
return c.IndexDelete(ctx, meta)
default:
return nil, fmt.Errorf("invalid method")
}
}
func (c *Client) Get(ctx context.Context, meta metadata) (*types.Response, error) {
getResp, err := c.elastic.Get().Index(meta.index).Id(meta.id).Do(ctx)
if err != nil {
return nil, err
}
return types.NewResponse().
SetData(getResp.Source).
SetMetadataKeyValue("id", meta.id), nil
}
func (c *Client) Set(ctx context.Context, meta metadata, value []byte) (*types.Response, error) {
setResp, err := c.elastic.Index().Index(meta.index).Id(meta.id).BodyString(string(value)).Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to set document id %s: %s", meta.id, err)
}
return types.NewResponse().
SetMetadataKeyValue("id", setResp.Id).
SetMetadataKeyValue("result", setResp.Result),
nil
}
func (c *Client) Delete(ctx context.Context, meta metadata) (*types.Response, error) {
delResp, err := c.elastic.Delete().Index(meta.index).Id(meta.id).Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to delete id '%s',%w", meta.id, err)
}
return types.NewResponse().
SetMetadataKeyValue("id", delResp.Id).
SetMetadataKeyValue("result", delResp.Result),
nil
}
func (c *Client) IndexExists(ctx context.Context, meta metadata) (*types.Response, error) {
exists, err := c.elastic.IndexExists(meta.index).Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to execute index exist '%s',%w", meta.index, err)
}
return types.NewResponse().
SetMetadataKeyValue("exists", fmt.Sprintf("%t", exists)),
nil
}
func (c *Client) IndexCreate(ctx context.Context, meta metadata, value []byte) (*types.Response, error) {
result, err := c.elastic.CreateIndex(meta.index).BodyString(string(value)).Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create index'%s',%w", meta.index, err)
}
return types.NewResponse().
SetMetadataKeyValue("acknowledged", fmt.Sprintf("%t", result.Acknowledged)).
SetMetadataKeyValue("shards_acknowledged", fmt.Sprintf("%t", result.ShardsAcknowledged)).
SetMetadataKeyValue("index", result.Index),
nil
}
func (c *Client) IndexDelete(ctx context.Context, meta metadata) (*types.Response, error) {
result, err := c.elastic.DeleteIndex(meta.index).Do(ctx)
if err != nil {
return nil, fmt.Errorf("failed to delete index'%s',%w", meta.index, err)
}
return types.NewResponse().
SetMetadataKeyValue("acknowledged", fmt.Sprintf("%t", result.Acknowledged)),
nil
}
func (c *Client) Stop() error {
return nil
}