-
Notifications
You must be signed in to change notification settings - Fork 800
/
client_v6.go
91 lines (80 loc) · 3.24 KB
/
client_v6.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package esutils
import (
"fmt"
"os"
"testing"
"time"
"github.com/olivere/elastic"
"github.com/stretchr/testify/require"
)
type (
v6Client struct {
client *elastic.Client
}
)
func newV6Client(url string) (*v6Client, error) {
esClient, err := elastic.NewClient(
elastic.SetURL(url),
elastic.SetRetrier(elastic.NewBackoffRetrier(elastic.NewExponentialBackoff(128*time.Millisecond, 513*time.Millisecond))),
)
return &v6Client{
client: esClient,
}, err
}
func (es *v6Client) PutIndexTemplate(t *testing.T, templateConfigFile, templateName string) {
// This function is used exclusively in tests. Excluding it from security checks.
// #nosec
template, err := os.ReadFile(templateConfigFile)
require.NoError(t, err)
putTemplate, err := es.client.IndexPutTemplate(templateName).BodyString(string(template)).Do(createContext())
require.NoError(t, err)
require.True(t, putTemplate.Acknowledged)
}
func (es *v6Client) CreateIndex(t *testing.T, indexName string) {
exists, err := es.client.IndexExists(indexName).Do(createContext())
require.NoError(t, err)
if exists {
deleteTestIndex, err := es.client.DeleteIndex(indexName).Do(createContext())
require.Nil(t, err)
require.True(t, deleteTestIndex.Acknowledged)
}
createTestIndex, err := es.client.CreateIndex(indexName).Do(createContext())
require.Nil(t, err)
require.True(t, createTestIndex.Acknowledged)
}
func (es *v6Client) DeleteIndex(t *testing.T, indexName string) {
deleteTestIndex, err := es.client.DeleteIndex(indexName).Do(createContext())
require.Nil(t, err)
require.True(t, deleteTestIndex.Acknowledged)
}
func (es *v6Client) PutMaxResultWindow(t *testing.T, indexName string, maxResultWindow int) error {
_, err := es.client.IndexPutSettings(indexName).
BodyString(fmt.Sprintf(`{"max_result_window" : %d}`, maxResultWindow)).
Do(createContext())
require.NoError(t, err)
return err
}
func (es *v6Client) GetMaxResultWindow(t *testing.T, indexName string) (string, error) {
settings, err := es.client.IndexGetSettings(indexName).Do(createContext())
require.NoError(t, err)
return settings[indexName].Settings["index"].(map[string]interface{})["max_result_window"].(string), nil
}