-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
PreferWriteOnlyAttribute()
validators to resourcevalidator
an…
…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
Showing
40 changed files
with
1,448 additions
and
0 deletions.
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,5 @@ | ||
kind: FEATURES | ||
body: 'boolvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:09:03.733715-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'dynamicvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:11:30.572285-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'float32validator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:11:59.137017-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'float64validator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:12:24.396457-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'int32validator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:12:43.521669-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'int64validator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:13:08.483387-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'listvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:14:02.302221-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'mapvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:14:18.324953-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'numbervalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:14:40.942642-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'objectvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:15:00.176757-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'resourcevalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:15:19.618622-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'setvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:15:33.908556-05:00 | ||
custom: | ||
Issue: "263" |
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,5 @@ | ||
kind: FEATURES | ||
body: 'stringvalidator: Added `PreferWriteOnlyAttribute` validator' | ||
time: 2025-02-06T12:15:53.337804-05:00 | ||
custom: | ||
Issue: "263" |
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,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, | ||
} | ||
} |
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,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, | ||
}, | ||
}, | ||
} | ||
} |
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,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
34
dynamicvalidator/prefer_write_only_attribute_example_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,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, | ||
}, | ||
}, | ||
} | ||
} |
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,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
34
float32validator/prefer_write_only_attribute_example_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,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, | ||
}, | ||
}, | ||
} | ||
} |
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,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
34
float64validator/prefer_write_only_attribute_example_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,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, | ||
}, | ||
}, | ||
} | ||
} |
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,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
34
int32validator/prefer_write_only_attribute_example_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,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, | ||
}, | ||
}, | ||
} | ||
} |
Oops, something went wrong.