From 4140261cc764bc48e39b4fee2d586e94790aaf85 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Sat, 10 Sep 2022 17:35:09 +0900 Subject: [PATCH 1/4] Pass context to api calls --- internal/clients/api_client.go | 8 +- internal/clients/cluster.go | 33 +++---- internal/clients/index.go | 89 ++++++++++--------- internal/clients/security.go | 25 +++--- internal/elasticsearch/cluster/settings.go | 8 +- internal/elasticsearch/cluster/slm.go | 8 +- .../cluster/snapshot_repository.go | 12 +-- .../snapshot_repository_data_source.go | 8 +- .../elasticsearch/index/component_template.go | 8 +- internal/elasticsearch/index/data_stream.go | 8 +- internal/elasticsearch/index/ilm.go | 6 +- internal/elasticsearch/index/index.go | 18 ++-- internal/elasticsearch/index/template.go | 8 +- internal/elasticsearch/ingest/pipeline.go | 8 +- internal/elasticsearch/security/role.go | 8 +- internal/elasticsearch/security/user.go | 8 +- .../security/user_data_source.go | 4 +- 17 files changed, 135 insertions(+), 132 deletions(-) diff --git a/internal/clients/api_client.go b/internal/clients/api_client.go index 2f5e3c6a6..e5dc733e5 100644 --- a/internal/clients/api_client.go +++ b/internal/clients/api_client.go @@ -173,9 +173,9 @@ func (a *ApiClient) GetESClient() *elasticsearch.Client { return a.es } -func (a *ApiClient) ID(resourceId string) (*CompositeId, diag.Diagnostics) { +func (a *ApiClient) ID(ctx context.Context, resourceId string) (*CompositeId, diag.Diagnostics) { var diags diag.Diagnostics - clusterId, diags := a.ClusterID() + clusterId, diags := a.ClusterID(ctx) if diags.HasError() { return nil, diags } @@ -183,9 +183,9 @@ func (a *ApiClient) ID(resourceId string) (*CompositeId, diag.Diagnostics) { return &CompositeId{*clusterId, resourceId}, diags } -func (a *ApiClient) ClusterID() (*string, diag.Diagnostics) { +func (a *ApiClient) ClusterID(ctx context.Context) (*string, diag.Diagnostics) { var diags diag.Diagnostics - res, err := a.es.Info() + res, err := a.es.Info(a.es.Info.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } diff --git a/internal/clients/cluster.go b/internal/clients/cluster.go index c1a546f15..f3dc7057d 100644 --- a/internal/clients/cluster.go +++ b/internal/clients/cluster.go @@ -2,6 +2,7 @@ package clients import ( "bytes" + "context" "encoding/json" "fmt" "log" @@ -12,14 +13,14 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" ) -func (a *ApiClient) PutElasticsearchSnapshotRepository(repository *models.SnapshotRepository) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchSnapshotRepository(ctx context.Context, repository *models.SnapshotRepository) diag.Diagnostics { var diags diag.Diagnostics snapRepoBytes, err := json.Marshal(repository) if err != nil { return diag.FromErr(err) } log.Printf("[TRACE] sending snapshot repository definition to ES API: %s", snapRepoBytes) - res, err := a.es.Snapshot.CreateRepository(repository.Name, bytes.NewReader(snapRepoBytes)) + res, err := a.es.Snapshot.CreateRepository(repository.Name, bytes.NewReader(snapRepoBytes), a.es.Snapshot.CreateRepository.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -31,10 +32,10 @@ func (a *ApiClient) PutElasticsearchSnapshotRepository(repository *models.Snapsh return diags } -func (a *ApiClient) GetElasticsearchSnapshotRepository(name string) (*models.SnapshotRepository, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchSnapshotRepository(ctx context.Context, name string) (*models.SnapshotRepository, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Snapshot.GetRepository.WithRepository(name) - res, err := a.es.Snapshot.GetRepository(req) + res, err := a.es.Snapshot.GetRepository(req, a.es.Snapshot.GetRepository.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -68,9 +69,9 @@ func (a *ApiClient) GetElasticsearchSnapshotRepository(name string) (*models.Sna return nil, diags } -func (a *ApiClient) DeleteElasticsearchSnapshotRepository(name string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchSnapshotRepository(ctx context.Context, name string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Snapshot.DeleteRepository([]string{name}) + res, err := a.es.Snapshot.DeleteRepository([]string{name}, a.es.Snapshot.DeleteRepository.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -81,7 +82,7 @@ func (a *ApiClient) DeleteElasticsearchSnapshotRepository(name string) diag.Diag return diags } -func (a *ApiClient) PutElasticsearchSlm(slm *models.SnapshotPolicy) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchSlm(ctx context.Context, slm *models.SnapshotPolicy) diag.Diagnostics { var diags diag.Diagnostics slmBytes, err := json.Marshal(slm) @@ -90,7 +91,7 @@ func (a *ApiClient) PutElasticsearchSlm(slm *models.SnapshotPolicy) diag.Diagnos } log.Printf("[TRACE] sending SLM to ES API: %s", slmBytes) req := a.es.SlmPutLifecycle.WithBody(bytes.NewReader(slmBytes)) - res, err := a.es.SlmPutLifecycle(slm.Id, req) + res, err := a.es.SlmPutLifecycle(slm.Id, req, a.es.SlmPutLifecycle.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -102,10 +103,10 @@ func (a *ApiClient) PutElasticsearchSlm(slm *models.SnapshotPolicy) diag.Diagnos return diags } -func (a *ApiClient) GetElasticsearchSlm(slmName string) (*models.SnapshotPolicy, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchSlm(ctx context.Context, slmName string) (*models.SnapshotPolicy, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.SlmGetLifecycle.WithPolicyID(slmName) - res, err := a.es.SlmGetLifecycle(req) + res, err := a.es.SlmGetLifecycle(req, a.es.SlmGetLifecycle.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -134,9 +135,9 @@ func (a *ApiClient) GetElasticsearchSlm(slmName string) (*models.SnapshotPolicy, return nil, diags } -func (a *ApiClient) DeleteElasticsearchSlm(slmName string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchSlm(ctx context.Context, slmName string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.SlmDeleteLifecycle(slmName) + res, err := a.es.SlmDeleteLifecycle(slmName, a.es.SlmDeleteLifecycle.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -148,14 +149,14 @@ func (a *ApiClient) DeleteElasticsearchSlm(slmName string) diag.Diagnostics { return diags } -func (a *ApiClient) PutElasticsearchSettings(settings map[string]interface{}) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchSettings(ctx context.Context, settings map[string]interface{}) diag.Diagnostics { var diags diag.Diagnostics settingsBytes, err := json.Marshal(settings) if err != nil { diag.FromErr(err) } log.Printf("[TRACE] settings to set: %s", settingsBytes) - res, err := a.es.Cluster.PutSettings(bytes.NewReader(settingsBytes)) + res, err := a.es.Cluster.PutSettings(bytes.NewReader(settingsBytes), a.es.Cluster.PutSettings.WithContext(ctx)) if err != nil { diag.FromErr(err) } @@ -166,10 +167,10 @@ func (a *ApiClient) PutElasticsearchSettings(settings map[string]interface{}) di return diags } -func (a *ApiClient) GetElasticsearchSettings() (map[string]interface{}, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchSettings(ctx context.Context) (map[string]interface{}, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Cluster.GetSettings.WithFlatSettings(true) - res, err := a.es.Cluster.GetSettings(req) + res, err := a.es.Cluster.GetSettings(req, a.es.Cluster.GetSettings.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } diff --git a/internal/clients/index.go b/internal/clients/index.go index 15a42251d..5ffd791fb 100644 --- a/internal/clients/index.go +++ b/internal/clients/index.go @@ -2,6 +2,7 @@ package clients import ( "bytes" + "context" "encoding/json" "fmt" "log" @@ -13,7 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" ) -func (a *ApiClient) PutElasticsearchIlm(policy *models.Policy) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchIlm(ctx context.Context, policy *models.Policy) diag.Diagnostics { var diags diag.Diagnostics policyBytes, err := json.Marshal(map[string]interface{}{"policy": policy}) if err != nil { @@ -21,7 +22,7 @@ func (a *ApiClient) PutElasticsearchIlm(policy *models.Policy) diag.Diagnostics } log.Printf("[TRACE] sending new ILM policy to ES API: %s", policyBytes) req := a.es.ILM.PutLifecycle.WithBody(bytes.NewReader(policyBytes)) - res, err := a.es.ILM.PutLifecycle(policy.Name, req) + res, err := a.es.ILM.PutLifecycle(policy.Name, req, a.es.ILM.PutLifecycle.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -32,10 +33,10 @@ func (a *ApiClient) PutElasticsearchIlm(policy *models.Policy) diag.Diagnostics return diags } -func (a *ApiClient) GetElasticsearchIlm(policyName string) (*models.PolicyDefinition, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchIlm(ctx context.Context, policyName string) (*models.PolicyDefinition, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.ILM.GetLifecycle.WithPolicy(policyName) - res, err := a.es.ILM.GetLifecycle(req) + res, err := a.es.ILM.GetLifecycle(req, a.es.ILM.GetLifecycle.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -65,10 +66,10 @@ func (a *ApiClient) GetElasticsearchIlm(policyName string) (*models.PolicyDefini return nil, diags } -func (a *ApiClient) DeleteElasticsearchIlm(policyName string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchIlm(ctx context.Context, policyName string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.ILM.DeleteLifecycle(policyName) + res, err := a.es.ILM.DeleteLifecycle(policyName, a.es.ILM.DeleteLifecycle.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -79,7 +80,7 @@ func (a *ApiClient) DeleteElasticsearchIlm(policyName string) diag.Diagnostics { return diags } -func (a *ApiClient) PutElasticsearchComponentTemplate(template *models.ComponentTemplate) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchComponentTemplate(ctx context.Context, template *models.ComponentTemplate) diag.Diagnostics { var diags diag.Diagnostics templateBytes, err := json.Marshal(template) if err != nil { @@ -87,7 +88,7 @@ func (a *ApiClient) PutElasticsearchComponentTemplate(template *models.Component } log.Printf("[TRACE] sending request to ES: %s to create component template '%s' ", templateBytes, template.Name) - res, err := a.es.Cluster.PutComponentTemplate(template.Name, bytes.NewReader(templateBytes)) + res, err := a.es.Cluster.PutComponentTemplate(template.Name, bytes.NewReader(templateBytes), a.es.Cluster.PutComponentTemplate.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -99,10 +100,10 @@ func (a *ApiClient) PutElasticsearchComponentTemplate(template *models.Component return diags } -func (a *ApiClient) GetElasticsearchComponentTemplate(templateName string) (*models.ComponentTemplateResponse, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchComponentTemplate(ctx context.Context, templateName string) (*models.ComponentTemplateResponse, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Cluster.GetComponentTemplate.WithName(templateName) - res, err := a.es.Cluster.GetComponentTemplate(req) + res, err := a.es.Cluster.GetComponentTemplate(req, a.es.Cluster.GetComponentTemplate.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -132,9 +133,9 @@ func (a *ApiClient) GetElasticsearchComponentTemplate(templateName string) (*mod return &tpl, diags } -func (a *ApiClient) DeleteElasticsearchComponentTemplate(templateName string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchComponentTemplate(ctx context.Context, templateName string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Cluster.DeleteComponentTemplate(templateName) + res, err := a.es.Cluster.DeleteComponentTemplate(templateName, a.es.Cluster.DeleteComponentTemplate.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -145,7 +146,7 @@ func (a *ApiClient) DeleteElasticsearchComponentTemplate(templateName string) di return diags } -func (a *ApiClient) PutElasticsearchIndexTemplate(template *models.IndexTemplate) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchIndexTemplate(ctx context.Context, template *models.IndexTemplate) diag.Diagnostics { var diags diag.Diagnostics templateBytes, err := json.Marshal(template) if err != nil { @@ -153,7 +154,7 @@ func (a *ApiClient) PutElasticsearchIndexTemplate(template *models.IndexTemplate } log.Printf("[TRACE] sending request to ES: %s to create template '%s' ", templateBytes, template.Name) - res, err := a.es.Indices.PutIndexTemplate(template.Name, bytes.NewReader(templateBytes)) + res, err := a.es.Indices.PutIndexTemplate(template.Name, bytes.NewReader(templateBytes), a.es.Indices.PutIndexTemplate.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -165,10 +166,10 @@ func (a *ApiClient) PutElasticsearchIndexTemplate(template *models.IndexTemplate return diags } -func (a *ApiClient) GetElasticsearchIndexTemplate(templateName string) (*models.IndexTemplateResponse, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchIndexTemplate(ctx context.Context, templateName string) (*models.IndexTemplateResponse, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Indices.GetIndexTemplate.WithName(templateName) - res, err := a.es.Indices.GetIndexTemplate(req) + res, err := a.es.Indices.GetIndexTemplate(req, a.es.Indices.GetIndexTemplate.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -199,9 +200,9 @@ func (a *ApiClient) GetElasticsearchIndexTemplate(templateName string) (*models. return &tpl, diags } -func (a *ApiClient) DeleteElasticsearchIndexTemplate(templateName string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchIndexTemplate(ctx context.Context, templateName string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Indices.DeleteIndexTemplate(templateName) + res, err := a.es.Indices.DeleteIndexTemplate(templateName, a.es.Indices.DeleteIndexTemplate.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -212,7 +213,7 @@ func (a *ApiClient) DeleteElasticsearchIndexTemplate(templateName string) diag.D return diags } -func (a *ApiClient) PutElasticsearchIndex(index *models.Index) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchIndex(ctx context.Context, index *models.Index) diag.Diagnostics { var diags diag.Diagnostics indexBytes, err := json.Marshal(index) if err != nil { @@ -221,7 +222,7 @@ func (a *ApiClient) PutElasticsearchIndex(index *models.Index) diag.Diagnostics log.Printf("[TRACE] index definition: %s", indexBytes) req := a.es.Indices.Create.WithBody(bytes.NewReader(indexBytes)) - res, err := a.es.Indices.Create(index.Name, req) + res, err := a.es.Indices.Create(index.Name, req, a.es.Indices.Create.WithContext(ctx)) if err != nil { diag.FromErr(err) } @@ -232,10 +233,10 @@ func (a *ApiClient) PutElasticsearchIndex(index *models.Index) diag.Diagnostics return diags } -func (a *ApiClient) DeleteElasticsearchIndex(name string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchIndex(ctx context.Context, name string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Indices.Delete([]string{name}) + res, err := a.es.Indices.Delete([]string{name}, a.es.Indices.Delete.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -247,11 +248,11 @@ func (a *ApiClient) DeleteElasticsearchIndex(name string) diag.Diagnostics { return diags } -func (a *ApiClient) GetElasticsearchIndex(name string) (*models.Index, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchIndex(ctx context.Context, name string) (*models.Index, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Indices.Get.WithFlatSettings(true) - res, err := a.es.Indices.Get([]string{name}, req) + res, err := a.es.Indices.Get([]string{name}, req, a.es.Indices.Get.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -273,10 +274,10 @@ func (a *ApiClient) GetElasticsearchIndex(name string) (*models.Index, diag.Diag return &index, diags } -func (a *ApiClient) DeleteElasticsearchIndexAlias(index string, aliases []string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchIndexAlias(ctx context.Context, index string, aliases []string) diag.Diagnostics { var diags diag.Diagnostics log.Printf("[TRACE] Deleting aliases for index %s: %v", index, aliases) - res, err := a.es.Indices.DeleteAlias([]string{index}, aliases) + res, err := a.es.Indices.DeleteAlias([]string{index}, aliases, a.es.Indices.DeleteAlias.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -287,7 +288,7 @@ func (a *ApiClient) DeleteElasticsearchIndexAlias(index string, aliases []string return diags } -func (a *ApiClient) UpdateElasticsearchIndexAlias(index string, alias *models.IndexAlias) diag.Diagnostics { +func (a *ApiClient) UpdateElasticsearchIndexAlias(ctx context.Context, index string, alias *models.IndexAlias) diag.Diagnostics { var diags diag.Diagnostics aliasBytes, err := json.Marshal(alias) if err != nil { @@ -295,7 +296,7 @@ func (a *ApiClient) UpdateElasticsearchIndexAlias(index string, alias *models.In } log.Printf("[TRACE] updaing index %s alias: %s", index, aliasBytes) req := a.es.Indices.PutAlias.WithBody(bytes.NewReader(aliasBytes)) - res, err := a.es.Indices.PutAlias([]string{index}, alias.Name, req) + res, err := a.es.Indices.PutAlias([]string{index}, alias.Name, req, a.es.Indices.PutAlias.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -306,7 +307,7 @@ func (a *ApiClient) UpdateElasticsearchIndexAlias(index string, alias *models.In return diags } -func (a *ApiClient) UpdateElasticsearchIndexSettings(index string, settings map[string]interface{}) diag.Diagnostics { +func (a *ApiClient) UpdateElasticsearchIndexSettings(ctx context.Context, index string, settings map[string]interface{}) diag.Diagnostics { var diags diag.Diagnostics settingsBytes, err := json.Marshal(settings) if err != nil { @@ -314,7 +315,7 @@ func (a *ApiClient) UpdateElasticsearchIndexSettings(index string, settings map[ } log.Printf("[TRACE] updaing index %s settings: %s", index, settingsBytes) req := a.es.Indices.PutSettings.WithIndex(index) - res, err := a.es.Indices.PutSettings(bytes.NewReader(settingsBytes), req) + res, err := a.es.Indices.PutSettings(bytes.NewReader(settingsBytes), req, a.es.Indices.PutSettings.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -325,11 +326,11 @@ func (a *ApiClient) UpdateElasticsearchIndexSettings(index string, settings map[ return diags } -func (a *ApiClient) UpdateElasticsearchIndexMappings(index, mappings string) diag.Diagnostics { +func (a *ApiClient) UpdateElasticsearchIndexMappings(ctx context.Context, index, mappings string) diag.Diagnostics { var diags diag.Diagnostics log.Printf("[TRACE] updaing index %s mappings: %s", index, mappings) req := a.es.Indices.PutMapping.WithIndex(index) - res, err := a.es.Indices.PutMapping(strings.NewReader(mappings), req) + res, err := a.es.Indices.PutMapping(strings.NewReader(mappings), req, a.es.Indices.PutMapping.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -340,10 +341,10 @@ func (a *ApiClient) UpdateElasticsearchIndexMappings(index, mappings string) dia return diags } -func (a *ApiClient) PutElasticsearchDataStream(dataStreamName string) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchDataStream(ctx context.Context, dataStreamName string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Indices.CreateDataStream(dataStreamName) + res, err := a.es.Indices.CreateDataStream(dataStreamName, a.es.Indices.CreateDataStream.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -355,10 +356,10 @@ func (a *ApiClient) PutElasticsearchDataStream(dataStreamName string) diag.Diagn return diags } -func (a *ApiClient) GetElasticsearchDataStream(dataStreamName string) (*models.DataStream, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchDataStream(ctx context.Context, dataStreamName string) (*models.DataStream, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Indices.GetDataStream.WithName(dataStreamName) - res, err := a.es.Indices.GetDataStream(req) + res, err := a.es.Indices.GetDataStream(req, a.es.Indices.GetDataStream.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -380,10 +381,10 @@ func (a *ApiClient) GetElasticsearchDataStream(dataStreamName string) (*models.D return &ds, diags } -func (a *ApiClient) DeleteElasticsearchDataStream(dataStreamName string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchDataStream(ctx context.Context, dataStreamName string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Indices.DeleteDataStream([]string{dataStreamName}) + res, err := a.es.Indices.DeleteDataStream([]string{dataStreamName}, a.es.Indices.DeleteDataStream.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -395,7 +396,7 @@ func (a *ApiClient) DeleteElasticsearchDataStream(dataStreamName string) diag.Di return diags } -func (a *ApiClient) PutElasticsearchIngestPipeline(pipeline *models.IngestPipeline) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchIngestPipeline(ctx context.Context, pipeline *models.IngestPipeline) diag.Diagnostics { var diags diag.Diagnostics pipelineBytes, err := json.Marshal(pipeline) if err != nil { @@ -403,7 +404,7 @@ func (a *ApiClient) PutElasticsearchIngestPipeline(pipeline *models.IngestPipeli } log.Printf("[TRACE] creating ingest pipeline %s: %s", pipeline.Name, pipelineBytes) - res, err := a.es.Ingest.PutPipeline(pipeline.Name, bytes.NewReader(pipelineBytes)) + res, err := a.es.Ingest.PutPipeline(pipeline.Name, bytes.NewReader(pipelineBytes), a.es.Ingest.PutPipeline.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -415,10 +416,10 @@ func (a *ApiClient) PutElasticsearchIngestPipeline(pipeline *models.IngestPipeli return diags } -func (a *ApiClient) GetElasticsearchIngestPipeline(name *string) (*models.IngestPipeline, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchIngestPipeline(ctx context.Context, name *string) (*models.IngestPipeline, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Ingest.GetPipeline.WithPipelineID(*name) - res, err := a.es.Ingest.GetPipeline(req) + res, err := a.es.Ingest.GetPipeline(req, a.es.Ingest.GetPipeline.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -441,10 +442,10 @@ func (a *ApiClient) GetElasticsearchIngestPipeline(name *string) (*models.Ingest return &pipeline, diags } -func (a *ApiClient) DeleteElasticsearchIngestPipeline(name *string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchIngestPipeline(ctx context.Context, name *string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Ingest.DeletePipeline(*name) + res, err := a.es.Ingest.DeletePipeline(*name, a.es.Ingest.DeletePipeline.WithContext(ctx)) if err != nil { return diags } diff --git a/internal/clients/security.go b/internal/clients/security.go index 1ef608839..01dcca802 100644 --- a/internal/clients/security.go +++ b/internal/clients/security.go @@ -2,6 +2,7 @@ package clients import ( "bytes" + "context" "encoding/json" "fmt" "log" @@ -12,14 +13,14 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" ) -func (a *ApiClient) PutElasticsearchUser(user *models.User) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchUser(ctx context.Context, user *models.User) diag.Diagnostics { var diags diag.Diagnostics userBytes, err := json.Marshal(user) if err != nil { return diag.FromErr(err) } log.Printf("[TRACE] sending request to ES: %s", userBytes) - res, err := a.es.Security.PutUser(user.Username, bytes.NewReader(userBytes)) + res, err := a.es.Security.PutUser(user.Username, bytes.NewReader(userBytes), a.es.Security.PutUser.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -30,10 +31,10 @@ func (a *ApiClient) PutElasticsearchUser(user *models.User) diag.Diagnostics { return diags } -func (a *ApiClient) GetElasticsearchUser(username string) (*models.User, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchUser(ctx context.Context, username string) (*models.User, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Security.GetUser.WithUsername(username) - res, err := a.es.Security.GetUser(req) + res, err := a.es.Security.GetUser(req, a.es.Security.GetUser.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -64,9 +65,9 @@ func (a *ApiClient) GetElasticsearchUser(username string) (*models.User, diag.Di return nil, diags } -func (a *ApiClient) DeleteElasticsearchUser(username string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchUser(ctx context.Context, username string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Security.DeleteUser(username) + res, err := a.es.Security.DeleteUser(username, a.es.Security.DeleteUser.WithContext(ctx)) if err != nil && res.IsError() { return diag.FromErr(err) } @@ -77,7 +78,7 @@ func (a *ApiClient) DeleteElasticsearchUser(username string) diag.Diagnostics { return diags } -func (a *ApiClient) PutElasticsearchRole(role *models.Role) diag.Diagnostics { +func (a *ApiClient) PutElasticsearchRole(ctx context.Context, role *models.Role) diag.Diagnostics { var diags diag.Diagnostics roleBytes, err := json.Marshal(role) @@ -85,7 +86,7 @@ func (a *ApiClient) PutElasticsearchRole(role *models.Role) diag.Diagnostics { return diag.FromErr(err) } log.Printf("[TRACE] sending request to ES: %s", roleBytes) - res, err := a.es.Security.PutRole(role.Name, bytes.NewReader(roleBytes)) + res, err := a.es.Security.PutRole(role.Name, bytes.NewReader(roleBytes), a.es.Security.PutRole.WithContext(ctx)) if err != nil { return diag.FromErr(err) } @@ -97,11 +98,11 @@ func (a *ApiClient) PutElasticsearchRole(role *models.Role) diag.Diagnostics { return diags } -func (a *ApiClient) GetElasticsearchRole(rolename string) (*models.Role, diag.Diagnostics) { +func (a *ApiClient) GetElasticsearchRole(ctx context.Context, rolename string) (*models.Role, diag.Diagnostics) { var diags diag.Diagnostics req := a.es.Security.GetRole.WithName(rolename) - res, err := a.es.Security.GetRole(req) + res, err := a.es.Security.GetRole(req, a.es.Security.GetRole.WithContext(ctx)) if err != nil { return nil, diag.FromErr(err) } @@ -128,9 +129,9 @@ func (a *ApiClient) GetElasticsearchRole(rolename string) (*models.Role, diag.Di return nil, diags } -func (a *ApiClient) DeleteElasticsearchRole(rolename string) diag.Diagnostics { +func (a *ApiClient) DeleteElasticsearchRole(ctx context.Context, rolename string) diag.Diagnostics { var diags diag.Diagnostics - res, err := a.es.Security.DeleteRole(rolename) + res, err := a.es.Security.DeleteRole(rolename, a.es.Security.DeleteRole.WithContext(ctx)) if err != nil { return diag.FromErr(err) } diff --git a/internal/elasticsearch/cluster/settings.go b/internal/elasticsearch/cluster/settings.go index 6821689b3..8524f15e6 100644 --- a/internal/elasticsearch/cluster/settings.go +++ b/internal/elasticsearch/cluster/settings.go @@ -90,7 +90,7 @@ func resourceClusterSettingsPut(ctx context.Context, d *schema.ResourceData, met if err != nil { return diag.FromErr(err) } - id, diags := client.ID("cluster-settings") + id, diags := client.ID(ctx, "cluster-settings") if diags.HasError() { return diags } @@ -108,7 +108,7 @@ func resourceClusterSettingsPut(ctx context.Context, d *schema.ResourceData, met } } } - if diags := client.PutElasticsearchSettings(settings); diags.HasError() { + if diags := client.PutElasticsearchSettings(ctx, settings); diags.HasError() { return diags } d.SetId(id.String()) @@ -209,7 +209,7 @@ func resourceClusterSettingsRead(ctx context.Context, d *schema.ResourceData, me if err != nil { return diag.FromErr(err) } - clusterSettings, diags := client.GetElasticsearchSettings() + clusterSettings, diags := client.GetElasticsearchSettings(ctx) if diags.HasError() { return diags } @@ -283,7 +283,7 @@ func resourceClusterSettingsDelete(ctx context.Context, d *schema.ResourceData, "persistent": pSettings, "transient": tSettings, } - if diags := client.PutElasticsearchSettings(settings); diags.HasError() { + if diags := client.PutElasticsearchSettings(ctx, settings); diags.HasError() { return diags } diff --git a/internal/elasticsearch/cluster/slm.go b/internal/elasticsearch/cluster/slm.go index 246fa8989..2ca8aabc3 100644 --- a/internal/elasticsearch/cluster/slm.go +++ b/internal/elasticsearch/cluster/slm.go @@ -159,7 +159,7 @@ func resourceSlmPut(ctx context.Context, d *schema.ResourceData, meta interface{ return diag.FromErr(err) } slmId := d.Get("name").(string) - id, diags := client.ID(slmId) + id, diags := client.ID(ctx, slmId) if diags.HasError() { return diags } @@ -230,7 +230,7 @@ func resourceSlmPut(ctx context.Context, d *schema.ResourceData, meta interface{ slm.Config = &slmConfig - if diags := client.PutElasticsearchSlm(&slm); diags.HasError() { + if diags := client.PutElasticsearchSlm(ctx, &slm); diags.HasError() { return diags } d.SetId(id.String()) @@ -248,7 +248,7 @@ func resourceSlmRead(ctx context.Context, d *schema.ResourceData, meta interface return diags } - slm, diags := client.GetElasticsearchSlm(id.ResourceId) + slm, diags := client.GetElasticsearchSlm(ctx, id.ResourceId) if slm == nil && diags == nil { d.SetId("") return diags @@ -336,7 +336,7 @@ func resourceSlmDelete(ctx context.Context, d *schema.ResourceData, meta interfa if diags.HasError() { return diags } - if diags := client.DeleteElasticsearchSlm(id.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchSlm(ctx, id.ResourceId); diags.HasError() { return diags } d.SetId("") diff --git a/internal/elasticsearch/cluster/snapshot_repository.go b/internal/elasticsearch/cluster/snapshot_repository.go index 13a4d12c4..c504598c9 100644 --- a/internal/elasticsearch/cluster/snapshot_repository.go +++ b/internal/elasticsearch/cluster/snapshot_repository.go @@ -57,7 +57,7 @@ func ResourceSnapshotRepository() *schema.Resource { }, } - //-- repos specific settings + // -- repos specific settings fsSettings := map[string]*schema.Schema{ "location": { @@ -201,7 +201,7 @@ func ResourceSnapshotRepository() *schema.Resource { }, } - //-- + // -- snapRepoSchema := map[string]*schema.Schema{ "id": { @@ -320,7 +320,7 @@ func resourceSnapRepoPut(ctx context.Context, d *schema.ResourceData, meta inter return diag.FromErr(err) } repoId := d.Get("name").(string) - id, diags := client.ID(repoId) + id, diags := client.ID(ctx, repoId) if diags.HasError() { return diags } @@ -344,7 +344,7 @@ func resourceSnapRepoPut(ctx context.Context, d *schema.ResourceData, meta inter } snapRepo.Settings = snapRepoSettings - if diags := client.PutElasticsearchSnapshotRepository(&snapRepo); diags.HasError() { + if diags := client.PutElasticsearchSnapshotRepository(ctx, &snapRepo); diags.HasError() { return diags } d.SetId(id.String()) @@ -372,7 +372,7 @@ func resourceSnapRepoRead(ctx context.Context, d *schema.ResourceData, meta inte return diags } - currentRepo, diags := client.GetElasticsearchSnapshotRepository(compId.ResourceId) + currentRepo, diags := client.GetElasticsearchSnapshotRepository(ctx, compId.ResourceId) if currentRepo == nil && diags == nil { d.SetId("") return diags @@ -451,7 +451,7 @@ func resourceSnapRepoDelete(ctx context.Context, d *schema.ResourceData, meta in return diags } - if diags := client.DeleteElasticsearchSnapshotRepository(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchSnapshotRepository(ctx, compId.ResourceId); diags.HasError() { return diags } d.SetId("") diff --git a/internal/elasticsearch/cluster/snapshot_repository_data_source.go b/internal/elasticsearch/cluster/snapshot_repository_data_source.go index 667078dff..ab91150cd 100644 --- a/internal/elasticsearch/cluster/snapshot_repository_data_source.go +++ b/internal/elasticsearch/cluster/snapshot_repository_data_source.go @@ -47,7 +47,7 @@ func DataSourceSnapshotRespository() *schema.Resource { }, } - //-- repos specific settings + // -- repos specific settings fsSettings := map[string]*schema.Schema{ "location": { @@ -172,7 +172,7 @@ func DataSourceSnapshotRespository() *schema.Resource { }, } - //-- + // -- snapRepoSchema := map[string]*schema.Schema{ "id": { @@ -258,11 +258,11 @@ func dataSourceSnapRepoRead(ctx context.Context, d *schema.ResourceData, meta in return diag.FromErr(err) } repoName := d.Get("name").(string) - id, diags := client.ID(repoName) + id, diags := client.ID(ctx, repoName) if diags.HasError() { return diags } - currentRepo, diags := client.GetElasticsearchSnapshotRepository(repoName) + currentRepo, diags := client.GetElasticsearchSnapshotRepository(ctx, repoName) if diags.HasError() { return diags } diff --git a/internal/elasticsearch/index/component_template.go b/internal/elasticsearch/index/component_template.go index dbb34b906..022796015 100644 --- a/internal/elasticsearch/index/component_template.go +++ b/internal/elasticsearch/index/component_template.go @@ -137,7 +137,7 @@ func resourceComponentTemplatePut(ctx context.Context, d *schema.ResourceData, m return diag.FromErr(err) } componentId := d.Get("name").(string) - id, diags := client.ID(componentId) + id, diags := client.ID(ctx, componentId) if diags.HasError() { return diags } @@ -216,7 +216,7 @@ func resourceComponentTemplatePut(ctx context.Context, d *schema.ResourceData, m componentTemplate.Version = &definedVer } - if diags := client.PutElasticsearchComponentTemplate(&componentTemplate); diags.HasError() { + if diags := client.PutElasticsearchComponentTemplate(ctx, &componentTemplate); diags.HasError() { return diags } @@ -236,7 +236,7 @@ func resourceComponentTemplateRead(ctx context.Context, d *schema.ResourceData, } templateId := compId.ResourceId - tpl, diags := client.GetElasticsearchComponentTemplate(templateId) + tpl, diags := client.GetElasticsearchComponentTemplate(ctx, templateId) if tpl == nil && diags == nil { d.SetId("") return diags @@ -290,7 +290,7 @@ func resourceComponentTemplateDelete(ctx context.Context, d *schema.ResourceData if diags.HasError() { return diags } - if diags := client.DeleteElasticsearchComponentTemplate(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchComponentTemplate(ctx, compId.ResourceId); diags.HasError() { return diags } d.SetId("") diff --git a/internal/elasticsearch/index/data_stream.go b/internal/elasticsearch/index/data_stream.go index 5727c9ce0..1acc94849 100644 --- a/internal/elasticsearch/index/data_stream.go +++ b/internal/elasticsearch/index/data_stream.go @@ -122,12 +122,12 @@ func resourceDataStreamPut(ctx context.Context, d *schema.ResourceData, meta int return diag.FromErr(err) } dsId := d.Get("name").(string) - id, diags := client.ID(dsId) + id, diags := client.ID(ctx, dsId) if diags.HasError() { return diags } - if diags := client.PutElasticsearchDataStream(dsId); diags.HasError() { + if diags := client.PutElasticsearchDataStream(ctx, dsId); diags.HasError() { return diags } @@ -147,7 +147,7 @@ func resourceDataStreamRead(ctx context.Context, d *schema.ResourceData, meta in return diags } - ds, diags := client.GetElasticsearchDataStream(compId.ResourceId) + ds, diags := client.GetElasticsearchDataStream(ctx, compId.ResourceId) if ds == nil && diags == nil { // no data stream found on ES side d.SetId("") @@ -220,7 +220,7 @@ func resourceDataStreamDelete(ctx context.Context, d *schema.ResourceData, meta if diags.HasError() { return diags } - if diags := client.DeleteElasticsearchDataStream(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchDataStream(ctx, compId.ResourceId); diags.HasError() { return diags } diff --git a/internal/elasticsearch/index/ilm.go b/internal/elasticsearch/index/ilm.go index 07d74d365..c71fb9c34 100644 --- a/internal/elasticsearch/index/ilm.go +++ b/internal/elasticsearch/index/ilm.go @@ -381,7 +381,7 @@ func resourceIlmPut(ctx context.Context, d *schema.ResourceData, meta interface{ return diag.FromErr(err) } ilmId := d.Get("name").(string) - id, diags := client.ID(ilmId) + id, diags := client.ID(ctx, ilmId) if diags.HasError() { return diags } @@ -392,7 +392,7 @@ func resourceIlmPut(ctx context.Context, d *schema.ResourceData, meta interface{ } policy.Name = ilmId - if diags := client.PutElasticsearchIlm(policy); diags.HasError() { + if diags := client.PutElasticsearchIlm(ctx, policy); diags.HasError() { return diags } @@ -538,7 +538,7 @@ func resourceIlmRead(ctx context.Context, d *schema.ResourceData, meta interface } policyId := compId.ResourceId - ilmDef, diags := client.GetElasticsearchIlm(policyId) + ilmDef, diags := client.GetElasticsearchIlm(ctx, policyId) if ilmDef == nil && diags == nil { d.SetId("") return diags diff --git a/internal/elasticsearch/index/index.go b/internal/elasticsearch/index/index.go index 19e7772c5..e4a9b0f28 100644 --- a/internal/elasticsearch/index/index.go +++ b/internal/elasticsearch/index/index.go @@ -172,7 +172,7 @@ If specified, this mapping can include: field names, [field data types](https:// return nil, fmt.Errorf("Failed to parse provided ID") } indexName := compId.ResourceId - index, diags := client.GetElasticsearchIndex(indexName) + index, diags := client.GetElasticsearchIndex(ctx, indexName) if diags.HasError() { return nil, fmt.Errorf("Failed to get an ES Index") } @@ -271,7 +271,7 @@ func resourceIndexCreate(ctx context.Context, d *schema.ResourceData, meta inter return diag.FromErr(err) } indexName := d.Get("name").(string) - id, diags := client.ID(indexName) + id, diags := client.ID(ctx, indexName) if diags.HasError() { return diags } @@ -308,7 +308,7 @@ func resourceIndexCreate(ctx context.Context, d *schema.ResourceData, meta inter index.Settings = sets } - if diags := client.PutElasticsearchIndex(&index); diags.HasError() { + if diags := client.PutElasticsearchIndex(ctx, &index); diags.HasError() { return diags } @@ -345,14 +345,14 @@ func resourceIndexUpdate(ctx context.Context, d *schema.ResourceData, meta inter } } if len(aliasesToDelete) > 0 { - if diags := client.DeleteElasticsearchIndexAlias(indexName, aliasesToDelete); diags.HasError() { + if diags := client.DeleteElasticsearchIndexAlias(ctx, indexName, aliasesToDelete); diags.HasError() { return diags } } // keep new aliases up-to-date for _, v := range enew { - if diags := client.UpdateElasticsearchIndexAlias(indexName, &v); diags.HasError() { + if diags := client.UpdateElasticsearchIndexAlias(ctx, indexName, &v); diags.HasError() { return diags } } @@ -376,7 +376,7 @@ func resourceIndexUpdate(ctx context.Context, d *schema.ResourceData, meta inter } } log.Printf("[TRACE] settings to update: %+v", ns) - if diags := client.UpdateElasticsearchIndexSettings(indexName, ns); diags.HasError() { + if diags := client.UpdateElasticsearchIndexSettings(ctx, indexName, ns); diags.HasError() { return diags } } @@ -385,7 +385,7 @@ func resourceIndexUpdate(ctx context.Context, d *schema.ResourceData, meta inter if d.HasChange("mappings") { // at this point we know there are mappings defined and there is a change which we can apply mappings := d.Get("mappings").(string) - if diags := client.UpdateElasticsearchIndexMappings(indexName, mappings); diags.HasError() { + if diags := client.UpdateElasticsearchIndexMappings(ctx, indexName, mappings); diags.HasError() { return diags } } @@ -421,7 +421,7 @@ func resourceIndexRead(ctx context.Context, d *schema.ResourceData, meta interfa return diag.FromErr(err) } - index, diags := client.GetElasticsearchIndex(indexName) + index, diags := client.GetElasticsearchIndex(ctx, indexName) if index == nil && diags == nil { // no index found on ES side d.SetId("") @@ -473,7 +473,7 @@ func resourceIndexDelete(ctx context.Context, d *schema.ResourceData, meta inter if diags.HasError() { return diags } - if diags := client.DeleteElasticsearchIndex(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchIndex(ctx, compId.ResourceId); diags.HasError() { return diags } d.SetId("") diff --git a/internal/elasticsearch/index/template.go b/internal/elasticsearch/index/template.go index 0fe91a37d..8b53a1229 100644 --- a/internal/elasticsearch/index/template.go +++ b/internal/elasticsearch/index/template.go @@ -185,7 +185,7 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta return diag.FromErr(err) } templateId := d.Get("name").(string) - id, diags := client.ID(templateId) + id, diags := client.ID(ctx, templateId) if diags.HasError() { return diags } @@ -293,7 +293,7 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta indexTemplate.Version = &definedVer } - if diags := client.PutElasticsearchIndexTemplate(&indexTemplate); diags.HasError() { + if diags := client.PutElasticsearchIndexTemplate(ctx, &indexTemplate); diags.HasError() { return diags } @@ -313,7 +313,7 @@ func resourceIndexTemplateRead(ctx context.Context, d *schema.ResourceData, meta } templateId := compId.ResourceId - tpl, diags := client.GetElasticsearchIndexTemplate(templateId) + tpl, diags := client.GetElasticsearchIndexTemplate(ctx, templateId) if tpl == nil && diags == nil { d.SetId("") return diags @@ -417,7 +417,7 @@ func resourceIndexTemplateDelete(ctx context.Context, d *schema.ResourceData, me if diags.HasError() { return diags } - if diags := client.DeleteElasticsearchIndexTemplate(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchIndexTemplate(ctx, compId.ResourceId); diags.HasError() { return diags } d.SetId("") diff --git a/internal/elasticsearch/ingest/pipeline.go b/internal/elasticsearch/ingest/pipeline.go index cdf37e21d..7701e7de5 100644 --- a/internal/elasticsearch/ingest/pipeline.go +++ b/internal/elasticsearch/ingest/pipeline.go @@ -86,7 +86,7 @@ func resourceIngestPipelineTemplatePut(ctx context.Context, d *schema.ResourceDa return diag.FromErr(err) } pipelineId := d.Get("name").(string) - id, diags := client.ID(pipelineId) + id, diags := client.ID(ctx, pipelineId) if diags.HasError() { return diags } @@ -126,7 +126,7 @@ func resourceIngestPipelineTemplatePut(ctx context.Context, d *schema.ResourceDa pipeline.Metadata = metadata } - if diags := client.PutElasticsearchIngestPipeline(&pipeline); diags.HasError() { + if diags := client.PutElasticsearchIngestPipeline(ctx, &pipeline); diags.HasError() { return diags } @@ -146,7 +146,7 @@ func resourceIngestPipelineTemplateRead(ctx context.Context, d *schema.ResourceD return diags } - pipeline, diags := client.GetElasticsearchIngestPipeline(&compId.ResourceId) + pipeline, diags := client.GetElasticsearchIngestPipeline(ctx, &compId.ResourceId) if pipeline == nil && diags == nil { d.SetId("") return diags @@ -214,7 +214,7 @@ func resourceIngestPipelineTemplateDelete(ctx context.Context, d *schema.Resourc return diags } - if diags := client.DeleteElasticsearchIngestPipeline(&compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchIngestPipeline(ctx, &compId.ResourceId); diags.HasError() { return diags } diff --git a/internal/elasticsearch/security/role.go b/internal/elasticsearch/security/role.go index e634be592..2f48c40bc 100644 --- a/internal/elasticsearch/security/role.go +++ b/internal/elasticsearch/security/role.go @@ -176,7 +176,7 @@ func resourceSecurityRolePut(ctx context.Context, d *schema.ResourceData, meta i return diag.FromErr(err) } roleId := d.Get("name").(string) - id, diags := client.ID(roleId) + id, diags := client.ID(ctx, roleId) if diags.HasError() { return diags } @@ -300,7 +300,7 @@ func resourceSecurityRolePut(ctx context.Context, d *schema.ResourceData, meta i role.RusAs = runs } - if diags := client.PutElasticsearchRole(&role); diags.HasError() { + if diags := client.PutElasticsearchRole(ctx, &role); diags.HasError() { return diags } @@ -320,7 +320,7 @@ func resourceSecurityRoleRead(ctx context.Context, d *schema.ResourceData, meta } roleId := compId.ResourceId - role, diags := client.GetElasticsearchRole(roleId) + role, diags := client.GetElasticsearchRole(ctx, roleId) if role == nil && diags == nil { d.SetId("") return diags @@ -427,7 +427,7 @@ func resourceSecurityRoleDelete(ctx context.Context, d *schema.ResourceData, met return diags } - if diags := client.DeleteElasticsearchRole(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchRole(ctx, compId.ResourceId); diags.HasError() { return diags } diff --git a/internal/elasticsearch/security/user.go b/internal/elasticsearch/security/user.go index eb5313dcf..cc81466b0 100644 --- a/internal/elasticsearch/security/user.go +++ b/internal/elasticsearch/security/user.go @@ -108,7 +108,7 @@ func resourceSecurityUserPut(ctx context.Context, d *schema.ResourceData, meta i return diag.FromErr(err) } usernameId := d.Get("username").(string) - id, diags := client.ID(usernameId) + id, diags := client.ID(ctx, usernameId) if diags.HasError() { return diags } @@ -148,7 +148,7 @@ func resourceSecurityUserPut(ctx context.Context, d *schema.ResourceData, meta i user.Metadata = metadata } - if diags := client.PutElasticsearchUser(&user); diags.HasError() { + if diags := client.PutElasticsearchUser(ctx, &user); diags.HasError() { return diags } @@ -168,7 +168,7 @@ func resourceSecurityUserRead(ctx context.Context, d *schema.ResourceData, meta } usernameId := compId.ResourceId - user, diags := client.GetElasticsearchUser(usernameId) + user, diags := client.GetElasticsearchUser(ctx, usernameId) if user == nil && diags == nil { d.SetId("") return diags @@ -216,7 +216,7 @@ func resourceSecurityUserDelete(ctx context.Context, d *schema.ResourceData, met return diags } - if diags := client.DeleteElasticsearchUser(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchUser(ctx, compId.ResourceId); diags.HasError() { return diags } diff --git a/internal/elasticsearch/security/user_data_source.go b/internal/elasticsearch/security/user_data_source.go index 24225de11..233d73559 100644 --- a/internal/elasticsearch/security/user_data_source.go +++ b/internal/elasticsearch/security/user_data_source.go @@ -70,12 +70,12 @@ func dataSourceSecurityUserRead(ctx context.Context, d *schema.ResourceData, met return diag.FromErr(err) } usernameId := d.Get("username").(string) - id, diags := client.ID(usernameId) + id, diags := client.ID(ctx, usernameId) if diags.HasError() { return diags } - user, diags := client.GetElasticsearchUser(usernameId) + user, diags := client.GetElasticsearchUser(ctx, usernameId) if diags.HasError() { return diags } From bf48c3502366e311b00b6bba339b713cef593221 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Mon, 12 Sep 2022 10:24:58 +0900 Subject: [PATCH 2/4] Fix missing param --- internal/elasticsearch/index/ilm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/elasticsearch/index/ilm.go b/internal/elasticsearch/index/ilm.go index c71fb9c34..d4d9dbc4e 100644 --- a/internal/elasticsearch/index/ilm.go +++ b/internal/elasticsearch/index/ilm.go @@ -647,7 +647,7 @@ func resourceIlmDelete(ctx context.Context, d *schema.ResourceData, meta interfa return diags } - if diags := client.DeleteElasticsearchIlm(compId.ResourceId); diags.HasError() { + if diags := client.DeleteElasticsearchIlm(ctx, compId.ResourceId); diags.HasError() { return diags } From e04de6d4580ff48b768818e2fe4a73e8c38eebd4 Mon Sep 17 00:00:00 2001 From: k-yomo Date: Mon, 12 Sep 2022 10:28:19 +0900 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4751dd629..bdd729dd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Added - Allow use of `api_key` instead of `username`/`password` for authentication ([#130](https://github.com/elastic/terraform-provider-elasticstack/pull/130)) ### Fixed +- Make API calls context aware to be able to handle timeouts - Correctly identify a missing security user ([#101](https://github.com/elastic/terraform-provider-elasticstack/issues/101)) - Support **7.x** Elasticsearch < **7.15** by removing the default `media_type` attribute in the Append processor ([#118](https://github.com/elastic/terraform-provider-elasticstack/pull/118)) - Add `allow_restricted_indices` setting to security role ([#125](https://github.com/elastic/terraform-provider-elasticstack/issues/125)) From 3de2c0737adc110b21f52cdeff4fe36ecb76dd8f Mon Sep 17 00:00:00 2001 From: k-yomo Date: Mon, 12 Sep 2022 10:29:24 +0900 Subject: [PATCH 4/4] Add PR link to CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdd729dd9..bbb2a2b49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ### Added - Allow use of `api_key` instead of `username`/`password` for authentication ([#130](https://github.com/elastic/terraform-provider-elasticstack/pull/130)) ### Fixed -- Make API calls context aware to be able to handle timeouts +- Make API calls context aware to be able to handle timeouts ([#138](https://github.com/elastic/terraform-provider-elasticstack/pull/138)) - Correctly identify a missing security user ([#101](https://github.com/elastic/terraform-provider-elasticstack/issues/101)) - Support **7.x** Elasticsearch < **7.15** by removing the default `media_type` attribute in the Append processor ([#118](https://github.com/elastic/terraform-provider-elasticstack/pull/118)) - Add `allow_restricted_indices` setting to security role ([#125](https://github.com/elastic/terraform-provider-elasticstack/issues/125))