Skip to content
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

tfsdk: Return warning diagnostic when using Attribute or Schema type DeprecationMessage field #93

Merged
merged 4 commits into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions .changelog/93.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tfsdk: Return warning diagnostic when using `Attribute` or `Schema` type `DeprecationMessage` field
```
9 changes: 9 additions & 0 deletions tfsdk/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,13 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r
return
}
}

if a.DeprecationMessage != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't we only want to return the error if the value wasn't null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we would! Good catch. Testing updated to catch this:

--- FAIL: TestAttributeValidate (0.00s)
    --- FAIL: TestAttributeValidate/deprecation-message-null (0.00s)
        attribute_test.go:1313: Unexpected response (+wanted, -got):   tfsdk.ValidateAttributeResponse{
            -   Diagnostics: []*tfprotov6.Diagnostic{
            -           &{
            -                   Severity:  s"WARNING",
            -                   Summary:   "Attribute Deprecated",
            -                   Detail:    "Use something else instead.",
            -                   Attribute: s`AttributeName("test")`,
            -           },
            -   },
            +   Diagnostics: nil,
              }

Fix will be included in next commit.

resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "Attribute Deprecated",
Detail: a.DeprecationMessage,
Attribute: req.AttributePath,
})
}
}
33 changes: 33 additions & 0 deletions tfsdk/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,39 @@ func TestAttributeValidate(t *testing.T) {
},
resp: ValidateAttributeResponse{},
},
"deprecation-message": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love to see a test for when the value is:

  • present
  • absent
  • unknown

req: ValidateAttributeRequest{
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
Config: Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
}, map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "testvalue"),
}),
Schema: Schema{
Attributes: map[string]Attribute{
"test": {
Type: types.StringType,
Required: true,
DeprecationMessage: "Use something else instead.",
},
},
},
},
},
resp: ValidateAttributeResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "Attribute Deprecated",
Detail: "Use something else instead.",
Attribute: tftypes.NewAttributePath().WithAttributeName("test"),
},
},
},
},
"warnings": {
req: ValidateAttributeRequest{
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
Expand Down
8 changes: 8 additions & 0 deletions tfsdk/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,12 @@ func (s Schema) validate(ctx context.Context, req ValidateSchemaRequest, resp *V

resp.Diagnostics = attributeResp.Diagnostics
}

if s.DeprecationMessage != "" {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "Deprecated",
Detail: s.DeprecationMessage,
})
}
}
37 changes: 37 additions & 0 deletions tfsdk/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,43 @@ func TestSchemaValidate(t *testing.T) {
},
resp: ValidateSchemaResponse{},
},
"deprecation-message": {
req: ValidateSchemaRequest{
Config: Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"attr1": tftypes.String,
"attr2": tftypes.String,
},
}, map[string]tftypes.Value{
"attr1": tftypes.NewValue(tftypes.String, "attr1value"),
"attr2": tftypes.NewValue(tftypes.String, "attr2value"),
}),
Schema: Schema{
Attributes: map[string]Attribute{
"attr1": {
Type: types.StringType,
Required: true,
},
"attr2": {
Type: types.StringType,
Required: true,
},
},
DeprecationMessage: "Use something else instead.",
},
},
},
resp: ValidateSchemaResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "Deprecated",
Detail: "Use something else instead.",
},
},
},
},
"warnings": {
req: ValidateSchemaRequest{
Config: Config{
Expand Down
14 changes: 14 additions & 0 deletions tfsdk/serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,20 @@ func TestServerValidateProviderConfig(t *testing.T) {
}),
provider: &testServeProvider{},
providerType: testServeProviderProviderType,

expectedDiags: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "Attribute Deprecated",
Detail: `Deprecated, please use "optional" instead`,
Attribute: tftypes.NewAttributePath().WithAttributeName("deprecated"),
},
{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "Deprecated",
Detail: "Deprecated in favor of other_resource",
},
},
},
"config_validators_no_diags": {
config: tftypes.NewValue(testServeResourceTypeConfigValidatorsType, map[string]tftypes.Value{
Expand Down