-
Notifications
You must be signed in to change notification settings - Fork 13
Add PreferWriteOnlyAttribute()
validators to resourcevalidator
and all value validator packages.
#263
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
Merged
Merged
Add PreferWriteOnlyAttribute()
validators to resourcevalidator
and all value validator packages.
#263
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
79932f2
Update `terraform-plugin-framework` dependency
SBGoods 1a90401
Implement `PreferWriteOnlyAttribute` resource validator
SBGoods f486861
Implement `PreferWriteOnlyAttribute` schema validator and attribute v…
SBGoods 07de31e
Merge remote-tracking branch 'origin/main' into SBGoods/prefer-write-…
SBGoods 256e81d
Add `PreferWriteOnlyAttribute()` to `dynamicvalidator` package
SBGoods c573312
Fix misspells
SBGoods afeec7d
Add copywrite headers
SBGoods 87f8480
Add changelog entries
SBGoods c9a10f4
Add `WriteOnly` field to tests
SBGoods c36d263
Remove set validator
SBGoods 69c05cf
Merge branch 'main' into SBGoods/prefer-write-only
SBGoods f7fe8fd
Resolve linters
SBGoods 67ecac0
Merge branch 'main' into SBGoods/prefer-write-only
SBGoods 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 hidden or 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, | ||
}, | ||
}, | ||
} | ||
} |
This file contains hidden or 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 int64validator | ||
|
||
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.Int64 { | ||
return schemavalidator.PreferWriteOnlyAttribute{ | ||
WriteOnlyAttribute: writeOnlyAttribute, | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.