@@ -2,16 +2,12 @@ use std::collections::HashMap;
2
2
3
3
use aws_types:: credentials:: SharedCredentialsProvider ;
4
4
use aws_types:: region:: Region ;
5
- use bytes:: { Buf , Bytes } ;
6
- use http:: { Response , StatusCode , Uri } ;
7
- use hyper:: { body, Body } ;
8
- use serde:: Deserialize ;
5
+ use bytes:: Bytes ;
6
+ use http:: { StatusCode , Uri } ;
9
7
use snafu:: ResultExt ;
10
- use vector_core:: config:: proxy:: ProxyConfig ;
11
8
12
9
use super :: {
13
- request_builder:: ElasticsearchRequestBuilder , ElasticsearchApiVersion , ElasticsearchEncoder ,
14
- InvalidHostSnafu , Request ,
10
+ request_builder:: ElasticsearchRequestBuilder , ElasticsearchEncoder , InvalidHostSnafu , Request ,
15
11
} ;
16
12
use crate :: {
17
13
http:: { Auth , HttpClient , MaybeAuth } ,
@@ -42,12 +38,7 @@ pub struct ElasticsearchCommon {
42
38
}
43
39
44
40
impl ElasticsearchCommon {
45
- pub async fn parse_config (
46
- config : & ElasticsearchConfig ,
47
- endpoint : & str ,
48
- proxy_config : & ProxyConfig ,
49
- version : & mut Option < usize > ,
50
- ) -> crate :: Result < Self > {
41
+ pub async fn parse_config ( config : & ElasticsearchConfig , endpoint : & str ) -> crate :: Result < Self > {
51
42
// Test the configured host, but ignore the result
52
43
let uri = format ! ( "{}/_test" , endpoint) ;
53
44
let uri = uri
@@ -87,6 +78,16 @@ impl ElasticsearchCommon {
87
78
88
79
let mode = config. common_mode ( ) ?;
89
80
81
+ let doc_type = config. doc_type . clone ( ) . unwrap_or_else ( || "_doc" . into ( ) ) ;
82
+ let request_builder = ElasticsearchRequestBuilder {
83
+ compression : config. compression ,
84
+ encoder : ElasticsearchEncoder {
85
+ transformer : config. encoding . clone ( ) ,
86
+ doc_type,
87
+ suppress_type_name : config. suppress_type_name ,
88
+ } ,
89
+ } ;
90
+
90
91
let tower_request = config
91
92
. request
92
93
. tower
@@ -102,13 +103,11 @@ impl ElasticsearchCommon {
102
103
query_params. insert ( "pipeline" . into ( ) , pipeline. into ( ) ) ;
103
104
}
104
105
105
- let bulk_url = {
106
- let mut query = url:: form_urlencoded:: Serializer :: new ( String :: new ( ) ) ;
107
- for ( p, v) in & query_params {
108
- query. append_pair ( & p[ ..] , & v[ ..] ) ;
109
- }
110
- format ! ( "{}/_bulk?{}" , base_url, query. finish( ) )
111
- } ;
106
+ let mut query = url:: form_urlencoded:: Serializer :: new ( String :: new ( ) ) ;
107
+ for ( p, v) in & query_params {
108
+ query. append_pair ( & p[ ..] , & v[ ..] ) ;
109
+ }
110
+ let bulk_url = format ! ( "{}/_bulk?{}" , base_url, query. finish( ) ) ;
112
111
let bulk_uri = bulk_url. parse :: < Uri > ( ) . unwrap ( ) ;
113
112
114
113
let tls_settings = TlsSettings :: from_options ( & config. tls ) ?;
@@ -123,46 +122,6 @@ impl ElasticsearchCommon {
123
122
124
123
let region = config. aws . as_ref ( ) . and_then ( |config| config. region ( ) ) ;
125
124
126
- let version = if let Some ( version) = * version {
127
- version
128
- } else {
129
- let ver = match config. api_version {
130
- ElasticsearchApiVersion :: V6 => 6 ,
131
- ElasticsearchApiVersion :: V7 => 7 ,
132
- ElasticsearchApiVersion :: V8 => 8 ,
133
- ElasticsearchApiVersion :: Auto => {
134
- get_version (
135
- & base_url,
136
- & http_auth,
137
- & aws_auth,
138
- & region,
139
- & request,
140
- & tls_settings,
141
- proxy_config,
142
- )
143
- . await ?
144
- }
145
- } ;
146
- * version = Some ( ver) ;
147
- ver
148
- } ;
149
-
150
- let doc_type = config. doc_type . clone ( ) . unwrap_or_else ( || "_doc" . into ( ) ) ;
151
- let suppress_type_name = if let Some ( suppress_type_name) = config. suppress_type_name {
152
- warn ! ( message = "DEPRECATION, use of deprecated option `suppress_type_name`. Please use `api_version` option instead." ) ;
153
- suppress_type_name
154
- } else {
155
- version >= 7
156
- } ;
157
- let request_builder = ElasticsearchRequestBuilder {
158
- compression : config. compression ,
159
- encoder : ElasticsearchEncoder {
160
- transformer : config. encoding . clone ( ) ,
161
- doc_type,
162
- suppress_type_name,
163
- } ,
164
- } ;
165
-
166
125
Ok ( Self {
167
126
http_auth,
168
127
base_url,
@@ -179,17 +138,11 @@ impl ElasticsearchCommon {
179
138
}
180
139
181
140
/// Parses endpoints into a vector of ElasticsearchCommons. The resulting vector is guaranteed to not be empty.
182
- pub async fn parse_many (
183
- config : & ElasticsearchConfig ,
184
- proxy_config : & ProxyConfig ,
185
- ) -> crate :: Result < Vec < Self > > {
186
- let mut version = None ;
141
+ pub async fn parse_many ( config : & ElasticsearchConfig ) -> crate :: Result < Vec < Self > > {
187
142
if let Some ( endpoint) = config. endpoint . as_ref ( ) {
188
143
warn ! ( message = "DEPRECATION, use of deprecated option `endpoint`. Please use `endpoints` option instead." ) ;
189
144
if config. endpoints . is_empty ( ) {
190
- Ok ( vec ! [
191
- Self :: parse_config( config, endpoint, proxy_config, & mut version) . await ?,
192
- ] )
145
+ Ok ( vec ! [ Self :: parse_config( config, endpoint) . await ?] )
193
146
} else {
194
147
Err ( ParseError :: EndpointsExclusive . into ( ) )
195
148
}
@@ -198,8 +151,7 @@ impl ElasticsearchCommon {
198
151
} else {
199
152
let mut commons = Vec :: new ( ) ;
200
153
for endpoint in config. endpoints . iter ( ) {
201
- commons
202
- . push ( Self :: parse_config ( config, endpoint, proxy_config, & mut version) . await ?) ;
154
+ commons. push ( Self :: parse_config ( config, endpoint) . await ?) ;
203
155
}
204
156
Ok ( commons)
205
157
}
@@ -208,25 +160,30 @@ impl ElasticsearchCommon {
208
160
/// Parses a single endpoint, else panics.
209
161
#[ cfg( test) ]
210
162
pub async fn parse_single ( config : & ElasticsearchConfig ) -> crate :: Result < Self > {
211
- let mut commons =
212
- Self :: parse_many ( config, crate :: config:: SinkContext :: new_test ( ) . proxy ( ) ) . await ?;
213
- assert_eq ! ( commons. len( ) , 1 ) ;
163
+ let mut commons = Self :: parse_many ( config) . await ?;
164
+ assert ! ( commons. len( ) == 1 ) ;
214
165
Ok ( commons. remove ( 0 ) )
215
166
}
216
167
217
168
pub async fn healthcheck ( self , client : HttpClient ) -> crate :: Result < ( ) > {
218
- match get (
219
- & self . base_url ,
220
- & self . http_auth ,
221
- & self . aws_auth ,
222
- & self . region ,
223
- & self . request ,
224
- client,
225
- "/_cluster/health" ,
226
- )
227
- . await ?
228
- . status ( )
229
- {
169
+ let mut builder = Request :: get ( format ! ( "{}/_cluster/health" , self . base_url) ) ;
170
+
171
+ if let Some ( authorization) = & self . http_auth {
172
+ builder = authorization. apply_builder ( builder) ;
173
+ }
174
+
175
+ for ( header, value) in & self . request . headers {
176
+ builder = builder. header ( & header[ ..] , & value[ ..] ) ;
177
+ }
178
+
179
+ let mut request = builder. body ( Bytes :: new ( ) ) ?;
180
+
181
+ if let Some ( credentials_provider) = & self . aws_auth {
182
+ sign_request ( & mut request, credentials_provider, & self . region ) . await ?;
183
+ }
184
+ let response = client. send ( request. map ( hyper:: Body :: from) ) . await ?;
185
+
186
+ match response. status ( ) {
230
187
StatusCode :: OK => Ok ( ( ) ) ,
231
188
status => Err ( HealthcheckError :: UnexpectedStatus { status } . into ( ) ) ,
232
189
}
@@ -240,67 +197,3 @@ pub async fn sign_request(
240
197
) -> crate :: Result < ( ) > {
241
198
crate :: aws:: sign_request ( "es" , request, credentials_provider, region) . await
242
199
}
243
-
244
- async fn get_version (
245
- base_url : & str ,
246
- http_auth : & Option < Auth > ,
247
- aws_auth : & Option < SharedCredentialsProvider > ,
248
- region : & Option < Region > ,
249
- request : & RequestConfig ,
250
- tls_settings : & TlsSettings ,
251
- proxy_config : & ProxyConfig ,
252
- ) -> crate :: Result < usize > {
253
- #[ derive( Deserialize ) ]
254
- struct ClusterState {
255
- version : usize ,
256
- }
257
-
258
- let client = HttpClient :: new ( tls_settings. clone ( ) , proxy_config) ?;
259
- let response = get (
260
- base_url,
261
- http_auth,
262
- aws_auth,
263
- region,
264
- request,
265
- client,
266
- "/_cluster/state/version" ,
267
- )
268
- . await
269
- . map_err ( |error| format ! ( "Failed to get Elasticsearch API version: {}" , error) ) ?;
270
-
271
- let ( _, body) = response. into_parts ( ) ;
272
- let mut body = body:: aggregate ( body) . await ?;
273
- let body = body. copy_to_bytes ( body. remaining ( ) ) ;
274
- let ClusterState { version } = serde_json:: from_slice ( & body) ?;
275
- Ok ( version)
276
- }
277
-
278
- async fn get (
279
- base_url : & str ,
280
- http_auth : & Option < Auth > ,
281
- aws_auth : & Option < SharedCredentialsProvider > ,
282
- region : & Option < Region > ,
283
- request : & RequestConfig ,
284
- client : HttpClient ,
285
- path : & str ,
286
- ) -> crate :: Result < Response < Body > > {
287
- let mut builder = Request :: get ( format ! ( "{}{}" , base_url, path) ) ;
288
-
289
- if let Some ( authorization) = & http_auth {
290
- builder = authorization. apply_builder ( builder) ;
291
- }
292
-
293
- for ( header, value) in & request. headers {
294
- builder = builder. header ( & header[ ..] , & value[ ..] ) ;
295
- }
296
-
297
- let mut request = builder. body ( Bytes :: new ( ) ) ?;
298
-
299
- if let Some ( credentials_provider) = aws_auth {
300
- sign_request ( & mut request, credentials_provider, region) . await ?;
301
- }
302
- client
303
- . send ( request. map ( hyper:: Body :: from) )
304
- . await
305
- . map_err ( Into :: into)
306
- }
0 commit comments