From 2748f29877e57754c96a9bac31d55033edefb7de Mon Sep 17 00:00:00 2001 From: Ryan Hristovski <61257223+ryanhristovski@users.noreply.github.com> Date: Wed, 21 Aug 2024 10:52:24 -0400 Subject: [PATCH 1/2] Setup validations for resource --- internal/provider/resource_repository.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/provider/resource_repository.go b/internal/provider/resource_repository.go index f2c4e23..6e92297 100644 --- a/internal/provider/resource_repository.go +++ b/internal/provider/resource_repository.go @@ -4,16 +4,19 @@ import ( "context" "fmt" "log" + "regexp" "strings" "github.com/docker/terraform-provider-docker/internal/pkg/hubclient" "github.com/docker/terraform-provider-docker/internal/pkg/repositoryutils" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" ) @@ -177,6 +180,12 @@ func (r *RepositoryResource) Schema(_ context.Context, _ resource.SchemaRequest, PlanModifiers: []planmodifier.String{ stringplanmodifier.RequiresReplace(), }, + Validators: []validator.String{ + stringvalidator.RegexMatches( + regexp.MustCompile(`^[a-zA-Z0-9]([-.a-zA-Z0-9]*[a-zA-Z0-9])?$`), + "Name must only contain alphanumeric characters, '.', or '-', and must start and end with an alphanumeric character", + ), + }, }, "description": schema.StringAttribute{ MarkdownDescription: "Repository description", From a3491c069fe1be6f2e8a0048586c9bb2ddbc47c0 Mon Sep 17 00:00:00 2001 From: Ryan Hristovski <61257223+ryanhristovski@users.noreply.github.com> Date: Wed, 21 Aug 2024 13:06:32 -0400 Subject: [PATCH 2/2] use same regex as source --- internal/provider/resource_repository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/provider/resource_repository.go b/internal/provider/resource_repository.go index 6e92297..c7cfc03 100644 --- a/internal/provider/resource_repository.go +++ b/internal/provider/resource_repository.go @@ -182,7 +182,7 @@ func (r *RepositoryResource) Schema(_ context.Context, _ resource.SchemaRequest, }, Validators: []validator.String{ stringvalidator.RegexMatches( - regexp.MustCompile(`^[a-zA-Z0-9]([-.a-zA-Z0-9]*[a-zA-Z0-9])?$`), + regexp.MustCompile(`^[a-z0-9]+(?:[._-][a-z0-9]+)*$`), "Name must only contain alphanumeric characters, '.', or '-', and must start and end with an alphanumeric character", ), },