Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd0f5e21

Browse files
authored
fix: usecodersdk functions for validating name attributes (#130)
1 parent4b8a4b1 commitd0f5e21

File tree

11 files changed

+114
-24
lines changed

11 files changed

+114
-24
lines changed

‎.golangci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Visit https://golangci-lint.run/ for usage documentation
2-
#and information onother useful linters
1+
# Visit https://golangci-lint.run/ for usage documentation and information on
2+
# other useful linters
33
issues:
44
max-per-linter:0
55
max-same-issues:0
@@ -24,4 +24,4 @@ linters:
2424
-unconvert
2525
-unparam
2626
-unused
27-
-vet
27+
-vet
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
funcDisplayName() validator.String {
9+
returnvalidatorFromFunc(codersdk.DisplayNameValid,"value must be a valid display name")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
funcGroupName() validator.String {
9+
returnvalidatorFromFunc(codersdk.GroupNameValid,"value must be a valid group name")
10+
}

‎internal/codersdkvalidator/name.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
funcName() validator.String {
9+
returnvalidatorFromFunc(codersdk.NameValid,"value must be a valid name")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
funcTemplateVersionName() validator.String {
9+
returnvalidatorFromFunc(codersdk.TemplateVersionNameValid,"value must be a valid template version name")
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"github.com/coder/coder/v2/codersdk"
5+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
6+
)
7+
8+
funcUserRealName() validator.String {
9+
returnvalidatorFromFunc(codersdk.UserRealNameValid,"value must be a valid name for a user")
10+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package codersdkvalidator
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
)
9+
10+
typefunctionValidatorstruct {
11+
checkfunc(string)error
12+
defaultMessagestring
13+
errerror
14+
}
15+
16+
funcvalidatorFromFunc(checkfunc(string)error,defaultMessagestring)functionValidator {
17+
returnfunctionValidator{
18+
check:check,
19+
defaultMessage:defaultMessage,
20+
}
21+
}
22+
23+
var_ validator.String=functionValidator{}
24+
25+
func (vfunctionValidator)ValidateString(ctx context.Context,req validator.StringRequest,resp*validator.StringResponse) {
26+
ifreq.ConfigValue.IsNull()||req.ConfigValue.IsUnknown() {
27+
return
28+
}
29+
30+
name:=req.ConfigValue.ValueString()
31+
ifv.err=v.check(name);v.err!=nil {
32+
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
33+
req.Path,
34+
v.Description(ctx),
35+
name,
36+
))
37+
}
38+
}
39+
40+
var_ validator.Describer=functionValidator{}
41+
42+
func (vfunctionValidator)Description(_ context.Context)string {
43+
ifv.err!=nil {
44+
returnv.err.Error()
45+
}
46+
returnv.defaultMessage
47+
}
48+
49+
func (vfunctionValidator)MarkdownDescription(ctx context.Context)string {
50+
returnv.Description(ctx)
51+
}

‎internal/provider/group_resource.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"strings"
77

88
"github.com/coder/coder/v2/codersdk"
9+
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
910
"github.com/google/uuid"
10-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1111
"github.com/hashicorp/terraform-plugin-framework/attr"
1212
"github.com/hashicorp/terraform-plugin-framework/diag"
1313
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -77,17 +77,15 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
7777
MarkdownDescription:"The unique name of the group.",
7878
Required:true,
7979
Validators: []validator.String{
80-
stringvalidator.LengthBetween(1,36),
81-
stringvalidator.RegexMatches(nameValidRegex,"Group names must be alpahnumeric with hyphens."),
80+
codersdkvalidator.GroupName(),
8281
},
8382
},
8483
"display_name": schema.StringAttribute{
8584
MarkdownDescription:"The display name of the group. Defaults to the group name.",
8685
Computed:true,
8786
Optional:true,
8887
Validators: []validator.String{
89-
stringvalidator.LengthBetween(1,64),
90-
stringvalidator.RegexMatches(displayNameRegex,"Group display names must be alphanumeric with spaces"),
88+
codersdkvalidator.DisplayName(),
9189
},
9290
Default:stringdefault.StaticString(""),
9391
},

‎internal/provider/template_resource.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/coder/coder/v2/coderd/util/ptr"
1414
"github.com/coder/coder/v2/codersdk"
1515
"github.com/coder/coder/v2/provisionersdk"
16+
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
1617
"github.com/google/uuid"
1718
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
1819
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
@@ -258,17 +259,15 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
258259
MarkdownDescription:"The name of the template.",
259260
Required:true,
260261
Validators: []validator.String{
261-
stringvalidator.LengthBetween(1,32),
262-
stringvalidator.RegexMatches(nameValidRegex,"Template names must be alphanumeric with hyphens."),
262+
codersdkvalidator.Name(),
263263
},
264264
},
265265
"display_name": schema.StringAttribute{
266266
MarkdownDescription:"The display name of the template. Defaults to the template name.",
267267
Optional:true,
268268
Computed:true,
269269
Validators: []validator.String{
270-
stringvalidator.LengthBetween(1,64),
271-
stringvalidator.RegexMatches(displayNameRegex,"Template display names must be alphanumeric with spaces."),
270+
codersdkvalidator.DisplayName(),
272271
},
273272
},
274273
"description": schema.StringAttribute{
@@ -418,8 +417,7 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
418417
Optional:true,
419418
Computed:true,
420419
Validators: []validator.String{
421-
stringvalidator.LengthBetween(1,64),
422-
stringvalidator.RegexMatches(templateVersionNameRegex,"Template version names must be alphanumeric with underscores and dots."),
420+
codersdkvalidator.TemplateVersionName(),
423421
},
424422
},
425423
"message": schema.StringAttribute{

‎internal/provider/user_resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/hashicorp/terraform-plugin-log/tflog"
2323

2424
"github.com/coder/coder/v2/codersdk"
25+
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
2526
)
2627

2728
// Ensure provider defined types fully satisfy framework interfaces.
@@ -71,16 +72,15 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
7172
MarkdownDescription:"Username of the user.",
7273
Required:true,
7374
Validators: []validator.String{
74-
stringvalidator.LengthBetween(1,32),
75-
stringvalidator.RegexMatches(nameValidRegex,"Username must be alphanumeric with hyphens."),
75+
codersdkvalidator.Name(),
7676
},
7777
},
7878
"name": schema.StringAttribute{
7979
MarkdownDescription:"Display name of the user. Defaults to username.",
8080
Computed:true,
8181
Optional:true,
8282
Validators: []validator.String{
83-
stringvalidator.LengthBetween(1,128),
83+
codersdkvalidator.UserRealName(),
8484
},
8585
},
8686
"email": schema.StringAttribute{

‎internal/provider/util.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,11 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11-
"regexp"
1211

1312
"github.com/coder/coder/v2/codersdk"
1413
"github.com/google/uuid"
1514
)
1615

17-
var (
18-
nameValidRegex=regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
19-
templateVersionNameRegex=regexp.MustCompile(`^[a-zA-Z0-9]+(?:[_.-]{1}[a-zA-Z0-9]+)*$`)
20-
displayNameRegex=regexp.MustCompile(`^[^\s](.*[^\s])?$`)
21-
)
22-
2316
funcPrintOrNull(vany)string {
2417
ifv==nil {
2518
return"null"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp