From c2a4012a2600193ab45bfc5d66e921b4e8db2604 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Thu, 1 Jun 2023 17:20:25 +0700 Subject: [PATCH] Add vaidation rules Signed-off-by: Zack Siri --- go.mod | 1 + go.sum | 2 ++ instellar/cluster/resource.go | 14 ++++++++++++++ instellar/node/resource.go | 9 +++++++++ 4 files changed, 26 insertions(+) diff --git a/go.mod b/go.mod index 9c7a2bc..4086f4e 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/google/uuid v1.3.0 github.com/hashicorp/terraform-plugin-docs v0.14.1 github.com/hashicorp/terraform-plugin-framework v1.2.0 + github.com/hashicorp/terraform-plugin-framework-validators v0.10.0 github.com/hashicorp/terraform-plugin-go v0.15.0 github.com/hashicorp/terraform-plugin-log v0.8.0 github.com/hashicorp/terraform-plugin-testing v1.2.0 diff --git a/go.sum b/go.sum index 01ec4c0..9af5e22 100644 --- a/go.sum +++ b/go.sum @@ -95,6 +95,8 @@ github.com/hashicorp/terraform-plugin-docs v0.14.1 h1:MikFi59KxrP/ewrZoaowrB9he5 github.com/hashicorp/terraform-plugin-docs v0.14.1/go.mod h1:k2NW8+t113jAus6bb5tQYQgEAX/KueE/u8X2Z45V1GM= github.com/hashicorp/terraform-plugin-framework v1.2.0 h1:MZjFFfULnFq8fh04FqrKPcJ/nGpHOvX4buIygT3MSNY= github.com/hashicorp/terraform-plugin-framework v1.2.0/go.mod h1:nToI62JylqXDq84weLJ/U3umUsBhZAaTmU0HXIVUOcw= +github.com/hashicorp/terraform-plugin-framework-validators v0.10.0 h1:4L0tmy/8esP6OcvocVymw52lY0HyQ5OxB7VNl7k4bS0= +github.com/hashicorp/terraform-plugin-framework-validators v0.10.0/go.mod h1:qdQJCdimB9JeX2YwOpItEu+IrfoJjWQ5PhLpAOMDQAE= github.com/hashicorp/terraform-plugin-go v0.15.0 h1:1BJNSUFs09DS8h/XNyJNJaeusQuWc/T9V99ylU9Zwp0= github.com/hashicorp/terraform-plugin-go v0.15.0/go.mod h1:tk9E3/Zx4RlF/9FdGAhwxHExqIHHldqiQGt20G6g+nQ= github.com/hashicorp/terraform-plugin-log v0.8.0 h1:pX2VQ/TGKu+UU1rCay0OlzosNKe4Nz1pepLXj95oyy0= diff --git a/instellar/cluster/resource.go b/instellar/cluster/resource.go index c2add66..e03d465 100644 --- a/instellar/cluster/resource.go +++ b/instellar/cluster/resource.go @@ -3,6 +3,7 @@ package cluster import ( "context" "fmt" + "regexp" "strconv" "time" @@ -14,7 +15,10 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema" "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" + + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" ) var ( @@ -61,6 +65,13 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re "name": schema.StringAttribute{ Description: "Name assigned by the user", Required: true, + Validators: []validator.String{ + stringvalidator.LengthBetween(3, 32), + stringvalidator.RegexMatches( + regexp.MustCompile(`^[a-z0-9\-]+$`), + "must contain only lowercase alphanumeric characters", + ), + }, }, "slug": schema.StringAttribute{ Description: "Unique slug for cluster", @@ -73,6 +84,9 @@ func (r *clusterResource) Schema(_ context.Context, _ resource.SchemaRequest, re "provider_name": schema.StringAttribute{ Description: "Provider of the infrastructure", Required: true, + Validators: []validator.String{ + stringvalidator.OneOf([]string{"aws", "hcloud", "digitalocean", "google", "azurerm"}...), + }, }, "region": schema.StringAttribute{ Description: "Region of the cluster", diff --git a/instellar/node/resource.go b/instellar/node/resource.go index 558e312..1ddf6c6 100644 --- a/instellar/node/resource.go +++ b/instellar/node/resource.go @@ -3,16 +3,19 @@ package node import ( "context" "fmt" + "regexp" "strconv" "time" instc "github.com/upmaru/instellar-go" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" "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" ) @@ -57,6 +60,12 @@ func (r *nodeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp "slug": schema.StringAttribute{ Description: "Node slug", Required: true, + Validators: []validator.String{ + stringvalidator.RegexMatches( + regexp.MustCompile(`^[a-z0-9\-]+$`), + "must contain only lowercase alphanumeric characters", + ), + }, }, "current_state": schema.StringAttribute{ Description: "Current state",