-
Notifications
You must be signed in to change notification settings - Fork 50
/
clients_v7.go
216 lines (171 loc) · 5.29 KB
/
clients_v7.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
package es
import (
"bytes"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/signer/v4"
elasticsearch7 "github.com/elastic/go-elasticsearch/v7"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/sha1sum/aws_signing_client"
)
type Logger interface {
Debug(format string, args ...interface{})
Info(format string, args ...interface{})
}
type ClientV7 struct {
elasticsearch7.Client
}
var (
mtx sync.Mutex
ecl = map[string]*ClientV7{}
)
type clientBuilder func(logger Logger, url string) (*ClientV7, error)
var factory = map[string]clientBuilder{
"default": func(logger Logger, url string) (*ClientV7, error) {
cfg := elasticsearch7.Config{
Addresses: []string{
url,
},
}
elasticClient, err := elasticsearch7.NewClient(cfg)
if err != nil {
return nil, fmt.Errorf("can't create ES client v7: %w", err)
}
client := &ClientV7{*elasticClient}
return client, err
},
"aws": func(logger Logger, url string) (*ClientV7, error) {
client, err := GetAwsClient(logger, url)
return client, err
},
}
func NewClient(config cfg.Config, logger Logger, name string) (*ClientV7, error) {
clientTypeKey := fmt.Sprintf("es_%s_type", name)
clientType := config.GetString(clientTypeKey)
urlKey := fmt.Sprintf("es_%s_endpoint", name)
url := config.GetString(urlKey)
client, err := NewSimpleClient(logger, url, clientType)
if err != nil {
return nil, fmt.Errorf("can not create the client: %w", err)
}
templateKey := fmt.Sprintf("es_%s_templates", name)
templatePath := config.GetStringSlice(templateKey)
if err = putTemplates(logger, client, name, templatePath); err != nil {
return nil, fmt.Errorf("can not put templates: %w", err)
}
return client, nil
}
func NewSimpleClient(logger Logger, url string, clientType string) (*ClientV7, error) {
logger.Info("creating client ", clientType, " for host ", url)
client, err := factory[clientType](logger, url)
if err != nil {
return nil, fmt.Errorf("error creating the client: %w", err)
}
return client, nil
}
func ProvideClient(config cfg.Config, logger Logger, name string) (*ClientV7, error) {
mtx.Lock()
defer mtx.Unlock()
if client, ok := ecl[name]; ok {
return client, nil
}
var err error
if ecl[name], err = NewClient(config, logger, name); err != nil {
return nil, err
}
return ecl[name], nil
}
func GetAwsClient(logger Logger, url string) (*ClientV7, error) {
configTemplate := &aws.Config{
CredentialsChainVerboseErrors: aws.Bool(true),
Region: aws.String(endpoints.EuCentral1RegionID),
LogLevel: aws.LogLevel(aws.LogDebug | aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors),
Logger: aws.LoggerFunc(func(args ...interface{}) {
logger.Debug(fmt.Sprint(args...))
}),
HTTPClient: &http.Client{
Timeout: 1 * time.Minute,
},
}
sess := session.Must(session.NewSession(configTemplate))
creds := sess.Config.Credentials
signer := v4.NewSigner(creds)
awsClient, err := aws_signing_client.New(signer, nil, "es", endpoints.EuCentral1RegionID)
if err != nil {
return nil, fmt.Errorf("error creating the elastic aws client: %w", err)
}
configES := elasticsearch7.Config{
Addresses: []string{url},
Transport: awsClient.Transport,
}
elasticClient, err := elasticsearch7.NewClient(configES)
client := &ClientV7{*elasticClient}
return client, err
}
func putTemplates(logger Logger, client *ClientV7, name string, paths []string) error {
files, err := getTemplateFiles(logger, paths)
if err != nil {
return fmt.Errorf("could not get template files: %w", err)
}
for _, file := range files {
buf, err := os.ReadFile(file)
if err != nil {
return fmt.Errorf("could not read es-templates file %s: %w", file, err)
}
// Create template
res, err := client.Indices.PutTemplate(
name,
bytes.NewReader(buf),
)
if err != nil {
return fmt.Errorf("could not put the es-template in file %s for es client %s: %w", file, name, err)
}
defer func() {
closeError := res.Body.Close()
if closeError != nil {
msg := "could not close response reader for PutTemplates"
logger.Info(msg)
}
}()
if res.IsError() {
return fmt.Errorf("could not put template from file %s: got error from ES: %s, %s", file, res.Status(), res.String())
}
}
return nil
}
func getTemplateFiles(logger Logger, paths []string) ([]string, error) {
files := make([]string, 0)
for _, p := range paths {
fileInfo, err := os.Stat(p)
if err != nil {
return nil, fmt.Errorf("there was an error with the es-tempates path %s. Does it exist?: %w", p, err)
}
if fileInfo.Mode().IsRegular() {
files = append(files, p)
continue
}
if !fileInfo.Mode().IsDir() {
return nil, fmt.Errorf("the es-tempates path %s is neither a file or a directory: %w", p, err)
}
fileInfos, err := os.ReadDir(p)
if err != nil {
return nil, fmt.Errorf("could not scan the the es-tempates path %s: %w", p, err)
}
for _, fileInfo := range fileInfos {
filename := filepath.Join(p, fileInfo.Name())
scan, err := getTemplateFiles(logger, []string{filename})
if err != nil {
return nil, fmt.Errorf("could not get template files: %w", err)
}
files = append(files, scan...)
}
}
return files, nil
}