-
Notifications
You must be signed in to change notification settings - Fork 314
/
client.go
executable file
·237 lines (195 loc) · 4.91 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2017 The OpenSDS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package etcd
import (
"context"
"strings"
"sync"
"time"
"github.com/coreos/etcd/pkg/transport"
"github.com/sodafoundation/api/pkg/utils/config"
"github.com/sodafoundation/api/pkg/utils/pwd"
"github.com/coreos/etcd/clientv3"
log "github.com/golang/glog"
"github.com/sodafoundation/api/pkg/utils"
)
var (
timeOut = 3 * time.Second
retryNum = 3
pwdEncrypter = "aes"
)
// Request
type Request struct {
Url string `json:"url"`
Content string `json:"content"`
NewContent string `json:"newContent"`
}
// Response
type Response struct {
Status string `json:"status"`
Message []string `json:"message"`
Error string `json:"error"`
}
type clientInterface interface {
Create(req *Request) *Response
Get(req *Request) *Response
List(req *Request) *Response
Update(req *Request) *Response
Delete(req *Request) *Response
}
// Init
func Init(etcd *config.Database) *client {
var cliv3 *clientv3.Client
clientV3Config := clientv3.Config{
Endpoints: strings.Split(etcd.Endpoint, ","),
DialTimeout: timeOut,
}
if etcd.EnableTLS {
tlsInfo := transport.TLSInfo{
CertFile: etcd.CertFile,
KeyFile: etcd.KeyFile,
TrustedCAFile: etcd.TrustedCAFile,
ClientCertAuth: etcd.AllowClientAuth,
}
tlsConfig, err := tlsInfo.ClientConfig()
if err != nil {
log.Fatal(err)
}
clientV3Config.TLS = tlsConfig
}
if etcd.Username != "" && etcd.Password != "" {
clientV3Config.Username = etcd.Username
pwdTool := pwd.NewPwdEncrypter(pwdEncrypter)
password, err := pwdTool.Decrypter(etcd.Password)
if err != nil {
panic(err)
}
clientV3Config.Password = password
}
err := utils.Retry(retryNum, "Get etcd client", false, func(retryIdx int, lastErr error) error {
var err error
cliv3, err = clientv3.New(clientV3Config)
return err
})
if err != nil {
panic(err)
}
return &client{cli: cliv3}
}
type client struct {
cli *clientv3.Client
lock sync.Mutex
}
func (c *client) Create(req *Request) *Response {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
c.lock.Lock()
defer c.lock.Unlock()
err := utils.Retry(retryNum, "Etcd put", false, func(retryIdx int, lastErr error) error {
_, err := c.cli.Put(ctx, req.Url, req.Content)
return err
})
if err != nil {
log.Error("When create db request:", err)
return &Response{
Status: "Failure",
Error: err.Error(),
}
}
return &Response{
Status: "Success",
Message: []string{req.Content},
}
}
func (c *client) Get(req *Request) *Response {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
c.lock.Lock()
defer c.lock.Unlock()
resp, err := c.cli.Get(ctx, req.Url)
if err != nil {
log.Error("When get db request:", err)
return &Response{
Status: "Failure",
Error: err.Error(),
}
}
if len(resp.Kvs) == 0 {
return &Response{
Status: "Failure",
Error: "Wrong resource uuid provided!",
}
}
return &Response{
Status: "Success",
Message: []string{string(resp.Kvs[0].Value)},
}
}
func (c *client) List(req *Request) *Response {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
c.lock.Lock()
defer c.lock.Unlock()
resp, err := c.cli.Get(ctx, req.Url, clientv3.WithPrefix())
if err != nil {
log.Error("When get db request:", err)
return &Response{
Status: "Failure",
Error: err.Error(),
}
}
var message = []string{}
for _, v := range resp.Kvs {
message = append(message, string(v.Value))
}
return &Response{
Status: "Success",
Message: message,
}
}
func (c *client) Update(req *Request) *Response {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.cli.Put(ctx, req.Url, req.NewContent)
if err != nil {
log.Error("When update db request:", err)
return &Response{
Status: "Failure",
Error: err.Error(),
}
}
return &Response{
Status: "Success",
Message: []string{req.NewContent},
}
}
func (c *client) Delete(req *Request) *Response {
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.cli.Delete(ctx, req.Url)
if err != nil {
log.Error("When delete db request:", err)
return &Response{
Status: "Failure",
Error: err.Error(),
}
}
return &Response{
Status: "Success",
}
}