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

Add script resource #173

Merged
merged 12 commits into from
Nov 1, 2022
Merged

Add script resource #173

merged 12 commits into from
Nov 1, 2022

Conversation

k-yomo
Copy link
Contributor

@k-yomo k-yomo commented Oct 29, 2022

Resolves #107

This PR adds script resource in reference to the below docs.
https://www.elastic.co/guide/en/elasticsearch/reference/current/create-stored-script-api.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/get-stored-script-api.html

📝 context and params can't be retrieved by GET endpoint, so they are not importable as of now.

@elasticmachine
Copy link

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@k-yomo k-yomo force-pushed the add-script-resource branch 6 times, most recently from 8f29b85 to d11a0ca Compare October 30, 2022 05:21
@k-yomo k-yomo marked this pull request as ready for review October 30, 2022 05:41
Comment on lines 196 to 199
type getScriptResponse = struct {
Script *models.Script `json:"script"`
}
var scriptResponse getScriptResponse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type getScriptResponse = struct {
Script *models.Script `json:"script"`
}
var scriptResponse getScriptResponse
var scriptResponse struct {
Script *models.Script `json:"script"`
}

Any benefit to the named type here?

Copy link
Contributor Author

@k-yomo k-yomo Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, unnamed type would be fine, fixed so!

Comment on lines 196 to 199
type getScriptResponse = struct {
Script *models.Script `json:"script"`
}
var scriptResponse getScriptResponse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
type getScriptResponse = struct {
Script *models.Script `json:"script"`
}
var scriptResponse getScriptResponse
var scriptResponse struct {
Script *models.Script `json:"script"`
}

Any benefit to the named type here?

---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "elasticstack_elasticsearch_script Resource - terraform-provider-elasticstack"
subcategory: ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subcategory: "Cluster" (I think, not sure if this warrants it's own category)

Might need to add a template

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh totally missed it, will fix.
I want this😂
hashicorp/terraform-plugin-docs#156

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, yep very much want that one too :)

req := struct {
Script *models.Script `json:"script"`
}{
Script: script,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Script: script,
script,

if res.StatusCode == http.StatusNotFound {
return nil, nil
}
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get the script: %s", id)); diags.HasError() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get the script: %s", id)); diags.HasError() {
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get saved script: %s", id)); diags.HasError() {

return diag.FromErr(err)
}
defer res.Body.Close()
if diags := utils.CheckError(res, "Unable to put the script"); diags.HasError() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if diags := utils.CheckError(res, "Unable to put the script"); diags.HasError() {
if diags := utils.CheckError(res, "Unable to put saved script"); diags.HasError() {

Description: "Script language. For search templates, use `mustache`. Defaults to `painless`.",
Type: schema.TypeString,
Optional: true,
Default: "painless",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default makes sense in the context of a stored script, but not when used to manage a search template. IMO it would be better to mirror the API and have this as a required field without a default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's true, fixed to make it required field.


script, diags := client.GetElasticsearchScript(ctx, compId.ResourceId)
if !d.IsNewResource() && script == nil && diags == nil {
tflog.Warn(ctx, fmt.Sprintf(`Script "%s" not found, removing from state`, compId.ResourceId))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like we log a message in this case for the other resources. Any reason this should be a warning?

Copy link
Contributor Author

@k-yomo k-yomo Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the reason is to let the user know that the resource is removed from state when actually not found on API which is sometimes unexpected for users.
I think it's pretty common pattern which the other providers do too.
https://github.com/hashicorp/terraform-provider-aws/search?q=removing+from+state

It it makes sense, I'll add it to the other resources too with !d.IsNewResource() check.
hashicorp/terraform-provider-aws#16796

Copy link
Contributor Author

@k-yomo k-yomo Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that adding !d.IsNewResource() is not good here since it can cause null pointer error in the following code.
It might be better to return error when the resource is d.IsNewResource()) as aws provider does, but I simply removed to align with the other resources for now.

"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourceScript(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test managing a search template as well?

@k-yomo k-yomo requested a review from tobio October 31, 2022 10:07
Copy link
Member

@tobio tobio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of minor docs changes.

resource "elasticstack_elasticsearch_script" "my_script" {
script_id = "my_script"
source = "Math.log(_score * 2) + params['my_modifier']"
context = "score"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is no longer valid, and needs lang defined.

Copy link
Contributor Author

@k-yomo k-yomo Nov 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch, fixed🙏
5bde0d4


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

## Example Usage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have said this earlier sorry, having a search template example would be nice too.

@k-yomo k-yomo requested a review from tobio November 1, 2022 02:03
@tobio tobio merged commit 1131f2a into elastic:main Nov 1, 2022
@k-yomo k-yomo deleted the add-script-resource branch November 2, 2022 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Support for search templates / stored scripts
3 participants