Skip to content

Commit

Permalink
Add PreferWriteOnlyAttribute() validators to resourcevalidator an…
Browse files Browse the repository at this point in the history
…d all value validator packages. (#263)

* Update `terraform-plugin-framework` dependency

* Implement `PreferWriteOnlyAttribute` resource validator

* Implement `PreferWriteOnlyAttribute` schema validator and attribute validator implementations

* Add `PreferWriteOnlyAttribute()` to `dynamicvalidator` package

* Fix misspells

* Add copywrite headers

* Add changelog entries

* Add `WriteOnly` field to tests

* Remove set validator

* Resolve linters
  • Loading branch information
SBGoods authored Feb 19, 2025
1 parent 6be3c39 commit 13b6d4d
Show file tree
Hide file tree
Showing 40 changed files with 1,448 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-120903.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'boolvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:09:03.733715-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121130.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'dynamicvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:11:30.572285-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121159.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'float32validator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:11:59.137017-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121224.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'float64validator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:12:24.396457-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121243.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'int32validator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:12:43.521669-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121308.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'int64validator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:13:08.483387-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121402.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'listvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:14:02.302221-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121418.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'mapvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:14:18.324953-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121440.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'numbervalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:14:40.942642-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121500.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'objectvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:15:00.176757-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121519.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'resourcevalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:15:19.618622-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121533.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'setvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:15:33.908556-05:00
custom:
Issue: "263"
5 changes: 5 additions & 0 deletions .changes/unreleased/FEATURES-20250206-121553.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: FEATURES
body: 'stringvalidator: Added `PreferWriteOnlyAttribute` validator'
time: 2025-02-06T12:15:53.337804-05:00
custom:
Issue: "263"
28 changes: 28 additions & 0 deletions boolvalidator/prefer_write_only_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package boolvalidator

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
)

// PreferWriteOnlyAttribute returns a warning if the Terraform client supports
// write-only attributes, and the attribute that the validator is applied to has a value.
// It takes in a path.Expression that represents the write-only attribute schema location,
// and the warning message will indicate that the write-only attribute should be preferred.
//
// This validator should only be used for resource attributes as other schema types do not
// support write-only attributes.
//
// This implements the validation logic declaratively within the schema.
// Refer to [resourcevalidator.PreferWriteOnlyAttribute]
// for declaring this type of validation outside the schema definition.
func PreferWriteOnlyAttribute(writeOnlyAttribute path.Expression) validator.Bool {
return schemavalidator.PreferWriteOnlyAttribute{
WriteOnlyAttribute: writeOnlyAttribute,
}
}
34 changes: 34 additions & 0 deletions boolvalidator/prefer_write_only_attribute_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package boolvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
)

func ExamplePreferWriteOnlyAttribute() {
// Used within a Schema method of a Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.BoolAttribute{
Optional: true,
Validators: []validator.Bool{
// Throws a warning diagnostic encouraging practitioners to use
// write_only_attr if example_attr has a value.
boolvalidator.PreferWriteOnlyAttribute(
path.MatchRoot("write_only_attr"),
),
},
},
"write_only_attr": schema.BoolAttribute{
WriteOnly: true,
Optional: true,
},
},
}
}
28 changes: 28 additions & 0 deletions dynamicvalidator/prefer_write_only_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dynamicvalidator

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
)

// PreferWriteOnlyAttribute returns a warning if the Terraform client supports
// write-only attributes, and the attribute that the validator is applied to has a value.
// It takes in a path.Expression that represents the write-only attribute schema location,
// and the warning message will indicate that the write-only attribute should be preferred.
//
// This validator should only be used for resource attributes as other schema types do not
// support write-only attributes.
//
// This implements the validation logic declaratively within the schema.
// Refer to [resourcevalidator.PreferWriteOnlyAttribute]
// for declaring this type of validation outside the schema definition.
func PreferWriteOnlyAttribute(writeOnlyAttribute path.Expression) validator.Dynamic {
return schemavalidator.PreferWriteOnlyAttribute{
WriteOnlyAttribute: writeOnlyAttribute,
}
}
34 changes: 34 additions & 0 deletions dynamicvalidator/prefer_write_only_attribute_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package dynamicvalidator_test

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/dynamicvalidator"
)

func ExamplePreferWriteOnlyAttribute() {
// Used within a Schema method of a Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.DynamicAttribute{
Optional: true,
Validators: []validator.Dynamic{
// Throws a warning diagnostic encouraging practitioners to use
// write_only_attr if example_attr has a value.
dynamicvalidator.PreferWriteOnlyAttribute(
path.MatchRoot("write_only_attr"),
),
},
},
"write_only_attr": schema.DynamicAttribute{
WriteOnly: true,
Optional: true,
},
},
}
}
28 changes: 28 additions & 0 deletions float32validator/prefer_write_only_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package float32validator

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
)

