Skip to content

Commit 3633a63

Browse files
author
k-yomo
committed
Fixed reviewed ones
1 parent f21d27b commit 3633a63

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

docs/resources/elasticsearch_script.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
# generated by https://github.com/hashicorp/terraform-plugin-docs
3-
page_title: "elasticstack_elasticsearch_script Resource - terraform-provider-elasticstack"
4-
subcategory: ""
2+
subcategory: "Snapshot"
3+
layout: ""
4+
page_title: "Elasticstack: elasticstack_elasticsearch_script Resource"
55
description: |-
6-
Creates or updates a stored script or search template. See https://www.elastic.co/guide/en/elasticsearch/reference/current/create-stored-script-api.html
6+
Creates or updates a stored script or search template.
77
---
88

9-
# elasticstack_elasticsearch_script (Resource)
9+
# Resource: elasticstack_elasticsearch_script
1010

1111
Creates or updates a stored script or search template. See https://www.elastic.co/guide/en/elasticsearch/reference/current/create-stored-script-api.html
1212

@@ -29,14 +29,14 @@ resource "elasticstack_elasticsearch_script" "my_script" {
2929

3030
### Required
3131

32+
- `lang` (String) Script language. For search templates, use `mustache`.
3233
- `script_id` (String) Identifier for the stored script. Must be unique within the cluster.
3334
- `source` (String) For scripts, a string containing the script. For search templates, an object containing the search template.
3435

3536
### Optional
3637

3738
- `context` (String) Context in which the script or search template should run.
3839
- `elasticsearch_connection` (Block List, Max: 1) Used to establish connection to Elasticsearch server. Overrides environment variables if present. (see [below for nested schema](#nestedblock--elasticsearch_connection))
39-
- `lang` (String) Script language. For search templates, use `mustache`. Defaults to `painless`.
4040
- `params` (String) Parameters for the script or search template.
4141

4242
### Read-Only

internal/clients/cluster.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,12 @@ func (a *ApiClient) GetElasticsearchScript(ctx context.Context, id string) (*mod
190190
if res.StatusCode == http.StatusNotFound {
191191
return nil, nil
192192
}
193-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get the script: %s", id)); diags.HasError() {
193+
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get stored script: %s", id)); diags.HasError() {
194194
return nil, diags
195195
}
196-
type getScriptResponse = struct {
196+
var scriptResponse struct {
197197
Script *models.Script `json:"script"`
198198
}
199-
var scriptResponse getScriptResponse
200199
if err := json.NewDecoder(res.Body).Decode(&scriptResponse); err != nil {
201200
return nil, diag.FromErr(err)
202201
}
@@ -208,7 +207,7 @@ func (a *ApiClient) PutElasticsearchScript(ctx context.Context, script *models.S
208207
req := struct {
209208
Script *models.Script `json:"script"`
210209
}{
211-
Script: script,
210+
script,
212211
}
213212
scriptBytes, err := json.Marshal(req)
214213
if err != nil {
@@ -219,7 +218,7 @@ func (a *ApiClient) PutElasticsearchScript(ctx context.Context, script *models.S
219218
return diag.FromErr(err)
220219
}
221220
defer res.Body.Close()
222-
if diags := utils.CheckError(res, "Unable to put the script"); diags.HasError() {
221+
if diags := utils.CheckError(res, "Unable to put stored script"); diags.HasError() {
223222
return diags
224223
}
225224
return nil

internal/elasticsearch/cluster/script.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ func ResourceScript() *schema.Resource {
2323
ForceNew: true,
2424
},
2525
"lang": {
26-
Description: "Script language. For search templates, use `mustache`. Defaults to `painless`.",
26+
Description: "Script language. For search templates, use `mustache`.",
2727
Type: schema.TypeString,
28-
Optional: true,
29-
Default: "painless",
28+
Required: true,
3029
ValidateFunc: validation.StringInSlice([]string{"painless", "expression", "mustache", "java"}, false),
3130
},
3231
"source": {

internal/elasticsearch/cluster/script_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ func TestAccResourceScript(t *testing.T) {
4141
})
4242
}
4343

44+
func TestAccResourceScriptSearchTemplate(t *testing.T) {
45+
scriptID := sdkacctest.RandStringFromCharSet(10, sdkacctest.CharSetAlphaNum)
46+
47+
resource.UnitTest(t, resource.TestCase{
48+
PreCheck: func() { acctest.PreCheck(t) },
49+
CheckDestroy: checkScriptDestroy,
50+
ProviderFactories: acctest.Providers,
51+
Steps: []resource.TestStep{
52+
{
53+
Config: testAccSearchTemplateCreate(scriptID),
54+
Check: resource.ComposeTestCheckFunc(
55+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_script.search_template_test", "script_id", scriptID),
56+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_script.search_template_test", "lang", "mustache"),
57+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_script.search_template_test", "source", `{"query":{"match":{"message":"{{query_string}}"}},"from":"{{from}}","size":"{{size}}"}`),
58+
resource.TestCheckResourceAttr("elasticstack_elasticsearch_script.search_template_test", "params", `{"query_string":"My query string"}`),
59+
),
60+
},
61+
},
62+
})
63+
}
64+
4465
func testAccScriptCreate(id string) string {
4566
return fmt.Sprintf(`
4667
provider "elasticstack" {
@@ -49,6 +70,7 @@ provider "elasticstack" {
4970
5071
resource "elasticstack_elasticsearch_script" "test" {
5172
script_id = "%s"
73+
lang = "painless"
5274
source = "Math.log(_score * 2) + params['my_modifier']"
5375
context = "score"
5476
}
@@ -63,6 +85,7 @@ provider "elasticstack" {
6385
6486
resource "elasticstack_elasticsearch_script" "test" {
6587
script_id = "%s"
88+
lang = "painless"
6689
source = "Math.log(_score * 4) + params['changed_modifier']"
6790
params = jsonencode({
6891
changed_modifier = 2
@@ -71,6 +94,31 @@ resource "elasticstack_elasticsearch_script" "test" {
7194
`, id)
7295
}
7396

97+
func testAccSearchTemplateCreate(id string) string {
98+
return fmt.Sprintf(`
99+
provider "elasticstack" {
100+
elasticsearch {}
101+
}
102+
103+
resource "elasticstack_elasticsearch_script" "search_template_test" {
104+
script_id = "%s"
105+
lang = "mustache"
106+
source = jsonencode({
107+
query = {
108+
match = {
109+
message = "{{query_string}}"
110+
}
111+
}
112+
from = "{{from}}"
113+
size = "{{size}}"
114+
})
115+
params = jsonencode({
116+
query_string = "My query string"
117+
})
118+
}
119+
`, id)
120+
}
121+
74122
func checkScriptDestroy(s *terraform.State) error {
75123
client := acctest.Provider.Meta().(*clients.ApiClient)
76124

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
subcategory: "Snapshot"
3+
layout: ""
4+
page_title: "Elasticstack: elasticstack_elasticsearch_script Resource"
5+
description: |-
6+
Creates or updates a stored script or search template.
7+
---
8+
9+
# Resource: elasticstack_elasticsearch_script
10+
11+
Creates or updates a stored script or search template. See https://www.elastic.co/guide/en/elasticsearch/reference/current/create-stored-script-api.html
12+
13+
## Example Usage
14+
15+
{{ tffile "examples/resources/elasticstack_elasticsearch_script/resource.tf" }}
16+
17+
{{ .SchemaMarkdown | trimspace }}
18+
19+
## Import
20+
21+
Import is supported using the following syntax:
22+
23+
{{ codefile "shell" "examples/resources/elasticstack_elasticsearch_script/import.sh" }}

0 commit comments

Comments
 (0)