Skip to content

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 13 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
},
},
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.7

require (
github.com/google/go-cmp v0.6.0
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework v1.13.1-0.20250204221559-2b72dc0a12ab
github.com/hashicorp/terraform-plugin-go v0.26.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/go-hclog v1.5.0 h1:bI2ocEMgcVlz55Oj1xZNBsVi900c7II+fWDyV9o+13c=
github.com/hashicorp/go-hclog v1.5.0/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
github.com/hashicorp/terraform-plugin-framework v1.13.1-0.20250204221559-2b72dc0a12ab h1:x1GxE9ztKndo/6CPnkA8yBfmj8z13063U0pY6ayKNFo=
github.com/hashicorp/terraform-plugin-framework v1.13.1-0.20250204221559-2b72dc0a12ab/go.mod h1:qBKUqe1lv1NZcsO5pBjiKd5YuNDUeqvV1w8w5df/8WI=
github.com/hashicorp/terraform-plugin-go v0.26.0 h1:cuIzCv4qwigug3OS7iKhpGAbZTiypAfFQmw8aE65O2M=
github.com/hashicorp/terraform-plugin-go v0.26.0/go.mod h1:+CXjuLDiFgqR+GcrM5a2E2Kal5t5q2jb0E3D57tTdNY=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand Down
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,
},
},
}
}
28 changes: 28 additions & 0 deletions int64validator/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 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,
}
}
Loading