-
Notifications
You must be signed in to change notification settings - Fork 99
Add List Resource Config Validation #1178
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
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
4606268
Added List Resource Config Validation
rainkwan 53ff1ec
Updating ListResource to return diag.Diagnotics instead of error
rainkwan ff6d367
Fix linter complaint
rainkwan 0fe81af
Updated ListResource to return diags inside ListResult
rainkwan a20cb35
Merge branch 'main' into rk/list-resource-config
rainkwan b1bf9e9
Added List Resource to tests in getmetadata
rainkwan 99229ad
Added List Resource to tests in getmetadata
rainkwan 177467d
Added ResourcesMethod to tests in server_validatelistresourceconfig_t…
rainkwan cdcb3c7
gofmt error
rainkwan 3506d06
Merge branch 'main' into rk/list-resource-config
rainkwan 394e375
Added actions
rainkwan 94c6c14
rebasing
rainkwan bdb63f1
Added actions
rainkwan c066bd8
updated with more actions
rainkwan 0e23b3e
Merge branch 'main' into rk/list-resource-config
rainkwan 710ed93
Addressed some PR comments
rainkwan 6a6f19c
Updated Configure to be resource instead of List
rainkwan c9060a6
Updated for linter
rainkwan a78febb
Updated to add test as suggested in PR
rainkwan 11ad893
Merge branch 'main' into rk/list-resource-config
rainkwan 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,31 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package fromproto5 | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
| ) | ||
|
|
||
| // ValidateListResourceConfigRequest returns the *fwserver.ValidateListResourceConfigRequest | ||
| // equivalent of a *tfprotov5.ValidateListResourceConfigRequest. | ||
| func ValidateListResourceConfigRequest(ctx context.Context, proto5 *tfprotov5.ValidateListResourceConfigRequest, listResource list.ListResource, listResourceSchema fwschema.Schema) (*fwserver.ValidateListResourceConfigRequest, diag.Diagnostics) { | ||
| if proto5 == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| fw := &fwserver.ValidateListResourceConfigRequest{} | ||
|
|
||
| config, diags := Config(ctx, proto5.Config, listResourceSchema) | ||
|
|
||
| fw.Config = config | ||
| fw.ListResource = listResource | ||
|
|
||
| return fw, diags | ||
| } |
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,108 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package fromproto5_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fromproto5" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-framework/list/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
| "github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
| "github.com/hashicorp/terraform-plugin-go/tftypes" | ||
| ) | ||
|
|
||
| func TestValidateListResourceConfigRequest(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testProto5Type := tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "test_attribute": tftypes.String, | ||
| }, | ||
| } | ||
|
|
||
| testProto5Value := tftypes.NewValue(testProto5Type, map[string]tftypes.Value{ | ||
| "test_attribute": tftypes.NewValue(tftypes.String, "test-value"), | ||
| }) | ||
|
|
||
| testProto5DynamicValue, err := tfprotov5.NewDynamicValue(testProto5Type, testProto5Value) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err) | ||
| } | ||
|
|
||
| testFwSchema := schema.Schema{ | ||
| Attributes: map[string]schema.Attribute{ | ||
| "test_attribute": schema.StringAttribute{ | ||
| Required: true, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| testCases := map[string]struct { | ||
| input *tfprotov5.ValidateListResourceConfigRequest | ||
| listResourceSchema fwschema.Schema | ||
| listResource list.ListResource | ||
| expected *fwserver.ValidateListResourceConfigRequest | ||
| expectedDiagnostics diag.Diagnostics | ||
| }{ | ||
| "nil": { | ||
| input: nil, | ||
| expected: nil, | ||
| }, | ||
| "empty": { | ||
| input: &tfprotov5.ValidateListResourceConfigRequest{}, | ||
| expected: &fwserver.ValidateListResourceConfigRequest{}, | ||
| }, | ||
| "config-missing-schema": { | ||
| input: &tfprotov5.ValidateListResourceConfigRequest{ | ||
| Config: &testProto5DynamicValue, | ||
| }, | ||
| expected: &fwserver.ValidateListResourceConfigRequest{}, | ||
| expectedDiagnostics: diag.Diagnostics{ | ||
| diag.NewErrorDiagnostic( | ||
| "Unable to Convert Configuration", | ||
| "An unexpected error was encountered when converting the configuration from the protocol type. "+ | ||
| "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ | ||
| "Please report this to the provider developer:\n\n"+ | ||
| "Missing schema.", | ||
| ), | ||
| }, | ||
| }, | ||
| "config": { | ||
| input: &tfprotov5.ValidateListResourceConfigRequest{ | ||
| Config: &testProto5DynamicValue, | ||
| }, | ||
| listResourceSchema: testFwSchema, | ||
| expected: &fwserver.ValidateListResourceConfigRequest{ | ||
| Config: &tfsdk.Config{ | ||
| Raw: testProto5Value, | ||
| Schema: testFwSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, testCase := range testCases { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, diags := fromproto5.ValidateListResourceConfigRequest(context.Background(), testCase.input, testCase.listResource, testCase.listResourceSchema) | ||
|
|
||
| if diff := cmp.Diff(got, testCase.expected); diff != "" { | ||
| t.Errorf("unexpected difference: %s", diff) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { | ||
| t.Errorf("unexpected diagnostics difference: %s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
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,31 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package fromproto6 | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
| ) | ||
|
|
||
| // ValidateListResourceConfigRequest returns the *fwserver.ValidateListResourceConfigRequest | ||
| // equivalent of a *tfprotov6.ValidateListResourceConfigRequest. | ||
| func ValidateListResourceConfigRequest(ctx context.Context, proto6 *tfprotov6.ValidateListResourceConfigRequest, listResource list.ListResource, listResourceSchema fwschema.Schema) (*fwserver.ValidateListResourceConfigRequest, diag.Diagnostics) { | ||
| if proto6 == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| fw := &fwserver.ValidateListResourceConfigRequest{} | ||
|
|
||
| config, diags := Config(ctx, proto6.Config, listResourceSchema) | ||
|
|
||
| fw.Config = config | ||
| fw.ListResource = listResource | ||
|
|
||
| return fw, diags | ||
| } |
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,108 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package fromproto6_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/hashicorp/terraform-plugin-framework/diag" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fromproto6" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwschema" | ||
| "github.com/hashicorp/terraform-plugin-framework/internal/fwserver" | ||
| "github.com/hashicorp/terraform-plugin-framework/list" | ||
| "github.com/hashicorp/terraform-plugin-framework/list/schema" | ||
| "github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
| "github.com/hashicorp/terraform-plugin-go/tfprotov6" | ||
| "github.com/hashicorp/terraform-plugin-go/tftypes" | ||
| ) | ||
|
|
||
| func TestValidateListResourceConfigRequest(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testProto6Type := tftypes.Object{ | ||
| AttributeTypes: map[string]tftypes.Type{ | ||
| "test_attribute": tftypes.String, | ||
| }, | ||
| } | ||
|
|
||
| testProto6Value := tftypes.NewValue(testProto6Type, map[string]tftypes.Value{ | ||
| "test_attribute": tftypes.NewValue(tftypes.String, "test-value"), | ||
| }) | ||
|
|
||
| testProto6DynamicValue, err := tfprotov6.NewDynamicValue(testProto6Type, testProto6Value) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("unexpected error calling tfprotov6.NewDynamicValue(): %s", err) | ||
| } | ||
|
|
||
| testFwSchema := schema.Schema{ | ||
| Attributes: map[string]schema.Attribute{ | ||
| "test_attribute": schema.StringAttribute{ | ||
| Required: true, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| testCases := map[string]struct { | ||
| input *tfprotov6.ValidateListResourceConfigRequest | ||
| listResourceSchema fwschema.Schema | ||
| listResource list.ListResource | ||
| expected *fwserver.ValidateListResourceConfigRequest | ||
| expectedDiagnostics diag.Diagnostics | ||
| }{ | ||
| "nil": { | ||
| input: nil, | ||
| expected: nil, | ||
| }, | ||
| "empty": { | ||
| input: &tfprotov6.ValidateListResourceConfigRequest{}, | ||
| expected: &fwserver.ValidateListResourceConfigRequest{}, | ||
| }, | ||
| "config-missing-schema": { | ||
| input: &tfprotov6.ValidateListResourceConfigRequest{ | ||
| Config: &testProto6DynamicValue, | ||
| }, | ||
| expected: &fwserver.ValidateListResourceConfigRequest{}, | ||
| expectedDiagnostics: diag.Diagnostics{ | ||
| diag.NewErrorDiagnostic( | ||
| "Unable to Convert Configuration", | ||
| "An unexpected error was encountered when converting the configuration from the protocol type. "+ | ||
| "This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+ | ||
| "Please report this to the provider developer:\n\n"+ | ||
| "Missing schema.", | ||
| ), | ||
| }, | ||
| }, | ||
| "config": { | ||
| input: &tfprotov6.ValidateListResourceConfigRequest{ | ||
| Config: &testProto6DynamicValue, | ||
| }, | ||
| listResourceSchema: testFwSchema, | ||
| expected: &fwserver.ValidateListResourceConfigRequest{ | ||
| Config: &tfsdk.Config{ | ||
| Raw: testProto6Value, | ||
| Schema: testFwSchema, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for name, testCase := range testCases { | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, diags := fromproto6.ValidateListResourceConfigRequest(context.Background(), testCase.input, testCase.listResource, testCase.listResourceSchema) | ||
|
|
||
| if diff := cmp.Diff(got, testCase.expected); diff != "" { | ||
| t.Errorf("unexpected difference: %s", diff) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" { | ||
| t.Errorf("unexpected diagnostics difference: %s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
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.
💭 Here's a thought to consider – completely non-blocking for merging this PR.
While we're adding new kinds of schemas, I wonder if this diagnostic is precise enough to be actionable by a provider developer.
"Missing list resource schema for ``random_pet``"seems nice to have here. And also at odds with the universal message infromproto6.Config().So I'm curious how this diagnostic reads when it is rendered by the Terraform CLI and whether it includes precise context. If it's not precise, I suggest we adjust the
Configdiagnostic to be more flexible.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.
FWIW, this error message is only for our team, and isn't actionable by the provider developer outside of reporting it to us:
terraform-plugin-framework/internal/fromproto5/config.go
Lines 25 to 26 in 3f40cbd
The only way we'd hit this error is if there was a bug in Terraform core
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.
Agreed, no action here 🏁