Skip to content
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

Handle empty strings in config values #700

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions ec/ecresource/deploymentresource/apm/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,30 @@ func ApmConfigSchema() schema.Attribute {
"user_settings_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_yaml' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (This field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_yaml' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_json' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (These field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_json' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
},
}
Expand Down
12 changes: 12 additions & 0 deletions ec/ecresource/deploymentresource/elasticsearch/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,30 @@ func elasticsearchConfigSchema() schema.Attribute {
"user_settings_json": schema.StringAttribute{
Description: `JSON-formatted user level "elasticsearch.yml" setting overrides`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_json": schema.StringAttribute{
Description: `JSON-formatted admin (ECE) level "elasticsearch.yml" setting overrides`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_yaml": schema.StringAttribute{
Description: `YAML-formatted user level "elasticsearch.yml" setting overrides`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_yaml": schema.StringAttribute{
Description: `YAML-formatted admin (ECE) level "elasticsearch.yml" setting overrides`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
},
}
Expand Down
12 changes: 12 additions & 0 deletions ec/ecresource/deploymentresource/enterprisesearch/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,30 @@ func EnterpriseSearchSchema() schema.Attribute {
"user_settings_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_yaml' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (This field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_yaml' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_json' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (These field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_json' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,30 @@ func IntegrationsServerSchema() schema.Attribute {
"user_settings_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_yaml' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (This field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_yaml' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_json' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (These field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_json' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
},
},
Expand Down
12 changes: 12 additions & 0 deletions ec/ecresource/deploymentresource/kibana/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,30 @@ func KibanaSchema() schema.Attribute {
"user_settings_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_yaml' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (This field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_json": schema.StringAttribute{
Description: `An arbitrary JSON object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_yaml' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing (non-admin) cluster owners to set their parameters (only one of this and 'user_settings_json' is allowed), provided they are on the whitelist ('user_settings_whitelist') and not on the blacklist ('user_settings_blacklist'). (These field together with 'user_settings_override*' and 'system_settings' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
"user_settings_override_yaml": schema.StringAttribute{
Description: `An arbitrary YAML object allowing ECE admins owners to set clusters' parameters (only one of this and 'user_settings_override_json' is allowed), ie in addition to the documented 'system_settings'. (This field together with 'system_settings' and 'user_settings*' defines the total set of resource settings)`,
Optional: true,
PlanModifiers: []planmodifier.String{
planmodifiers.SetNullWhenEmptyString(),
},
},
},
},
Expand Down
53 changes: 53 additions & 0 deletions ec/internal/planmodifiers/set_null_when_empty_string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package planmodifiers

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

func SetNullWhenEmptyString() planmodifier.String {
return setNullWhenEmptyString{}
}

type setNullWhenEmptyString struct{}

func (m setNullWhenEmptyString) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) {
if req.PlanValue.IsNull() || req.PlanValue.IsUnknown() {
return
}

if req.PlanValue.ValueString() != "" {
return
}

resp.PlanValue = types.StringNull()
}

// Description returns a human-readable description of the plan modifier.
func (r setNullWhenEmptyString) Description(ctx context.Context) string {
return "Set the plan value to null if currently configured to null."
}

// MarkdownDescription returns a markdown description of the plan modifier.
func (r setNullWhenEmptyString) MarkdownDescription(ctx context.Context) string {
return "Set the plan value to null if currently configured to null."
}
72 changes: 72 additions & 0 deletions ec/internal/planmodifiers/set_null_when_empty_string_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package planmodifiers_test

import (
"context"
"testing"

"github.com/elastic/terraform-provider-ec/ec/internal/planmodifiers"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/require"
)

func TestSetNullWhenEmptyString(t *testing.T) {
tests := []struct {
name string
planValue types.String
expectedPlanValue types.String
}{
{
name: "should do nothing when the plan value is null",
planValue: types.StringNull(),
expectedPlanValue: types.StringNull(),
},
{
name: "should do nothing when the plan value is unknown",
planValue: types.StringUnknown(),
expectedPlanValue: types.StringUnknown(),
},
{
name: "should do nothing when the plan value is not empty",
planValue: types.StringValue("hello"),
expectedPlanValue: types.StringValue("hello"),
},
{
name: "should set the plan value to null when the plan value is empty",
planValue: types.StringValue(""),
expectedPlanValue: types.StringNull(),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
modifier := planmodifiers.SetNullWhenEmptyString()

resp := planmodifier.StringResponse{
PlanValue: tt.planValue,
}
modifier.PlanModifyString(context.Background(), planmodifier.StringRequest{
PlanValue: tt.planValue,
}, &resp)

require.Equal(t, tt.expectedPlanValue, resp.PlanValue)
})
}
}