-
Notifications
You must be signed in to change notification settings - Fork 89
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
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
ec/ecresource/deploymentresource/deployment/v2/version_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
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), | ||
) | ||
} |
73 changes: 73 additions & 0 deletions
73
ec/ecresource/deploymentresource/deployment/v2/version_validator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
ec/ecresource/deploymentresource/elasticsearch/v2/node_roles_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
ec/ecresource/deploymentresource/elasticsearch/v2/node_roles_validator_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
ec/ecresource/deploymentresource/elasticsearch/v2/node_types_validator.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
toresp.Diagnostics
?