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

[bugfix] making use of _template endpoint for opensearch versions < 2.0.0 #129

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 22 additions & 3 deletions provider/resource_opensearch_index_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"encoding/json"
"log"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
elastic7 "github.com/olivere/elastic/v7"
elastic6 "gopkg.in/olivere/elastic.v6"
)

var maximumOSTemplateVersion, _ = version.NewVersion("2.0.0")

func resourceOpensearchIndexTemplate() *schema.Resource {
return &schema.Resource{
Description: "Provides an Opensearch index template resource.",
Expand Down Expand Up @@ -53,10 +56,12 @@ func resourceOpensearchIndexTemplateRead(d *schema.ResourceData, meta interface{

var result string
var err error

osClient, err := getClient(meta.(*ProviderConf))
if err != nil {
return err
}

result, err = elastic7IndexGetTemplate(osClient, id)
if err != nil {
if elastic7.IsNotFound(err) || elastic6.IsNotFound(err) {
Expand Down Expand Up @@ -122,17 +127,31 @@ func resourceOpensearchPutIndexTemplate(d *schema.ResourceData, meta interface{}
body := d.Get("body").(string)

var err error
var openSearchVersion *version.Version

providerConf := meta.(*ProviderConf)
osClient, err := getClient(meta.(*ProviderConf))
if err != nil {
return err
}
err = elastic7IndexPutTemplate(osClient, name, body, create)

openSearchVersion, err = version.NewVersion(providerConf.osVersion)
if err == nil {
err = elastic7IndexPutTemplate(openSearchVersion, osClient, name, body, create)
}

return err
}

func elastic7IndexPutTemplate(client *elastic7.Client, name string, body string, create bool) error {
_, err := client.IndexPutIndexTemplate(name).BodyString(body).Create(create).Do(context.TODO())
func elastic7IndexPutTemplate(openSearchVersion *version.Version, client *elastic7.Client, name string, body string, create bool) error {
var err error

// making use of _template endpoint (legacy index templates) for older opensearch versions (< 2.0.0)
if openSearchVersion.LessThan(maximumOSTemplateVersion) {
_, err = client.IndexPutTemplate(name).BodyString(body).Create(create).Do(context.TODO())
} else {
_, err = client.IndexPutIndexTemplate(name).BodyString(body).Create(create).Do(context.TODO())
}

return err
}
Loading