forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
54 lines (47 loc) · 1.43 KB
/
store.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
package setting
import (
"fmt"
"os"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/slice"
"github.com/rancher/rancher/pkg/api/customization/setting"
"github.com/rancher/rancher/pkg/settings"
)
type Store struct {
types.Store
}
func New(store types.Store) types.Store {
return &Store{
&transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
v, ok := data["value"]
id := convert.ToString(data["id"])
value, ok2 := os.LookupEnv(settings.GetEnvKey(id))
switch {
case ok2:
data["value"] = value
data["customized"] = false
data["source"] = "env"
case !ok || v == "":
data["value"] = data["default"]
data["customized"] = false
data["source"] = "default"
default:
data["customized"] = true
data["source"] = "db"
}
return data, nil
},
},
}
}
func (s *Store) Delete(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
if slice.ContainsString(setting.ReadOnlySettings, id) {
return nil, httperror.NewAPIError(httperror.MethodNotAllowed, fmt.Sprintf("Cannot delete readOnly setting %s.", id))
}
return s.Store.Delete(apiContext, schema, id)
}