Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass context to api calls #138

Merged
merged 4 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ([#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))
Expand Down
8 changes: 4 additions & 4 deletions internal/clients/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ 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
}
log.Printf("[TRACE] cluster UUID: %s", *clusterId)
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)
}
Expand Down
33 changes: 17 additions & 16 deletions internal/clients/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clients

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
Loading