Skip to content

Commit

Permalink
Add missing fields and tests cfor IndexGetIndexTemplate
Browse files Browse the repository at this point in the history
The `Index<Get|Put>IndexTemplate` APIs are a newer version of the
deprecated `Index<Get|Put>Template` APIs. This commit adds a test for
them, and fills up missing/new fields from the latest ES API.

Close #1550
  • Loading branch information
olivere committed Jan 7, 2022
1 parent c76a1be commit 3e40684
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 9 deletions.
44 changes: 38 additions & 6 deletions indices_get_index_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,21 @@ func (s *IndicesGetIndexTemplateService) Do(ctx context.Context) (*IndicesGetInd

// IndicesGetIndexTemplateResponse is the response of IndicesGetIndexTemplateService.Do.
type IndicesGetIndexTemplateResponse struct {
IndexTemplates []IndicesGetIndexTemplates `json:"index_templates"`
IndexTemplates IndicesGetIndexTemplatesSlice `json:"index_templates"`
}

// IndicesGetIndexTemplatesSlice is a slice of IndicesGetIndexTemplates.
type IndicesGetIndexTemplatesSlice []IndicesGetIndexTemplates

// ByName returns the template with the given name, if it exists.
// The bool indicates whether a template with that name has been found.
func (slice IndicesGetIndexTemplatesSlice) ByName(name string) (*IndicesGetIndexTemplates, bool) {
for _, t := range slice {
if t.Name == name {
return &t, true
}
}
return nil, false
}

type IndicesGetIndexTemplates struct {
Expand All @@ -200,15 +214,33 @@ type IndicesGetIndexTemplates struct {
}

type IndicesGetIndexTemplate struct {
IndexPatterns []string `json:"index_patterns,omitempty"`
ComposedOf []string `json:"composed_of,omitempty"`
Priority int `json:"priority,omitempty"`
Version int `json:"version,omitempty"`
Template *IndicesGetIndexTemplateData `json:"template,omitempty"`
IndexPatterns []string `json:"index_patterns,omitempty"`
ComposedOf []string `json:"composed_of,omitempty"`
Priority int `json:"priority,omitempty"`
Version int `json:"version,omitempty"`
Template *IndicesGetIndexTemplateData `json:"template,omitempty"`
Meta map[string]interface{} `json:"_meta,omitempty"`
DataStream *IndicesDataStream `json:"data_stream,omitempty"`
AllowAutoCreate bool `json:"allow_auto_create,omitempty"`
}

type IndicesGetIndexTemplateData struct {
Settings map[string]interface{} `json:"settings,omitempty"`
Mappings map[string]interface{} `json:"mappings,omitempty"`
Aliases map[string]interface{} `json:"aliases,omitempty"`
}

type IndicesDataStream struct {
Name string `json:"name,omitempty"`
TimestampField string `json:"timestamp_field,omitempty"`
Indices []string `json:"indices,omitempty"`
Generation int64 `json:"generation,omitempty"`
Status string `json:"status,omitempty"`
IndexTemplate string `json:"template,omitempty"`
IlmPolicy string `json:"ilm_policy,omitempty"`
Meta map[string]interface{} `json:"_meta,omitempty"`
Hidden bool `json:"hidden,omitempty"`
System bool `json:"system,omitempty"`
AllowCustomRouting bool `json:"allow_custom_routing,omitempty"`
Replicated bool `json:"replicated,omitempty"`
}
96 changes: 96 additions & 0 deletions indices_get_index_template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import (
"context"
"log"
"os"
"testing"
)

func TestIndexGetIndexTemplateURL(t *testing.T) {
client := setupTestClientAndCreateIndex(t)

tests := []struct {
Name string
Expected string
}{
{
"template_1",
"/_index_template/template_1",
},
}

for _, test := range tests {
path, _, err := client.IndexGetIndexTemplate(test.Name).buildURL()
if err != nil {
t.Fatal(err)
}
if path != test.Expected {
t.Errorf("expected %q; got: %q", test.Expected, path)
}
}
}

func TestIndexGetIndexTemplateService(t *testing.T) {
client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0)))
// client := setupTestClientAndCreateIndex(t)

create := true
body := `
{
"index_patterns": ["te*"],
"priority": 1,
"template": {
"settings": {
"index": {
"number_of_shards": 1
}
},
"mappings": {
"_source": {
"enabled": false
},
"properties": {
"host_name": {
"type": "keyword"
},
"created_at": {
"type": "date",
"format": "yyyy MM dd HH:mm:ss Z"
}
}
}
}
}
`
_, err := client.IndexPutIndexTemplate("template_1").BodyString(body).Create(create).Pretty(true).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
defer client.IndexDeleteIndexTemplate("template_1").Pretty(true).Do(context.TODO())

res, err := client.IndexGetIndexTemplate("template_1").Pretty(true).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
if res == nil {
t.Fatalf("expected result; got: %v", res)
}
template, found := res.IndexTemplates.ByName("template_1")
if !found {
t.Fatalf("expected template %q to be found; got: %v", "template_1", found)
}
if template == nil {
t.Fatalf("expected template %q to be != nil; got: %v", "template_1", template)
}
if template.IndexTemplate == nil {
t.Fatalf("expected index template of template %q to be != nil; got: %v", "template_1", template.IndexTemplate)
}
if len(template.IndexTemplate.IndexPatterns) != 1 || template.IndexTemplate.IndexPatterns[0] != "te*" {
t.Fatalf("expected index settings of %q to be [\"index1\"]; got: %v", testIndexName, template.IndexTemplate.IndexPatterns)
}
}
8 changes: 5 additions & 3 deletions indices_get_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ func TestIndexGetTemplateURL(t *testing.T) {
}

func TestIndexGetTemplateService(t *testing.T) {
// client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0)))
client := setupTestClientAndCreateIndex(t)

create := true
body := `
{
Expand All @@ -68,13 +70,13 @@ func TestIndexGetTemplateService(t *testing.T) {
}
}
`
_, err := client.IndexPutTemplate("template_1").BodyString(body).Create(create).Do(context.TODO())
_, err := client.IndexPutTemplate("template_1").BodyString(body).Create(create).Pretty(true).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
defer client.IndexDeleteTemplate("template_1").Do(context.TODO())
defer client.IndexDeleteTemplate("template_1").Pretty(true).Do(context.TODO())

res, err := client.IndexGetTemplate("template_1").Do(context.TODO())
res, err := client.IndexGetTemplate("template_1").Pretty(true).Do(context.TODO())
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 3e40684

Please sign in to comment.