/
validator.go
149 lines (120 loc) Β· 3.34 KB
/
validator.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
package setupuser
import (
"context"
"fmt"
"os"
"strings"
"gopkg.in/yaml.v2"
)
const (
DefaultUsername = "eksa"
DefaultGroup = "EKSAUsers"
DefaultGlobalRole = "EKSAGlobalRole"
DefaultUserRole = "EKSAUserRole"
DefaultAdminRole = "EKSACloudAdminRole"
)
type Connection struct {
Server string `yaml:"server"`
Insecure bool `yaml:"insecure"`
}
type Objects struct {
Networks []string `yaml:"networks"`
Datastores []string `yaml:"datastores"`
ResourcePools []string `yaml:"resourcePools"`
Folders []string `yaml:"folders"`
Templates []string `yaml:"templates"`
}
type VSphereUserSpec struct {
Datacenter string `yaml:"datacenter"`
VSphereDomain string `yaml:"vSphereDomain"`
Connection Connection `yaml:"connection"`
Objects Objects `yaml:"objects"`
// Below are optional fields with defaults
Username string `yaml:"username"`
GroupName string `yaml:"group"`
GlobalRole string `yaml:"globalRole"`
UserRole string `yaml:"userRole"`
AdminRole string `yaml:"adminRole"`
}
type VSphereSetupUserConfig struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Spec VSphereUserSpec `yaml:"spec"`
}
func GenerateConfig(ctx context.Context, filepath string) (*VSphereSetupUserConfig, error) {
c, err := readConfig(ctx, filepath)
if err != nil {
return nil, err
}
err = validate(c)
if err != nil {
return nil, err
}
setDefaults(c)
return c, nil
}
func readConfig(ctx context.Context, filepath string) (*VSphereSetupUserConfig, error) {
file, err := os.ReadFile(filepath)
if err != nil {
return nil, fmt.Errorf("failed to read file %s, err = %v", filepath, err)
}
c := VSphereSetupUserConfig{}
if err = yaml.Unmarshal(file, &c); err != nil {
return nil, fmt.Errorf("failed to parse %s, err = %v", filepath, err)
}
return &c, nil
}
func validate(c *VSphereSetupUserConfig) error {
errs := []string{}
if c.Spec.Datacenter == "" {
errs = append(errs, "datacenter cannot be empty")
}
if c.Spec.VSphereDomain == "" {
errs = append(errs, "vSphereDomain cannot be empty")
}
if c.Spec.Connection.Server == "" {
errs = append(errs, "server cannot be empty")
}
if len(errs) > 0 {
return fmt.Errorf("validations failed: %s", strings.Join(errs[:], ","))
}
return nil
}
func setDefaults(c *VSphereSetupUserConfig) {
if c.Spec.GlobalRole == "" {
c.Spec.GlobalRole = DefaultGlobalRole
}
if c.Spec.UserRole == "" {
c.Spec.UserRole = DefaultUserRole
}
if c.Spec.AdminRole == "" {
c.Spec.AdminRole = DefaultAdminRole
}
if c.Spec.GroupName == "" {
c.Spec.GroupName = DefaultGroup
}
if c.Spec.Username == "" {
c.Spec.Username = DefaultUsername
}
}
// ValidateVSphereObjects validates objects do not exist before configuring user.
func ValidateVSphereObjects(ctx context.Context, c *VSphereSetupUserConfig, govc GovcClient) error {
exists, err := govc.GroupExists(ctx, c.Spec.GroupName)
if err != nil {
return err
}
if exists {
return fmt.Errorf("group %s already exists, please use force=true to ignore", c.Spec.GroupName)
}
roles := []string{c.Spec.GlobalRole, c.Spec.UserRole, c.Spec.AdminRole}
for _, r := range roles {
exists, err := govc.RoleExists(ctx, r)
if err != nil {
return err
}
if exists {
return fmt.Errorf("role %s already exists, please use force=true to ignore", r)
}
}
return nil
}