forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
66 lines (52 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
55
56
57
58
59
60
61
62
63
64
65
66
package cert
import (
"time"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/cert"
"github.com/rancher/types/client/project/v3"
)
func Wrap(store types.Store) types.Store {
return &Store{
Store: store,
}
}
type Store struct {
types.Store
}
func (s *Store) Create(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}) (map[string]interface{}, error) {
if err := AddCertInfo(data); err != nil {
return nil, err
}
return s.Store.Create(apiContext, schema, data)
}
func (s *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) {
if err := AddCertInfo(data); err != nil {
return nil, err
}
return s.Store.Update(apiContext, schema, data, id)
}
func AddCertInfo(data map[string]interface{}) error {
certs, _ := data[client.CertificateFieldCerts].(string)
key, _ := data[client.CertificateFieldKey].(string)
if certs == "" || key == "" {
return nil
}
certInfo, err := cert.Info(certs, key)
if err != nil {
return httperror.NewFieldAPIError(httperror.InvalidBodyContent, "certs", err.Error())
}
certData, err := convert.EncodeToMap(certInfo)
if err != nil {
return err
}
for k, v := range certData {
if t, ok := v.(time.Time); ok {
data[k] = convert.ToString(t)
} else {
data[k] = v
}
}
return nil
}