forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setting.go
70 lines (60 loc) · 2.04 KB
/
setting.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
package setting
import (
"fmt"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/slice"
"github.com/rancher/rancher/pkg/auth/providerrefresh"
v3client "github.com/rancher/types/client/management/v3"
)
var readOnlySettings = []string{
"cacerts",
}
func PipelineFormatter(apiContext *types.APIContext, resource *types.RawResource) {
v, ok := resource.Values["value"]
if !ok || v == "" {
resource.Values["value"] = resource.Values["default"]
resource.Values["customized"] = false
} else {
resource.Values["customized"] = true
}
}
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
if convert.ToString(resource.Values["source"]) == "env" {
delete(resource.Links, "update")
} else if slice.ContainsString(readOnlySettings, resource.ID) {
delete(resource.Links, "update")
}
}
func Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
var setting v3client.Setting
if err := access.ByID(request, request.Version, v3client.SettingType, request.ID, &setting); err != nil {
return err
}
if setting.Source == "env" {
return httperror.NewAPIError(httperror.MethodNotAllowed, fmt.Sprintf("%s is readOnly because its value is from environment variable", request.ID))
} else if slice.ContainsString(readOnlySettings, setting.ID) {
return httperror.NewAPIError(httperror.MethodNotAllowed, fmt.Sprintf("%s is readOnly", request.ID))
}
newValue, ok := data["value"]
if !ok {
return fmt.Errorf("value not found")
}
newValueString, ok := newValue.(string)
if !ok {
return fmt.Errorf("value not string")
}
var err error
switch request.ID {
case "auth-user-info-max-age-seconds":
_, err = providerrefresh.ParseMaxAge(newValueString)
case "auth-user-info-resync-cron":
_, err = providerrefresh.ParseCron(newValueString)
}
if err != nil {
return httperror.NewAPIError(httperror.InvalidBodyContent, fmt.Sprintf("%v", err))
}
return nil
}