-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for ElasticSearch 7.8+ Index templates (#120)
* Add resource_elasticsearch_template_index.go and update ES7 client to latest version * Remove phases.delete.actions.delete.delete_searchable_snapshot when true from IL Policy * Fix ineffassign errors * Add testing for new resource, adjust some error messages and add new diff supress function * Update go.sum * Rename resource and functions from template index to composable index template * Add documentation page * cleanup, add changelog * back out phases.delete.actions.delete.delete_searchable_snapshot diff supress for now * fix linting * update tests for default value * split es6 from es7 fixtures Co-authored-by: Victor Cabezas <[email protected]> Co-authored-by: Phillip Baker <[email protected]>
- Loading branch information
1 parent
5302ba1
commit 8b5a81f
Showing
11 changed files
with
498 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
--- | ||
layout: "elasticsearch" | ||
page_title: "Elasticsearch: elasticsearch_composable_index_template" | ||
subcategory: "Elasticsearch Opensource" | ||
description: |- | ||
Provides an Elasticsearch Composable index template resource. | ||
--- | ||
|
||
# elasticsearch_composable_index_template | ||
|
||
Provides an Elasticsearch Composable index template resource. This resource uses the `/_index_template` | ||
endpoint of Elasticsearch API that is available since version 7.8. Use `elasticsearch_index_template` if | ||
you are using older versions of Elasticsearch or if you want to keep using legacy Index Templates in Elasticsearch 7.8+. | ||
|
||
## Example Usage | ||
|
||
```tf | ||
# Create an index template | ||
resource "elasticsearch_composable_index_template" "template_1" { | ||
name = "template_1" | ||
body = <<EOF | ||
{ | ||
"index_patterns": ["te*", "bar*"], | ||
"template": { | ||
"settings": { | ||
"index": { | ||
"number_of_shards": 1 | ||
} | ||
}, | ||
"mappings": { | ||
"properties": { | ||
"host_name": { | ||
"type": "keyword" | ||
}, | ||
"created_at": { | ||
"type": "date", | ||
"format": "EEE MMM dd HH:mm:ss Z yyyy" | ||
} | ||
} | ||
}, | ||
"aliases": { | ||
"mydata": { } | ||
} | ||
}, | ||
"priority": 200, | ||
"version": 3 | ||
} | ||
EOF | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `name` - (Required) The name of the index template. | ||
* `body` - (Required) The JSON body of the index template. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id` - The name of the index template. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package es | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
elastic7 "github.com/olivere/elastic/v7" | ||
) | ||
|
||
func resourceElasticsearchComposableIndexTemplate() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceElasticsearchComposableIndexTemplateCreate, | ||
Read: resourceElasticsearchComposableIndexTemplateRead, | ||
Update: resourceElasticsearchComposableIndexTemplateUpdate, | ||
Delete: resourceElasticsearchComposableIndexTemplateDelete, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
ForceNew: true, | ||
Required: true, | ||
}, | ||
"body": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
DiffSuppressFunc: diffSuppressComposableIndexTemplate, | ||
ValidateFunc: validation.StringIsJSON, | ||
}, | ||
}, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
} | ||
} | ||
|
||
func resourceElasticsearchComposableIndexTemplateCreate(d *schema.ResourceData, meta interface{}) error { | ||
err := resourceElasticsearchPutComposableIndexTemplate(d, meta, true) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId(d.Get("name").(string)) | ||
return nil | ||
} | ||
|
||
func resourceElasticsearchComposableIndexTemplateRead(d *schema.ResourceData, meta interface{}) error { | ||
id := d.Id() | ||
|
||
var result, version string | ||
var err error | ||
switch client := meta.(type) { | ||
case *elastic7.Client: | ||
version, err = elastic7GetVersion(client) | ||
if err == nil { | ||
if version < "7.8.0" { | ||
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) | ||
} else { | ||
result, err = elastic7GetIndexTemplate(client, id) | ||
} | ||
} | ||
default: | ||
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") | ||
} | ||
if err != nil { | ||
if elastic7.IsNotFound(err) { | ||
log.Printf("[WARN] Index template (%s) not found, removing from state", id) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
ds := &resourceDataSetter{d: d} | ||
ds.set("name", d.Id()) | ||
ds.set("body", result) | ||
return ds.err | ||
} | ||
|
||
func elastic7GetIndexTemplate(client *elastic7.Client, id string) (string, error) { | ||
res, err := client.IndexGetIndexTemplate(id).Do(context.TODO()) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// No more than 1 element is expected, if the index template is not found, previous call should | ||
// return a 404 error | ||
t := res.IndexTemplates[0].IndexTemplate | ||
tj, err := json.Marshal(t) | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(tj), nil | ||
} | ||
|
||
func resourceElasticsearchComposableIndexTemplateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
return resourceElasticsearchPutComposableIndexTemplate(d, meta, false) | ||
} | ||
|
||
func resourceElasticsearchComposableIndexTemplateDelete(d *schema.ResourceData, meta interface{}) error { | ||
id := d.Id() | ||
|
||
var version string | ||
var err error | ||
switch client := meta.(type) { | ||
case *elastic7.Client: | ||
version, err = elastic7GetVersion(client) | ||
if err == nil { | ||
if version < "7.8.0" { | ||
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version %s", version) | ||
} else { | ||
err = elastic7DeleteIndexTemplate(client, id) | ||
} | ||
} | ||
default: | ||
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func elastic7DeleteIndexTemplate(client *elastic7.Client, id string) error { | ||
_, err := client.IndexDeleteIndexTemplate(id).Do(context.TODO()) | ||
return err | ||
} | ||
|
||
func resourceElasticsearchPutComposableIndexTemplate(d *schema.ResourceData, meta interface{}, create bool) error { | ||
name := d.Get("name").(string) | ||
body := d.Get("body").(string) | ||
|
||
var err error | ||
switch client := meta.(type) { | ||
case *elastic7.Client: | ||
err = elastic7PutIndexTemplate(client, name, body, create) | ||
default: | ||
err = fmt.Errorf("index_template endpoint only available from ElasticSearch >= 7.8, got version < 7.0.0") | ||
} | ||
|
||
return err | ||
} | ||
|
||
func elastic7PutIndexTemplate(client *elastic7.Client, name string, body string, create bool) error { | ||
_, err := client.IndexPutIndexTemplate(name).BodyString(body).Create(create).Do(context.TODO()) | ||
return err | ||
} |
Oops, something went wrong.