Skip to content

Commit e555f21

Browse files
authored
Merge pull request #96 from jetstack/uuid_validators
Add validators for UUID attributes
2 parents 3e2b64f + 4d7520c commit e555f21

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

internal/provider/cloudprovider_gcp_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"fmt"
99

1010
"terraform-provider-tlspc/internal/tlspc"
11+
"terraform-provider-tlspc/internal/validators"
1112

1213
"github.com/hashicorp/terraform-plugin-framework/path"
1314
"github.com/hashicorp/terraform-plugin-framework/resource"
1415
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1516
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
18+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1719
"github.com/hashicorp/terraform-plugin-framework/types"
1820
)
1921

@@ -60,6 +62,9 @@ func (r *cloudProviderGCPResource) Schema(_ context.Context, _ resource.SchemaRe
6062
"team": schema.StringAttribute{
6163
Required: true,
6264
MarkdownDescription: "The ID of the owning Team",
65+
Validators: []validator.String{
66+
validators.Uuid(),
67+
},
6368
},
6469
"service_account_email": schema.StringAttribute{
6570
Required: true,

internal/provider/firefly_config_resource.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"fmt"
99

1010
"terraform-provider-tlspc/internal/tlspc"
11+
"terraform-provider-tlspc/internal/validators"
1112

13+
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1214
"github.com/hashicorp/terraform-plugin-framework/path"
1315
"github.com/hashicorp/terraform-plugin-framework/resource"
1416
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1517
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
19+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1720
"github.com/hashicorp/terraform-plugin-framework/types"
1821
)
1922

@@ -52,16 +55,25 @@ func (r *fireflyConfigResource) Schema(_ context.Context, _ resource.SchemaReque
5255
"subca_provider": schema.StringAttribute{
5356
Required: true,
5457
MarkdownDescription: "The ID of the Firefly SubCA Provider",
58+
Validators: []validator.String{
59+
validators.Uuid(),
60+
},
5561
},
5662
"service_accounts": schema.SetAttribute{
5763
Required: true,
5864
ElementType: types.StringType,
5965
MarkdownDescription: "A list of service account IDs",
66+
Validators: []validator.Set{
67+
setvalidator.ValueStringsAre(validators.Uuid()),
68+
},
6069
},
6170
"policies": schema.SetAttribute{
6271
Required: true,
6372
ElementType: types.StringType,
6473
MarkdownDescription: "A list of Firefly Issuance Policy IDs",
74+
Validators: []validator.Set{
75+
setvalidator.ValueStringsAre(validators.Uuid()),
76+
},
6577
},
6678
},
6779
}

internal/provider/registry_account_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
"fmt"
99

1010
"terraform-provider-tlspc/internal/tlspc"
11+
"terraform-provider-tlspc/internal/validators"
1112

1213
"github.com/hashicorp/terraform-plugin-framework/path"
1314
"github.com/hashicorp/terraform-plugin-framework/resource"
1415
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1516
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
18+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1719
"github.com/hashicorp/terraform-plugin-framework/types"
1820
)
1921

@@ -52,6 +54,9 @@ func (r *registryAccountResource) Schema(_ context.Context, _ resource.SchemaReq
5254
"owner": schema.StringAttribute{
5355
Required: true,
5456
MarkdownDescription: "ID of the team that owns this service account",
57+
Validators: []validator.String{
58+
validators.Uuid(),
59+
},
5560
},
5661
"scopes": schema.SetAttribute{
5762
Required: true,

internal/provider/service_account_resource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ import (
88
"fmt"
99

1010
"terraform-provider-tlspc/internal/tlspc"
11+
"terraform-provider-tlspc/internal/validators"
1112

13+
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1214
"github.com/hashicorp/terraform-plugin-framework/path"
1315
"github.com/hashicorp/terraform-plugin-framework/resource"
1416
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1517
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
19+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1720
"github.com/hashicorp/terraform-plugin-framework/types"
1821
)
1922

@@ -52,6 +55,9 @@ func (r *serviceAccountResource) Schema(_ context.Context, _ resource.SchemaRequ
5255
"owner": schema.StringAttribute{
5356
Required: true,
5457
MarkdownDescription: "ID of the team that owns this service account",
58+
Validators: []validator.String{
59+
validators.Uuid(),
60+
},
5561
},
5662
"scopes": schema.SetAttribute{
5763
Required: true,
@@ -92,6 +98,9 @@ A list of scopes that this service account is authorised for. Available options
9298
Optional: true,
9399
ElementType: types.StringType,
94100
MarkdownDescription: "List of Applications which this service account is authorised for",
101+
Validators: []validator.Set{
102+
setvalidator.ValueStringsAre(validators.Uuid()),
103+
},
95104
},
96105
},
97106
}

internal/provider/team_resource.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"reflect"
1010

1111
"terraform-provider-tlspc/internal/tlspc"
12+
"terraform-provider-tlspc/internal/validators"
1213

14+
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
1315
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1416
"github.com/hashicorp/terraform-plugin-framework/path"
1517
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -64,6 +66,9 @@ func (r *teamResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
6466
Required: true,
6567
ElementType: types.StringType,
6668
MarkdownDescription: "List of user ids",
69+
Validators: []validator.Set{
70+
setvalidator.ValueStringsAre(validators.Uuid()),
71+
},
6772
},
6873
"user_matching_rules": schema.SetNestedAttribute{
6974
Optional: true,

internal/validators/uuid.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Venafi, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package validators
5+
6+
import (
7+
"context"
8+
"fmt"
9+
10+
"github.com/google/uuid"
11+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
12+
)
13+
14+
func Uuid() uuidValidator {
15+
return uuidValidator{}
16+
}
17+
18+
type uuidValidator struct {
19+
}
20+
21+
// Description returns a plain text description of the validator's behavior, suitable for a practitioner to understand its impact.
22+
func (v uuidValidator) Description(ctx context.Context) string {
23+
return "string must be a uuid"
24+
}
25+
26+
// MarkdownDescription returns a markdown formatted description of the validator's behavior, suitable for a practitioner to understand its impact.
27+
func (v uuidValidator) MarkdownDescription(ctx context.Context) string {
28+
return "string must be a uuid"
29+
}
30+
31+
// Validate runs the main validation logic of the validator, reading configuration data out of `req` and updating `resp` with diagnostics.
32+
func (v uuidValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
33+
// If the value is unknown or null, there is nothing to validate.
34+
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() {
35+
return
36+
}
37+
38+
if err := uuid.Validate(req.ConfigValue.ValueString()); err != nil {
39+
40+
resp.Diagnostics.AddAttributeError(
41+
req.Path,
42+
"Invalid uuid",
43+
fmt.Sprintf("String must be a uuid: %s", err),
44+
)
45+
46+
return
47+
}
48+
}

0 commit comments

Comments
 (0)