Skip to content

Commit

Permalink
Refactor elasticsearch index client methods (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Bruinhout authored Nov 22, 2022
1 parent 434193a commit 7ed4d0a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 79 deletions.
113 changes: 57 additions & 56 deletions internal/clients/index.go → internal/clients/elasticsearch/index.go

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions internal/elasticsearch/index/component_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -218,7 +219,7 @@ func resourceComponentTemplatePut(ctx context.Context, d *schema.ResourceData, m
componentTemplate.Version = &definedVer
}

if diags := client.PutElasticsearchComponentTemplate(ctx, &componentTemplate); diags.HasError() {
if diags := elasticsearch.PutComponentTemplate(ctx, client, &componentTemplate); diags.HasError() {
return diags
}

Expand All @@ -238,7 +239,7 @@ func resourceComponentTemplateRead(ctx context.Context, d *schema.ResourceData,
}
templateId := compId.ResourceId

tpl, diags := client.GetElasticsearchComponentTemplate(ctx, templateId)
tpl, diags := elasticsearch.GetComponentTemplate(ctx, client, templateId)
if tpl == nil && diags == nil {
tflog.Warn(ctx, fmt.Sprintf(`Component template "%s" not found, removing from state`, compId.ResourceId))
d.SetId("")
Expand Down Expand Up @@ -293,7 +294,7 @@ func resourceComponentTemplateDelete(ctx context.Context, d *schema.ResourceData
if diags.HasError() {
return diags
}
if diags := client.DeleteElasticsearchComponentTemplate(ctx, compId.ResourceId); diags.HasError() {
if diags := elasticsearch.DeleteComponentTemplate(ctx, client, compId.ResourceId); diags.HasError() {
return diags
}
return diags
Expand Down
7 changes: 4 additions & 3 deletions internal/elasticsearch/index/data_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -128,7 +129,7 @@ func resourceDataStreamPut(ctx context.Context, d *schema.ResourceData, meta int
return diags
}

if diags := client.PutElasticsearchDataStream(ctx, dsId); diags.HasError() {
if diags := elasticsearch.PutDataStream(ctx, client, dsId); diags.HasError() {
return diags
}

Expand All @@ -148,7 +149,7 @@ func resourceDataStreamRead(ctx context.Context, d *schema.ResourceData, meta in
return diags
}

ds, diags := client.GetElasticsearchDataStream(ctx, compId.ResourceId)
ds, diags := elasticsearch.GetDataStream(ctx, client, compId.ResourceId)
if ds == nil && diags == nil {
// no data stream found on ES side
tflog.Warn(ctx, fmt.Sprintf(`Data stream "%s" not found, removing from state`, compId.ResourceId))
Expand Down Expand Up @@ -221,7 +222,7 @@ func resourceDataStreamDelete(ctx context.Context, d *schema.ResourceData, meta
if diags.HasError() {
return diags
}
if diags := client.DeleteElasticsearchDataStream(ctx, compId.ResourceId); diags.HasError() {
if diags := elasticsearch.DeleteDataStream(ctx, client, compId.ResourceId); diags.HasError() {
return diags
}

Expand Down
7 changes: 4 additions & 3 deletions internal/elasticsearch/index/ilm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -405,7 +406,7 @@ func resourceIlmPut(ctx context.Context, d *schema.ResourceData, meta interface{
}
policy.Name = ilmId

if diags := client.PutElasticsearchIlm(ctx, policy); diags.HasError() {
if diags := elasticsearch.PutIlm(ctx, client, policy); diags.HasError() {
return diags
}

Expand Down Expand Up @@ -569,7 +570,7 @@ func resourceIlmRead(ctx context.Context, d *schema.ResourceData, meta interface
}
policyId := compId.ResourceId

ilmDef, diags := client.GetElasticsearchIlm(ctx, policyId)
ilmDef, diags := elasticsearch.GetIlm(ctx, client, policyId)
if ilmDef == nil && diags == nil {
tflog.Warn(ctx, fmt.Sprintf(`ILM policy "%s" not found, removing from state`, compId.ResourceId))
d.SetId("")
Expand Down Expand Up @@ -685,7 +686,7 @@ func resourceIlmDelete(ctx context.Context, d *schema.ResourceData, meta interfa
return diags
}

if diags := client.DeleteElasticsearchIlm(ctx, compId.ResourceId); diags.HasError() {
if diags := elasticsearch.DeleteIlm(ctx, client, compId.ResourceId); diags.HasError() {
return diags
}

Expand Down
17 changes: 9 additions & 8 deletions internal/elasticsearch/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -544,7 +545,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(ctx, indexName)
index, diags := elasticsearch.GetIndex(ctx, client, indexName)
if diags.HasError() {
return nil, fmt.Errorf("failed to get an ES Index")
}
Expand Down Expand Up @@ -749,7 +750,7 @@ func resourceIndexCreate(ctx context.Context, d *schema.ResourceData, meta inter
}
}

if diags := client.PutElasticsearchIndex(ctx, &index); diags.HasError() {
if diags := elasticsearch.PutIndex(ctx, client, &index); diags.HasError() {
return diags
}

Expand Down Expand Up @@ -786,14 +787,14 @@ func resourceIndexUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
}
if len(aliasesToDelete) > 0 {
if diags := client.DeleteElasticsearchIndexAlias(ctx, indexName, aliasesToDelete); diags.HasError() {
if diags := elasticsearch.DeleteIndexAlias(ctx, client, indexName, aliasesToDelete); diags.HasError() {
return diags
}
}

// keep new aliases up-to-date
for _, v := range enew {
if diags := client.UpdateElasticsearchIndexAlias(ctx, indexName, &v); diags.HasError() {
if diags := elasticsearch.UpdateIndexAlias(ctx, client, indexName, &v); diags.HasError() {
return diags
}
}
Expand Down Expand Up @@ -833,7 +834,7 @@ func resourceIndexUpdate(ctx context.Context, d *schema.ResourceData, meta inter
}
if len(updatedSettings) > 0 {
tflog.Trace(ctx, fmt.Sprintf("settings to update: %+v", updatedSettings))
if diags := client.UpdateElasticsearchIndexSettings(ctx, indexName, updatedSettings); diags.HasError() {
if diags := elasticsearch.UpdateIndexSettings(ctx, client, indexName, updatedSettings); diags.HasError() {
return diags
}
}
Expand All @@ -842,7 +843,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(ctx, indexName, mappings); diags.HasError() {
if diags := elasticsearch.UpdateIndexMappings(ctx, client, indexName, mappings); diags.HasError() {
return diags
}
}
Expand Down Expand Up @@ -878,7 +879,7 @@ func resourceIndexRead(ctx context.Context, d *schema.ResourceData, meta interfa
return diag.FromErr(err)
}

index, diags := client.GetElasticsearchIndex(ctx, indexName)
index, diags := elasticsearch.GetIndex(ctx, client, indexName)
if index == nil && diags == nil {
// no index found on ES side
tflog.Warn(ctx, fmt.Sprintf(`Index "%s" not found, removing from state`, compId.ResourceId))
Expand Down Expand Up @@ -932,7 +933,7 @@ func resourceIndexDelete(ctx context.Context, d *schema.ResourceData, meta inter
if diags.HasError() {
return diags
}
if diags := client.DeleteElasticsearchIndex(ctx, compId.ResourceId); diags.HasError() {
if diags := elasticsearch.DeleteIndex(ctx, client, compId.ResourceId); diags.HasError() {
return diags
}
return diags
Expand Down
7 changes: 4 additions & 3 deletions internal/elasticsearch/index/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -295,7 +296,7 @@ func resourceIndexTemplatePut(ctx context.Context, d *schema.ResourceData, meta
indexTemplate.Version = &definedVer
}

if diags := client.PutElasticsearchIndexTemplate(ctx, &indexTemplate); diags.HasError() {
if diags := elasticsearch.PutIndexTemplate(ctx, client, &indexTemplate); diags.HasError() {
return diags
}

Expand All @@ -315,7 +316,7 @@ func resourceIndexTemplateRead(ctx context.Context, d *schema.ResourceData, meta
}
templateId := compId.ResourceId

tpl, diags := client.GetElasticsearchIndexTemplate(ctx, templateId)
tpl, diags := elasticsearch.GetIndexTemplate(ctx, client, templateId)
if tpl == nil && diags == nil {
tflog.Warn(ctx, fmt.Sprintf(`Index template "%s" not found, removing from state`, compId.ResourceId))
d.SetId("")
Expand Down Expand Up @@ -420,7 +421,7 @@ func resourceIndexTemplateDelete(ctx context.Context, d *schema.ResourceData, me
if diags.HasError() {
return diags
}
if diags := client.DeleteElasticsearchIndexTemplate(ctx, compId.ResourceId); diags.HasError() {
if diags := elasticsearch.DeleteIndexTemplate(ctx, client, compId.ResourceId); diags.HasError() {
return diags
}
return diags
Expand Down
7 changes: 4 additions & 3 deletions internal/elasticsearch/ingest/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/clients/elasticsearch"
"github.com/elastic/terraform-provider-elasticstack/internal/models"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-log/tflog"
Expand Down Expand Up @@ -128,7 +129,7 @@ func resourceIngestPipelineTemplatePut(ctx context.Context, d *schema.ResourceDa
pipeline.Metadata = metadata
}

if diags := client.PutElasticsearchIngestPipeline(ctx, &pipeline); diags.HasError() {
if diags := elasticsearch.PutIngestPipeline(ctx, client, &pipeline); diags.HasError() {
return diags
}

Expand All @@ -148,7 +149,7 @@ func resourceIngestPipelineTemplateRead(ctx context.Context, d *schema.ResourceD
return diags
}

pipeline, diags := client.GetElasticsearchIngestPipeline(ctx, &compId.ResourceId)
pipeline, diags := elasticsearch.GetIngestPipeline(ctx, client, &compId.ResourceId)
if pipeline == nil && diags == nil {
tflog.Warn(ctx, fmt.Sprintf(`Injest pipeline "%s" not found, removing from state`, compId.ResourceId))
d.SetId("")
Expand Down Expand Up @@ -217,7 +218,7 @@ func resourceIngestPipelineTemplateDelete(ctx context.Context, d *schema.Resourc
return diags
}

if diags := client.DeleteElasticsearchIngestPipeline(ctx, &compId.ResourceId); diags.HasError() {
if diags := elasticsearch.DeleteIngestPipeline(ctx, client, &compId.ResourceId); diags.HasError() {
return diags
}

Expand Down

0 comments on commit 7ed4d0a

Please sign in to comment.