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

all: Migrate implementations to support terraform-plugin-framework version 0.17.0 #80

Merged
merged 4 commits into from Nov 30, 2022
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.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
31 changes: 31 additions & 0 deletions .changelog/80.txt
@@ -0,0 +1,31 @@
```release-note:breaking-change
all: Migrated implementations to support terraform-plugin-framework version 0.17.0 `datasource/schema`, `provider/schema`, and `resource/schema` packages with type-specific validation
```

```release-note:breaking-change
listvalidator: The `ValuesAre` validator has been removed and split into element type-specific validators in the same package, such as `StringValuesAre`
```

```release-note:breaking-change
mapvalidator: The `ValuesAre` validator has been removed and split into element type-specific validators in the same package, such as `StringValuesAre`
```

```release-note:breaking-change
metavalidator: The `All` and `Any` validators have been removed and split into type-specific packages, such as `stringvalidator.Any`
```

```release-note:breaking-change
schemavalidator: The `AlsoRequires`, `AtLeastOneOf`, `ConflictsWith`, and `ExactlyOneOf` validators have been removed and split into type-specific packages, such as `stringvalidator.ConflictsWith`
```

```release-note:breaking-change
setvalidator: The `ValuesAre` validator has been removed and split into element type-specific validators in the same package, such as `StringValuesAre`
```

```release-note:feature
boolvalidator: New package which contains boolean type specific validators
```

```release-note:feature
objectvalidator: New package which contains object type specific validators
```
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,3 +1,5 @@
go.work
go.work.sum
# JetBrains IDEs project files
.idea/
*.iws
23 changes: 23 additions & 0 deletions boolvalidator/also_requires.go
@@ -0,0 +1,23 @@
package boolvalidator

import (
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// AlsoRequires checks that a set of path.Expression has a non-null value,
// if the current attribute or block also has a non-null value.
//
// This implements the validation logic declaratively within the schema.
// Refer to [datasourcevalidator.RequiredTogether],
// [providervalidator.RequiredTogether], or [resourcevalidator.RequiredTogether]
// for declaring this type of validation outside the schema definition.
//
// Relative path.Expression will be resolved using the attribute or block
// being validated.
func AlsoRequires(expressions ...path.Expression) validator.Bool {
return schemavalidator.AlsoRequiresValidator{
PathExpressions: expressions,
}
}
28 changes: 28 additions & 0 deletions boolvalidator/also_requires_example_test.go
@@ -0,0 +1,28 @@
package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func ExampleAlsoRequires() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Optional: true,
Validators: []validator.Bool{
// Validate this attribute must be configured with other_attr.
boolvalidator.AlsoRequires(path.Expressions{
path.MatchRoot("other_attr"),
}...),
},
},
"other_attr": schema.StringAttribute{
Optional: true,
},
},
}
}
24 changes: 24 additions & 0 deletions boolvalidator/at_least_one_of.go
@@ -0,0 +1,24 @@
package boolvalidator

import (
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// AtLeastOneOf checks that of a set of path.Expression,
// including the attribute this validator is applied to,
// at least one has a non-null value.
//
// This implements the validation logic declaratively within the tfsdk.Schema.
// Refer to [datasourcevalidator.AtLeastOneOf],
// [providervalidator.AtLeastOneOf], or [resourcevalidator.AtLeastOneOf]
// for declaring this type of validation outside the schema definition.
//
// Any relative path.Expression will be resolved using the attribute being
// validated.
func AtLeastOneOf(expressions ...path.Expression) validator.Bool {
return schemavalidator.AtLeastOneOfValidator{
PathExpressions: expressions,
}
}
28 changes: 28 additions & 0 deletions boolvalidator/at_least_one_of_example_test.go
@@ -0,0 +1,28 @@
package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func ExampleAtLeastOneOf() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Optional: true,
Validators: []validator.Bool{
// Validate at least this attribute or other_attr should be configured.
boolvalidator.AtLeastOneOf(path.Expressions{
path.MatchRoot("other_attr"),
}...),
},
},
"other_attr": schema.StringAttribute{
Optional: true,
},
},
}
}
24 changes: 24 additions & 0 deletions boolvalidator/conflicts_with.go
@@ -0,0 +1,24 @@
package boolvalidator

