-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add script resource #173
Conversation
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? |
8f29b85
to
d11a0ca
Compare
d11a0ca
to
3f4af9f
Compare
3f4af9f
to
d027f42
Compare
internal/clients/cluster.go
Outdated
type getScriptResponse = struct { | ||
Script *models.Script `json:"script"` | ||
} | ||
var scriptResponse getScriptResponse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
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!
internal/clients/cluster.go
Outdated
type getScriptResponse = struct { | ||
Script *models.Script `json:"script"` | ||
} | ||
var scriptResponse getScriptResponse |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: "" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :)
internal/clients/cluster.go
Outdated
req := struct { | ||
Script *models.Script `json:"script"` | ||
}{ | ||
Script: script, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script: script, | |
script, |
internal/clients/cluster.go
Outdated
if res.StatusCode == http.StatusNotFound { | ||
return nil, nil | ||
} | ||
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get the script: %s", id)); diags.HasError() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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() { |
internal/clients/cluster.go
Outdated
return diag.FromErr(err) | ||
} | ||
defer res.Body.Close() | ||
if diags := utils.CheckError(res, "Unable to put the script"); diags.HasError() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this 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" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
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
andparams
can't be retrieved by GET endpoint, so they are not importable as of now.