Skip to content

fix: mark invalid UUIDs as known #148

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

Merged
merged 2 commits into from
Nov 28, 2024
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
21 changes: 21 additions & 0 deletions internal/provider/user_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func TestAccUserDataSource(t *testing.T) {
Username: ptr.Ref(user.Username),
}
resource.Test(t, resource.TestCase{
IsUnitTest: true,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
Expand All @@ -75,6 +76,7 @@ func TestAccUserDataSource(t *testing.T) {
ID: ptr.Ref(user.ID.String()),
}
resource.Test(t, resource.TestCase{
IsUnitTest: true,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
// User by ID
Expand All @@ -92,6 +94,7 @@ func TestAccUserDataSource(t *testing.T) {
Token: client.SessionToken(),
}
resource.Test(t, resource.TestCase{
IsUnitTest: true,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
// Neither ID nor Username
Expand All @@ -104,6 +107,24 @@ func TestAccUserDataSource(t *testing.T) {
})
})

t.Run("InvalidUUIDError", func(t *testing.T) {
cfg := testAccUserDataSourceConfig{
URL: client.URL.String(),
Token: client.SessionToken(),
ID: ptr.Ref("invalid-uuid"),
}
resource.Test(t, resource.TestCase{
IsUnitTest: true,
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: cfg.String(t),
ExpectError: regexp.MustCompile(`The provided value cannot be parsed as a UUID`),
},
},
})
})
}

type testAccUserDataSourceConfig struct {
Expand Down
20 changes: 10 additions & 10 deletions internal/provider/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ func (t uuidType) ValueFromString(ctx context.Context, in basetypes.StringValue)
return NewUUIDUnknown(), diags
}

value, err := uuid.Parse(in.ValueString())
if err != nil {
// The framework doesn't want us to return validation errors here
// for some reason. They get caught by `ValidateAttribute` instead,
// and this function isn't called directly by our provider - UUIDValue
// takes a valid UUID instead of a string.
return NewUUIDUnknown(), diags
}

return UUIDValue(value), diags
// This function deliberately does not handle invalid UUIDs.
// Instead, `ValidateAttribute` will be called
// on the stored string during `validate` `plan` and `apply`,
// which will also create an error diagnostic.
// For that reason, storing the zero UUID is fine.
v, _ := uuid.Parse(in.ValueString())
return UUID{
StringValue: in,
value: v,
}, diags
}

// ValueFromTerraform implements basetypes.StringTypable.
Expand Down
10 changes: 7 additions & 3 deletions internal/provider/uuid_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -37,9 +38,12 @@ func TestUUIDTypeValueFromTerraform(t *testing.T) {
expected: UUIDValue(ValidUUID),
},
{
name: "invalid UUID",
input: tftypes.NewValue(tftypes.String, "invalid"),
expected: NewUUIDUnknown(),
name: "invalid UUID",
input: tftypes.NewValue(tftypes.String, "invalid"),
expected: UUID{
StringValue: basetypes.NewStringValue("invalid"),
value: uuid.Nil,
},
},
}

Expand Down