-
Notifications
You must be signed in to change notification settings - Fork 43
/
secret.go
128 lines (115 loc) · 2.96 KB
/
secret.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
package models
import (
"fmt"
"reflect"
"time"
specV1 "github.com/baetyl/baetyl-go/v2/spec/v1"
"github.com/jinzhu/copier"
)
type SecretList struct {
Total int `json:"total"`
ListOptions *ListOptions `json:"listOptions"`
Items []specV1.Secret `json:"items"`
}
func FromSecret(s *specV1.Secret) *Registry {
if v, ok := s.Labels[specV1.SecretLabel]; !ok || v != specV1.SecretRegistry {
return nil
}
res := &Registry{}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
if v, ok := s.Data["address"]; ok {
res.Address = string(v)
}
if v, ok := s.Data["password"]; ok {
res.Password = string(v)
}
if v, ok := s.Data["username"]; ok {
res.Username = string(v)
}
return res
}
func FromSecretList(s *SecretList) *RegistryList {
res := &RegistryList{
Total: s.Total,
ListOptions: s.ListOptions,
Items: []Registry{},
}
for _, sd := range s.Items {
r := FromSecret(&sd)
if r == nil {
res.Total--
} else {
res.Items = append(res.Items, *r)
}
}
return res
}
type SecretView struct {
Name string `json:"name,omitempty" validate:"omitempty,resourceName"`
Namespace string `json:"namespace,omitempty"`
Data map[string]string `json:"data,omitempty" binding:"required"`
CreationTimestamp time.Time `json:"createTime,omitempty"`
UpdateTimestamp time.Time `json:"updateTime,omitempty"`
Description string `json:"description"`
Version string `json:"version,omitempty"`
}
func (s *SecretView) Equal(target *SecretView) bool {
return reflect.DeepEqual(s.Data, target.Data) &&
reflect.DeepEqual(s.Description, target.Description)
}
type SecretViewList struct {
Total int `json:"total"`
ListOptions *ListOptions `json:"listOptions"`
Items []SecretView `json:"items"`
}
func (s *SecretView) ToSecret() *specV1.Secret {
res := &specV1.Secret{
Labels: map[string]string{
specV1.SecretLabel: specV1.SecretConfig,
},
}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
res.Data = map[string][]byte{}
for k, v := range s.Data {
res.Data[k] = []byte(v)
}
return res
}
func FromSecretToView(s *specV1.Secret) *SecretView {
if v, ok := s.Labels[specV1.SecretLabel]; !ok || v != specV1.SecretConfig {
return nil
}
res := &SecretView{}
err := copier.Copy(res, s)
if err != nil {
panic(fmt.Sprintf("copier exception: %s", err.Error()))
}
res.Data = map[string]string{}
for k, v := range s.Data {
res.Data[k] = string(v)
}
return res
}
func FromSecretListToView(s *SecretList) *SecretViewList {
res := &SecretViewList{
Total: s.Total,
ListOptions: s.ListOptions,
Items: []SecretView{},
}
for _, sd := range s.Items {
r := FromSecretToView(&sd)
if r == nil {
res.Total--
} else {
r.Data = nil
res.Items = append(res.Items, *r)
}
}
return res
}