Skip to content

Commit

Permalink
internal/fwserver: Delay deprecated attribute/block warnings for unkn…
Browse files Browse the repository at this point in the history
…own values (hashicorp#465)

Reference: hashicorp/terraform-plugin-sdk#1047
Reference: hashicorp/terraform#31730

This change is made to keep terraform-plugin-sdk and terraform-plugin-framework behaviors for deprecation warning handling in sync.
  • Loading branch information
bflad authored Sep 6, 2022
1 parent 47f5b81 commit a5593d4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .changelog/465.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
internal/fwserver: Delayed deprecated attribute/block warnings for unknown values, which may be null
```
26 changes: 7 additions & 19 deletions internal/fwserver/attribute_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,25 +116,13 @@ func AttributeValidate(ctx context.Context, a fwschema.Attribute, req tfsdk.Vali

AttributeValidateNestedAttributes(ctx, a, req, resp)

if a.GetDeprecationMessage() != "" && attributeConfig != nil {
tfValue, err := attributeConfig.ToTerraformValue(ctx)
if err != nil {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Attribute Validation Error",
"Attribute validation cannot convert value. Report this to the provider developer:\n\n"+err.Error(),
)

return
}

if !tfValue.IsNull() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Attribute Deprecated",
a.GetDeprecationMessage(),
)
}
// Show deprecation warnings only for known values.
if a.GetDeprecationMessage() != "" && !attributeConfig.IsNull() && !attributeConfig.IsUnknown() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Attribute Deprecated",
a.GetDeprecationMessage(),
)
}
}

Expand Down
10 changes: 1 addition & 9 deletions internal/fwserver/attribute_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,7 @@ func TestAttributeValidate(t *testing.T) {
},
},
},
resp: tfsdk.ValidateAttributeResponse{
Diagnostics: diag.Diagnostics{
diag.NewAttributeWarningDiagnostic(
path.Root("test"),
"Attribute Deprecated",
"Use something else instead.",
),
},
},
resp: tfsdk.ValidateAttributeResponse{},
},
"warnings": {
req: tfsdk.ValidateAttributeRequest{
Expand Down
27 changes: 7 additions & 20 deletions internal/fwserver/block_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,13 @@ func BlockValidate(ctx context.Context, b fwschema.Block, req tfsdk.ValidateAttr
return
}

if b.GetDeprecationMessage() != "" && attributeConfig != nil {
tfValue, err := attributeConfig.ToTerraformValue(ctx)

if err != nil {
resp.Diagnostics.AddAttributeError(
req.AttributePath,
"Block Validation Error",
"Block validation cannot convert value. Report this to the provider developer:\n\n"+err.Error(),
)

return
}

if !tfValue.IsNull() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Block Deprecated",
b.GetDeprecationMessage(),
)
}
// Show deprecation warning only on known values.
if b.GetDeprecationMessage() != "" && !attributeConfig.IsNull() && !attributeConfig.IsUnknown() {
resp.Diagnostics.AddAttributeWarning(
req.AttributePath,
"Block Deprecated",
b.GetDeprecationMessage(),
)
}
}

Expand Down
10 changes: 1 addition & 9 deletions internal/fwserver/block_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,7 @@ func TestBlockValidate(t *testing.T) {
},
},
},
resp: tfsdk.ValidateAttributeResponse{
Diagnostics: diag.Diagnostics{
diag.NewAttributeWarningDiagnostic(
path.Root("test"),
"Block Deprecated",
"Use something else instead.",
),
},
},
resp: tfsdk.ValidateAttributeResponse{},
},
"warnings": {
req: tfsdk.ValidateAttributeRequest{
Expand Down

0 comments on commit a5593d4

Please sign in to comment.