From 91141c5a9dbba42fd90ad6b4820cbbd779d3a5c0 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 15 May 2024 21:04:34 +1000 Subject: [PATCH 1/4] Support Serverless build dates --- internal/models/models.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/models/models.go b/internal/models/models.go index 35fb78f6b..4f4918a14 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -7,6 +7,29 @@ import ( "time" ) +type BuildDate struct { + time.Time +} + +func (b *BuildDate) UnmarshalJSON(dateBytes []byte) error { + dateStr := strings.Trim(string(dateBytes), "\"") + if dateStr == "null" { + b.Time = time.Time{} + return nil + } + + t, err := time.Parse("2006-01-02T15:04:05Z07:00", dateStr) + if err != nil { + t, err = time.Parse("2006-01-02", dateStr) + if err != nil { + return err + } + } + + b.Time = t + return nil +} + type ClusterInfo struct { Name string `json:"name"` ClusterName string `json:"cluster_name"` @@ -16,7 +39,7 @@ type ClusterInfo struct { BuildType string `json:"build_type"` BuildHash string `json:"build_hash"` BuildFlavor string `json:"build_flavor"` - BuildDate time.Time `json:"build_date"` + BuildDate BuildDate `json:"build_date"` BuildSnapshot bool `json:"build_snapshot"` LuceneVersion string `json:"lucene_version"` MinimumWireCompatibilityVersion string `json:"minimum_wire_compatibility_version"` From cc176bedf414649a01aedad2a39990ab96aab997 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 15 May 2024 21:05:12 +1000 Subject: [PATCH 2/4] Ignore wait_for_active_shards and master_timeout values when configuring Serverless projects --- internal/clients/api_client.go | 9 ++++++++ internal/elasticsearch/index/index.go | 30 ++++++++++++++++++--------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/internal/clients/api_client.go b/internal/clients/api_client.go index 1887818e2..e60e8d349 100644 --- a/internal/clients/api_client.go +++ b/internal/clients/api_client.go @@ -336,6 +336,15 @@ func (a *ApiClient) ServerVersion(ctx context.Context) (*version.Version, diag.D return serverVersion, nil } +func (a *ApiClient) ServerFlavor(ctx context.Context) (string, diag.Diagnostics) { + info, diags := a.serverInfo(ctx) + if diags.HasError() { + return "", diags + } + + return info.Version.BuildFlavor, nil +} + func (a *ApiClient) ClusterID(ctx context.Context) (*string, diag.Diagnostics) { info, diags := a.serverInfo(ctx) if diags.HasError() { diff --git a/internal/elasticsearch/index/index.go b/internal/elasticsearch/index/index.go index f193ae3e0..5a861a03e 100644 --- a/internal/elasticsearch/index/index.go +++ b/internal/elasticsearch/index/index.go @@ -544,13 +544,13 @@ If specified, this mapping can include: field names, [field data types](https:// }, "wait_for_active_shards": { Type: schema.TypeString, - Description: "The number of shard copies that must be active before proceeding with the operation. Set to `all` or any positive integer up to the total number of shards in the index (number_of_replicas+1). Default: `1`, the primary shard.", + Description: "The number of shard copies that must be active before proceeding with the operation. Set to `all` or any positive integer up to the total number of shards in the index (number_of_replicas+1). Default: `1`, the primary shard. This value is ignored when running against Serverless projects.", Optional: true, Default: "1", }, "master_timeout": { Type: schema.TypeString, - Description: "Period to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`.", + Description: "Period to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`. This value is ignored when running against Serverless projects.", Optional: true, Default: "30s", ValidateFunc: utils.StringIsDuration, @@ -759,25 +759,35 @@ func resourceIndexCreate(ctx context.Context, d *schema.ResourceData, meta inter } } - params := models.PutIndexParams{ - WaitForActiveShards: d.Get("wait_for_active_shards").(string), - IncludeTypeName: d.Get("include_type_name").(bool), - } serverVersion, diags := client.ServerVersion(ctx) if diags.HasError() { return diags } + + serverFlavor, diags := client.ServerFlavor(ctx) + if diags.HasError() { + return diags + } + + params := models.PutIndexParams{ + IncludeTypeName: d.Get("include_type_name").(bool), + } + if includeTypeName := d.Get("include_type_name").(bool); includeTypeName { if serverVersion.GreaterThanOrEqual(includeTypeNameMinUnsupportedVersion) { return diag.FromErr(fmt.Errorf("'include_type_name' field is supported only for elasticsearch v7.x")) } params.IncludeTypeName = includeTypeName } - masterTimeout, err := time.ParseDuration(d.Get("master_timeout").(string)) - if err != nil { - return diag.FromErr(err) + + if serverFlavor != "serverless" { + params.WaitForActiveShards = d.Get("wait_for_active_shards").(string) + masterTimeout, err := time.ParseDuration(d.Get("master_timeout").(string)) + if err != nil { + return diag.FromErr(err) + } + params.MasterTimeout = masterTimeout } - params.MasterTimeout = masterTimeout timeout, err := time.ParseDuration(d.Get("timeout").(string)) if err != nil { From c6f0a22eb66a78d675f6384cf173dc796a786520 Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 15 May 2024 21:09:02 +1000 Subject: [PATCH 3/4] Regenerate docs --- docs/resources/elasticsearch_index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/resources/elasticsearch_index.md b/docs/resources/elasticsearch_index.md index 5d1e3a08a..89768961f 100644 --- a/docs/resources/elasticsearch_index.md +++ b/docs/resources/elasticsearch_index.md @@ -94,7 +94,7 @@ If specified, this mapping can include: field names, [field data types](https:// **NOTE:** - Changing datatypes in the existing _mappings_ will force index to be re-created. - Removing field will be ignored by default same as elasticsearch. You need to recreate the index to remove field completely. -- `master_timeout` (String) Period to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`. +- `master_timeout` (String) Period to wait for a connection to the master node. If no response is received before the timeout expires, the request fails and returns an error. Defaults to `30s`. This value is ignored when running against Serverless projects. - `max_docvalue_fields_search` (Number) The maximum number of `docvalue_fields` that are allowed in a query. - `max_inner_result_window` (Number) The maximum value of `from + size` for inner hits definition and top hits aggregations to this index. - `max_ngram_diff` (Number) The maximum allowed difference between min_gram and max_gram for NGramTokenizer and NGramTokenFilter. @@ -130,7 +130,7 @@ If specified, this mapping can include: field names, [field data types](https:// - `sort_order` (List of String) The direction to sort shards in. Accepts `asc`, `desc`. - `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`. - `unassigned_node_left_delayed_timeout` (String) Time to delay the allocation of replica shards which become unassigned because a node has left, in time units, e.g. `10s` -- `wait_for_active_shards` (String) The number of shard copies that must be active before proceeding with the operation. Set to `all` or any positive integer up to the total number of shards in the index (number_of_replicas+1). Default: `1`, the primary shard. +- `wait_for_active_shards` (String) The number of shard copies that must be active before proceeding with the operation. Set to `all` or any positive integer up to the total number of shards in the index (number_of_replicas+1). Default: `1`, the primary shard. This value is ignored when running against Serverless projects. ### Read-Only From 84281962a21fe66a6efc8f777352fcd85c47e8fb Mon Sep 17 00:00:00 2001 From: Toby Brain Date: Wed, 15 May 2024 21:24:51 +1000 Subject: [PATCH 4/4] Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ccec2224a..90f69126e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Prevent a provider panic in `elasticstack_kibana_data_view` when a `field_format` does not include a `pattern`. ([#619](https://github.com/elastic/terraform-provider-elasticstack/pull/619/files)) - Fixed a bug where the `id` attribute for `elasticstack_kibana_slo` resources was ignored by renaming the attribute to `slo_id`. ([#622](https://github.com/elastic/terraform-provider-elasticstack/pull/622)) - Fixed a bug where the `rule_id` attribute for `elasticstack_kibana_alerting_rule` was ignored. ([#626](https://github.com/elastic/terraform-provider-elasticstack/pull/626)) +- Fix provider crash when running against Serverless projects ([#630](https://github.com/elastic/terraform-provider-elasticstack/pull/630)) ### Added