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

Introduce attr.TypeWithValidate handling to internal/reflect and tfsdk #82

Merged
merged 13 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions .changelog/82.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:breaking-change
Methods on the `tfsdk.Config`, `tfsdk.Plan`, and `tfsdk.State` types now return `[]*tfprotov6.Diagnostic` instead of `error`
```

```release-note:feature
Support `attr.Type` validation
```

```release-note:enhancement
Errors from methods on the `tfsdk.Config`, `tfsdk.Plan`, and `tfsdk.State` types now include rich diagnostic information
```
71 changes: 71 additions & 0 deletions internal/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package diagnostics

import (
"fmt"
"strings"

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

// TODO: Replace with diagnostics abstraction
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/24
func DiagsContainsDetail(in []*tfprotov6.Diagnostic, detail string) bool {
for _, diag := range in {
if diag == nil {
continue
}

if strings.Contains(diag.Detail, detail) {
return true
}
}
return false
}

// TODO: Replace with diagnostics abstraction
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/24
func DiagsHasErrors(in []*tfprotov6.Diagnostic) bool {
for _, diag := range in {
if diag == nil {
continue
}
if diag.Severity == tfprotov6.DiagnosticSeverityError {
return true
}
}
return false
}

// TODO: Replace with diagnostics abstraction
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/24
func DiagsString(in []*tfprotov6.Diagnostic) string {
var b strings.Builder

for _, diag := range in {
if diag == nil {
continue
}

// Diagnostic does not have .String() method
b.WriteString("\n")
b.WriteString(DiagString(diag))
b.WriteString("\n")
}

return b.String()
}

// TODO: Replace with diagnostics abstraction
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/24
func DiagString(diag *tfprotov6.Diagnostic) string {
var b strings.Builder

b.WriteString(fmt.Sprintf("Severity: %s\n", diag.Severity))
b.WriteString(fmt.Sprintf("Summary: %s\n", diag.Summary))
b.WriteString(fmt.Sprintf("Detail: %s\n", diag.Detail))
if diag.Attribute != nil {
b.WriteString(fmt.Sprintf("Attribute: %s\n", diag.Attribute))
}

return b.String()
}
17 changes: 9 additions & 8 deletions internal/reflect/build_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"testing"

"github.com/hashicorp/terraform-plugin-framework/internal/diagnostics"
refl "github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
Expand All @@ -14,24 +15,24 @@ func TestBuildValue_unhandledNull(t *testing.T) {
t.Parallel()

var s string
_, err := refl.BuildValue(context.Background(), types.StringType, tftypes.NewValue(tftypes.String, nil), reflect.ValueOf(s), refl.Options{}, tftypes.NewAttributePath())
if err == nil {
_, diags := refl.BuildValue(context.Background(), types.StringType, tftypes.NewValue(tftypes.String, nil), reflect.ValueOf(s), refl.Options{}, tftypes.NewAttributePath())
if !diagnostics.DiagsHasErrors(diags) {
t.Error("Expected error, didn't get one")
}
if expected := `unhandled null value`; expected != err.Error() {
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
if expected := `unhandled null value`; !diagnostics.DiagsContainsDetail(diags, expected) {
t.Errorf("Expected error to be %q, got %s", expected, diagnostics.DiagsString(diags))
}
}

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

var s string
_, err := refl.BuildValue(context.Background(), types.StringType, tftypes.NewValue(tftypes.String, tftypes.UnknownValue), reflect.ValueOf(s), refl.Options{}, tftypes.NewAttributePath())
if err == nil {
_, diags := refl.BuildValue(context.Background(), types.StringType, tftypes.NewValue(tftypes.String, tftypes.UnknownValue), reflect.ValueOf(s), refl.Options{}, tftypes.NewAttributePath())
if !diagnostics.DiagsHasErrors(diags) {
t.Error("Expected error, didn't get one")
}
if expected := `unhandled unknown value`; expected != err.Error() {
t.Errorf("Expected error to be %q, got %q", expected, err.Error())
if expected := `unhandled unknown value`; !diagnostics.DiagsContainsDetail(diags, expected) {
t.Errorf("Expected error to be %q, got %s", expected, diagnostics.DiagsString(diags))
}
}
42 changes: 42 additions & 0 deletions internal/reflect/diags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package reflect

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

func toTerraform5ValueErrorDiag(err error, path *tftypes.AttributePath) *tfprotov6.Diagnostic {
return &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Value Conversion Error",
Detail: "An unexpected error was encountered trying to convert into a Terraform value. This is always an error in the provider. Please report the following to the provider developer:\n\n" + err.Error(),
Attribute: path,
}
}

func toTerraformValueErrorDiag(err error, path *tftypes.AttributePath) *tfprotov6.Diagnostic {
return &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Value Conversion Error",
Detail: "An unexpected error was encountered trying to convert the Attribute value into a Terraform value. This is always an error in the provider. Please report the following to the provider developer:\n\n" + err.Error(),
Attribute: path,
}
}

func validateValueErrorDiag(err error, path *tftypes.AttributePath) *tfprotov6.Diagnostic {
return &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Value Conversion Error",
Detail: "An unexpected error was encountered trying to validate the Terraform value type. This is always an error in the provider. Please report the following to the provider developer:\n\n" + err.Error(),
Attribute: path,
}
}

func valueFromTerraformErrorDiag(err error, path *tftypes.AttributePath) *tfprotov6.Diagnostic {
return &tfprotov6.Diagnostic{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Value Conversion Error",
Detail: "An unexpected error was encountered trying to convert the Terraform value. This is always an error in the provider. Please report the following to the provider developer:\n\n" + err.Error(),
Attribute: path,
}
}
Loading