forked from miku/esbulk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
administration.go
81 lines (74 loc) · 1.87 KB
/
administration.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
package esbulk
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/sethgrid/pester"
)
// FlushIndex flushes index.
func FlushIndex(idx int, options Options) error {
server := options.Servers[idx]
link := fmt.Sprintf("%s/%s/_flush", server, options.Index)
req, err := http.NewRequest("POST", link, nil)
if err != nil {
return err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return err
}
if options.Verbose {
log.Printf("index flushed: %s\n", resp.Status)
}
return nil
}
// GetSettings fetches the settings of the index.
func GetSettings(idx int, options Options) (map[string]interface{}, error) {
server := options.Servers[idx]
link := fmt.Sprintf("%s/%s/_settings", server, options.Index)
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return nil, err
}
if options.Username != "" && options.Password != "" {
req.SetBasicAuth(options.Username, options.Password)
}
req.Header.Set("Content-Type", "application/json")
resp, err := pester.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("could not get settings: %s", link)
}
doc := make(map[string]interface{})
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&doc); err != nil {
return nil, fmt.Errorf("failed to decode settings: %v", err)
}
// Example response.
// {
// "ai": {
// "settings": {
// "index": {
// "refresh_interval": "1s",
// "number_of_shards": "5",
// "provided_name": "ai",
// "creation_date": "1523372145102",
// "number_of_replicas": "1",
// "uuid": "5k-id0OZTKKU4A7DeeUNdQ",
// "version": {
// "created": "6020399"
// }
// }
// }
// }
// }
return doc, nil
}