-
Notifications
You must be signed in to change notification settings - Fork 645
/
validators.go
1370 lines (1198 loc) · 36.5 KB
/
validators.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package validate
import (
"encoding/json"
"errors"
"fmt"
"net"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
homedir "github.com/mitchellh/go-homedir"
"github.com/IBM-Cloud/bluemix-go/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
)
var (
validHRef *regexp.Regexp
)
func init() {
validHRef = regexp.MustCompile(`^http(s)?:\/\/([^\/?#]*)([^?#]*)(\?([^#]*))?(#(.*))?$`)
}
func ValidateSecondaryIPCount(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value != 4 && value != 8 {
errors = append(errors, fmt.Errorf(
"%q must be either 4 or 8", k))
}
return
}
func validateServiceTags(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 2048 {
errors = append(errors, fmt.Errorf(
"%q must contain tags whose maximum length is 2048 characters", k))
}
return
}
func ValidateAllowedStringValues(validValues []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
input := v.(string)
existed := false
for _, s := range validValues {
if s == input {
existed = true
break
}
}
if !existed {
errors = append(errors, fmt.Errorf(
"%q must contain a value from %#v, got %q",
k, validValues, input))
}
return
}
}
func ValidBucketLifecycleTimestamp(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
_, err := time.Parse(time.RFC3339, fmt.Sprintf("%sT00:00:00Z", value))
if err != nil {
errors = append(errors, fmt.Errorf(
"%q cannot be parsed as RFC3339 Timestamp Format", value))
}
return
}
func validateRegexpLen(min, max int, regex string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
acceptedcharacters, _ := regexp.MatchString(regex, value)
if acceptedcharacters {
if (len(value) < min) || (len(value) > max) && (min > 0 && max > 0) {
errors = append(errors, fmt.Errorf(
"%q (%q) must contain from %d to %d characters ", k, value, min, max))
}
} else {
errors = append(errors, fmt.Errorf(
"%q (%q) should match regexp %s ", k, v, regex))
}
return
}
}
func ValidateAllowedIntValues(is []int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
existed := false
for _, i := range is {
if i == value {
existed = true
break
}
}
if !existed {
errors = append(errors, fmt.Errorf(
"%q must contain a valid int value should in array %#v, got %q",
k, is, value))
}
return
}
}
func ValidateAllowedEnterpriseNameValue() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < 3 || len(value) > 60 {
errors = append(errors, fmt.Errorf(
"%q must contain a valid string value with length between 3 and 60", value))
}
return
}
}
func ValidateRoutePath(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
//Somehow API allows this
if value == "" {
return
}
if (len(value) < 2) || (len(value) > 128) {
errors = append(errors, fmt.Errorf(
"%q (%q) must contain from 2 to 128 characters ", k, value))
}
if !(strings.HasPrefix(value, "/")) {
errors = append(errors, fmt.Errorf(
"%q (%q) must start with a forward slash '/'", k, value))
}
if strings.Contains(value, "?") {
errors = append(errors, fmt.Errorf(
"%q (%q) must not contain a '?'", k, value))
}
return
}
func ValidateRoutePort(v interface{}, k string) (ws []string, errors []error) {
return ValidatePortRange(1024, 65535)(v, k)
}
func validateAppPort(v interface{}, k string) (ws []string, errors []error) {
return ValidatePortRange(1024, 65535)(v, k)
}
func ValidateLBListenerPolicyPriority(v interface{}, k string) (ws []string, errors []error) {
interval := v.(int)
if interval < 1 || interval > 10 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 10",
k))
}
return
}
func ValidateStringLength(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if (len(value) < 1) || (len(value) > 128) {
errors = append(errors, fmt.Errorf(
"%q (%q) must contain from 1 to 128 characters ", k, value))
}
return
}
func ValidatePortRange(start, end int) func(v interface{}, k string) (ws []string, errors []error) {
f := func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if (value < start) || (value > end) {
errors = append(errors, fmt.Errorf(
"%q (%d) must be in the range of %d to %d", k, value, start, end))
}
return
}
return f
}
func ValidateDomainName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !(strings.Contains(value, ".")) {
errors = append(errors, fmt.Errorf(
"%q (%q) must contain a '.',example.com,foo.example.com", k, value))
}
return
}
func validateAppInstance(v interface{}, k string) (ws []string, errors []error) {
instances := v.(int)
if instances < 0 {
errors = append(errors, fmt.Errorf(
"%q (%q) must be greater than 0", k, instances))
}
return
}
func ValidateWorkerNum(v interface{}, k string) (ws []string, errors []error) {
workerNum := v.(int)
if workerNum <= 0 {
errors = append(errors, fmt.Errorf(
"%q must be greater than 0", k))
}
return
}
func validateAppZipPath(v interface{}, k string) (ws []string, errors []error) {
path := v.(string)
applicationZip, err := homedir.Expand(path)
if err != nil {
errors = append(errors, fmt.Errorf(
"%q (%q) home directory in the given path couldn't be expanded", k, path))
}
if !helpers.FileExists(applicationZip) {
errors = append(errors, fmt.Errorf(
"%q (%q) doesn't exist", k, path))
}
return
}
func ValidateNotes(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 1000 {
errors = append(errors, fmt.Errorf(
"%q should not exceed 1000 characters", k))
}
return
}
func ValidatePublicBandwidth(v interface{}, k string) (ws []string, errors []error) {
bandwidth := v.(int)
if bandwidth < 0 {
errors = append(errors, fmt.Errorf(
"%q (%q) must be greater than 0", k, bandwidth))
return
}
validBandwidths := []int{250, 1000, 5000, 10000, 20000}
for _, b := range validBandwidths {
if b == bandwidth {
return
}
}
errors = append(errors, fmt.Errorf(
"%q (%d) must be one of the value from %d", k, bandwidth, validBandwidths))
return
}
func ValidateMaxConn(v interface{}, k string) (ws []string, errors []error) {
maxConn := v.(int)
if maxConn < 1 || maxConn > 64000 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 64000",
k))
return
}
return
}
func ValidateKeyLifeTime(v interface{}, k string) (ws []string, errors []error) {
secs := v.(int)
if secs < 1800 || secs > 86400 {
errors = append(errors, fmt.Errorf(
"%q must be between 1800 and 86400",
k))
return
}
return
}
func ValidateWeight(v interface{}, k string) (ws []string, errors []error) {
weight := v.(int)
if weight < 0 || weight > 100 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 100",
k))
}
return
}
func ValidateSizePerZone(v interface{}, k string) (ws []string, errors []error) {
sizePerZone := v.(int)
if sizePerZone <= 0 {
errors = append(errors, fmt.Errorf(
"%q must be greater than 0",
k))
}
return
}
func ValidateInterval(v interface{}, k string) (ws []string, errors []error) {
interval := v.(int)
if interval < 2 || interval > 60 {
errors = append(errors, fmt.Errorf(
"%q must be between 2 and 60",
k))
}
return
}
func ValidateMaxRetries(v interface{}, k string) (ws []string, errors []error) {
maxRetries := v.(int)
if maxRetries < 1 || maxRetries > 10 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 10",
k))
}
return
}
func ValidateTimeout(v interface{}, k string) (ws []string, errors []error) {
timeout := v.(int)
if timeout < 1 || timeout > 59 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 59",
k))
}
return
}
func ValidateURLPath(v interface{}, k string) (ws []string, errors []error) {
urlPath := v.(string)
if len(urlPath) > 250 || !strings.HasPrefix(urlPath, "/") {
errors = append(errors, fmt.Errorf(
"%q should start with ‘/‘ and has a max length of 250 characters.",
k))
}
return
}
func ValidateSecurityRuleDirection(v interface{}, k string) (ws []string, errors []error) {
validDirections := map[string]bool{
"ingress": true,
"egress": true,
}
value := v.(string)
_, found := validDirections[value]
if !found {
strarray := make([]string, 0, len(validDirections))
for key := range validDirections {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid security group rule direction %q. Valid types are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func ValidateSecurityRuleEtherType(v interface{}, k string) (ws []string, errors []error) {
validEtherTypes := map[string]bool{
"IPv4": true,
"IPv6": true,
}
value := v.(string)
_, found := validEtherTypes[value]
if !found {
strarray := make([]string, 0, len(validEtherTypes))
for key := range validEtherTypes {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid security group rule ethernet type %q. Valid types are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
// ValidateIP...
func ValidateIP(v interface{}, k string) (ws []string, errors []error) {
address := v.(string)
if net.ParseIP(address) == nil {
errors = append(errors, fmt.Errorf(
"%q must be a valid ip address",
k))
}
return
}
// ValidateCIDR...
func ValidateCIDR(v interface{}, k string) (ws []string, errors []error) {
address := v.(string)
_, _, err := net.ParseCIDR(address)
if err != nil {
errors = append(errors, fmt.Errorf(
"%q must be a valid cidr address",
k))
}
return
}
// validateCIDRAddress...
func validateCIDRAddress() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
address := v.(string)
_, _, err := net.ParseCIDR(address)
if err != nil {
errors = append(errors, fmt.Errorf(
"%q must be a valid cidr address",
k))
}
return
}
}
// validateOverlappingAddress...
func validateOverlappingAddress() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
nonOverlappingCIDR := map[string]bool{
"127.0.0.0/8": true,
"161.26.0.0/16": true,
"166.8.0.0/14": true,
"169.254.0.0/16": true,
"224.0.0.0/4": true,
}
address := v.(string)
_, found := nonOverlappingCIDR[address]
if found {
errors = append(errors, fmt.Errorf(
"%q the request is overlapping with reserved address ranges",
k))
}
return
}
}
// ValidateRemoteIP...
func ValidateRemoteIP(v interface{}, k string) (ws []string, errors []error) {
_, err1 := ValidateCIDR(v, k)
_, err2 := ValidateIP(v, k)
if len(err1) != 0 && len(err2) != 0 {
errors = append(errors, fmt.Errorf(
"%q must be a valid remote ip address (cidr or ip)",
k))
}
return
}
// validateIPorCIDR...
func validateIPorCIDR() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
_, err1 := ValidateCIDR(v, k)
_, err2 := ValidateIP(v, k)
if len(err1) != 0 && len(err2) != 0 {
errors = append(errors, fmt.Errorf(
"%q must be a valid remote ip address (cidr or ip)",
k))
}
return
}
}
func ValidateSecurityRuleProtocol(v interface{}, k string) (ws []string, errors []error) {
validProtocols := map[string]bool{
"icmp": true,
"tcp": true,
"udp": true,
}
value := v.(string)
_, found := validProtocols[value]
if !found {
strarray := make([]string, 0, len(validProtocols))
for key := range validProtocols {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid security group rule ethernet type %q. Valid types are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
//func validateJSONString(v interface{}, k string) (ws []string, errors []error) {
// if _, err := flex.NormalizeJSONString(v); err != nil {
// errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
// }
// if err := validateKeyValue(v); err != nil {
// errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
// }
// return
//}
func validateJSONString() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
if _, err := flex.NormalizeJSONString(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
}
if err := validateKeyValue(v); err != nil {
errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err))
}
return
}
}
func ValidateRegexps(regex string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
acceptedcharacters, _ := regexp.MatchString(regex, value)
if !acceptedcharacters {
errors = append(errors, fmt.Errorf(
"%q (%q) should match regexp %s ", k, v, regex))
}
return
}
}
// NoZeroValues is a SchemaValidateFunc which tests if the provided value is
// not a zero value. It's useful in situations where you want to catch
// explicit zero values on things like required fields during validation.
func validateNoZeroValues() schema.SchemaValidateFunc {
return func(i interface{}, k string) (ws []string, errors []error) {
if reflect.ValueOf(i).Interface() == reflect.Zero(reflect.TypeOf(i)).Interface() {
switch reflect.TypeOf(i).Kind() {
case reflect.String:
errors = append(errors, fmt.Errorf("%s value must not be empty.", k))
case reflect.Int, reflect.Float64:
errors = append(errors, fmt.Errorf("%s value must not be zero.", k))
default:
// this validator should only ever be applied to TypeString, TypeInt and TypeFloat
errors = append(errors, fmt.Errorf("can't use NoZeroValues with %T attribute %s", k, i))
}
}
return
}
}
func validateBindedPackageName() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if !(strings.HasPrefix(value, "/")) {
errors = append(errors, fmt.Errorf(
"%q (%q) must start with a forward slash '/'.The package name should be '/whisk.system/cloudant', '/test@in.ibm.com_new/utils' or '/_/utils'", k, value))
}
index := strings.LastIndex(value, "/")
if index < 2 || index == len(value)-1 {
errors = append(errors, fmt.Errorf(
"%q (%q) is not a valid bind package name.The package name should be '/whisk.system/cloudant','/test@in.ibm.com_new/utils' or '/_/utils'", k, value))
}
return
}
}
func validateKeyValue(jsonString interface{}) error {
var j [](map[string]interface{})
if jsonString == nil || jsonString.(string) == "" {
return nil
}
s := jsonString.(string)
err := json.Unmarshal([]byte(s), &j)
if err != nil {
return err
}
for _, v := range j {
_, exists := v["key"]
if !exists {
return errors.New("'key' is missing from json")
}
_, exists = v["value"]
if !exists {
return errors.New("'value' is missing from json")
}
}
return nil
}
func validateActionName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if strings.HasPrefix(value, "/") {
errors = append(errors, fmt.Errorf(
"%q (%q) must not start with a forward slash '/'.The action name should be like 'myaction' or utils/cloudant'", k, value))
}
const alphaNumeric = "abcdefghijklmnopqrstuvwxyz0123456789/_@.-"
for _, char := range value {
if !strings.Contains(alphaNumeric, strings.ToLower(string(char))) {
errors = append(errors, fmt.Errorf(
"%q (%q) The name of the package contains illegal characters", k, value))
}
}
return
}
func validateActionKind(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
kindList := []string{"php:7.3", "nodejs:8", "swift:3", "nodejs", "blackbox", "java", "sequence", "nodejs:10", "python:3", "python", "python:2", "swift", "swift:4.2"}
if !stringInSlice(value, kindList) {
errors = append(errors, fmt.Errorf(
"%q (%q) Invalid kind is provided.Supported list of kinds of actions are (%q)", k, value, kindList))
}
return
}
func stringInSlice(str string, list []string) bool {
for _, v := range list {
if v == str {
return true
}
}
return false
}
func validateFunctionName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
var validName = regexp.MustCompile(`\A([\w]|[\w][\w@ .-]*[\w@.-]+)\z`)
if !validName.MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q (%q) The name contains illegal characters", k, value))
}
return
}
func ValidateStorageType(v interface{}, k string) (ws []string, errors []error) {
validEtherTypes := map[string]bool{
"Endurance": true,
"Performance": true,
}
value := v.(string)
_, found := validEtherTypes[value]
if !found {
strarray := make([]string, 0, len(validEtherTypes))
for key := range validEtherTypes {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid storage type %q. Valid types are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func validateRole(v interface{}, k string) (ws []string, errors []error) {
validRolesTypes := map[string]bool{
"Writer": true,
"Reader": true,
"Manager": true,
"Administrator": true,
"Operator": true,
"Viewer": true,
"Editor": true,
}
value := v.(string)
_, found := validRolesTypes[value]
if !found {
strarray := make([]string, 0, len(validRolesTypes))
for key := range validRolesTypes {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid role %q. Valid roles are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func ValidateDayOfWeek(v interface{}, k string) (ws []string, errors []error) {
validDayTypes := map[string]bool{
"SUNDAY": true,
"MONDAY": true,
"TUESDAY": true,
"WEDNESDAY": true,
"THURSDAY": true,
"FRIDAY": true,
"SATURDAY": true,
}
value := v.(string)
_, found := validDayTypes[value]
if !found {
strarray := make([]string, 0, len(validDayTypes))
for key := range validDayTypes {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid day %q. Valid days are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func ValidateScheduleType(v interface{}, k string) (ws []string, errors []error) {
validSchdTypes := map[string]bool{
"HOURLY": true,
"DAILY": true,
"WEEKLY": true,
}
value := v.(string)
_, found := validSchdTypes[value]
if !found {
strarray := make([]string, 0, len(validSchdTypes))
for key := range validSchdTypes {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid schedule type %q. Valid schedules are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func ValidateHour(start, end int) func(v interface{}, k string) (ws []string, errors []error) {
f := func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if (value < start) || (value > end) {
errors = append(errors, fmt.Errorf(
"%q (%d) must be in the range of %d to %d", k, value, start, end))
}
return
}
return f
}
func ValidateMinute(start, end int) func(v interface{}, k string) (ws []string, errors []error) {
f := func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if (value < start) || (value > end) {
errors = append(errors, fmt.Errorf(
"%q (%d) must be in the range of %d to %d", k, value, start, end))
}
return
}
return f
}
func ValidateDatacenterOption(v []interface{}, allowedValues []string) error {
for _, option := range v {
if option == nil {
return fmt.Errorf("Provide a valid `datacenter_choice`")
}
values := option.(map[string]interface{})
for k := range values {
if !stringInSlice(k, allowedValues) {
return fmt.Errorf(
"%q Invalid values are provided in `datacenter_choice`. Supported list of keys are (%q)", k, allowedValues)
}
}
}
return nil
}
func ValidateLBTimeout(v interface{}, k string) (ws []string, errors []error) {
timeout := v.(int)
if timeout <= 0 || timeout > 3600 {
errors = append(errors, fmt.Errorf(
"%q must be between 1 and 3600",
k))
}
return
}
// validateRecordType ensures that the dns record type is valid
func validateRecordType(t string, proxied bool) error {
switch t {
case "A", "AAAA", "CNAME":
return nil
case "TXT", "SRV", "LOC", "MX", "NS", "SPF", "CAA", "CERT", "DNSKEY", "DS", "NAPTR", "SMIMEA", "SSHFP", "TLSA", "URI":
if !proxied {
return nil
}
default:
return fmt.Errorf(
`Invalid type %q. Valid types are "A", "AAAA", "CNAME", "TXT", "SRV", "LOC", "MX", "NS", "SPF", "CAA", "CERT", "DNSKEY", "DS", "NAPTR", "SMIMEA", "SSHFP", "TLSA" or "URI".`, t)
}
return fmt.Errorf("Type %q cannot be proxied", t)
}
// validateRecordName ensures that based on supplied record type, the name content matches
// Currently only validates A and AAAA types
func validateRecordName(t string, value string) error {
switch t {
case "A":
// Must be ipv4 addr
addr := net.ParseIP(value)
if addr == nil || !strings.Contains(value, ".") {
return fmt.Errorf("A record must be a valid IPv4 address, got: %q", value)
}
case "AAAA":
// Must be ipv6 addr
addr := net.ParseIP(value)
if addr == nil || !strings.Contains(value, ":") {
return fmt.Errorf("AAAA record must be a valid IPv6 address, got: %q", value)
}
case "TXT":
// Must be printable ASCII
for i := 0; i < len(value); i++ {
char := value[i]
if (char < 0x20) || (0x7F < char) {
return fmt.Errorf("TXT record must contain printable ASCII, found: %q", char)
}
}
}
return nil
}
func ValidateVLANName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 20 {
errors = append(errors, fmt.Errorf(
"Length provided for '%q' is too long. Maximum length is 20 characters", k))
}
return
}
func ValidateAuthProtocol(v interface{}, k string) (ws []string, errors []error) {
authProtocol := v.(string)
if authProtocol != "MD5" && authProtocol != "SHA1" && authProtocol != "SHA256" {
errors = append(errors, fmt.Errorf(
"%q auth protocol can be MD5 or SHA1 or SHA256", k))
}
return
}
// ValidateIPVersion
func ValidateIPVersion(v interface{}, k string) (ws []string, errors []error) {
validVersions := map[string]bool{
"ipv4": true,
"ipv6": true,
}
value := v.(string)
_, found := validVersions[value]
if !found {
strarray := make([]string, 0, len(validVersions))
for key := range validVersions {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid ip version type %q. Valid types are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func IsSecurityGroupAddress(s string) bool {
return net.ParseIP(s) != nil
}
func IsSecurityGroupCIDR(s string) bool {
_, _, err := net.ParseCIDR(s)
return err == nil
}
func isSecurityGroupIdentityByCRN(s string) bool {
segments := strings.Split(s, ":")
return len(segments) == 10 && segments[0] == "crn"
}
func isSecurityGroupIdentityByHRef(s string) bool {
return validHRef.MatchString(s)
}
func validateGeneration(v interface{}, k string) (ws []string, errors []error) {
validVersions := map[string]bool{
"gc": true,
"gt": true,
}
value := v.(string)
_, found := validVersions[value]
if !found {
strarray := make([]string, 0, len(validVersions))
for key := range validVersions {
strarray = append(strarray, key)
}
errors = append(errors, fmt.Errorf(
"%q contains an invalid generation type %q. Valid types are %q.",
k, value, strings.Join(strarray, ",")))
}
return
}
func ValidateEncyptionProtocol(v interface{}, k string) (ws []string, errors []error) {
encyptionProtocol := v.(string)
if encyptionProtocol != "DES" && encyptionProtocol != "3DES" && encyptionProtocol != "AES128" && encyptionProtocol != "AES192" && encyptionProtocol != "AES256" {
errors = append(errors, fmt.Errorf(
"%q encryption protocol can be DES or 3DES or AES128 or AES192 or AES256", k))
}
return
}
func validateDeadPeerDetectionInterval(v interface{}, k string) (ws []string, errors []error) {
secs := v.(int)
if secs < 15 || secs > 86399 {
errors = append(errors, fmt.Errorf(
"%q must be between 15 and 86399",
k))
return
}
return
}
func ValidateDiffieHellmanGroup(v interface{}, k string) (ws []string, errors []error) {
diffieHellmanGroup := v.(int)
if diffieHellmanGroup != 0 && diffieHellmanGroup != 1 && diffieHellmanGroup != 2 && diffieHellmanGroup != 5 {
errors = append(errors, fmt.Errorf(
"%q Diffie Hellman Group can be 0 or 1 or 2 or 5", k))
}
return
}
func ValidateAllowedRangeInt(start, end int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < start || value > end {
errors = append(errors, fmt.Errorf(
"%q must contain a valid int value should be in range(%d, %d), got %d",
k, start, end, value))
}
return
}
}
func ValidateAllowedPolicyType(policyType []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
var setTrue bool
for _, s := range policyType {
if value == s {
setTrue = true
break
}
}
if !setTrue {
errors = append(errors, fmt.Errorf(
"%q must contain a valid policy type, the value should be exactly either of (%s), got %s",
k, policyType, value))
}
return
}
}
func validateDeadPeerDetectionTimeout(v interface{}, k string) (ws []string, errors []error) {
secs := v.(int)
if secs < 15 || secs > 86399 {
errors = append(errors, fmt.Errorf(
"%q must be between 15 and 86399",
k))
return
}
return
}
func Validatekeylife(v interface{}, k string) (ws []string, errors []error) {
keylife := v.(int)
if keylife < 120 || keylife > 172800 {
errors = append(errors, fmt.Errorf(
"%q keylife value can be between 120 and 172800", k))