Skip to content

Commit d1d10ba

Browse files
authored
🧹 Make validators reusable (#347)
1 parent 33c372b commit d1d10ba

4 files changed

Lines changed: 32 additions & 34 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mondoovalidator
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
func Id() validator.String {
11+
return stringvalidator.RegexMatches(
12+
regexp.MustCompile(`^[a-z\d]([\d-_]|[a-z]){2,62}[a-z\d]$`),
13+
"must contain 4 to 64 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
14+
)
15+
}
16+
17+
func Name() validator.String {
18+
return stringvalidator.RegexMatches(
19+
regexp.MustCompile(`^([a-zA-Z \-'_]|\d){2,64}$`),
20+
"must contain 2 to 64 characters, where each character can be a letter (uppercase or lowercase), a space, a dash, an underscore, or a digit",
21+
)
22+
}

‎internal/provider/organization_resource.go‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6-
"regexp"
76

8-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
97
"github.com/hashicorp/terraform-plugin-framework/path"
108
"github.com/hashicorp/terraform-plugin-framework/resource"
119
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
@@ -14,6 +12,7 @@ import (
1412
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1513
"github.com/hashicorp/terraform-plugin-framework/types"
1614
"github.com/hashicorp/terraform-plugin-log/tflog"
15+
"go.mondoo.com/terraform-provider-mondoo/internal/mondoovalidator"
1716
)
1817

1918
var _ resource.Resource = (*organizationResource)(nil)
@@ -44,10 +43,7 @@ func (r *organizationResource) Schema(ctx context.Context, req resource.SchemaRe
4443
MarkdownDescription: "Name of the space.",
4544
Required: true,
4645
Validators: []validator.String{
47-
stringvalidator.RegexMatches(
48-
regexp.MustCompile(`^([a-zA-Z \-'_]|\d){2,64}$`),
49-
"must contain 2 to 64 characters, where each character can be a letter (uppercase or lowercase), a space, a dash, an underscore, or a digit",
50-
),
46+
mondoovalidator.Name(),
5147
},
5248
},
5349
"id": schema.StringAttribute{
@@ -58,10 +54,7 @@ func (r *organizationResource) Schema(ctx context.Context, req resource.SchemaRe
5854
stringplanmodifier.UseStateForUnknown(),
5955
},
6056
Validators: []validator.String{
61-
stringvalidator.RegexMatches(
62-
regexp.MustCompile(`^[a-z\d]([\d-_]|[a-z]){2,62}[a-z\d]$`),
63-
"must contain 4 to 64 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
64-
),
57+
mondoovalidator.Id(),
6558
},
6659
},
6760
"mrn": schema.StringAttribute{

‎internal/provider/space_resource.go‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ package provider
66
import (
77
"context"
88
"fmt"
9-
"regexp"
109

11-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1210
"github.com/hashicorp/terraform-plugin-framework/attr"
1311
"github.com/hashicorp/terraform-plugin-framework/diag"
1412
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -22,6 +20,7 @@ import (
2220
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2321
"github.com/hashicorp/terraform-plugin-log/tflog"
2422
mondoov1 "go.mondoo.com/mondoo-go"
23+
"go.mondoo.com/terraform-provider-mondoo/internal/mondoovalidator"
2524
)
2625

2726
// Ensure provider defined types fully satisfy framework interfaces.
@@ -208,10 +207,7 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest,
208207
MarkdownDescription: "Name of the space.",
209208
Optional: true,
210209
Validators: []validator.String{
211-
stringvalidator.RegexMatches(
212-
regexp.MustCompile(`^([a-zA-Z \-'_]|\d){2,64}$`),
213-
"must contain 2 to 64 characters, where each character can be a letter (uppercase or lowercase), a space, a dash, an underscore, or a digit",
214-
),
210+
mondoovalidator.Name(),
215211
},
216212
},
217213
"description": schema.StringAttribute{
@@ -226,10 +222,7 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest,
226222
stringplanmodifier.UseStateForUnknown(),
227223
},
228224
Validators: []validator.String{
229-
stringvalidator.RegexMatches(
230-
regexp.MustCompile(`^[a-z\d]([\d-_]|[a-z]){2,62}[a-z\d]$`),
231-
"must contain 4 to 64 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
232-
),
225+
mondoovalidator.Id(),
233226
},
234227
},
235228
"mrn": schema.StringAttribute{
@@ -243,10 +236,7 @@ func (r *SpaceResource) Schema(ctx context.Context, req resource.SchemaRequest,
243236
MarkdownDescription: "ID of the organization.",
244237
Required: true,
245238
Validators: []validator.String{
246-
stringvalidator.RegexMatches(
247-
regexp.MustCompile(`^[a-z\d]([\d-_]|[a-z]){4,62}[a-z\d]$`),
248-
"must contain 4 to 64 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
249-
),
239+
mondoovalidator.Id(),
250240
},
251241
},
252242
"space_settings": schema.SingleNestedAttribute{

‎internal/provider/team_resource.go‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ package provider
66
import (
77
"context"
88
"fmt"
9-
"regexp"
109

11-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1210
"github.com/hashicorp/terraform-plugin-framework/resource"
1311
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1412
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
@@ -17,6 +15,7 @@ import (
1715
"github.com/hashicorp/terraform-plugin-framework/types"
1816
"github.com/hashicorp/terraform-plugin-log/tflog"
1917
mondoov1 "go.mondoo.com/mondoo-go"
18+
"go.mondoo.com/terraform-provider-mondoo/internal/mondoovalidator"
2019
)
2120

2221
// Ensure provider defined types fully satisfy framework interfaces.
@@ -76,10 +75,7 @@ resource "mondoo_iam_binding" "security_team_permissions" {
7675
stringplanmodifier.RequiresReplace(),
7776
},
7877
Validators: []validator.String{
79-
stringvalidator.RegexMatches(
80-
regexp.MustCompile(`^[a-z\d]([\d-_]|[a-z]){2,62}[a-z\d]$`),
81-
"must contain 4 to 64 digits, dashes, underscores, or lowercase letters, and ending with either a lowercase letter or a digit",
82-
),
78+
mondoovalidator.Id(),
8379
},
8480
},
8581
"mrn": schema.StringAttribute{
@@ -96,10 +92,7 @@ resource "mondoo_iam_binding" "security_team_permissions" {
9692
stringplanmodifier.RequiresReplace(),
9793
},
9894
Validators: []validator.String{
99-
stringvalidator.RegexMatches(
100-
regexp.MustCompile(`^([a-zA-Z \-'_]|\d){2,64}$`),
101-
"must contain 2 to 64 characters, where each character can be a letter (uppercase or lowercase), a space, a dash, an underscore, or a digit",
102-
),
95+
mondoovalidator.Name(),
10396
},
10497
},
10598
"description": schema.StringAttribute{

0 commit comments

Comments
 (0)