@@ -21,7 +21,6 @@ import (
21
21
"github.com./elastic/terraform-provider-elasticstack/internal/models"
22
22
"github.com./elastic/terraform-provider-elasticstack/internal/utils"
23
23
"github.com./hashicorp/go-version"
24
- fwdiag "github.com./hashicorp/terraform-plugin-framework/diag"
25
24
"github.com./hashicorp/terraform-plugin-framework/types"
26
25
"github.com./hashicorp/terraform-plugin-log/tflog"
27
26
"github.com./hashicorp/terraform-plugin-sdk/v2/diag"
@@ -76,20 +75,6 @@ type ApiClient struct {
76
75
version string
77
76
}
78
77
79
- type ElasticsearchConnection struct {
80
- Username types.String `tfsdk:"username"`
81
- Password types.String `tfsdk:"password"`
82
- APIKey types.String `tfsdk:"api_key"`
83
- Endpoints types.List `tfsdk:"endpoints"`
84
- Insecure types.Bool `tfsdk:"insecure"`
85
- CAFile types.String `tfsdk:"ca_file"`
86
- CAData types.String `tfsdk:"ca_data"`
87
- CertFile types.String `tfsdk:"cert_file"`
88
- KeyFile types.String `tfsdk:"key_file"`
89
- CertData types.String `tfsdk:"cert_data"`
90
- KeyData types.String `tfsdk:"key_data"`
91
- }
92
-
93
78
func NewApiClientFunc (version string ) func (context.Context , * schema.ResourceData ) (interface {}, diag.Diagnostics ) {
94
79
return func (ctx context.Context , d * schema.ResourceData ) (interface {}, diag.Diagnostics ) {
95
80
return newApiClient (d , version )
@@ -360,108 +345,6 @@ func (a *ApiClient) ClusterID(ctx context.Context) (*string, diag.Diagnostics) {
360
345
return nil , diags
361
346
}
362
347
363
- func NewFWApiClient (ctx context.Context , esConn * ElasticsearchConnection , version string , useEnvAsDefault bool ) (* ApiClient , fwdiag.Diagnostics ) {
364
- var diags fwdiag.Diagnostics
365
- config := elasticsearch.Config {}
366
- config .Username = getStringValue (esConn .Username , "ELASTICSEARCH_USERNAME" , true )
367
- config .Password = getStringValue (esConn .Password , "ELASTICSEARCH_PASSWORD" , true )
368
- config .APIKey = getStringValue (esConn .APIKey , "ELASTICSEARCH_API_KEY" , true )
369
-
370
- var addrs []string
371
- diags .Append (esConn .Endpoints .ElementsAs (ctx , & addrs , true )... )
372
- if diags .HasError () {
373
- return nil , diags
374
- }
375
- if len (addrs ) == 0 && useEnvAsDefault {
376
- if endpoints := os .Getenv ("ELASTICSEARCH_ENDPOINTS" ); endpoints != "" {
377
- for _ , e := range strings .Split (endpoints , "," ) {
378
- addrs = append (addrs , strings .TrimSpace (e ))
379
- }
380
- }
381
- }
382
- config .Addresses = addrs
383
-
384
- envInsecure , _ := strconv .ParseBool (os .Getenv ("ELASTICSEARCH_INSECURE" ))
385
- if esConn .Insecure .ValueBool () || envInsecure {
386
- tlsClientConfig := ensureTLSClientConfig (& config )
387
- tlsClientConfig .InsecureSkipVerify = true
388
- }
389
-
390
- if esConn .CAFile .ValueString () != "" {
391
- caCert , err := os .ReadFile (esConn .CAFile .ValueString ())
392
- if err != nil {
393
- diags .Append (fwdiag .NewErrorDiagnostic (
394
- "Unable to read CA File" ,
395
- err .Error (),
396
- ))
397
- return nil , diags
398
- }
399
- config .CACert = caCert
400
- }
401
- if esConn .CAData .ValueString () != "" {
402
- config .CACert = []byte (esConn .CAData .ValueString ())
403
- }
404
-
405
- if certFile := esConn .CertFile .ValueString (); certFile != "" {
406
- if keyFile := esConn .KeyFile .ValueString (); keyFile != "" {
407
- cert , err := tls .LoadX509KeyPair (certFile , keyFile )
408
- if err != nil {
409
- diags .Append (fwdiag .NewErrorDiagnostic (
410
- "Unable to read certificate or key file" ,
411
- err .Error (),
412
- ))
413
- return nil , diags
414
- }
415
- tlsClientConfig := ensureTLSClientConfig (& config )
416
- tlsClientConfig .Certificates = []tls.Certificate {cert }
417
- } else {
418
- diags .Append (fwdiag .NewErrorDiagnostic (
419
- "Unable to read key file" ,
420
- "Path to key file has not been configured or is empty" ,
421
- ))
422
- return nil , diags
423
- }
424
- }
425
- if certData := esConn .CertData .ValueString (); certData != "" {
426
- if keyData := esConn .KeyData .ValueString (); keyData != "" {
427
- cert , err := tls .X509KeyPair ([]byte (certData ), []byte (keyData ))
428
- if err != nil {
429
- diags .Append (fwdiag .NewErrorDiagnostic (
430
- "Unable to parse certificate or key" ,
431
- err .Error (),
432
- ))
433
- return nil , diags
434
- }
435
- tlsClientConfig := ensureTLSClientConfig (& config )
436
- tlsClientConfig .Certificates = []tls.Certificate {cert }
437
- } else {
438
- diags .Append (fwdiag .NewErrorDiagnostic (
439
- "Unable to parse key" ,
440
- "Key data has not been configured or is empty" ,
441
- ))
442
- return nil , diags
443
- }
444
- }
445
-
446
- es , err := elasticsearch .NewClient (config )
447
- if err != nil {
448
- diags .Append (fwdiag .NewErrorDiagnostic (
449
- "Unable to create Elasticsearch client" ,
450
- err .Error (),
451
- ))
452
- return nil , diags
453
- }
454
- if logging .IsDebugOrHigher () {
455
- config .EnableDebugLogger = true
456
- config .Logger = & debugLogger {Name : "elasticsearch" }
457
- }
458
-
459
- return & ApiClient {
460
- elasticsearch : es ,
461
- version : version ,
462
- }, diags
463
- }
464
-
465
348
func getStringValue (s types.String , envKey string , useEnvAsDefault bool ) string {
466
349
if s .IsNull () {
467
350
if useEnvAsDefault {
0 commit comments