-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.go
162 lines (154 loc) · 4.07 KB
/
elasticsearch.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
package elasticsearch
import (
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
elasticsearch8 "github.com/elastic/go-elasticsearch/v8"
"github.com/elastic/go-elasticsearch/v8/esapi"
"github.com/robertlestak/pushx/pkg/flags"
"github.com/robertlestak/pushx/pkg/utils"
log "github.com/sirupsen/logrus"
)
type Elasticsearch struct {
Client *elasticsearch8.Client
Address string
Username string
Password string
// TLS
EnableTLS *bool
TLSInsecure *bool
TLSCert *string
TLSKey *string
TLSCA *string
Index *string
Key *string
}
func (d *Elasticsearch) LoadEnv(prefix string) error {
l := log.WithFields(log.Fields{
"pkg": "elasticsearch",
"fn": "LoadEnv",
})
l.Debug("Loading environment")
if os.Getenv(prefix+"ELASTICSEARCH_ADDRESS") != "" {
d.Address = os.Getenv(prefix + "ELASTICSEARCH_ADDRESS")
}
if os.Getenv(prefix+"ELASTICSEARCH_USERNAME") != "" {
d.Username = os.Getenv(prefix + "ELASTICSEARCH_USERNAME")
}
if os.Getenv(prefix+"ELASTICSEARCH_PASSWORD") != "" {
d.Password = os.Getenv(prefix + "ELASTICSEARCH_PASSWORD")
}
if os.Getenv(prefix+"ELASTICSEARCH_TLS_SKIP_VERIFY") != "" {
v := os.Getenv(prefix+"ELASTICSEARCH_TLS_SKIP_VERIFY") == "true"
d.TLSInsecure = &v
}
if os.Getenv(prefix+"ELASTICSEARCH_INDEX") != "" {
v := os.Getenv(prefix + "ELASTICSEARCH_INDEX")
d.Index = &v
}
if os.Getenv(prefix+"ELASTICSEARCH_DOC_ID") != "" {
v := os.Getenv(prefix + "ELASTICSEARCH_DOC_ID")
d.Key = &v
}
if os.Getenv(prefix+"ELASTICSEARCH_ENABLE_TLS") != "" {
v := os.Getenv(prefix+"ELASTICSEARCH_ENABLE_TLS") == "true"
d.EnableTLS = &v
}
if os.Getenv(prefix+"ELASTICSEARCH_TLS_CA_FILE") != "" {
v := os.Getenv(prefix + "ELASTICSEARCH_TLS_CA_FILE")
d.TLSCA = &v
}
if os.Getenv(prefix+"ELASTICSEARCH_TLS_CERT_FILE") != "" {
v := os.Getenv(prefix + "ELASTICSEARCH_TLS_CERT_FILE")
d.TLSCert = &v
}
if os.Getenv(prefix+"ELASTICSEARCH_TLS_KEY_FILE") != "" {
v := os.Getenv(prefix + "ELASTICSEARCH_TLS_KEY_FILE")
d.TLSKey = &v
}
return nil
}
func (d *Elasticsearch) LoadFlags() error {
l := log.WithFields(log.Fields{
"pkg": "elasticsearch",
"fn": "LoadFlags",
})
l.Debug("Loading flags")
d.Address = *flags.ElasticsearchAddress
d.Username = *flags.ElasticsearchUsername
d.Password = *flags.ElasticsearchPassword
d.Key = flags.ElasticsearchDocID
d.TLSInsecure = flags.ElasticsearchTLSSkipVerify
d.EnableTLS = flags.ElasticsearchEnableTLS
d.TLSCert = flags.ElasticsearchCertFile
d.TLSKey = flags.ElasticsearchKeyFile
d.TLSCA = flags.ElasticsearchCAFile
d.Index = flags.ElasticsearchIndex
return nil
}
func (d *Elasticsearch) Init() error {
l := log.WithFields(log.Fields{
"pkg": "elasticsearch",
"fn": "Init",
})
l.Debug("Initializing elasticsearch driver")
tc, err := utils.TlsConfig(d.EnableTLS, d.TLSInsecure, d.TLSCA, d.TLSCert, d.TLSKey)
if err != nil {
return err
}
client, err := elasticsearch8.NewClient(elasticsearch8.Config{
Transport: &http.Transport{
TLSClientConfig: tc,
},
Addresses: []string{d.Address},
Username: d.Username,
Password: d.Password,
})
if err != nil {
l.Errorf("error creating client: %v", err)
return err
}
d.Client = client
return nil
}
func (d *Elasticsearch) Push(r io.Reader) error {
l := log.WithFields(log.Fields{
"pkg": "elasticsearch",
"fn": "Push",
})
l.Debug("Putting work")
put := esapi.IndexRequest{
DocumentID: *d.Key,
}
if d.Index != nil {
put.Index = *d.Index
}
put.Body = r
putResponse, err := put.Do(context.Background(), d.Client)
if err != nil {
l.Errorf("error putting work: %v", err)
return err
}
if putResponse.StatusCode != 200 && putResponse.StatusCode != 201 {
bd, err := ioutil.ReadAll(putResponse.Body)
if err != nil {
l.Errorf("error reading response body: %v", err)
return err
}
l.Errorf("error putting work(%d): %v", putResponse.StatusCode, string(bd))
return errors.New("error putting work")
}
return nil
}
func (d *Elasticsearch) Cleanup() error {
l := log.WithFields(log.Fields{
"pkg": "elasticsearch",
"fn": "Cleanup",
})
l.Debug("Cleaning up")
l.Debug("Cleaned up")
return nil
}