Skip to content

Commit

Permalink
diag: Move (Diagnostics).ToTfprotov6Diagnostics() method to internal/…
Browse files Browse the repository at this point in the history
…toproto6 package (#313)

Reference: #215
  • Loading branch information
bflad committed May 3, 2022
1 parent 72e4d38 commit 12176c9
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 146 deletions.
3 changes: 3 additions & 0 deletions .changelog/313.txt
@@ -0,0 +1,3 @@
```release-note:breaking-change
diag: Removed `Diagnostics` type `ToTfprotov6Diagnostics()` method. This was not intended for usage by provider developers.
```
31 changes: 0 additions & 31 deletions diag/diagnostic_test.go

This file was deleted.

25 changes: 0 additions & 25 deletions diag/diagnostics.go
@@ -1,7 +1,6 @@
package diag

import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -71,27 +70,3 @@ func (diags Diagnostics) HasError() bool {

return false
}

// ToTfprotov6Diagnostics converts the diagnostics into the tfprotov6 collection type.
//
// Usage of this method outside the framework is not supported nor considered
// for backwards compatibility promises.
func (diags Diagnostics) ToTfprotov6Diagnostics() []*tfprotov6.Diagnostic {
var results []*tfprotov6.Diagnostic

for _, diag := range diags {
tfprotov6Diagnostic := &tfprotov6.Diagnostic{
Detail: diag.Detail(),
Severity: diag.Severity().ToTfprotov6DiagnosticSeverity(),
Summary: diag.Summary(),
}

if diagWithPath, ok := diag.(DiagnosticWithPath); ok {
tfprotov6Diagnostic.Attribute = diagWithPath.Path()
}

results = append(results, tfprotov6Diagnostic)
}

return results
}
78 changes: 0 additions & 78 deletions diag/diagnostics_test.go
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -519,80 +518,3 @@ func TestDiagnosticsHasError(t *testing.T) {
})
}
}

func TestDiagnosticsToTfprotov6Diagnostics(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
diags diag.Diagnostics
expected []*tfprotov6.Diagnostic
}{
"nil": {
diags: nil,
expected: nil,
},
"Diagnostic-SeverityInvalid": {
diags: diag.Diagnostics{
invalidSeverityDiagnostic{},
},
expected: []*tfprotov6.Diagnostic{
{
Detail: invalidSeverityDiagnostic{}.Detail(),
Severity: tfprotov6.DiagnosticSeverityInvalid,
Summary: invalidSeverityDiagnostic{}.Summary(),
},
},
},
"Diagnostic": {
diags: diag.Diagnostics{
diag.NewErrorDiagnostic("one summary", "one detail"),
diag.NewWarningDiagnostic("two summary", "two detail"),
},
expected: []*tfprotov6.Diagnostic{
{
Detail: "one detail",
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "one summary",
},
{
Detail: "two detail",
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "two summary",
},
},
},
"DiagnosticWithPath": {
diags: diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(tftypes.NewAttributePath(), "one summary", "one detail"),
diag.NewAttributeWarningDiagnostic(tftypes.NewAttributePath().WithAttributeName("test"), "two summary", "two detail"),
},
expected: []*tfprotov6.Diagnostic{
{
Attribute: tftypes.NewAttributePath(),
Detail: "one detail",
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "one summary",
},
{
Attribute: tftypes.NewAttributePath().WithAttributeName("test"),
Detail: "two detail",
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "two summary",
},
},
},
}

for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

got := tc.diags.ToTfprotov6Diagnostics()

