Skip to content

Commit

Permalink
[112] making use of _template endpoint (legacy index templates) for o…
Browse files Browse the repository at this point in the history
…lder opensearch versions (<= 1.0.0)

Signed-off-by: premkiran <[email protected]>
  • Loading branch information
premkirank committed Nov 15, 2023
1 parent 7128bed commit 12ee583
Showing 1 changed file with 22 additions and 3 deletions.
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("1.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 (<= 1.0.0)
if openSearchVersion.LessThanOrEqual(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
}

0 comments on commit 12ee583

Please sign in to comment.