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

Validate node_roles and node_types are supported in the version used #683

Merged
merged 3 commits into from
Aug 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .changelog/683.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/deployment: Validates that the node_types/node_roles configuration used is supported by the specified Stack version.
```
4 changes: 4 additions & 0 deletions ec/ecresource/deploymentresource/deployment/v2/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand Down Expand Up @@ -63,6 +64,9 @@ func DeploymentSchema() schema.Schema {

-> Read the [ESS stack version policy](https://www.elastic.co/guide/en/cloud/current/ec-version-policy.html#ec-version-policy-available) to understand which versions are available.`,
Required: true,
Validators: []validator.String{
isVersion{},
},
},
"region": schema.StringAttribute{
Description: "Elasticsearch Service (ESS) region where the deployment should be hosted. For Elastic Cloud Enterprise (ECE) installations, set to `\"ece-region\".",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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 v2

import (
"context"
"fmt"

"github.com/blang/semver"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

var _ validator.String = isVersion{}

type isVersion struct{}

func (r isVersion) Description(ctx context.Context) string {
return "Validates that the specified version is a valid semver."
}

func (r isVersion) MarkdownDescription(ctx context.Context) string {
return "Validates that the specified version is a valid semver."
}

// ValidateString should perform the validation.
func (v isVersion) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

version := req.ConfigValue.ValueString()
_, err := semver.Parse(version)
if err == nil {
return
}

resp.Diagnostics.AddAttributeError(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to add err to resp.Diagnostics?

req.Path,
fmt.Sprintf("[%s] is not a valid semver", req.Path),
fmt.Sprintf("The specified version [%s] is not a valid version string", version),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 v2

import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/require"
)

func TestVersionValidator_ValidateString(t *testing.T) {
tests := []struct {
name string
isValid bool
version types.String
}{
{
name: "should treat null values as valid",
isValid: true,
version: types.StringNull(),
},
{
name: "should treat unknown values as valid",
isValid: true,
version: types.StringUnknown(),
},
{
name: "should treat valid version strings as valid",
isValid: true,
version: types.StringValue("7.9.0"),
},
{
name: "should treat valid, tagged version strings as valid",
isValid: true,
version: types.StringValue("7.9.0-foo"),
},
{
name: "should treat invalid version strings as invalid",
isValid: false,
version: types.StringValue("not a real version"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := isVersion{}
resp := validator.StringResponse{}
v.ValidateString(context.Background(), validator.StringRequest{
ConfigValue: tt.version,
}, &resp)

require.Equal(t, tt.isValid, !resp.Diagnostics.HasError())
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 v2

import (
"context"
"fmt"

"github.com/blang/semver"
"github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/utils"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func VersionSupportsNodeRoles() validator.Set {
return versionSupportsNodeRoles{}
}

var _ validator.Set = versionSupportsNodeRoles{}

type versionSupportsNodeRoles struct{}

func (r versionSupportsNodeRoles) Description(ctx context.Context) string {
return "Validates the node_roles can only be defined if the stack version supports node roles."
}

func (r versionSupportsNodeRoles) MarkdownDescription(ctx context.Context) string {
return "Validates the node_roles can only be defined if the stack version supports node roles."
}

// ValidateString should perform the validation.
func (v versionSupportsNodeRoles) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

var version string
resp.Diagnostics = req.Config.GetAttribute(ctx, path.Root("version"), &version)
if resp.Diagnostics.HasError() {
return
}

parsedVersion, err := semver.Parse(version)
if err != nil {
// Ignore this error, it's validated as part of the version schema definition
return
}

if utils.DataTiersVersion.GT(parsedVersion) {
resp.Diagnostics.AddAttributeError(
req.Path,
fmt.Sprintf("[%s] not supported in the specified stack version", req.Path),
fmt.Sprintf("The resources stack version [%s] does not support node_roles. Either convert your deployment resource to use node_types or use a stack version of at least [%s]", version, utils.DataTiersVersion),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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 v2_test

import (
"context"
"testing"

deploymentv2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/deployment/v2"
v2 "github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/elasticsearch/v2"
"github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/utils"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/stretchr/testify/require"
)

func TestNodeRolesValidator_ValidateSet(t *testing.T) {
tests := []struct {
name string
version string
attrValue types.Set
isValid bool
}{
{
name: "should treat null attribute values as valid",
version: utils.DataTiersVersion.String(),
attrValue: types.SetNull(types.StringType),
isValid: true,
},
{
name: "should treat unknown attribute values as valid",
version: utils.DataTiersVersion.String(),
attrValue: types.SetUnknown(types.StringType),
isValid: true,
},
{
name: "should fail if the deployment version is lt the threshold and the attribute is set",
version: "7.0.0",
attrValue: types.SetValueMust(types.StringType, []attr.Value{types.StringValue("hot")}),
isValid: false,
},
{
name: "should pass if the deployment version is gte the threshold and the attribute is set",
version: utils.DataTiersVersion.String(),
attrValue: types.SetValueMust(types.StringType, []attr.Value{types.StringValue("hot")}),
isValid: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
v := v2.VersionSupportsNodeRoles()
config := tftypesValueFromGoTypeValue(t, &deploymentv2.Deployment{
Version: tt.version,
}, deploymentv2.DeploymentSchema().Type())
resp := validator.SetResponse{}
v.ValidateSet(context.Background(), validator.SetRequest{
ConfigValue: tt.attrValue,
Config: tfsdk.Config{
Raw: config,
Schema: deploymentv2.DeploymentSchema(),
},
}, &resp)

require.Equal(t, tt.isValid, !resp.Diagnostics.HasError())
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 v2

import (
"context"
"fmt"

"github.com/blang/semver"
"github.com/elastic/terraform-provider-ec/ec/ecresource/deploymentresource/utils"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

var _ validator.String = versionSupportsNodeTypes{}

func VersionSupportsNodeTypes() validator.String {
return versionSupportsNodeTypes{}
}

type versionSupportsNodeTypes struct{}

func (r versionSupportsNodeTypes) Description(ctx context.Context) string {
return "Validates the node_types can only be defined if the stack version supports node types."
}

func (r versionSupportsNodeTypes) MarkdownDescription(ctx context.Context) string {
return "Validates the node_types can only be defined if the stack version supports node types."
}

// ValidateString should perform the validation.
func (v versionSupportsNodeTypes) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

var version string
resp.Diagnostics = req.Config.GetAttribute(ctx, path.Root("version"), &version)
if resp.Diagnostics.HasError() {
return
}

parsedVersion, err := semver.Parse(version)
if err != nil {
// Ignore this error, it's validated as part of the version schema definition
return
}

if utils.MinVersionWithoutNodeTypes.LTE(parsedVersion) {
resp.Diagnostics.AddAttributeError(
req.Path,
fmt.Sprintf("[%s] not supported in the specified stack version", req.Path),
fmt.Sprintf("The resources stack version [%s] does not support node_types. Either convert your deployment resource to use node_roles or use a stack version less than [%s]", version, utils.MinVersionWithoutNodeTypes),
)
}
}
Loading