@@ -3,6 +3,7 @@ package heroku
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "github.com/hashicorp/terraform/helper/validation"
6
7
"log"
7
8
"net/url"
8
9
"regexp"
@@ -15,10 +16,6 @@ import (
15
16
heroku "github.com/heroku/heroku-go/v5"
16
17
)
17
18
18
- const (
19
- AddonNameMaxLength = 256
20
- )
21
-
22
19
// Global lock to prevent parallelism for heroku_addon since
23
20
// the Heroku API cannot handle a single application requesting
24
21
// multiple addons simultaneously.
@@ -52,9 +49,10 @@ func resourceHerokuAddon() *schema.Resource {
52
49
},
53
50
54
51
"name" : {
55
- Type : schema .TypeString ,
56
- Optional : true ,
57
- Computed : true ,
52
+ Type : schema .TypeString ,
53
+ Optional : true ,
54
+ Computed : true ,
55
+ ValidateFunc : validateCustomAddonName ,
58
56
},
59
57
60
58
"config" : {
@@ -79,6 +77,25 @@ func resourceHerokuAddon() *schema.Resource {
79
77
}
80
78
}
81
79
80
+ func validateCustomAddonName (v interface {}, k string ) (ws []string , errors []error ) {
81
+ // Check length
82
+ v1 := validation .StringLenBetween (1 , 256 )
83
+ _ , errs1 := v1 (v , k )
84
+ for _ , err := range errs1 {
85
+ errors = append (errors , err )
86
+ }
87
+
88
+ // Check validity
89
+ valRegex := regexp .MustCompile (`^[a-zA-Z][A-Za-z0-9_-]+$` )
90
+ v2 := validation .StringMatch (valRegex , "Invalid custom addon name: must start with a letter and can only contain lowercase letters, numbers, and dashes" )
91
+ _ , errs2 := v2 (v , k )
92
+ for _ , err := range errs2 {
93
+ errors = append (errors , err )
94
+ }
95
+
96
+ return ws , errors
97
+ }
98
+
82
99
func resourceHerokuAddonCreate (d * schema.ResourceData , meta interface {}) error {
83
100
addonLock .Lock ()
84
101
defer addonLock .Unlock ()
@@ -99,12 +116,6 @@ func resourceHerokuAddonCreate(d *schema.ResourceData, meta interface{}) error {
99
116
}
100
117
101
118
if v := d .Get ("name" ).(string ); v != "" {
102
- // Validate name with regex
103
- valErr := validateAddonName (v )
104
- if valErr != nil {
105
- return valErr
106
- }
107
-
108
119
opts .Name = & v
109
120
}
110
121
@@ -256,32 +267,3 @@ func AddOnStateRefreshFunc(client *heroku.Service, appID, addOnID string) resour
256
267
return (* heroku .AddOn )(addon ), addon .State , nil
257
268
}
258
269
}
259
-
260
- // validateAddonName uses the documented regex expression to make sure the user provided addon name is valid.
261
- //
262
- // Reference: https://devcenter.heroku.com/articles/platform-api-reference#add-on-create-optional-parameters
263
- func validateAddonName (name string ) error {
264
- errors := make ([]string , 0 )
265
-
266
- // First validate length. There is no documented length
267
- // but I've tried up to 256 characters so this will be max for now.
268
- if len (name ) > AddonNameMaxLength {
269
- errors = append (errors , fmt .Sprintf ("Length cannot exceed %v characters" , AddonNameMaxLength ))
270
- }
271
-
272
- // Then validate the content of the string against documented regex.
273
- regex := regexp .MustCompile (`^[a-zA-Z][A-Za-z0-9_-]+$` )
274
- matches := regex .FindStringSubmatch (name )
275
- if len (matches ) == 0 {
276
- errors = append (errors , "Needs to match this regex: ^[a-zA-Z][A-Za-z0-9_-]+$" )
277
- }
278
-
279
- if len (errors ) > 0 {
280
- errFormatted := ""
281
- for _ , err := range errors {
282
- errFormatted += fmt .Sprintf ("-%s\n " , err )
283
- }
284
- return fmt .Errorf ("Invalid custom addon name:\n " + errFormatted )
285
- }
286
- return nil
287
- }
0 commit comments