Skip to content

Fixup data views in 8.14 #663

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

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}
Loading