Skip to content

Commit f7eec90

Browse files
fix: mark invalid UUIDs as known (#148)
1 parent a640098 commit f7eec90

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

internal/provider/user_data_source_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func TestAccUserDataSource(t *testing.T) {
5757
Username: ptr.Ref(user.Username),
5858
}
5959
resource.Test(t, resource.TestCase{
60+
IsUnitTest: true,
6061
PreCheck: func() { testAccPreCheck(t) },
6162
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
6263
Steps: []resource.TestStep{
@@ -75,6 +76,7 @@ func TestAccUserDataSource(t *testing.T) {
7576
ID: ptr.Ref(user.ID.String()),
7677
}
7778
resource.Test(t, resource.TestCase{
79+
IsUnitTest: true,
7880
PreCheck: func() { testAccPreCheck(t) },
7981
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
8082
// User by ID
@@ -92,6 +94,7 @@ func TestAccUserDataSource(t *testing.T) {
9294
Token: client.SessionToken(),
9395
}
9496
resource.Test(t, resource.TestCase{
97+
IsUnitTest: true,
9598
PreCheck: func() { testAccPreCheck(t) },
9699
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
97100
// Neither ID nor Username
@@ -104,6 +107,24 @@ func TestAccUserDataSource(t *testing.T) {
104107
})
105108
})
106109

110+
t.Run("InvalidUUIDError", func(t *testing.T) {
111+
cfg := testAccUserDataSourceConfig{
112+
URL: client.URL.String(),
113+
Token: client.SessionToken(),
114+
ID: ptr.Ref("invalid-uuid"),
115+
}
116+
resource.Test(t, resource.TestCase{
117+
IsUnitTest: true,
118+
PreCheck: func() { testAccPreCheck(t) },
119+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
120+
Steps: []resource.TestStep{
121+
{
122+
Config: cfg.String(t),
123+
ExpectError: regexp.MustCompile(`The provided value cannot be parsed as a UUID`),
124+
},
125+
},
126+
})
127+
})
107128
}
108129

109130
type testAccUserDataSourceConfig struct {

internal/provider/uuid.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ func (t uuidType) ValueFromString(ctx context.Context, in basetypes.StringValue)
4848
return NewUUIDUnknown(), diags
4949
}
5050

51-
value, err := uuid.Parse(in.ValueString())
52-
if err != nil {
53-
// The framework doesn't want us to return validation errors here
54-
// for some reason. They get caught by `ValidateAttribute` instead,
55-
// and this function isn't called directly by our provider - UUIDValue
56-
// takes a valid UUID instead of a string.
57-
return NewUUIDUnknown(), diags
58-
}
59-
60-
return UUIDValue(value), diags
51+
// This function deliberately does not handle invalid UUIDs.
52+
// Instead, `ValidateAttribute` will be called
53+
// on the stored string during `validate` `plan` and `apply`,
54+
// which will also create an error diagnostic.
55+
// For that reason, storing the zero UUID is fine.
56+
v, _ := uuid.Parse(in.ValueString())
57+
return UUID{
58+
StringValue: in,
59+
value: v,
60+
}, diags
6161
}
6262

6363
// ValueFromTerraform implements basetypes.StringTypable.

internal/provider/uuid_internal_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/google/uuid"
88
"github.com/hashicorp/terraform-plugin-framework/attr"
99
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1011
"github.com/hashicorp/terraform-plugin-go/tftypes"
1112
"github.com/stretchr/testify/require"
1213
)
@@ -37,9 +38,12 @@ func TestUUIDTypeValueFromTerraform(t *testing.T) {
3738
expected: UUIDValue(ValidUUID),
3839
},
3940
{
40-
name: "invalid UUID",
41-
input: tftypes.NewValue(tftypes.String, "invalid"),
42-
expected: NewUUIDUnknown(),
41+
name: "invalid UUID",
42+
input: tftypes.NewValue(tftypes.String, "invalid"),
43+
expected: UUID{
44+
StringValue: basetypes.NewStringValue("invalid"),
45+
value: uuid.Nil,
46+
},
4347
},
4448
}
4549

0 commit comments

Comments
 (0)