// PreferWriteOnlyAttribute returns a warning if the Terraform client supports
// write-only attributes, and the attribute that the validator is applied to has a value.
// It takes in a path.Expression that represents the write-only attribute schema location,
// and the warning message will indicate that the write-only attribute should be preferred.
//
// This validator should only be used for resource attributes as other schema types do not
// support write-only attributes.
//
// This implements the validation logic declaratively within the schema.
// Refer to [resourcevalidator.PreferWriteOnlyAttribute]
// for declaring this type of validation outside the schema definition.
func PreferWriteOnlyAttribute(writeOnlyAttribute path.Expression) validator.Float32 {
return schemavalidator.PreferWriteOnlyAttribute{
WriteOnlyAttribute: writeOnlyAttribute,
}
}
34 changes: 34 additions & 0 deletions float32validator/prefer_write_only_attribute_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package float32validator_test

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/float32validator"
)

func ExamplePreferWriteOnlyAttribute() {
// Used within a Schema method of a Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.Float32Attribute{
Optional: true,
Validators: []validator.Float32{
// Throws a warning diagnostic encouraging practitioners to use
// write_only_attr if example_attr has a value.
float32validator.PreferWriteOnlyAttribute(
path.MatchRoot("write_only_attr"),
),
},
},
"write_only_attr": schema.Float32Attribute{
WriteOnly: true,
Optional: true,
},
},
}
}
28 changes: 28 additions & 0 deletions float64validator/prefer_write_only_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package float64validator

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
)

// PreferWriteOnlyAttribute returns a warning if the Terraform client supports
// write-only attributes, and the attribute that the validator is applied to has a value.
// It takes in a path.Expression that represents the write-only attribute schema location,
// and the warning message will indicate that the write-only attribute should be preferred.
//
// This validator should only be used for resource attributes as other schema types do not
// support write-only attributes.
//
// This implements the validation logic declaratively within the schema.
// Refer to [resourcevalidator.PreferWriteOnlyAttribute]
// for declaring this type of validation outside the schema definition.
func PreferWriteOnlyAttribute(writeOnlyAttribute path.Expression) validator.Float64 {
return schemavalidator.PreferWriteOnlyAttribute{
WriteOnlyAttribute: writeOnlyAttribute,
}
}
34 changes: 34 additions & 0 deletions float64validator/prefer_write_only_attribute_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package float64validator_test

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/float64validator"
)

func ExamplePreferWriteOnlyAttribute() {
// Used within a Schema method of a Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.Float64Attribute{
Optional: true,
Validators: []validator.Float64{
// Throws a warning diagnostic encouraging practitioners to use
// write_only_attr if example_attr has a value.
float64validator.PreferWriteOnlyAttribute(
path.MatchRoot("write_only_attr"),
),
},
},
"write_only_attr": schema.Float64Attribute{
WriteOnly: true,
Optional: true,
},
},
}
}
28 changes: 28 additions & 0 deletions int32validator/prefer_write_only_attribute.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package int32validator

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/internal/schemavalidator"
)

// PreferWriteOnlyAttribute returns a warning if the Terraform client supports
// write-only attributes, and the attribute that the validator is applied to has a value.
// It takes in a path.Expression that represents the write-only attribute schema location,
// and the warning message will indicate that the write-only attribute should be preferred.
//
// This validator should only be used for resource attributes as other schema types do not
// support write-only attributes.
//
// This implements the validation logic declaratively within the schema.
// Refer to [resourcevalidator.PreferWriteOnlyAttribute]
// for declaring this type of validation outside the schema definition.
func PreferWriteOnlyAttribute(writeOnlyAttribute path.Expression) validator.Int32 {
return schemavalidator.PreferWriteOnlyAttribute{
WriteOnlyAttribute: writeOnlyAttribute,
}
}
34 changes: 34 additions & 0 deletions int32validator/prefer_write_only_attribute_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package int32validator_test

import (
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"

"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
)

func ExamplePreferWriteOnlyAttribute() {
// Used within a Schema method of a Resource
_ = schema.Schema{
Attributes: map[string]schema.Attribute{
"example_attr": schema.Int32Attribute{
Optional: true,
Validators: []validator.Int32{
// Throws a warning diagnostic encouraging practitioners to use
// write_only_attr if example_attr has a value.
int32validator.PreferWriteOnlyAttribute(
path.MatchRoot("write_only_attr"),
),
},
},
"write_only_attr": schema.Int32Attribute{
WriteOnly: true,
Optional: true,
},
},
}
}
Loading

0 comments on commit 13b6d4d

Please sign in to comment.