if diff := cmp.Diff(got, tc.expected); diff != "" {
t.Errorf("Unexpected response (+wanted, -got): %s", diff)
}
})
}
}
20 changes: 10 additions & 10 deletions internal/proto6server/serve.go
Expand Up @@ -101,7 +101,7 @@ func (r getProviderSchemaResponse) toTfprotov6() *tfprotov6.GetProviderSchemaRes
ProviderMeta: r.ProviderMeta,
ResourceSchemas: r.ResourceSchemas,
DataSourceSchemas: r.DataSourceSchemas,
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down Expand Up @@ -240,7 +240,7 @@ type validateProviderConfigResponse struct {
func (r validateProviderConfigResponse) toTfprotov6() *tfprotov6.ValidateProviderConfigResponse {
return &tfprotov6.ValidateProviderConfigResponse{
PreparedConfig: r.PreparedConfig,
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down Expand Up @@ -353,7 +353,7 @@ type configureProviderResponse struct {

func (r configureProviderResponse) toTfprotov6() *tfprotov6.ConfigureProviderResponse {
return &tfprotov6.ConfigureProviderResponse{
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down Expand Up @@ -409,7 +409,7 @@ type validateResourceConfigResponse struct {

func (r validateResourceConfigResponse) toTfprotov6() *tfprotov6.ValidateResourceConfigResponse {
return &tfprotov6.ValidateResourceConfigResponse{
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down Expand Up @@ -536,7 +536,7 @@ type upgradeResourceStateResponse struct {

func (r upgradeResourceStateResponse) toTfprotov6() *tfprotov6.UpgradeResourceStateResponse {
return &tfprotov6.UpgradeResourceStateResponse{
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
UpgradedState: r.UpgradedState,
}
}
Expand Down Expand Up @@ -766,7 +766,7 @@ type readResourceResponse struct {
func (r readResourceResponse) toTfprotov6() *tfprotov6.ReadResourceResponse {
return &tfprotov6.ReadResourceResponse{
NewState: r.NewState,
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
Private: r.Private,
}
}
Expand Down Expand Up @@ -911,7 +911,7 @@ type planResourceChangeResponse struct {
func (r planResourceChangeResponse) toTfprotov6() *tfprotov6.PlanResourceChangeResponse {
return &tfprotov6.PlanResourceChangeResponse{
PlannedState: r.PlannedState,
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
RequiresReplace: r.RequiresReplace,
PlannedPrivate: r.PlannedPrivate,
}
Expand Down Expand Up @@ -1209,7 +1209,7 @@ func (r applyResourceChangeResponse) toTfprotov6() *tfprotov6.ApplyResourceChang
return &tfprotov6.ApplyResourceChangeResponse{
NewState: r.NewState,
Private: r.Private,
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down Expand Up @@ -1532,7 +1532,7 @@ type validateDataResourceConfigResponse struct {

func (r validateDataResourceConfigResponse) toTfprotov6() *tfprotov6.ValidateDataResourceConfigResponse {
return &tfprotov6.ValidateDataResourceConfigResponse{
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down Expand Up @@ -1660,7 +1660,7 @@ type readDataSourceResponse struct {
func (r readDataSourceResponse) toTfprotov6() *tfprotov6.ReadDataSourceResponse {
return &tfprotov6.ReadDataSourceResponse{
State: r.State,
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/proto6server/serve_import.go
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/logging"
"github.com/hashicorp/terraform-plugin-framework/internal/toproto6"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand Down Expand Up @@ -50,12 +51,12 @@ type importResourceStateResponse struct {

func (r importResourceStateResponse) toTfprotov6(ctx context.Context) *tfprotov6.ImportResourceStateResponse {
resp := &tfprotov6.ImportResourceStateResponse{
Diagnostics: r.Diagnostics.ToTfprotov6Diagnostics(),
Diagnostics: toproto6.Diagnostics(r.Diagnostics),
}

for _, ir := range r.ImportedResources {
irProto6, diags := ir.toTfprotov6(ctx)
resp.Diagnostics = append(resp.Diagnostics, diags.ToTfprotov6Diagnostics()...)
resp.Diagnostics = append(resp.Diagnostics, toproto6.Diagnostics(diags)...)
if diags.HasError() {
continue
}
Expand Down
27 changes: 27 additions & 0 deletions internal/toproto6/diagnostics.go
@@ -0,0 +1,27 @@
package toproto6

import (
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
)

// Diagnostics converts the diagnostics into the tfprotov6 collection type.
func Diagnostics(diagnostics diag.Diagnostics) []*tfprotov6.Diagnostic {
var results []*tfprotov6.Diagnostic

for _, diagnostic := range diagnostics {
tfprotov6Diagnostic := &tfprotov6.Diagnostic{
Detail: diagnostic.Detail(),
Severity: diagnostic.Severity().ToTfprotov6DiagnosticSeverity(),
Summary: diagnostic.Summary(),
}

if diagWithPath, ok := diagnostic.(diag.DiagnosticWithPath); ok {
tfprotov6Diagnostic.Attribute = diagWithPath.Path()
}

results = append(results, tfprotov6Diagnostic)
}

return results
}

0 comments on commit 12176c9

Please sign in to comment.