-
Notifications
You must be signed in to change notification settings - Fork 15
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
all: Add acceptance tests for write-only attributes #293
Draft
austinvalle
wants to merge
37
commits into
main
Choose a base branch
from
SBGoods/write-only-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
4ee9165
Add SDKv2 resource with write-only attribute and tests
SBGoods cc9487d
Merge branch 'main' into SBGoods/write-only-testing
austinvalle 699359a
updated go mod
austinvalle ef84121
add some initial smoke tests for write-only framework
austinvalle 4d3ff45
simplify w/o tests
austinvalle 2f731d9
some SDKv2 happy path tests
austinvalle 5fe0047
add double nested set
austinvalle c5a6557
enforce data consistency on sdkv2 tests
austinvalle 0e9d2de
write only negative tests for SDKv2
austinvalle dd723bb
Merge branch 'main' into SBGoods/write-only-testing
austinvalle 4229766
verify config on update
austinvalle 8e9d941
add nested attribute with write-only attr
austinvalle b73b034
remove unused schema attribute
austinvalle e08f258
remove unused provider attribute
austinvalle b9c211d
diff
austinvalle ffa3961
refactor test to avoid paths
austinvalle 200d66f
add negative tests for framework providers w/ old terraform versions
austinvalle a75e3da
testing version from pkg
austinvalle 05cdb67
write-once resource
austinvalle 6f7403d
more SDKv2 tests with validations
austinvalle 1e5bb88
testing state nulling for fw providers
austinvalle 922e213
add write-once tests to framework providers
austinvalle 3269cfa
add write-only validation tests for FW providers
austinvalle 0b7a3de
update copywrite
austinvalle b30fd01
add data check tests
austinvalle d33be2e
move resource state test
austinvalle 26d1f0e
comment
austinvalle ef4cd5e
upgrade resource state bugged test
austinvalle 4358d6b
comment
austinvalle 69570b8
Merge branch 'main' into SBGoods/write-only-testing
austinvalle 688c526
protocolv6 data checks
austinvalle 0a2580b
requires_replace temp example
austinvalle 04075d7
remove replace example and add import for sdkv2
austinvalle ac8f637
add TODO comment with readresource test
austinvalle 58fc242
import and move state fw tests
austinvalle f2caa1f
upgrade resource tests
austinvalle 5111b99
add upgrade test
austinvalle 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 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 |
---|---|---|
|
@@ -3,15 +3,18 @@ module github.com/hashicorp/terraform-provider-corner | |
go 1.22.7 | ||
|
||
require ( | ||
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 | ||
github.com/hashicorp/go-memdb v1.3.4 | ||
github.com/hashicorp/terraform-json v0.24.0 | ||
github.com/hashicorp/terraform-plugin-framework v1.13.0 | ||
github.com/hashicorp/terraform-plugin-framework v1.13.1-0.20250102211725-428efefe5acc | ||
github.com/hashicorp/terraform-plugin-framework-nettypes v0.2.0 | ||
github.com/hashicorp/terraform-plugin-framework-timeouts v0.4.1 | ||
github.com/hashicorp/terraform-plugin-framework-timetypes v0.5.0 | ||
github.com/hashicorp/terraform-plugin-go v0.25.0 | ||
github.com/hashicorp/terraform-plugin-mux v0.17.0 | ||
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0 | ||
github.com/hashicorp/terraform-plugin-testing v1.11.0 | ||
github.com/hashicorp/terraform-plugin-framework-validators v0.16.0 | ||
github.com/hashicorp/terraform-plugin-go v0.25.1-0.20241217173851-dcf8f64dbfaa | ||
github.com/hashicorp/terraform-plugin-mux v0.17.1-0.20241217174601-fdf2e5e009de | ||
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.1-0.20250103201413-5f29273ad6f0 | ||
github.com/hashicorp/terraform-plugin-testing v1.11.1-0.20250102212914-99297ce85154 | ||
Comment on lines
+14
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Update with released versions once available |
||
github.com/zclconf/go-cty v1.16.0 | ||
) | ||
|
||
|
@@ -26,7 +29,6 @@ require ( | |
github.com/hashicorp/errwrap v1.0.0 // indirect | ||
github.com/hashicorp/go-checkpoint v0.5.0 // indirect | ||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect | ||
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect | ||
github.com/hashicorp/go-hclog v1.6.3 // indirect | ||
github.com/hashicorp/go-immutable-radix v1.3.0 // indirect | ||
github.com/hashicorp/go-multierror v1.1.1 // indirect | ||
|
@@ -56,13 +58,13 @@ require ( | |
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect | ||
golang.org/x/crypto v0.31.0 // indirect | ||
golang.org/x/mod v0.21.0 // indirect | ||
golang.org/x/net v0.28.0 // indirect | ||
golang.org/x/net v0.29.0 // indirect | ||
golang.org/x/sync v0.10.0 // indirect | ||
golang.org/x/sys v0.28.0 // indirect | ||
golang.org/x/text v0.21.0 // indirect | ||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect | ||
google.golang.org/appengine v1.6.8 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect | ||
google.golang.org/grpc v1.67.1 // indirect | ||
google.golang.org/protobuf v1.35.1 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect | ||
google.golang.org/grpc v1.68.1 // indirect | ||
google.golang.org/protobuf v1.35.2 // indirect | ||
) |
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
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,76 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package cornertesting | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
) | ||
|
||
func PreferWriteOnlyAttribute(oldAttribute, newAttribute path.Expression) resource.ConfigValidator { | ||
return preferWriteOnlyAttributeValidator{ | ||
oldAttribute: oldAttribute, | ||
newAttribute: newAttribute, | ||
} | ||
} | ||
|
||
var _ resource.ConfigValidator = &preferWriteOnlyAttributeValidator{} | ||
|
||
type preferWriteOnlyAttributeValidator struct { | ||
oldAttribute path.Expression | ||
newAttribute path.Expression | ||
} | ||
|
||
func (v preferWriteOnlyAttributeValidator) Description(ctx context.Context) string { | ||
return v.MarkdownDescription(ctx) | ||
} | ||
|
||
func (v preferWriteOnlyAttributeValidator) MarkdownDescription(_ context.Context) string { | ||
return fmt.Sprintf("If the client supports write-only attributes (Terraform v1.11+), attribute %s should be used in-place of %s", v.newAttribute, v.oldAttribute) | ||
} | ||
|
||
func (v preferWriteOnlyAttributeValidator) ValidateResource(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) { | ||
// Write-only attributes are not supported in the client, so no warning should be raised. | ||
if !req.ClientCapabilities.WriteOnlyAttributesAllowed { | ||
return | ||
} | ||
|
||
matchedOldPaths, matchedOldPathsDiags := req.Config.PathMatches(ctx, v.oldAttribute) | ||
resp.Diagnostics.Append(matchedOldPathsDiags...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
var diags diag.Diagnostics | ||
for _, matchedOldPath := range matchedOldPaths { | ||
var value attr.Value | ||
getAttributeDiags := req.Config.GetAttribute(ctx, matchedOldPath, &value) | ||
|
||
diags.Append(getAttributeDiags...) | ||
|
||
// Collect all errors | ||
if getAttributeDiags.HasError() { | ||
continue | ||
} | ||
|
||
// Value must not be null or unknown to trigger validation error | ||
if value.IsNull() || value.IsUnknown() { | ||
continue | ||
} | ||
|
||
diags.AddAttributeWarning( | ||
matchedOldPath, | ||
"Available Write-Only Attribute Alternative", | ||
fmt.Sprintf("The attribute %s has a WriteOnly version %s available. "+ | ||
"Use the WriteOnly version of the attribute when possible.", matchedOldPath, v.newAttribute), | ||
) | ||
} | ||
|
||
resp.Diagnostics = diags | ||
} |
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,41 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
package cornertesting | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
) | ||
|
||
func RequiredOnCreate() planmodifier.String { | ||
return requiredOnCreateModifier{} | ||
} | ||
|
||
type requiredOnCreateModifier struct{} | ||
|
||
func (m requiredOnCreateModifier) Description(_ context.Context) string { | ||
return "This attribute is required only when creating the resource." | ||
} | ||
|
||
func (m requiredOnCreateModifier) MarkdownDescription(_ context.Context) string { | ||
return "This attribute is required only when creating the resource." | ||
} | ||
|
||
func (m requiredOnCreateModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { | ||
// If there is a non-null state, we are destroying or updating so no validation is needed | ||
if !req.State.Raw.IsNull() { | ||
return | ||
} | ||
|
||
// We are creating, but the attribute value is not present in config, return an error. | ||
if req.ConfigValue.IsNull() { | ||
resp.Diagnostics.AddAttributeError( | ||
req.Path, | ||
"Attribute Required when Creating", | ||
fmt.Sprintf("Must set a configuration value for the %s attribute when creating.", req.Path.String()), | ||
) | ||
return | ||
} | ||
} |
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
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,92 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-corner/internal/cornertesting" | ||
) | ||
|
||
var _ resource.Resource = WriteOnceResource{} | ||
|
||
func NewWriteOnceResource() resource.Resource { | ||
return &WriteOnceResource{} | ||
} | ||
|
||
type WriteOnceResource struct{} | ||
|
||
func (r WriteOnceResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_writeonce" | ||
} | ||
|
||
func (r WriteOnceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"trigger_attr": schema.StringAttribute{ | ||
Required: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
}, | ||
// The only place that validation can reference prior state (which is required to determine if the planned action is | ||
// a create, i.e, prior state is null) is during plan modification. So the plan modifier implementation is responsible | ||
// for applying the "write-once" validation | ||
"writeonce_string": schema.StringAttribute{ | ||
Optional: true, | ||
WriteOnly: true, | ||
PlanModifiers: []planmodifier.String{ | ||
cornertesting.RequiredOnCreate(), | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r WriteOnceResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var config WriteOnceResourceModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(assertWriteOnlyVal(ctx, req.Config, path.Root("writeonce_string"), types.StringValue("fakepassword"))...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Since all attributes are in configuration, we write it back directly to test that the write-only attributes | ||
// are nulled out before sending back to TF Core. | ||
resp.Diagnostics.Append(resp.State.Set(ctx, &config)...) | ||
} | ||
|
||
func (r WriteOnceResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data WriteOnceResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r WriteOnceResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
// Once created, the only operation that can occur is replacement (delete/create) | ||
} | ||
|
||
func (r WriteOnceResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
} | ||
|
||
type WriteOnceResourceModel struct { | ||
TriggerAttr types.String `tfsdk:"trigger_attr"` | ||
WriteOnlyString types.String `tfsdk:"writeonce_string"` | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Update with released version once available