forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
40 lines (36 loc) · 1.04 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
package nodetemplate
import (
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
"github.com/rancher/rancher/pkg/configfield"
)
const (
Amazonec2driver = "amazonec2"
Azuredriver = "azure"
Vmwaredriver = "vmwarevsphere"
DigitalOceandriver = "digitalocean"
)
var drivers = map[string]bool{
Amazonec2driver: true,
Azuredriver: true,
DigitalOceandriver: true,
Vmwaredriver: true,
}
func Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
driver := configfield.GetDriver(data)
if driver == "" {
return httperror.NewAPIError(httperror.MissingRequired, "a Config field must be set")
}
credID := convert.ToString(values.GetValueN(data, "cloudCredentialId"))
if credID == "" {
if _, ok := drivers[driver]; ok {
return httperror.NewAPIError(httperror.MissingRequired, "Cloud Credential must be set")
}
}
if data != nil {
data["driver"] = driver
}
return nil
}