Skip to content

Commit eba1397

Browse files
Copilottobio
andauthored
Migrate the Elasticsearch script resource to the terraform-plugin-framework and remove legacy SDKv2 implementation (#1298)
* Initial plan * Implement Plugin Framework script resource with full functionality Co-authored-by: tobio <[email protected]> * Complete script resource migration with lint fixes and changelog update Co-authored-by: tobio <[email protected]> * Add comprehensive test for StringIsJSON validator Co-authored-by: tobio <[email protected]> * Address review feedback: migrate to jsontypes.NormalizedType and framework diagnostics Co-authored-by: tobio <[email protected]> * Update documentation for script resource schema changes Co-authored-by: tobio <[email protected]> * Address review feedback: update original script functions to return framework diagnostics and move read function to read.go Co-authored-by: tobio <[email protected]> * Remove SDK version of script resource and SDK wrapper functions as requested in review Co-authored-by: tobio <[email protected]> * Apply suggestions from code review * Remove duplicate acceptance test * Tidy up watcher docs link --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: tobio <[email protected]> Co-authored-by: Toby Brain <[email protected]>
1 parent 789780d commit eba1397

File tree

17 files changed

+587
-225
lines changed

17 files changed

+587
-225
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- Migrate `elasticstack_kibana_action_connector` to the Terraform plugin framework ([#1269](https://github.com/elastic/terraform-provider-elasticstack/pull/1269))
1414
- Migrate `elasticstack_elasticsearch_security_role_mapping` resource and data source to Terraform Plugin Framework ([#1279](https://github.com/elastic/terraform-provider-elasticstack/pull/1279))
1515
- Add support for `inactivity_timeout` in `elasticstack_fleet_agent_policy` ([#641](https://github.com/elastic/terraform-provider-elasticstack/issues/641))
16+
- Migrate `elasticstack_elasticsearch_script` resource to Terraform Plugin Framework ([#1297](https://github.com/elastic/terraform-provider-elasticstack/pull/1297))
1617
- Add support for `kafka` output types in `elasticstack_fleet_output` ([#1302](https://github.com/elastic/terraform-provider-elasticstack/pull/1302))
1718
- Add support for `prevent_initial_backfill` to `elasticstack_kibana_slo` ([#1071](https://github.com/elastic/terraform-provider-elasticstack/pull/1071))
1819
- [Refactor] Regenerate the SLO client using the current OpenAPI spec ([#1303](https://github.com/elastic/terraform-provider-elasticstack/pull/1303))

docs/resources/elasticsearch_script.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ resource "elasticstack_elasticsearch_script" "my_search_template" {
5555
### Optional
5656

5757
- `context` (String) Context in which the script or search template should run.
58-
- `elasticsearch_connection` (Block List, Max: 1, Deprecated) Elasticsearch connection configuration block. This property will be removed in a future provider version. Configure the Elasticsearch connection via the provider configuration instead. (see [below for nested schema](#nestedblock--elasticsearch_connection))
58+
- `elasticsearch_connection` (Block List, Deprecated) Elasticsearch connection configuration block. (see [below for nested schema](#nestedblock--elasticsearch_connection))
5959
- `params` (String) Parameters for the script or search template.
6060

6161
### Read-Only
6262

63-
- `id` (String) The ID of this resource.
63+
- `id` (String) Internal identifier of the resource
6464

6565
<a id="nestedblock--elasticsearch_connection"></a>
6666
### Nested Schema for `elasticsearch_connection`

docs/resources/elasticsearch_watch.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
page_title: "elasticstack_elasticsearch_watch Resource - terraform-provider-elasticstack"
55
subcategory: "Elasticsearch"
66
description: |-
7-
Manage Watches. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api.html
7+
Manage Watches. See the Watcher API documentation https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api.html for more details.
88
---
99

1010
# elasticstack_elasticsearch_watch (Resource)
1111

12-
Manage Watches. See, https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api.html
12+
Manage Watches. See the [Watcher API documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/watcher-api.html) for more details.
1313

1414
## Example Usage
1515

internal/clients/elasticsearch/cluster.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
1111
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1212
"github.com/elastic/terraform-provider-elasticstack/internal/models"
13+
fwdiag "github.com/hashicorp/terraform-plugin-framework/diag"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1415
)
1516

@@ -234,68 +235,68 @@ func GetSettings(ctx context.Context, apiClient *clients.ApiClient) (map[string]
234235
return clusterSettings, diags
235236
}
236237

237-
func GetScript(ctx context.Context, apiClient *clients.ApiClient, id string) (*models.Script, diag.Diagnostics) {
238+
func GetScript(ctx context.Context, apiClient *clients.ApiClient, id string) (*models.Script, fwdiag.Diagnostics) {
238239
esClient, err := apiClient.GetESClient()
239240
if err != nil {
240-
return nil, diag.FromErr(err)
241+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to get ES client", err.Error())}
241242
}
242243
res, err := esClient.GetScript(id, esClient.GetScript.WithContext(ctx))
243244
if err != nil {
244-
return nil, diag.FromErr(err)
245+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to get script", err.Error())}
245246
}
246247
defer res.Body.Close()
247248
if res.StatusCode == http.StatusNotFound {
248249
return nil, nil
249250
}
250-
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to get stored script: %s", id)); diags.HasError() {
251+
if diags := diagutil.CheckErrorFromFW(res, fmt.Sprintf("Unable to get stored script: %s", id)); diags.HasError() {
251252
return nil, diags
252253
}
253254
var scriptResponse struct {
254255
Script *models.Script `json:"script"`
255256
}
256257
if err := json.NewDecoder(res.Body).Decode(&scriptResponse); err != nil {
257-
return nil, diag.FromErr(err)
258+
return nil, fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to decode script response", err.Error())}
258259
}
259260

260261
return scriptResponse.Script, nil
261262
}
262263

263-
func PutScript(ctx context.Context, apiClient *clients.ApiClient, script *models.Script) diag.Diagnostics {
264+
func PutScript(ctx context.Context, apiClient *clients.ApiClient, script *models.Script) fwdiag.Diagnostics {
264265
req := struct {
265266
Script *models.Script `json:"script"`
266267
}{
267268
script,
268269
}
269270
scriptBytes, err := json.Marshal(req)
270271
if err != nil {
271-
return diag.FromErr(err)
272+
return fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to marshal script", err.Error())}
272273
}
273274
esClient, err := apiClient.GetESClient()
274275
if err != nil {
275-
return diag.FromErr(err)
276+
return fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to get ES client", err.Error())}
276277
}
277278
res, err := esClient.PutScript(script.ID, bytes.NewReader(scriptBytes), esClient.PutScript.WithContext(ctx), esClient.PutScript.WithScriptContext(script.Context))
278279
if err != nil {
279-
return diag.FromErr(err)
280+
return fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to put script", err.Error())}
280281
}
281282
defer res.Body.Close()
282-
if diags := diagutil.CheckError(res, "Unable to put stored script"); diags.HasError() {
283+
if diags := diagutil.CheckErrorFromFW(res, "Unable to put stored script"); diags.HasError() {
283284
return diags
284285
}
285286
return nil
286287
}
287288

288-
func DeleteScript(ctx context.Context, apiClient *clients.ApiClient, id string) diag.Diagnostics {
289+
func DeleteScript(ctx context.Context, apiClient *clients.ApiClient, id string) fwdiag.Diagnostics {
289290
esClient, err := apiClient.GetESClient()
290291
if err != nil {
291-
return diag.FromErr(err)
292+
return fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to get ES client", err.Error())}
292293
}
293294
res, err := esClient.DeleteScript(id, esClient.DeleteScript.WithContext(ctx))
294295
if err != nil {
295-
return diag.FromErr(err)
296+
return fwdiag.Diagnostics{fwdiag.NewErrorDiagnostic("Failed to delete script", err.Error())}
296297
}
297298
defer res.Body.Close()
298-
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to delete script: %s", id)); diags.HasError() {
299+
if diags := diagutil.CheckErrorFromFW(res, fmt.Sprintf("Unable to delete script: %s", id)); diags.HasError() {
299300
return diags
300301
}
301302
return nil

internal/elasticsearch/cluster/script.go

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)