This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
helper.go
53 lines (50 loc) · 2.69 KB
/
helper.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
package common
import (
"fmt"
"strings"
validator "gopkg.in/go-playground/validator.v9"
)
// HandleValidationErrors is the helper function to catch validator.ValidationError
// based on Namespace of the error, and return customized error message.
func HandleValidationErrors(e validator.ValidationErrors) error {
err := e[0]
ns := err.Namespace()
switch ns {
case "Properties.OrchestratorProfile", "Properties.OrchestratorProfile.OrchestratorType",
"Properties.MasterProfile", "Properties.MasterProfile.DNSPrefix", "Properties.MasterProfile.VMSize",
"Properties.LinuxProfile", "Properties.ServicePrincipalProfile.ClientID",
"Properties.WindowsProfile.AdminUsername",
"Properties.WindowsProfile.AdminPassword":
return fmt.Errorf("missing %s", ns)
case "Properties.MasterProfile.Count":
return fmt.Errorf("MasterProfile count needs to be 1, 3, or 5")
case "Properties.MasterProfile.OSDiskSizeGB":
return fmt.Errorf("Invalid os disk size of %d specified. The range of valid values are [%d, %d]", err.Value().(int), MinDiskSizeGB, MaxDiskSizeGB)
case "Properties.MasterProfile.IPAddressCount":
return fmt.Errorf("MasterProfile.IPAddressCount needs to be in the range [%d,%d]", MinIPAddressCount, MaxIPAddressCount)
case "Properties.MasterProfile.StorageProfile":
return fmt.Errorf("Unknown storageProfile '%s'. Specify either %s or %s", err.Value().(string), StorageAccount, ManagedDisks)
default:
if strings.HasPrefix(ns, "Properties.AgentPoolProfiles") {
switch {
case strings.HasSuffix(ns, ".Name") || strings.HasSuffix(ns, "VMSize"):
return fmt.Errorf("missing %s", ns)
case strings.HasSuffix(ns, ".Count"):
return fmt.Errorf("AgentPoolProfile count needs to be in the range [%d,%d]", MinAgentCount, MaxAgentCount)
case strings.HasSuffix(ns, ".OSDiskSizeGB"):
return fmt.Errorf("Invalid os disk size of %d specified. The range of valid values are [%d, %d]", err.Value().(int), MinDiskSizeGB, MaxDiskSizeGB)
case strings.Contains(ns, ".Ports"):
return fmt.Errorf("AgentPoolProfile Ports must be in the range[%d, %d]", MinPort, MaxPort)
case strings.HasSuffix(ns, ".StorageProfile"):
return fmt.Errorf("Unknown storageProfile '%s'. Specify either %s or %s", err.Value().(string), StorageAccount, ManagedDisks)
case strings.Contains(ns, ".DiskSizesGB"):
return fmt.Errorf("A maximum of %d disks may be specified, The range of valid disk size values are [%d, %d]", MaxDisks, MinDiskSizeGB, MaxDiskSizeGB)
case strings.HasSuffix(ns, ".IPAddressCount"):
return fmt.Errorf("AgentPoolProfile.IPAddressCount needs to be in the range [%d,%d]", MinIPAddressCount, MaxIPAddressCount)
default:
break
}
}
}
return fmt.Errorf("Namespace %s is not caught, %+v", ns, e)
}