forked from HGS-shoust01/terraform-provider-ece
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ece_client.go
695 lines (538 loc) · 21.3 KB
/
ece_client.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
)
const elasticsearchResource = "/api/v1/clusters/elasticsearch"
const kibanaResource = "/api/v1/clusters/kibana"
const jsonContentType = "application/json"
// ECEClient is a client used for interactions with the ECE API.
type ECEClient struct {
// HTTPClient specifies the HTTP client that should be used for ECE API calls.
HTTPClient *http.Client
// BaseURL specifies the base URL for the ECE API.
BaseURL string
// Username specifies the Username to use for basic authentication.
Username string
// Password specifies the Password to use for basic authentication.
Password string
// Timeout in seconds for resource operations.
Timeout int
}
// CreateElasticsearchCluster creates a new elasticsearch cluster using the specified create request.
func (c *ECEClient) CreateElasticsearchCluster(createClusterRequest CreateElasticsearchClusterRequest) (crudResponse *ClusterCrudResponse, err error) {
log.Printf("[DEBUG] CreateElasticsearchCluster: %v\n", createClusterRequest)
// Example cluster creation request body.
// {
// "cluster_name" : "My Cluster",
// "plan" : {
// "elasticsearch" : {
// "version" : "7.1.0"
// },
// "cluster_topology" : [
// {
// "memory_per_node" : 2048,
// "node_count_per_zone" : 1,
// "node_type" : {
// "data" : true,
// "ingest" : true,
// "master" : true,
// "ml" : true
// },
// "zone_count" : 1
// }
// ]
// }
// }
jsonData, err := json.Marshal(createClusterRequest)
if err != nil {
return nil, err
}
jsonString := string(jsonData)
log.Printf("[DEBUG] CreateElasticsearchCluster request body: %s\n", jsonString)
body := strings.NewReader(jsonString)
resourceURL := c.BaseURL + elasticsearchResource
log.Printf("[DEBUG] CreateElasticsearchCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("POST", resourceURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
// Example response:
// {
// "elasticsearch_cluster_id": "5de00f3876e3442f8e4f83110af0e251",
// "credentials": {
// "username": "elastic",
// "password": "Ov8cmAVCqTr8biFfND2wtIuY"
// }
// }
log.Printf("[DEBUG] CreateElasticsearchCluster response: %v\n", resp)
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 {
return nil, fmt.Errorf("elasticsearch cluster could not be created: %v", string(respBytes))
}
log.Printf("[DEBUG] CreateElasticsearchCluster response body: %v\n", string(respBytes))
err = json.Unmarshal(respBytes, &crudResponse)
if err != nil {
return nil, err
}
return crudResponse, nil
}
// CreateKibanaCluster creates a new Kibana cluster using the specified create request.
func (c *ECEClient) CreateKibanaCluster(createKibanaRequest CreateKibanaRequest) (crudResponse *ClusterCrudResponse, err error) {
log.Printf("[DEBUG] CreateKibanaCluster: %v\n", createKibanaRequest)
jsonData, err := json.Marshal(createKibanaRequest)
if err != nil {
return nil, err
}
jsonString := string(jsonData)
log.Printf("[DEBUG] CreateKibanaCluster request body: %s\n", jsonString)
body := strings.NewReader(jsonString)
resourceURL := c.BaseURL + kibanaResource
log.Printf("[DEBUG] CreateKibanaCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("POST", resourceURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] CreateKibanaCluster response: %v\n", resp)
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 201 {
return nil, fmt.Errorf("kibana cluster could not be created: %v", string(respBytes))
}
log.Printf("[DEBUG] CreateKibanaCluster response body: %v\n", string(respBytes))
err = json.Unmarshal(respBytes, &crudResponse)
if err != nil {
return nil, err
}
return crudResponse, nil
}
// DeleteElasticsearchCluster deletes an existing elasticsearch cluster.
func (c *ECEClient) DeleteElasticsearchCluster(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] DeleteElasticsearchCluster ID: %s\n", id)
// NOTE: A cluster must be successfully _shutdown first before it can be deleted.
log.Printf("[DEBUG] Shutting down cluster ID: %s\n", id)
_, err = c.ShutdownElasticsearchCluster(id)
if err != nil {
return nil, err
}
// Wait for cluster shutdown.
log.Printf("[DEBUG] Waiting for shutdown of cluster ID: %s\n", id)
c.WaitForElasticsearchClusterStatus(id, "stopped", true)
resourceURL := c.BaseURL + elasticsearchResource + "/" + id
log.Printf("[DEBUG] DeleteElasticsearchCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("DELETE", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] DeleteElasticsearchCluster response: %v\n", resp)
if resp.StatusCode != 200 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster could not be deleted: %v", id, string(respBytes))
}
return resp, nil
}
// DeleteKibanaCluster deletes an existing kibana cluster.
func (c *ECEClient) DeleteKibanaCluster(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] DeleteKibanaCluster ID: %s\n", id)
// NOTE: A cluster must be successfully _shutdown first before it can be deleted.
log.Printf("[DEBUG] Shutting down cluster ID: %s\n", id)
_, err = c.ShutdownKibanaCluster(id)
if err != nil {
return nil, err
}
// Wait for cluster shutdown.
log.Printf("[DEBUG] Waiting for shutdown of cluster ID: %s\n", id)
c.WaitForKibanaClusterStatus(id, "stopped", true)
resourceURL := c.BaseURL + kibanaResource + "/" + id
log.Printf("[DEBUG] DeleteKibanaCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("DELETE", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] DeleteKibanaCluster response: %v\n", resp)
if resp.StatusCode != 200 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: kibana cluster could not be deleted: %v", id, string(respBytes))
}
return resp, nil
}
// GetElasticsearchCluster returns information for an existing elasticsearch cluster.
func (c *ECEClient) GetElasticsearchCluster(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] GetElasticsearchCluster ID: %s\n", id)
resourceURL := c.BaseURL + elasticsearchResource + "/" + id
log.Printf("[DEBUG] GetElasticsearchCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("GET", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] GetElasticsearchCluster response: %v\n", resp)
if resp.StatusCode != 200 && resp.StatusCode != 404 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster could not be retrieved: %v", id, string(respBytes))
}
return resp, nil
}
// GetElasticsearchClusterPlan returns the plan for an existing elasticsearch cluster.
func (c *ECEClient) GetElasticsearchClusterPlan(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] GetElasticsearchClusterPlan ID: %s\n", id)
// GET /api/v1/clusters/elasticsearch/{cluster_id}/plan
resourceURL := c.BaseURL + elasticsearchResource + "/" + id + "/plan"
log.Printf("[DEBUG] GetElasticsearchClusterPlan Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("GET", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] GetElasticsearchClusterPlan response: %v\n", resp)
if resp.StatusCode != 200 && resp.StatusCode != 404 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster plan could not be retrieved: %v", id, string(respBytes))
}
return resp, nil
}
// GetElasticsearchClusterPlanActivity returns the active and historical plan information for an elasticsearch cluster.
func (c *ECEClient) GetElasticsearchClusterPlanActivity(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] GetElasticsearchClusterPlanActivity ID: %s\n", id)
// GET /api/v1/clusters/elasticsearch/{cluster_id}/plan/activity
resourceURL := c.BaseURL + elasticsearchResource + "/" + id + "/plan/activity"
log.Printf("[DEBUG] GetElasticsearchClusterPlanActivity Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("GET", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] GetElasticsearchClusterPlanActivity response: %v\n", resp)
if resp.StatusCode != 200 && resp.StatusCode != 404 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster plan activity could not be retrieved: %v", id, string(respBytes))
}
return resp, nil
}
// GetKibanaCluster returns information for an existing Kibana cluster.
func (c *ECEClient) GetKibanaCluster(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] GetKibanaCluster ID: %s\n", id)
resourceURL := c.BaseURL + kibanaResource + "/" + id
log.Printf("[DEBUG] GetKibanaCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("GET", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] GetKibanaCluster response: %v\n", resp)
if resp.StatusCode != 200 && resp.StatusCode != 404 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: kibana cluster could not be retrieved: %v", id, string(respBytes))
}
return resp, nil
}
// GetKibanaClusterPlanActivity returns the active and historical plan information for a Kibana cluster.
func (c *ECEClient) GetKibanaClusterPlanActivity(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] GetKibanaClusterPlanActivity ID: %s\n", id)
// GET /api/v1/clusters/kibana/{cluster_id}/plan/activity
resourceURL := c.BaseURL + kibanaResource + "/" + id + "/plan/activity"
log.Printf("[DEBUG] GetKibanaClusterPlanActivity Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("GET", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] GetKibanaClusterPlanActivity response: %v\n", resp)
if resp.StatusCode != 200 && resp.StatusCode != 404 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: kibana cluster plan activity could not be retrieved: %v", id, string(respBytes))
}
return resp, nil
}
// GetResponseBodyAsJSON returns a response body as a JSON document.
func (c *ECEClient) GetResponseBodyAsJSON(resp *http.Response) (jsonResponse interface{}, err error) {
err = json.NewDecoder(resp.Body).Decode(&jsonResponse)
if err != nil {
return nil, fmt.Errorf("error unmarshalling response body: %+v: %+v", err, resp.Body)
}
log.Printf("[DEBUG] GetResponseBodyAsJSON body: %v\n", jsonResponse)
return jsonResponse, nil
}
// GetResponseBodyAsString returns a response body as a string.
func (c *ECEClient) GetResponseBodyAsString(resp *http.Response) (body string, err error) {
log.Printf("[DEBUG] GetResponseBodyAsString: %v\n", resp)
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
body = string(respBytes)
log.Printf("[DEBUG] GetResponseBodyAsString body: %v\n", body)
return body, nil
}
// UpdateElasticsearchCluster updates an existing elasticsearch cluster using the specified cluster plan.
func (c *ECEClient) UpdateElasticsearchCluster(id string, clusterPlan ElasticsearchClusterPlan) (resp *http.Response, err error) {
log.Printf("[DEBUG] UpdateElasticsearchCluster: %s: %v\n", id, clusterPlan)
jsonData, err := json.Marshal(clusterPlan)
if err != nil {
return nil, err
}
jsonString := string(jsonData)
body := strings.NewReader(jsonString)
resourceURL := c.BaseURL + elasticsearchResource + "/" + id + "/plan"
log.Printf("[DEBUG] UpdateElasticsearchCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("POST", resourceURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] UpdateElasticsearchCluster response: %v\n", resp)
if resp.StatusCode != 202 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster could not be updated: %v", id, string(respBytes))
}
return resp, nil
}
// UpdateElasticsearchClusterMetadata updates the metadata for an existing elasticsearch cluster.
func (c *ECEClient) UpdateElasticsearchClusterMetadata(id string, metadata ClusterMetadataSettings) (resp *http.Response, err error) {
log.Printf("[DEBUG] UpdateElasticsearchClusterMetadata: %s: %v\n", id, metadata)
jsonData, err := json.Marshal(metadata)
if err != nil {
return nil, err
}
jsonString := string(jsonData)
body := strings.NewReader(jsonString)
// PATCH /api/v1/clusters/elasticsearch/{cluster_id}/metadata/settings
resourceURL := c.BaseURL + elasticsearchResource + "/" + id + "/metadata/settings"
log.Printf("[DEBUG] UpdateElasticsearchClusterMetadata Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("PATCH", resourceURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] UpdateElasticsearchClusterMetadata response: %v\n", resp)
if resp.StatusCode != 200 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster metadata settings could not be updated: %v", id, string(respBytes))
}
return resp, nil
}
// UpdateKibanaCluster updates an existing Kibana cluster using the specified Kibana cluster plan.
func (c *ECEClient) UpdateKibanaCluster(id string, kibanaPlan *KibanaClusterPlan) (resp *http.Response, err error) {
log.Printf("[DEBUG] UpdateKibanaCluster: %s: %v\n", id, *kibanaPlan)
jsonData, err := json.Marshal(kibanaPlan)
if err != nil {
return nil, err
}
jsonString := string(jsonData)
body := strings.NewReader(jsonString)
resourceURL := c.BaseURL + kibanaResource + "/" + id + "/plan"
log.Printf("[DEBUG] UpdateKibanaCluster Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("POST", resourceURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] UpdateKibanaCluster response: %v\n", resp)
if resp.StatusCode != 202 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: kibana cluster could not be updated: %v", id, string(respBytes))
}
return resp, nil
}
// UpdateKibanaClusterMetadata updates the metadata for an existing Kibana cluster.
func (c *ECEClient) UpdateKibanaClusterMetadata(id string, metadata ClusterMetadataSettings) (resp *http.Response, err error) {
log.Printf("[DEBUG] UpdateKibanaClusterMetadata: %s: %v\n", id, metadata)
jsonData, err := json.Marshal(metadata)
if err != nil {
return nil, err
}
jsonString := string(jsonData)
body := strings.NewReader(jsonString)
// PATCH /api/v1/clusters/kibana/{cluster_id}/metadata/settings
resourceURL := c.BaseURL + kibanaResource + "/" + id + "/metadata/settings"
log.Printf("[DEBUG] UpdateKibanaClusterMetadata Resource URL: %s\n", resourceURL)
req, err := http.NewRequest("PATCH", resourceURL, body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] UpdateKibanaClusterMetadata response: %v\n", resp)
if resp.StatusCode != 200 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: kibana cluster metadata settings could not be updated: %v", id, string(respBytes))
}
return resp, nil
}
// ShutdownElasticsearchCluster shuts down an existing ECE cluster.
func (c *ECEClient) ShutdownElasticsearchCluster(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] ShutdownElasticsearchCluster ID: %s\n", id)
resourceURL := c.BaseURL + elasticsearchResource + "/" + id + "/_shutdown"
log.Printf("[DEBUG] ShutdownElasticsearchCluster resource URL: %s\n", resourceURL)
req, err := http.NewRequest("POST", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] ShutdownElasticsearchCluster response: %v\n", resp)
if resp.StatusCode != 202 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: elasticsearch cluster could not be shutdown: %v", id, string(respBytes))
}
return resp, nil
}
// ShutdownKibanaCluster shuts down an existing Kibana cluster.
func (c *ECEClient) ShutdownKibanaCluster(id string) (resp *http.Response, err error) {
log.Printf("[DEBUG] ShutdownKibanaCluster ID: %s\n", id)
resourceURL := c.BaseURL + kibanaResource + "/" + id + "/_shutdown"
log.Printf("[DEBUG] ShutdownKibanaCluster resource URL: %s\n", resourceURL)
req, err := http.NewRequest("POST", resourceURL, nil)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", jsonContentType)
req.SetBasicAuth(c.Username, c.Password)
resp, err = c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] ShutdownKibanaCluster response: %v\n", resp)
if resp.StatusCode != 202 {
respBytes, _ := ioutil.ReadAll(resp.Body)
return nil, fmt.Errorf("%q: kibana cluster could not be shutdown: %v", id, string(respBytes))
}
return resp, nil
}
// WaitForElasticsearchClusterStatus waits for an elasticsearch cluster to enter the specified status.
func (c *ECEClient) WaitForElasticsearchClusterStatus(id string, status string, allowMissing bool) error {
timeoutSeconds := time.Second * time.Duration(c.Timeout)
log.Printf("[DEBUG] WaitForElasticsearchClusterStatus will wait for %v seconds for '%s' status for cluster ID: %s\n", timeoutSeconds, status, id)
return resource.Retry(timeoutSeconds, func() *resource.RetryError {
resp, err := c.GetElasticsearchCluster(id)
if err != nil {
return resource.NonRetryableError(err)
}
if resp.StatusCode == 404 && allowMissing {
return nil
} else if resp.StatusCode == 200 {
var clusterInfo ElasticsearchClusterInfo
err = json.NewDecoder(resp.Body).Decode(&clusterInfo)
if err != nil {
return resource.NonRetryableError(err)
}
if clusterInfo.Status == status {
log.Printf("[DEBUG] WaitForElasticsearchClusterStatus desired cluster status reached: %s\n", clusterInfo.Status)
return nil
}
log.Printf("[DEBUG] WaitForElasticsearchClusterStatus current cluster status: %s. Desired status: %s\n", clusterInfo.Status, status)
}
return resource.RetryableError(
fmt.Errorf("%q: timeout while waiting for the elasticsearch cluster to reach %s status", id, status))
})
}
// WaitForKibanaClusterStatus waits for a Kibana cluster to enter the specified status.
func (c *ECEClient) WaitForKibanaClusterStatus(id string, status string, allowMissing bool) error {
timeoutSeconds := time.Second * time.Duration(c.Timeout)
log.Printf("[DEBUG] WaitForKibanaClusterStatus will wait for %v seconds for '%s' status for Kibana cluster ID: %s\n", timeoutSeconds, status, id)
return resource.Retry(timeoutSeconds, func() *resource.RetryError {
resp, err := c.GetKibanaCluster(id)
if err != nil {
return resource.NonRetryableError(err)
}
if resp.StatusCode == 404 && allowMissing {
return nil
} else if resp.StatusCode == 200 {
var clusterInfo KibanaClusterInfo
err = json.NewDecoder(resp.Body).Decode(&clusterInfo)
if err != nil {
return resource.NonRetryableError(err)
}
if clusterInfo.Status == status {
log.Printf("[DEBUG] WaitForKibanaClusterStatus desired Kibana cluster status reached: %s\n", clusterInfo.Status)
return nil
}
log.Printf("[DEBUG] WaitForKibanaClusterStatus current Kibana cluster status: %s. Desired status: %s\n", clusterInfo.Status, status)
}
return resource.RetryableError(
fmt.Errorf("%q: timeout while waiting for the Kibana cluster to reach %s status", id, status))
})
}