11package attributes
22
33import (
4+ "context"
5+ "regexp"
6+
47 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
58 "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
69 "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
710 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
811 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
12+ "github.com/hashicorp/terraform-plugin-framework/schema/validator"
913 "github.com/hashicorp/terraform-plugin-framework/types"
1014 "go.clever-cloud.com/terraform-provider/pkg"
1115)
@@ -21,7 +25,11 @@ type Addon struct {
2125var addonCommon = map [string ]schema.Attribute {
2226 "id" : schema.StringAttribute {Computed : true , MarkdownDescription : "Generated unique identifier" , PlanModifiers : []planmodifier.String {stringplanmodifier .UseStateForUnknown ()}},
2327 "name" : schema.StringAttribute {Required : true , MarkdownDescription : "Name of the service" },
24- "plan" : schema.StringAttribute {Required : true , MarkdownDescription : "Database size and spec" },
28+ "plan" : schema.StringAttribute {
29+ Required : true ,
30+ MarkdownDescription : "Database size and spec" ,
31+ Validators : []validator.String {slugValidator },
32+ },
2533 "region" : schema.StringAttribute {
2634 Optional : true ,
2735 Computed : true ,
@@ -34,3 +42,17 @@ var addonCommon = map[string]schema.Attribute{
3442func WithAddonCommons (runtimeSpecifics map [string ]schema.Attribute ) map [string ]schema.Attribute {
3543 return pkg .Merge (addonCommon , runtimeSpecifics )
3644}
45+
46+ // https://regex101.com/r/bMOotf/1
47+ var slugRegex = regexp .MustCompile (`^[a-z1-9_]*$` )
48+ var slugValidator = pkg .NewStringValidator (
49+ "expect slug value" ,
50+ func (ctx context.Context , req validator.StringRequest , res * validator.StringResponse ) {
51+ if req .ConfigValue .IsNull () || req .ConfigValue .IsUnknown () {
52+ return
53+ }
54+
55+ if ! slugRegex .MatchString (req .ConfigValue .ValueString ()) {
56+ res .Diagnostics .AddAttributeError (req .Path , "expect lowercase and underscore characters only" , "Invalid slug, expect something like `xs_tny`" )
57+ }
58+ })
0 commit comments