-
Notifications
You must be signed in to change notification settings - Fork 19
/
sync_desire.go
87 lines (75 loc) · 2.14 KB
/
sync_desire.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
package v1
import (
"encoding/json"
"github.com/baetyl/baetyl-go/v2/errors"
)
// DesireRequest body of request to sync desired data
type DesireRequest struct {
Infos []ResourceInfo `yaml:"infos" json:"infos"`
}
// DesireResponse body of response to sync desired data
type DesireResponse struct {
Values []ResourceValue `yaml:"values" json:"values"`
}
// ResourceInfo desired info
type ResourceInfo struct {
Kind Kind `yaml:"kind,omitempty" json:"kind,omitempty"`
Name string `yaml:"name,omitempty" json:"name,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
}
// ResourceValue desired value
type ResourceValue struct {
ResourceInfo `yaml:",inline" json:",inline"`
Value VariableValue `yaml:"value,omitempty" json:"value,omitempty"`
}
// App return app data if its kind is app
func (v *ResourceValue) App() *Application {
if v.Kind == KindApplication || v.Kind == KindApp {
if v.Value.Value == nil {
v.Value.Value = &Application{}
json.Unmarshal(v.Value.Data, v.Value.Value)
}
return v.Value.Value.(*Application)
}
return nil
}
// Config return config data if its kind is config
func (v *ResourceValue) Config() *Configuration {
if v.Kind == KindConfiguration || v.Kind == KindConfig {
if v.Value.Value == nil {
v.Value.Value = &Configuration{}
json.Unmarshal(v.Value.Data, v.Value.Value)
}
return v.Value.Value.(*Configuration)
}
return nil
}
// Secret return secret data if its kind is secret
func (v *ResourceValue) Secret() *Secret {
if v.Kind == KindSecret {
if v.Value.Value == nil {
v.Value.Value = &Secret{}
json.Unmarshal(v.Value.Data, v.Value.Value)
}
return v.Value.Value.(*Secret)
}
return nil
}
// VariableValue variable value which can be app, config or secret
type VariableValue struct {
Data []byte
Value interface{}
}
// UnmarshalJSON unmarshal from json data
func (v *VariableValue) UnmarshalJSON(b []byte) error {
v.Data = b
return nil
}
// MarshalJSON marshal to json data
func (v *VariableValue) MarshalJSON() ([]byte, error) {
var err error
if v.Data == nil && v.Value != nil {
v.Data, err = json.Marshal(v.Value)
}
return v.Data, errors.Trace(err)
}