import (
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// ConflictsWith checks that a set of path.Expression,
// including the attribute the validator is applied to,
// do not have a value simultaneously.
//
// This implements the validation logic declaratively within the schema.
// Refer to [datasourcevalidator.Conflicting],
// [providervalidator.Conflicting], or [resourcevalidator.Conflicting]
// for declaring this type of validation outside the schema definition.
//
// Relative path.Expression will be resolved using the attribute being
// validated.
func ConflictsWith(expressions ...path.Expression) validator.Bool {
return schemavalidator.ConflictsWithValidator{
PathExpressions: expressions,
}
}
28 changes: 28 additions & 0 deletions boolvalidator/conflicts_with_example_test.go
@@ -0,0 +1,28 @@
package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func ExampleConflictsWith() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Optional: true,
Validators: []validator.Bool{
// Validate this attribute must not be configured with other_attr.
boolvalidator.ConflictsWith(path.Expressions{
path.MatchRoot("other_attr"),
}...),
},
},
"other_attr": schema.StringAttribute{
Optional: true,
},
},
}
}
2 changes: 2 additions & 0 deletions boolvalidator/doc.go
@@ -0,0 +1,2 @@
// Package boolvalidator provides validators for types.Bool attributes.
package boolvalidator
25 changes: 25 additions & 0 deletions boolvalidator/exactly_one_of.go
@@ -0,0 +1,25 @@
package boolvalidator

import (
"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// ExactlyOneOf checks that of a set of path.Expression,
// including the attribute the validator is applied to,
// one and only one attribute has a value.
// It will also cause a validation error if none are specified.
//
// This implements the validation logic declaratively within the schema.
// Refer to [datasourcevalidator.ExactlyOneOf],
// [providervalidator.ExactlyOneOf], or [resourcevalidator.ExactlyOneOf]
// for declaring this type of validation outside the schema definition.
//
// Relative path.Expression will be resolved using the attribute being
// validated.
func ExactlyOneOf(expressions ...path.Expression) validator.Bool {
return schemavalidator.ExactlyOneOfValidator{
PathExpressions: expressions,
}
}
28 changes: 28 additions & 0 deletions boolvalidator/exactly_one_of_example_test.go
@@ -0,0 +1,28 @@
package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func ExampleExactlyOneOf() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Optional: true,
Validators: []validator.Bool{
// Validate only this attribute or other_attr is configured.
boolvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("other_attr"),
}...),
},
},
"other_attr": schema.StringAttribute{
Optional: true,
},
},
}
}
54 changes: 54 additions & 0 deletions float64validator/all.go
@@ -0,0 +1,54 @@
package float64validator

import (
"context"
"fmt"
"strings"

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

// All returns a validator which ensures that any configured attribute value
// attribute value validates against all the given validators.
//
// Use of All is only necessary when used in conjunction with Any or AnyWithAllWarnings
// as the Validators field automatically applies a logical AND.
func All(validators ...validator.Float64) validator.Float64 {
return allValidator{
validators: validators,
}
}

var _ validator.Float64 = allValidator{}

// allValidator implements the validator.
type allValidator struct {
validators []validator.Float64
}

// Description describes the validation in plain text formatting.
func (v allValidator) Description(ctx context.Context) string {
var descriptions []string

for _, subValidator := range v.validators {
descriptions = append(descriptions, subValidator.Description(ctx))
}

return fmt.Sprintf("Value must satisfy all of the validations: %s", strings.Join(descriptions, " + "))
}

// MarkdownDescription describes the validation in Markdown formatting.
func (v allValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

// ValidateFloat64 performs the validation.
func (v allValidator) ValidateFloat64(ctx context.Context, req validator.Float64Request, resp *validator.Float64Response) {
for _, subValidator := range v.validators {
validateResp := &validator.Float64Response{}

subValidator.ValidateFloat64(ctx, req, validateResp)

resp.Diagnostics.Append(validateResp.Diagnostics...)
}
}
30 changes: 30 additions & 0 deletions float64validator/all_example_test.go
@@ -0,0 +1,30 @@
package float64validator_test

import (
"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func ExampleAll() {
// Used within a Schema method of a DataSource, Provider, or Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.Float64Attribute{
Required: true,
Validators: []validator.Float64{
// Validate this Float64 value must either be:
// - 1.0
// - At least 2.0, but not 3.0
float64validator.Any(
float64validator.OneOf(1.0),
float64validator.All(
float64validator.AtLeast(2.0),
float64validator.NoneOf(3.0),
),
),
},
},
},
}
}