Skip to content

Commit 8c2fb9a

Browse files
committed
support for transform start/stop
1 parent a19faaf commit 8c2fb9a

File tree

6 files changed

+64
-28
lines changed

6 files changed

+64
-28
lines changed

docs/resources/elasticsearch_transform.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
# generated by https://github.com/hashicorp/terraform-plugin-docs
3-
page_title: "elasticstack_elasticsearch_transform Resource - terraform-provider-elasticstack"
4-
subcategory: ""
3+
page_title: 'elasticstack_elasticsearch_transform Resource - terraform-provider-elasticstack'
4+
subcategory: ''
55
description: |-
66
Manages Elasticsearch transforms. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html
77
---
@@ -59,11 +59,14 @@ resource "elasticstack_elasticsearch_transform" "transform_with_pivot" {
5959
}
6060
}
6161
62+
enabled = false
63+
6264
defer_validation = false
6365
}
6466
```
6567

6668
<!-- schema generated by tfplugindocs -->
69+
6770
## Schema
6871

6972
### Required
@@ -77,20 +80,22 @@ resource "elasticstack_elasticsearch_transform" "transform_with_pivot" {
7780
- `defer_validation` (Boolean) When true, deferrable validations are not run upon creation, but rather when the transform is started. This behavior may be desired if the source index does not exist until after the transform is created.
7881
- `description` (String) Free text description of the transform.
7982
- `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))
83+
- `enabled` (Boolean) Controls wether the transform is started or stopped. Default is `false` (stopped).
8084
- `frequency` (String) The interval between checks for changes in the source indices when the transform is running continuously. Defaults to `1m`.
8185
- `latest` (String) The latest method transforms the data by finding the latest document for each unique key. JSON definition expected. Either 'pivot' or 'latest' must be present.
8286
- `metadata` (String) Defines optional transform metadata.
8387
- `pivot` (String) The pivot method transforms the data by aggregating and grouping it. JSON definition expected. Either 'pivot' or 'latest' must be present.
8488
- `retention_policy` (Block List, Max: 1) Defines a retention policy for the transform. (see [below for nested schema](#nestedblock--retention_policy))
8589
- `settings` (Block List, Max: 1) Defines optional transform settings. (see [below for nested schema](#nestedblock--settings))
8690
- `sync` (Block List, Max: 1) Defines the properties transforms require to run continuously. (see [below for nested schema](#nestedblock--sync))
87-
- `timeout` (String) Period to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`.
91+
- `timeout` (String) Period to wait for a response from Elastisearch when performing any management operation. If no response is received before the timeout expires, the operation fails and returns an error. Defaults to `30s`.
8892

8993
### Read-Only
9094

9195
- `id` (String) Internal identifier of the resource
9296

9397
<a id="nestedblock--destination"></a>
98+
9499
### Nested Schema for `destination`
95100

96101
Required:
@@ -101,8 +106,8 @@ Optional:
101106

102107
- `pipeline` (String) The unique identifier for an ingest pipeline.
103108

104-
105109
<a id="nestedblock--source"></a>
110+
106111
### Nested Schema for `source`
107112

108113
Required:
@@ -114,8 +119,8 @@ Optional:
114119
- `query` (String) A query clause that retrieves a subset of data from the source index.
115120
- `runtime_mappings` (String) Definitions of search-time runtime fields that can be used by the transform.
116121

117-
118122
<a id="nestedblock--elasticsearch_connection"></a>
123+
119124
### Nested Schema for `elasticsearch_connection`
120125

121126
Optional:
@@ -132,25 +137,25 @@ Optional:
132137
- `password` (String, Sensitive) Password to use for API authentication to Elasticsearch.
133138
- `username` (String) Username to use for API authentication to Elasticsearch.
134139

135-
136140
<a id="nestedblock--retention_policy"></a>
141+
137142
### Nested Schema for `retention_policy`
138143

139144
Required:
140145

141146
- `time` (Block List, Min: 1, Max: 1) Specifies that the transform uses a time field to set the retention policy. (see [below for nested schema](#nestedblock--retention_policy--time))
142147

143148
<a id="nestedblock--retention_policy--time"></a>
149+
144150
### Nested Schema for `retention_policy.time`
145151

146152
Required:
147153

148154
- `field` (String) The date field that is used to calculate the age of the document.
149155
- `max_age` (String) Specifies the maximum age of a document in the destination index.
150156

151-
152-
153157
<a id="nestedblock--settings"></a>
158+
154159
### Nested Schema for `settings`
155160

156161
Optional:
@@ -163,15 +168,16 @@ Optional:
163168
- `num_failure_retries` (Number) Defines the number of retries on a recoverable failure before the transform task is marked as failed. The default value is the cluster-level setting num_transform_failure_retries.
164169
- `unattended` (Boolean) In unattended mode, the transform retries indefinitely in case of an error which means the transform never fails. Defaults to false.
165170

166-
167171
<a id="nestedblock--sync"></a>
172+
168173
### Nested Schema for `sync`
169174

170175
Required:
171176

172177
- `time` (Block List, Min: 1, Max: 1) Specifies that the transform uses a time field to synchronize the source and destination indices. (see [below for nested schema](#nestedblock--sync--time))
173178

174179
<a id="nestedblock--sync--time"></a>
180+
175181
### Nested Schema for `sync.time`
176182

177183
Required:
@@ -181,5 +187,3 @@ Required:
181187
Optional:
182188

183189
- `delay` (String) The time delay between the current time and the latest input data time. The default value is 60s.
184-
185-

examples/resources/elasticstack_elasticsearch_transform/resource.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ resource "elasticstack_elasticsearch_transform" "transform_with_pivot" {
4444
}
4545
}
4646

47+
enabled = false
48+
4749
defer_validation = false
48-
}
50+
}

internal/clients/elasticsearch/index.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ func DeleteIngestPipeline(ctx context.Context, apiClient *clients.ApiClient, nam
546546
}
547547

548548
func PutTransform(ctx context.Context, apiClient *clients.ApiClient, transform *models.Transform, params *models.PutTransformParams) diag.Diagnostics {
549-
fmt.Println("entering PutTransform")
549+
fmt.Println("entering PutTransform for", transform.Name)
550550
var diags diag.Diagnostics
551551
transformBytes, err := json.Marshal(transform)
552552
if err != nil {
@@ -576,11 +576,18 @@ func PutTransform(ctx context.Context, apiClient *clients.ApiClient, transform *
576576
return diags
577577
}
578578

579+
if params.Enabled {
580+
_, err := esClient.TransformStartTransform(transform.Name)
581+
if err != nil {
582+
return diag.FromErr(err)
583+
}
584+
}
585+
579586
return diags
580587
}
581588

582589
func GetTransform(ctx context.Context, apiClient *clients.ApiClient, name *string) (*models.Transform, diag.Diagnostics) {
583-
fmt.Println("entering GetTransform for ", *name)
590+
fmt.Println("entering GetTransform for", *name)
584591
var diags diag.Diagnostics
585592
esClient, err := apiClient.GetESClient()
586593
if err != nil {
@@ -615,7 +622,7 @@ func GetTransform(ctx context.Context, apiClient *clients.ApiClient, name *strin
615622
}
616623

617624
func UpdateTransform(ctx context.Context, apiClient *clients.ApiClient, transform *models.Transform, params *models.UpdateTransformParams) diag.Diagnostics {
618-
fmt.Println("entering UpdateTransform")
625+
fmt.Println("entering UpdateTransform with Enabled", params.Enabled)
619626
var diags diag.Diagnostics
620627
transformBytes, err := json.Marshal(transform)
621628
if err != nil {
@@ -645,24 +652,36 @@ func UpdateTransform(ctx context.Context, apiClient *clients.ApiClient, transfor
645652
return diags
646653
}
647654

655+
if params.Enabled {
656+
_, err := esClient.TransformStartTransform(transform.Name)
657+
if err != nil {
658+
return diag.FromErr(err)
659+
}
660+
} else {
661+
_, err := esClient.TransformStopTransform(transform.Name)
662+
if err != nil {
663+
return diag.FromErr(err)
664+
}
665+
}
666+
648667
return diags
649668
}
650669

651-
func DeleteTransform(ctx context.Context, apiClient *clients.ApiClient, name string) diag.Diagnostics {
652-
fmt.Println("entering DeleteTransform for ", name)
670+
func DeleteTransform(ctx context.Context, apiClient *clients.ApiClient, name *string) diag.Diagnostics {
671+
fmt.Println("entering DeleteTransform for", *name)
653672
var diags diag.Diagnostics
654673

655674
esClient, err := apiClient.GetESClient()
656675
if err != nil {
657676
return diag.FromErr(err)
658677
}
659678

660-
res, err := esClient.TransformDeleteTransform(name, esClient.TransformDeleteTransform.WithForce(true), esClient.TransformDeleteTransform.WithContext(ctx))
679+
res, err := esClient.TransformDeleteTransform(*name, esClient.TransformDeleteTransform.WithForce(true), esClient.TransformDeleteTransform.WithContext(ctx))
661680
if err != nil {
662681
return diag.FromErr(err)
663682
}
664683
defer res.Body.Close()
665-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to delete the transform: %s", name)); diags.HasError() {
684+
if diags := utils.CheckError(res, fmt.Sprintf("Unable to delete the transform: %s", *name)); diags.HasError() {
666685
return diags
667686
}
668687

internal/elasticsearch/transform/transform.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,17 @@ func ResourceTransform() *schema.Resource {
248248
},
249249
"timeout": {
250250
Type: schema.TypeString,
251-
Description: "Period to wait for a response. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`.",
251+
Description: "Period to wait for a response from Elastisearch when performing any management operation. If no response is received before the timeout expires, the operation fails and returns an error. Defaults to `30s`.",
252252
Optional: true,
253253
Default: "30s",
254254
ValidateFunc: utils.StringIsDuration,
255255
},
256+
"enabled": {
257+
Type: schema.TypeBool,
258+
Description: "Controls wether the transform is started or stopped. Default is `false` (stopped).",
259+
Optional: true,
260+
Default: false,
261+
},
256262
}
257263

258264
utils.AddConnectionSchema(transformSchema)
@@ -269,7 +275,6 @@ func ResourceTransform() *schema.Resource {
269275
}
270276

271277
func resourceTransformCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
272-
fmt.Println("entering resourceTransformCreate")
273278

274279
client, diags := clients.NewApiClient(d, meta)
275280
if diags.HasError() {
@@ -297,6 +302,8 @@ func resourceTransformCreate(ctx context.Context, d *schema.ResourceData, meta i
297302
}
298303
params.Timeout = timeout
299304

305+
params.Enabled = d.Get("enabled").(bool)
306+
300307
if diags := elasticsearch.PutTransform(ctx, client, transform, &params); diags.HasError() {
301308
return diags
302309
}
@@ -306,7 +313,7 @@ func resourceTransformCreate(ctx context.Context, d *schema.ResourceData, meta i
306313
}
307314

308315
func resourceTransformRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
309-
fmt.Println("entering resourceTransformRead")
316+
310317
client, diags := clients.NewApiClient(d, meta)
311318
if diags.HasError() {
312319
return diags
@@ -335,7 +342,6 @@ func resourceTransformRead(ctx context.Context, d *schema.ResourceData, meta int
335342
}
336343

337344
func resourceTransformUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
338-
fmt.Println("entering resourceTransformUpdate")
339345

340346
client, diags := clients.NewApiClient(d, meta)
341347
if diags.HasError() {
@@ -366,6 +372,8 @@ func resourceTransformUpdate(ctx context.Context, d *schema.ResourceData, meta i
366372
}
367373
params.Timeout = timeout
368374

375+
params.Enabled = d.Get("enabled").(bool)
376+
369377
if diags := elasticsearch.UpdateTransform(ctx, client, updatedTransform, &params); diags.HasError() {
370378
return diags
371379
}
@@ -374,7 +382,7 @@ func resourceTransformUpdate(ctx context.Context, d *schema.ResourceData, meta i
374382
}
375383

376384
func resourceTransformDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
377-
fmt.Println("entering resourceTransformDelete")
385+
378386
client, diags := clients.NewApiClient(d, meta)
379387
if diags.HasError() {
380388
return diags
@@ -386,7 +394,7 @@ func resourceTransformDelete(ctx context.Context, d *schema.ResourceData, meta i
386394
return diags
387395
}
388396

389-
if diags := elasticsearch.DeleteTransform(ctx, client, compId.ResourceId); diags.HasError() {
397+
if diags := elasticsearch.DeleteTransform(ctx, client, &compId.ResourceId); diags.HasError() {
390398
return diags
391399
}
392400

internal/elasticsearch/transform/transform_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
package transform_test
22

33
import (
4-
//"context"
54
"fmt"
6-
//"regexp"
75
"testing"
86

97
"github.com/elastic/terraform-provider-elasticstack/internal/acctest"
108
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
11-
//"github.com/elastic/terraform-provider-elasticstack/internal/elasticsearch/transform"
129
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1310
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1411
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
@@ -128,6 +125,7 @@ resource "elasticstack_elasticsearch_transform" "test_pivot" {
128125
}
129126
})
130127
frequency = "5m"
128+
enabled = false
131129
132130
defer_validation = true
133131
timeout = "1m"
@@ -171,6 +169,7 @@ resource "elasticstack_elasticsearch_transform" "test_pivot" {
171169
}
172170
})
173171
frequency = "10m"
172+
enabled = true
174173
175174
defer_validation = true
176175
timeout = "1m"
@@ -201,6 +200,7 @@ resource "elasticstack_elasticsearch_transform" "test_latest" {
201200
"sort": "order_date"
202201
})
203202
frequency = "2m"
203+
enabled = false
204204
205205
defer_validation = true
206206
timeout = "1m"
@@ -270,6 +270,7 @@ resource "elasticstack_elasticsearch_transform" "test_pivot" {
270270
}
271271
})
272272
frequency = "5m"
273+
enabled = false
273274
274275
defer_validation = false
275276
timeout = "1m"

internal/models/transform.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,13 @@ type TransformSettings struct {
6262
type PutTransformParams struct {
6363
DeferValidation bool
6464
Timeout time.Duration
65+
Enabled bool
6566
}
6667

6768
type UpdateTransformParams struct {
6869
DeferValidation bool
6970
Timeout time.Duration
71+
Enabled bool
7072
}
7173

7274
type GetTransformResponse struct {

0 commit comments

Comments
 (0)