Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Visit https://golangci-lint.run/ for usage documentation
# and information on other 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 deletions internal/codersdkvalidator/display_name.go
Original file line number Diff line number Diff 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 deletions internal/codersdkvalidator/name.go
Original file line number Diff line number Diff 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 deletions internal/codersdkvalidator/template_version_name.go
Original file line number Diff line number Diff 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 deletions internal/codersdkvalidator/user_real_name.go
Original file line number Diff line number Diff 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 deletions internal/codersdkvalidator/validator_from_func.go
Original file line number Diff line number Diff 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 "value must be a valid name"
}

func (v functionValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
8 changes: 3 additions & 5 deletions internal/provider/group_resource.go
Original file line number Diff line number Diff 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 Down Expand 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.Name(),
},
},
"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 deletions internal/provider/template_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cdr.dev/slog"
"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 Down Expand Up @@ -257,17 +258,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 Down Expand Up @@ -417,8 +416,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 deletions internal/provider/user_resource.go
Original file line number Diff line number Diff 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 Down Expand 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 deletions internal/provider/util.go
Original file line number Diff line number Diff 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 PtrTo[T any](v T) *T {
return &v
}
Expand Down
Loading