Skip to content

Commit abe64d6

Browse files
authored
tfsdk: Return warning diagnostic when using Attribute or Schema type DeprecationMessage field (#93)
Reference: #71
1 parent 7585988 commit abe64d6

File tree

6 files changed

+176
-0
lines changed

6 files changed

+176
-0
lines changed

.changelog/93.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
tfsdk: Return warning diagnostic when using `Attribute` or `Schema` type `DeprecationMessage` field
3+
```

tfsdk/attribute.go

+24
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,28 @@ func (a Attribute) validate(ctx context.Context, req ValidateAttributeRequest, r
344344
return
345345
}
346346
}
347+
348+
if a.DeprecationMessage != "" && attributeConfig != nil {
349+
tfValue, err := attributeConfig.ToTerraformValue(ctx)
350+
351+
if err != nil {
352+
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
353+
Severity: tfprotov6.DiagnosticSeverityError,
354+
Summary: "Attribute Validation Error",
355+
Detail: "Attribute validation cannot convert value. Report this to the provider developer:\n\n" + err.Error(),
356+
Attribute: req.AttributePath,
357+
})
358+
359+
return
360+
}
361+
362+
if tfValue != nil {
363+
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
364+
Severity: tfprotov6.DiagnosticSeverityWarning,
365+
Summary: "Attribute Deprecated",
366+
Detail: a.DeprecationMessage,
367+
Attribute: req.AttributePath,
368+
})
369+
}
370+
}
347371
}

tfsdk/attribute_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,96 @@ func TestAttributeValidate(t *testing.T) {
804804
},
805805
resp: ValidateAttributeResponse{},
806806
},
807+
"deprecation-message-known": {
808+
req: ValidateAttributeRequest{
809+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
810+
Config: Config{
811+
Raw: tftypes.NewValue(tftypes.Object{
812+
AttributeTypes: map[string]tftypes.Type{
813+
"test": tftypes.String,
814+
},
815+
}, map[string]tftypes.Value{
816+
"test": tftypes.NewValue(tftypes.String, "testvalue"),
817+
}),
818+
Schema: Schema{
819+
Attributes: map[string]Attribute{
820+
"test": {
821+
Type: types.StringType,
822+
Optional: true,
823+
DeprecationMessage: "Use something else instead.",
824+
},
825+
},
826+
},
827+
},
828+
},
829+
resp: ValidateAttributeResponse{
830+
Diagnostics: []*tfprotov6.Diagnostic{
831+
{
832+
Severity: tfprotov6.DiagnosticSeverityWarning,
833+
Summary: "Attribute Deprecated",
834+
Detail: "Use something else instead.",
835+
Attribute: tftypes.NewAttributePath().WithAttributeName("test"),
836+
},
837+
},
838+
},
839+
},
840+
"deprecation-message-null": {
841+
req: ValidateAttributeRequest{
842+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
843+
Config: Config{
844+
Raw: tftypes.NewValue(tftypes.Object{
845+
AttributeTypes: map[string]tftypes.Type{
846+
"test": tftypes.String,
847+
},
848+
}, map[string]tftypes.Value{
849+
"test": tftypes.NewValue(tftypes.String, nil),
850+
}),
851+
Schema: Schema{
852+
Attributes: map[string]Attribute{
853+
"test": {
854+
Type: types.StringType,
855+
Optional: true,
856+
DeprecationMessage: "Use something else instead.",
857+
},
858+
},
859+
},
860+
},
861+
},
862+
resp: ValidateAttributeResponse{},
863+
},
864+
"deprecation-message-unknown": {
865+
req: ValidateAttributeRequest{
866+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
867+
Config: Config{
868+
Raw: tftypes.NewValue(tftypes.Object{
869+
AttributeTypes: map[string]tftypes.Type{
870+
"test": tftypes.String,
871+
},
872+
}, map[string]tftypes.Value{
873+
"test": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
874+
}),
875+
Schema: Schema{
876+
Attributes: map[string]Attribute{
877+
"test": {
878+
Type: types.StringType,
879+
Optional: true,
880+
DeprecationMessage: "Use something else instead.",
881+
},
882+
},
883+
},
884+
},
885+
},
886+
resp: ValidateAttributeResponse{
887+
Diagnostics: []*tfprotov6.Diagnostic{
888+
{
889+
Severity: tfprotov6.DiagnosticSeverityWarning,
890+
Summary: "Attribute Deprecated",
891+
Detail: "Use something else instead.",
892+
Attribute: tftypes.NewAttributePath().WithAttributeName("test"),
893+
},
894+
},
895+
},
896+
},
807897
"warnings": {
808898
req: ValidateAttributeRequest{
809899
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),

tfsdk/schema.go

+8
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,12 @@ func (s Schema) validate(ctx context.Context, req ValidateSchemaRequest, resp *V
199199

200200
resp.Diagnostics = attributeResp.Diagnostics
201201
}
202+
203+
if s.DeprecationMessage != "" {
204+
resp.Diagnostics = append(resp.Diagnostics, &tfprotov6.Diagnostic{
205+
Severity: tfprotov6.DiagnosticSeverityWarning,
206+
Summary: "Deprecated",
207+
Detail: s.DeprecationMessage,
208+
})
209+
}
202210
}

tfsdk/schema_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,43 @@ func TestSchemaValidate(t *testing.T) {
556556
},
557557
resp: ValidateSchemaResponse{},
558558
},
559+
"deprecation-message": {
560+
req: ValidateSchemaRequest{
561+
Config: Config{
562+
Raw: tftypes.NewValue(tftypes.Object{
563+
AttributeTypes: map[string]tftypes.Type{
564+
"attr1": tftypes.String,
565+
"attr2": tftypes.String,
566+
},
567+
}, map[string]tftypes.Value{
568+
"attr1": tftypes.NewValue(tftypes.String, "attr1value"),
569+
"attr2": tftypes.NewValue(tftypes.String, "attr2value"),
570+
}),
571+
Schema: Schema{
572+
Attributes: map[string]Attribute{
573+
"attr1": {
574+
Type: types.StringType,
575+
Required: true,
576+
},
577+
"attr2": {
578+
Type: types.StringType,
579+
Required: true,
580+
},
581+
},
582+
DeprecationMessage: "Use something else instead.",
583+
},
584+
},
585+
},
586+
resp: ValidateSchemaResponse{
587+
Diagnostics: []*tfprotov6.Diagnostic{
588+
{
589+
Severity: tfprotov6.DiagnosticSeverityWarning,
590+
Summary: "Deprecated",
591+
Detail: "Use something else instead.",
592+
},
593+
},
594+
},
595+
},
559596
"warnings": {
560597
req: ValidateSchemaRequest{
561598
Config: Config{

tfsdk/serve_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,20 @@ func TestServerValidateProviderConfig(t *testing.T) {
413413
}),
414414
provider: &testServeProvider{},
415415
providerType: testServeProviderProviderType,
416+
417+
expectedDiags: []*tfprotov6.Diagnostic{
418+
{
419+
Severity: tfprotov6.DiagnosticSeverityWarning,
420+
Summary: "Attribute Deprecated",
421+
Detail: `Deprecated, please use "optional" instead`,
422+
Attribute: tftypes.NewAttributePath().WithAttributeName("deprecated"),
423+
},
424+
{
425+
Severity: tfprotov6.DiagnosticSeverityWarning,
426+
Summary: "Deprecated",
427+
Detail: "Deprecated in favor of other_resource",
428+
},
429+
},
416430
},
417431
"config_validators_no_diags": {
418432
config: tftypes.NewValue(testServeResourceTypeConfigValidatorsType, map[string]tftypes.Value{

0 commit comments

Comments
 (0)