forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch.go
174 lines (154 loc) · 4.99 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
163
164
165
166
167
168
169
170
171
172
173
174
package elasticsearch
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/compose/transporter/adaptor"
"github.com/compose/transporter/adaptor/elasticsearch/clients"
// used to call init function for each client to register itself
_ "github.com/compose/transporter/adaptor/elasticsearch/clients/all"
"github.com/compose/transporter/client"
"github.com/compose/transporter/log"
version "github.com/hashicorp/go-version"
)
const (
// DefaultIndex is used when there is not one included in the provided URI.
DefaultIndex = "test"
description = "an elasticsearch sink adaptor"
sampleConfig = `{
"uri": "${ELASTICSEARCH_URI}"
// "timeout": "10s", // defaults to 30s
// "aws_access_key": "ABCDEF", // used for signing requests to AWS Elasticsearch service
// "aws_access_secret": "ABCDEF" // used for signing requests to AWS Elasticsearch service
// "parent_id": "elastic_parent" // defaults to "elastic_parent" parent identifier for Elasticsearch
}`
)
var (
_ adaptor.Adaptor = &Elasticsearch{}
)
// Elasticsearch is an adaptor to connect a pipeline to
// an elasticsearch cluster.
type Elasticsearch struct {
adaptor.BaseConfig
AWSAccessKeyID string `json:"aws_access_key" doc:"credentials for use with AWS Elasticsearch service"`
AWSAccessSecret string `json:"aws_access_secret" doc:"credentials for use with AWS Elasticsearch service"`
ParentID string `json:"parent_id"`
}
// Description for the Elasticsearcb adaptor
func (e *Elasticsearch) Description() string {
return description
}
// SampleConfig for elasticsearch adaptor
func (e *Elasticsearch) SampleConfig() string {
return sampleConfig
}
func init() {
adaptor.Add(
"elasticsearch",
func() adaptor.Adaptor {
return &Elasticsearch{}
},
)
}
// Client returns a client that doesn't do anything other than fulfill the client.Client interface.
func (e *Elasticsearch) Client() (client.Client, error) {
return &client.Mock{}, nil
}
// Reader returns an error because this adaptor is currently not supported as a Source.
func (e *Elasticsearch) Reader() (client.Reader, error) {
return nil, adaptor.ErrFuncNotSupported{Name: "Reader()", Func: "elasticsearch"}
}
// Writer determines the which underlying writer to used based on the cluster's version.
func (e *Elasticsearch) Writer(done chan struct{}, wg *sync.WaitGroup) (client.Writer, error) {
return setupWriter(e)
}
func setupWriter(conf *Elasticsearch) (client.Writer, error) {
uri, err := url.Parse(conf.URI)
if err != nil {
return nil, client.InvalidURIError{URI: conf.URI, Err: err.Error()}
}
if uri.Path == "" {
uri.Path = fmt.Sprintf("/%s", DefaultIndex)
}
timeout, err := time.ParseDuration(conf.Timeout)
if err != nil {
log.Debugf("failed to parse duration, %s, falling back to default timeout of 30s", conf.Timeout)
timeout = 30 * time.Second
}
httpClient := &http.Client{
Timeout: timeout,
Transport: newTransport(conf.AWSAccessKeyID, conf.AWSAccessSecret),
}
hostsAndPorts := strings.Split(uri.Host, ",")
stringVersion, err := determineVersion(
fmt.Sprintf("%s://%s", uri.Scheme, hostsAndPorts[0]),
uri.User,
httpClient,
)
if err != nil {
return nil, err
}
v, err := version.NewVersion(stringVersion)
if err != nil {
return nil, client.VersionError{URI: conf.URI, V: stringVersion, Err: err.Error()}
}
for _, vc := range clients.Clients {
if vc.Constraint.Check(v) {
urls := make([]string, len(hostsAndPorts))
for i, hAndP := range hostsAndPorts {
urls[i] = fmt.Sprintf("%s://%s", uri.Scheme, hAndP)
}
opts := &clients.ClientOptions{
URLs: urls,
UserInfo: uri.User,
HTTPClient: httpClient,
Index: uri.Path[1:],
ParentID: conf.ParentID,
}
versionedClient, _ := vc.Creator(opts)
return versionedClient, nil
}
}
return nil, client.VersionError{URI: conf.URI, V: stringVersion, Err: "unsupported client"}
}
func determineVersion(uri string, user *url.Userinfo, httpClient *http.Client) (string, error) {
req, err := http.NewRequest(http.MethodGet, uri, nil)
if err != nil {
return "", err
}
if user != nil {
if pwd, ok := user.Password(); ok {
req.SetBasicAuth(user.Username(), pwd)
}
}
resp, err := httpClient.Do(req)
if err != nil {
return "", client.ConnectError{Reason: uri}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", client.VersionError{URI: uri, V: "", Err: "unable to read response body"}
}
defer resp.Body.Close()
var r struct {
Name string `json:"name"`
Version struct {
Number string `json:"number"`
} `json:"version"`
}
if resp.StatusCode != http.StatusOK {
return "", client.VersionError{URI: uri, V: "", Err: fmt.Sprintf("bad status code: %d", resp.StatusCode)}
}
err = json.Unmarshal(body, &r)
if err != nil {
return "", client.VersionError{URI: uri, V: "", Err: fmt.Sprintf("malformed JSON: %s", body)}
} else if r.Version.Number == "" {
return "", client.VersionError{URI: uri, V: "", Err: fmt.Sprintf("missing version: %s", body)}
}
return r.Version.Number, nil
}