forked from huaweicloud/terraform-provider-huaweicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_huaweicloud_as_group_v1.go
643 lines (581 loc) · 20.3 KB
/
resource_huaweicloud_as_group_v1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package huaweicloud
import (
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/autoscaling/v1/groups"
"github.com/huaweicloud/golangsdk/openstack/autoscaling/v1/instances"
)
func resourceASGroup() *schema.Resource {
return &schema.Resource{
Create: resourceASGroupCreate,
Read: resourceASGroupRead,
Update: resourceASGroupUpdate,
Delete: resourceASGroupDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"scaling_group_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: resourceASGroupValidateGroupName,
ForceNew: false,
},
"scaling_configuration_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
},
"desire_instance_number": {
Type: schema.TypeInt,
Optional: true,
ForceNew: false,
},
"min_instance_number": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
ForceNew: false,
},
"max_instance_number": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
ForceNew: false,
},
"cool_down_time": {
Type: schema.TypeInt,
Optional: true,
Default: 900,
ValidateFunc: resourceASGroupValidateCoolDownTime,
ForceNew: false,
Description: "The cooling duration, in seconds.",
},
"lb_listener_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
ValidateFunc: resourceASGroupValidateListenerId,
Description: "The system supports the binding of up to three ELB listeners, the IDs of which are separated using a comma.",
},
"available_zones": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: false,
},
"networks": {
Type: schema.TypeList,
MaxItems: 5,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
},
},
ForceNew: false,
},
"security_groups": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
},
},
ForceNew: false,
},
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"health_periodic_audit_method": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: resourceASGroupValidateHealthAuditMethod,
ForceNew: false,
Default: "NOVA_AUDIT",
},
"health_periodic_audit_time": {
Type: schema.TypeInt,
Optional: true,
Default: 5,
ValidateFunc: resourceASGroupValidateHealthAuditTime,
ForceNew: false,
Description: "The health check period for instances, in minutes.",
},
"instance_terminate_policy": {
Type: schema.TypeString,
Optional: true,
Default: "OLD_CONFIG_OLD_INSTANCE",
ValidateFunc: resourceASGroupValidateTerminatePolicy,
ForceNew: false,
},
"notifications": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: false,
},
"delete_publicip": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: false,
},
"delete_instances": {
Description: "Whether to delete instances when they are removed from the AS group.",
Type: schema.TypeString,
Optional: true,
Default: "no",
ForceNew: false,
},
"instances": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: false,
Description: "The instances id list in the as group.",
},
},
}
}
type Network struct {
ID string
}
type Group struct {
ID string
}
func expandNetworks(Networks []Network) []groups.NetworkOpts {
var networks []groups.NetworkOpts
for _, v := range Networks {
n := groups.NetworkOpts{
ID: v.ID,
}
networks = append(networks, n)
}
return networks
}
func expandGroups(Groups []Group) []groups.SecurityGroupOpts {
var asgroups []groups.SecurityGroupOpts
for _, v := range Groups {
n := groups.SecurityGroupOpts{
ID: v.ID,
}
asgroups = append(asgroups, n)
}
return asgroups
}
func getAllAvailableZones(d *schema.ResourceData) []string {
rawZones := d.Get("available_zones").([]interface{})
zones := make([]string, len(rawZones))
for i, raw := range rawZones {
zones[i] = raw.(string)
}
log.Printf("[DEBUG] getAvailableZones: %#v", zones)
return zones
}
func getAllNotifications(d *schema.ResourceData) []string {
rawNotifications := d.Get("notifications").([]interface{})
notifications := make([]string, len(rawNotifications))
for i, raw := range rawNotifications {
notifications[i] = raw.(string)
}
log.Printf("[DEBUG] getNotifications: %#v", notifications)
return notifications
}
func getAllNetworks(d *schema.ResourceData, meta interface{}) []Network {
var Networks []Network
networks := d.Get("networks").([]interface{})
for _, v := range networks {
network := v.(map[string]interface{})
networkID := network["id"].(string)
v := Network{
ID: networkID,
}
Networks = append(Networks, v)
}
log.Printf("[DEBUG] getNetworks: %#v", Networks)
return Networks
}
func getAllSecurityGroups(d *schema.ResourceData, meta interface{}) []Group {
var Groups []Group
asgroups := d.Get("security_groups").([]interface{})
for _, v := range asgroups {
group := v.(map[string]interface{})
groupID := group["id"].(string)
v := Group{
ID: groupID,
}
Groups = append(Groups, v)
}
log.Printf("[DEBUG] getGroups: %#v", Groups)
return Groups
}
func getInstancesInGroup(asClient *golangsdk.ServiceClient, groupID string, opts instances.ListOptsBuilder) ([]instances.Instance, error) {
var insList []instances.Instance
page, err := instances.List(asClient, groupID, opts).AllPages()
if err != nil {
return insList, fmt.Errorf("Error getting instances in ASGroup %q: %s", groupID, err)
}
insList, err = page.(instances.InstancePage).Extract()
return insList, err
}
func getInstancesIDs(allIns []instances.Instance) []string {
var allIDs []string
for _, ins := range allIns {
// Maybe the instance is pending, so we can't get the id,
// so unable to delete the instance this time, maybe next time to execute
// terraform destroy will works
if ins.ID != "" {
allIDs = append(allIDs, ins.ID)
}
}
log.Printf("[DEBUG] Get instances in ASGroups: %#v", allIDs)
return allIDs
}
func getInstancesLifeStates(allIns []instances.Instance) []string {
var allLifeStates []string
for _, ins := range allIns {
allLifeStates = append(allLifeStates, ins.LifeCycleStatus)
}
log.Printf("[DEBUG] Get instances lifecycle status in ASGroups: %#v", allLifeStates)
return allLifeStates
}
func refreshInstancesLifeStates(asClient *golangsdk.ServiceClient, groupID string, insNum int, checkInService bool) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
var opts instances.ListOptsBuilder
allIns, err := getInstancesInGroup(asClient, groupID, opts)
if err != nil {
return nil, "ERROR", err
}
// maybe the instances (or some of the instances) have not put in the asg when creating
if checkInService && len(allIns) != insNum {
return allIns, "PENDING", err
}
allLifeStatus := getInstancesLifeStates(allIns)
for _, lifeStatus := range allLifeStatus {
log.Printf("[DEBUG] Get lifecycle status in group %s: %s", groupID, lifeStatus)
// check for creation
if checkInService {
if lifeStatus == "PENDING" || lifeStatus == "REMOVING" {
return allIns, lifeStatus, err
}
}
// check for removal
if !checkInService {
if lifeStatus == "REMOVING" || lifeStatus != "INSERVICE" {
return allIns, lifeStatus, err
}
}
}
if checkInService {
return allIns, "INSERVICE", err
}
log.Printf("[DEBUG] Exit refreshInstancesLifeStates for %q!", groupID)
return allIns, "", err
}
}
func checkASGroupInstancesInService(asClient *golangsdk.ServiceClient, groupID string, insNum int, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"PENDING"},
Target: []string{"INSERVICE"}, //if there is no lifecyclestatus, meaning no instances in asg
Refresh: refreshInstancesLifeStates(asClient, groupID, insNum, true),
Timeout: timeout,
Delay: 10 * time.Second,
}
_, err := stateConf.WaitForState()
return err
}
func checkASGroupInstancesRemoved(asClient *golangsdk.ServiceClient, groupID string, timeout time.Duration) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"REMOVING"},
Target: []string{""}, //if there is no lifecyclestatus, meaning no instances in asg
Refresh: refreshInstancesLifeStates(asClient, groupID, 0, false),
Timeout: timeout,
Delay: 10 * time.Second,
}
_, err := stateConf.WaitForState()
return err
}
func resourceASGroupCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
asClient, err := config.autoscalingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud autoscaling client: %s", err)
}
log.Printf("[DEBUG] asClient: %#v", asClient)
minNum := d.Get("min_instance_number").(int)
maxNum := d.Get("max_instance_number").(int)
desireNum := d.Get("desire_instance_number").(int)
log.Printf("[DEBUG] Min instance number is: %#v", minNum)
log.Printf("[DEBUG] Max instance number is: %#v", maxNum)
log.Printf("[DEBUG] Desire instance number is: %#v", desireNum)
if desireNum < minNum || desireNum > maxNum {
return fmt.Errorf("Invalid parameters: it should be min_instance_number<=desire_instance_number<=max_instance_number")
}
var initNum int
if desireNum > 0 {
initNum = desireNum
} else {
initNum = minNum
}
log.Printf("[DEBUG] Init instance number is: %#v", initNum)
networks := getAllNetworks(d, meta)
asgNetworks := expandNetworks(networks)
secGroups := getAllSecurityGroups(d, meta)
asgSecGroups := expandGroups(secGroups)
log.Printf("[DEBUG] available_zones: %#v", d.Get("available_zones"))
createOpts := groups.CreateOpts{
Name: d.Get("scaling_group_name").(string),
ConfigurationID: d.Get("scaling_configuration_id").(string),
DesireInstanceNumber: desireNum,
MinInstanceNumber: minNum,
MaxInstanceNumber: maxNum,
CoolDownTime: d.Get("cool_down_time").(int),
LBListenerID: d.Get("lb_listener_id").(string),
AvailableZones: getAllAvailableZones(d),
Networks: asgNetworks,
SecurityGroup: asgSecGroups,
VpcID: d.Get("vpc_id").(string),
HealthPeriodicAuditMethod: d.Get("health_periodic_audit_method").(string),
HealthPeriodicAuditTime: d.Get("health_periodic_audit_time").(int),
InstanceTerminatePolicy: d.Get("instance_terminate_policy").(string),
Notifications: getAllNotifications(d),
IsDeletePublicip: d.Get("delete_publicip").(bool),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
asgId, err := groups.Create(asClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating ASGroup: %s", err)
}
d.SetId(asgId)
//enable asg
enableResult := groups.Enable(asClient, asgId)
if enableResult.Err != nil {
return fmt.Errorf("Error enabling ASGroup %q: %s", asgId, enableResult.Err)
}
log.Printf("[DEBUG] Enable ASGroup %q success!", asgId)
// check all instances are inservice
if initNum > 0 {
timeout := d.Timeout(schema.TimeoutCreate)
err = checkASGroupInstancesInService(asClient, asgId, initNum, timeout)
if err != nil {
return fmt.Errorf("Error waiting for instances in the ASGroup %q to become inservice!!: %s", asgId, err)
}
}
return resourceASGroupRead(d, meta)
}
func resourceASGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
asClient, err := config.autoscalingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud autoscaling client: %s", err)
}
asg, err := groups.Get(asClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "AS group")
}
log.Printf("[DEBUG] Retrieved ASGroup %q: %+v", d.Id(), asg)
log.Printf("[DEBUG] Retrieved ASGroup %q notifications: %+v", d.Id(), asg.Notifications)
log.Printf("[DEBUG] Retrieved ASGroup %q availablezones: %+v", d.Id(), asg.AvailableZones)
log.Printf("[DEBUG] Retrieved ASGroup %q networks: %+v", d.Id(), asg.Networks)
log.Printf("[DEBUG] Retrieved ASGroup %q secgroups: %+v", d.Id(), asg.SecurityGroups)
// set properties based on the read info
d.Set("scaling_group_name", asg.Name)
d.Set("desire_instance_number", asg.DesireInstanceNumber)
d.Set("min_instance_number", asg.MinInstanceNumber)
d.Set("max_instance_number", asg.MaxInstanceNumber)
d.Set("cool_down_time", asg.CoolDownTime)
d.Set("lb_listener_id", asg.LBListenerID)
d.Set("health_periodic_audit_method", asg.HealthPeriodicAuditMethod)
d.Set("health_periodic_audit_time", asg.HealthPeriodicAuditTime)
d.Set("instance_terminate_policy", asg.InstanceTerminatePolicy)
d.Set("scaling_configuration_id", asg.ConfigurationID)
d.Set("delete_publicip", asg.DeletePublicip)
if len(asg.Notifications) >= 1 {
d.Set("notifications", asg.Notifications)
}
var opts instances.ListOptsBuilder
allIns, err := getInstancesInGroup(asClient, d.Id(), opts)
if err != nil {
return fmt.Errorf("Can not get the instances in ASGroup %q!!: %s", d.Id(), err)
}
allIDs := getInstancesIDs(allIns)
d.Set("instances", allIDs)
d.Set("region", GetRegion(d, config))
return nil
}
func resourceASGroupUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
asClient, err := config.autoscalingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud autoscaling client: %s", err)
}
d.Partial(true)
if d.HasChange("min_instance_number") || d.HasChange("max_instance_number") || d.HasChange("desire_instance_number") {
minNum := d.Get("min_instance_number").(int)
maxNum := d.Get("max_instance_number").(int)
desireNum := d.Get("desire_instance_number").(int)
log.Printf("[DEBUG] Min instance number is: %#v", minNum)
log.Printf("[DEBUG] Max instance number is: %#v", maxNum)
log.Printf("[DEBUG] Desire instance number is: %#v", desireNum)
if desireNum < minNum || desireNum > maxNum {
return fmt.Errorf("Invalid parameters: it should be min_instance_number<=desire_instance_number<=max_instance_number")
}
}
networks := getAllNetworks(d, meta)
asgNetworks := expandNetworks(networks)
secGroups := getAllSecurityGroups(d, meta)
asgSecGroups := expandGroups(secGroups)
updateOpts := groups.UpdateOpts{
Name: d.Get("scaling_group_name").(string),
ConfigurationID: d.Get("scaling_configuration_id").(string),
DesireInstanceNumber: d.Get("desire_instance_number").(int),
MinInstanceNumber: d.Get("min_instance_number").(int),
MaxInstanceNumber: d.Get("max_instance_number").(int),
CoolDownTime: d.Get("cool_down_time").(int),
LBListenerID: d.Get("lb_listener_id").(string),
AvailableZones: getAllAvailableZones(d),
Networks: asgNetworks,
SecurityGroup: asgSecGroups,
HealthPeriodicAuditMethod: d.Get("health_periodic_audit_method").(string),
HealthPeriodicAuditTime: d.Get("health_periodic_audit_time").(int),
InstanceTerminatePolicy: d.Get("instance_terminate_policy").(string),
Notifications: getAllNotifications(d),
IsDeletePublicip: d.Get("delete_publicip").(bool),
}
asgID, err := groups.Update(asClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Error updating ASGroup %q: %s", asgID, err)
}
d.Partial(false)
return resourceASGroupRead(d, meta)
}
func resourceASGroupDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
asClient, err := config.autoscalingV1Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud autoscaling client: %s", err)
}
log.Printf("[DEBUG] Begin to get instances of ASGroup %q", d.Id())
var listOpts instances.ListOptsBuilder
allIns, err := getInstancesInGroup(asClient, d.Id(), listOpts)
if err != nil {
return fmt.Errorf("Error listing instances of asg: %s", err)
}
allLifeStatus := getInstancesLifeStates(allIns)
for _, lifeCycleState := range allLifeStatus {
if lifeCycleState != "INSERVICE" {
return fmt.Errorf("[DEBUG] Can't delete the ASGroup %q: There are some instances not in INSERVICE but in %s, try again latter.", d.Id(), lifeCycleState)
}
}
allIDs := getInstancesIDs(allIns)
log.Printf("[DEBUG] InstanceIDs in ASGroup %q: %+v", d.Id(), allIDs)
log.Printf("[DEBUG] There are %d instances in ASGroup %q", len(allIDs), d.Id())
if len(allLifeStatus) > 0 {
min_number := d.Get("min_instance_number").(int)
if min_number > 0 {
return fmt.Errorf("[DEBUG] Can't delete the ASGroup %q: The instance number after the removal will less than the min number %d, modify the min number to zero first.", d.Id(), min_number)
}
delete_ins := d.Get("delete_instances").(string)
log.Printf("[DEBUG] The flag delete_instances in ASGroup is %s", delete_ins)
batchResult := instances.BatchDelete(asClient, d.Id(), allIDs, delete_ins)
if batchResult.Err != nil {
return fmt.Errorf("Error removing instancess of asg: %s", batchResult.Err)
}
log.Printf("[DEBUG] Begin to remove instances of ASGroup %q", d.Id())
timeout := d.Timeout(schema.TimeoutDelete)
err = checkASGroupInstancesRemoved(asClient, d.Id(), timeout)
if err != nil {
return fmt.Errorf(
"[DEBUG] Error removing instances from ASGroup %q: %s", d.Id(), err)
}
}
log.Printf("[DEBUG] Begin to delete ASGroup %q", d.Id())
if delErr := groups.Delete(asClient, d.Id()).ExtractErr(); delErr != nil {
return fmt.Errorf("Error deleting ASGroup: %s", delErr)
}
return nil
}
var TerminatePolices = [4]string{"OLD_CONFIG_OLD_INSTANCE", "OLD_CONFIG_NEW_INSTANCE", "OLD_INSTANCE", "NEW_INSTANCE"}
func resourceASGroupValidateTerminatePolicy(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
for i := range TerminatePolices {
if value == TerminatePolices[i] {
return
}
}
errors = append(errors, fmt.Errorf("%q must be one of %v", k, TerminatePolices))
return
}
func resourceASGroupValidateCoolDownTime(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if 0 <= value && value <= 86400 {
return
}
errors = append(errors, fmt.Errorf("%q must be [0, 86400]", k))
return
}
func resourceASGroupValidateListenerId(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
split := strings.Split(value, ",")
if len(split) <= 3 {
return
}
errors = append(errors, fmt.Errorf("%q supports binding up to 3 ELB listeners which are separated by a comma.", k))
return
}
var HealthAuditMethods = [2]string{"ELB_AUDIT", "NOVA_AUDIT"}
func resourceASGroupValidateHealthAuditMethod(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
for i := range HealthAuditMethods {
if value == HealthAuditMethods[i] {
return
}
}
errors = append(errors, fmt.Errorf("%q must be one of %v", k, HealthAuditMethods))
return
}
var HealthAuditTime = [4]int{5, 15, 60, 180}
func resourceASGroupValidateHealthAuditTime(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
for i := range HealthAuditTime {
if value == HealthAuditTime[i] {
return
}
}
errors = append(errors, fmt.Errorf("%q must be one of %v", k, HealthAuditTime))
return
}
func resourceASGroupValidateGroupName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 64 || len(value) < 1 {
errors = append(errors, fmt.Errorf("%q must contain more than 1 and less than 64 characters", k))
}
if !regexp.MustCompile(`^[0-9a-zA-Z-_]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf("only alphanumeric characters, hyphens, and underscores allowed in %q", k))
}
return
}