Skip to content

Commit

Permalink
all: Migrate implementations to support terraform-plugin-framework ve…
Browse files Browse the repository at this point in the history
…rsion 0.17.0 (#80)

The next terraform-plugin-framework version will have separate `datasource/schema`, `provider/schema`, and `resource/schema` packages which use the type-specific `schema/validator` interfaces. This change updates all schema-based validators to the new interfaces. In the cases where the validators did not match a single type (e.g. `metavalidator`, `schemavalidator` package validators), those validators are now present in all the type-specific validator packages.
  • Loading branch information
bflad committed Nov 30, 2022
1 parent f03f26c commit 8928062
Show file tree
Hide file tree
Showing 382 changed files with 15,920 additions and 5,758 deletions.
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,
},
},
}
}
25 changes: 10 additions & 15 deletions datasourcevalidator/at_least_one_of_test.go
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand All @@ -28,15 +28,13 @@ func TestAtLeastOneOf(t *testing.T) {
},
req: datasource.ValidateConfigRequest{
Config: tfsdk.Config{
Schema: tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
Schema: schema.Schema{
Attributes: map[string]schema.Attribute{
"test": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
"other": {
"other": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
},
},
Expand All @@ -63,19 +61,16 @@ func TestAtLeastOneOf(t *testing.T) {
},
req: datasource.ValidateConfigRequest{
Config: tfsdk.Config{
Schema: tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test1": {
Schema: schema.Schema{
Attributes: map[string]schema.Attribute{
"test1": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
"test2": {
"test2": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
"other": {
"other": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
},
},
Expand Down
25 changes: 10 additions & 15 deletions datasourcevalidator/conflicting_test.go
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework-validators/datasourcevalidator"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand All @@ -28,15 +28,13 @@ func TestConflicting(t *testing.T) {
},
req: datasource.ValidateConfigRequest{
Config: tfsdk.Config{
Schema: tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test": {
Schema: schema.Schema{
Attributes: map[string]schema.Attribute{
"test": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
"other": {
"other": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
},
},
Expand All @@ -63,19 +61,16 @@ func TestConflicting(t *testing.T) {
},
req: datasource.ValidateConfigRequest{
Config: tfsdk.Config{
Schema: tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test1": {
Schema: schema.Schema{
Attributes: map[string]schema.Attribute{
"test1": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
"test2": {
"test2": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
"other": {
"other": schema.StringAttribute{
Optional: true,
Type: types.StringType,
},
},
},
Expand Down

0 comments on commit 8928062

Please sign in to comment.