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

fix: usecodersdk functions for validating name attributes#130

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
aslilac merged 4 commits intomainfromlilac/codersdkvalidator
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
6 changes: 3 additions & 3 deletions.golangci.yml
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
# Visit https://golangci-lint.run/ for usage documentation
#and information onother useful linters
# Visit https://golangci-lint.run/ for usage documentation and information on
# other useful linters
issues:
max-per-linter: 0
max-same-issues: 0
Expand All@@ -24,4 +24,4 @@ linters:
- unconvert
- unparam
- unused
- vet
- vet
10 changes: 10 additions & 0 deletionsinternal/codersdkvalidator/display_name.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
package codersdkvalidator

import (
"github.com/coder/coder/v2/codersdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func DisplayName() validator.String {
return validatorFromFunc(codersdk.DisplayNameValid, "value must be a valid display name")
}
10 changes: 10 additions & 0 deletionsinternal/codersdkvalidator/group_name.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
package codersdkvalidator

import (
"github.com/coder/coder/v2/codersdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func GroupName() validator.String {
return validatorFromFunc(codersdk.GroupNameValid, "value must be a valid group name")
}
10 changes: 10 additions & 0 deletionsinternal/codersdkvalidator/name.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
package codersdkvalidator

import (
"github.com/coder/coder/v2/codersdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func Name() validator.String {
return validatorFromFunc(codersdk.NameValid, "value must be a valid name")
}
10 changes: 10 additions & 0 deletionsinternal/codersdkvalidator/template_version_name.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
package codersdkvalidator

import (
"github.com/coder/coder/v2/codersdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func TemplateVersionName() validator.String {
return validatorFromFunc(codersdk.TemplateVersionNameValid, "value must be a valid template version name")
}
10 changes: 10 additions & 0 deletionsinternal/codersdkvalidator/user_real_name.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
package codersdkvalidator

import (
"github.com/coder/coder/v2/codersdk"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

func UserRealName() validator.String {
return validatorFromFunc(codersdk.UserRealNameValid, "value must be a valid name for a user")
}
51 changes: 51 additions & 0 deletionsinternal/codersdkvalidator/validator_from_func.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
package codersdkvalidator

import (
"context"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

type functionValidator struct {
check func(string) error
defaultMessage string
err error
}

func validatorFromFunc(check func(string) error, defaultMessage string) functionValidator {
return functionValidator{
check: check,
defaultMessage: defaultMessage,
}
}

var _ validator.String = functionValidator{}

func (v functionValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

name := req.ConfigValue.ValueString()
if v.err = v.check(name); v.err != nil {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
v.Description(ctx),
name,
))
}
}

var _ validator.Describer = functionValidator{}

func (v functionValidator) Description(_ context.Context) string {
if v.err != nil {
return v.err.Error()
}
return v.defaultMessage
}

func (v functionValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
8 changes: 3 additions & 5 deletionsinternal/provider/group_resource.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,8 +6,8 @@ import (
"strings"

"github.com/coder/coder/v2/codersdk"
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
Expand DownExpand Up@@ -77,17 +77,15 @@ func (r *GroupResource) Schema(ctx context.Context, req resource.SchemaRequest,
MarkdownDescription: "The unique name of the group.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 36),
stringvalidator.RegexMatches(nameValidRegex, "Group names must be alpahnumeric with hyphens."),
codersdkvalidator.GroupName(),
},
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the group. Defaults to the group name.",
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(displayNameRegex, "Group display names must be alphanumeric with spaces"),
codersdkvalidator.DisplayName(),
},
Default: stringdefault.StaticString(""),
},
Expand Down
10 changes: 4 additions & 6 deletionsinternal/provider/template_resource.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@ import (
"github.com/coder/coder/v2/coderd/util/ptr"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/provisionersdk"
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
Expand DownExpand Up@@ -258,17 +259,15 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
MarkdownDescription: "The name of the template.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 32),
stringvalidator.RegexMatches(nameValidRegex, "Template names must be alphanumeric with hyphens."),
codersdkvalidator.Name(),
},
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the template. Defaults to the template name.",
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(displayNameRegex, "Template display names must be alphanumeric with spaces."),
codersdkvalidator.DisplayName(),
},
},
"description": schema.StringAttribute{
Expand DownExpand Up@@ -418,8 +417,7 @@ func (r *TemplateResource) Schema(ctx context.Context, req resource.SchemaReques
Optional: true,
Computed: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 64),
stringvalidator.RegexMatches(templateVersionNameRegex, "Template version names must be alphanumeric with underscores and dots."),
codersdkvalidator.TemplateVersionName(),
},
},
"message": schema.StringAttribute{
Expand Down
6 changes: 3 additions & 3 deletionsinternal/provider/user_resource.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/coder/coder/v2/codersdk"
"github.com/coder/terraform-provider-coderd/internal/codersdkvalidator"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand DownExpand Up@@ -71,16 +72,15 @@ func (r *UserResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: "Username of the user.",
Required: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 32),
stringvalidator.RegexMatches(nameValidRegex, "Username must be alphanumeric with hyphens."),
codersdkvalidator.Name(),
},
},
"name": schema.StringAttribute{
MarkdownDescription: "Display name of the user. Defaults to username.",
Computed: true,
Optional: true,
Validators: []validator.String{
stringvalidator.LengthBetween(1, 128),
codersdkvalidator.UserRealName(),
},
},
"email": schema.StringAttribute{
Expand Down
7 changes: 0 additions & 7 deletionsinternal/provider/util.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,18 +8,11 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"

"github.com/coder/coder/v2/codersdk"
"github.com/google/uuid"
)

var (
nameValidRegex = regexp.MustCompile("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*$")
templateVersionNameRegex = regexp.MustCompile(`^[a-zA-Z0-9]+(?:[_.-]{1}[a-zA-Z0-9]+)*$`)
displayNameRegex = regexp.MustCompile(`^[^\s](.*[^\s])?$`)
)

func PrintOrNull(v any) string {
if v == nil {
return "null"
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp