This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
validation.go
79 lines (62 loc) · 2.01 KB
/
validation.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
package aci
import (
"fmt"
"github.com/docker/distribution/reference"
"github.com/go-playground/validator"
)
func validateLocationAvailable(location string, validLocations []string) error {
for _, loc := range validLocations {
if loc == location {
return nil
}
}
return fmt.Errorf("Location %s, is not a valid location for this subscription. Valid locations are: %s", location, validLocations)
}
var errInvalidMemoryValue = fmt.Errorf("Memory allocated to a Cloud run instance must a minimum of 512MB and less than 16384MB (16GB)\n")
var errInvalidCPUCount = fmt.Errorf("Invalid value for CPUCount, it is currently only possible to specify '1-4' CPUs\n")
var errInvalidVolume = fmt.Errorf("Container instance volumes must have one of 'azure_file_share' or 'git_repo' fields set\n")
func validateConfig(c Config) error {
v := validator.New()
v.RegisterStructValidation(validationVolumeStruct, Volume{})
err := v.Struct(c)
if err != nil {
errorMessage := ""
for _, err := range err.(validator.ValidationErrors) {
switch err.Namespace() {
case "Config.Capacity.Memory":
errorMessage += errInvalidMemoryValue.Error()
case "Config.Capacity.CPUCount":
errorMessage += errInvalidCPUCount.Error()
}
if err.Tag() == "one_of" {
errorMessage += errInvalidVolume.Error()
}
}
return fmt.Errorf(errorMessage)
}
return nil
}
func validationVolumeStruct(sl validator.StructLevel) {
vol := sl.Current().Interface().(Volume)
// Error if both set
if vol.AzureFileShare != nil && vol.GitRepoVolume != nil {
sl.ReportError(vol, "volume", "Volume", "one_of", "")
}
// Error if none
if vol.AzureFileShare == nil && vol.GitRepoVolume == nil {
sl.ReportError(vol, "volume", "Volume", "one_of", "")
}
}
// return the server component from an image name
func parseDockerServer(image string) string {
n, err := reference.ParseNamed(image)
if err != nil {
return "docker.io"
}
d := reference.Domain(n)
if d == "" {
// no domain, convention is main docker repo
d = "docker.io"
}
return d
}