Skip to content

Commit

Permalink
Fixup data views in 8.14 (#663)
Browse files Browse the repository at this point in the history
* Fixup data view creation in 8.14

Previously Kibana was treating an empty ID as un-set. This change leaves the ID undefined in the json body if it's unknown in the TF plan

* Generate docs

* Changelog

* Also enforce that the title isn't empty
  • Loading branch information
tobio authored Jun 12, 2024
1 parent 0b4e566 commit 332281f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
## [Unreleased]

### Breaking changes
- The `title` attribute is now required in the elasticstack_kibana_data_view resource. In practice the resource didn't work without this set, the schema now enforces it's correctly configured.

### Fixed

- Populate policy_id when importing fleet policies and integrations ([#646](https://github.com/elastic/terraform-provider-elasticstack/pull/646))
- Fix alerting rule update crash when backend responds with HTTP 4xx. ([#649](https://github.com/elastic/terraform-provider-elasticstack/pull/649))
- Fix the elasticstack_kibana_data_view resource when not specifying an `id` and running against Kibana 8.14 ([#663](https://github.com/elastic/terraform-provider-elasticstack/pull/663))
- Support allow_write_after_shrink when managing ILM policies ([#662](https://github.com/elastic/terraform-provider-elasticstack/pull/662))

## [0.11.3] - 2024-05-16
Expand Down
5 changes: 4 additions & 1 deletion docs/resources/kibana_data_view.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ resource "elasticstack_kibana_data_view" "my_data_view" {
<a id="nestedatt--data_view"></a>
### Nested Schema for `data_view`

Required:

- `title` (String) Comma-separated list of data streams, indices, and aliases that you want to search. Supports wildcards (*).

Optional:

- `allow_no_index` (Boolean) Allows the Data view saved object to exist before the data is available.
Expand All @@ -58,7 +62,6 @@ Optional:
- `runtime_field_map` (Attributes Map) Map of runtime field definitions by field name. (see [below for nested schema](#nestedatt--data_view--runtime_field_map))
- `source_filters` (List of String) List of field names you want to filter out in Discover.
- `time_field_name` (String) Timestamp field name, which you use for time-based Data views.
- `title` (String) Comma-separated list of data streams, indices, and aliases that you want to search. Supports wildcards (*).

<a id="nestedatt--data_view--field_attrs"></a>
### Nested Schema for `data_view.field_attrs`
Expand Down
13 changes: 10 additions & 3 deletions internal/kibana/data_view/acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,22 @@ func TestAccResourceDataView(t *testing.T) {
}

func testAccResourceDataViewPre8_8DV(indexName string) string {
return `
return fmt.Sprintf(`
provider "elasticstack" {
elasticsearch {}
kibana {}
}
resource "elasticstack_elasticsearch_index" "my_index" {
name = "%s"
deletion_protection = false
}
resource "elasticstack_kibana_data_view" "dv" {
data_view = {}
}`
data_view = {
title = "%s*"
}
}`, indexName, indexName)
}

func testAccResourceDataViewBasicDV(indexName string) string {
Expand Down
33 changes: 21 additions & 12 deletions internal/kibana/data_view/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/elastic/terraform-provider-elasticstack/generated/data_views"
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
Expand Down Expand Up @@ -60,10 +63,9 @@ func getSchema() schema.Schema {
Attributes: map[string]schema.Attribute{
"title": schema.StringAttribute{
Description: "Comma-separated list of data streams, indices, and aliases that you want to search. Supports wildcards (*).",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
Required: true,
Validators: []validator.String{
stringvalidator.LengthAtLeast(1),
},
},
"name": schema.StringAttribute{
Expand Down Expand Up @@ -406,16 +408,23 @@ func dataViewFromResponse(resp data_views.DataViewResponseObjectDataView) apiDat

func (m tfDataViewV0) ToCreateRequest(ctx context.Context, spaceID string) (data_views.CreateDataViewRequestObjectDataView, diag.Diagnostics) {
apiModel := data_views.CreateDataViewRequestObjectDataView{
Title: m.Title.ValueString(),
Name: m.Name.ValueStringPointer(),
Id: m.ID.ValueStringPointer(),
TimeFieldName: m.TimeFieldName.ValueStringPointer(),
AllowNoIndex: m.AllowNoIndex.ValueBoolPointer(),
Title: m.Title.ValueString(),
}

if utils.IsKnown(m.ID) {
apiModel.Id = m.ID.ValueStringPointer()
}

if utils.IsKnown(m.Name) {
apiModel.Name = m.Name.ValueStringPointer()
}

if utils.IsKnown(m.TimeFieldName) {
apiModel.TimeFieldName = m.TimeFieldName.ValueStringPointer()
}

// ES versions not supporting name (8.1-8.3) reject requests with this field supplied
if m.Name.IsUnknown() {
apiModel.Name = nil
if utils.IsKnown(m.AllowNoIndex) {
apiModel.AllowNoIndex = m.AllowNoIndex.ValueBoolPointer()
}

var sourceFilters []string
Expand Down
9 changes: 8 additions & 1 deletion internal/utils/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package utils

import "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func ExpandStringSet(set *schema.Set) []string {
var strs []string
Expand All @@ -9,3 +12,7 @@ func ExpandStringSet(set *schema.Set) []string {
}
return strs
}

func IsKnown(val attr.Value) bool {
return !(val.IsNull() || val.IsUnknown())
}

0 comments on commit 332281f

Please sign in to comment.