-
Notifications
You must be signed in to change notification settings - Fork 18
/
handler.go
280 lines (253 loc) · 7.51 KB
/
handler.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package spiff
import (
"encoding/json"
"strconv"
"github.com/mandelsoft/spiff/dynaml"
"github.com/mandelsoft/spiff/features"
"github.com/mandelsoft/spiff/spiffing"
"github.com/mandelsoft/spiff/yaml"
"github.com/sirupsen/logrus"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/compdesc"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/transfer/transferhandler"
"github.com/open-component-model/ocm/v2/pkg/contexts/ocm/transfer/transferhandler/standard"
"github.com/open-component-model/ocm/v2/pkg/errors"
"github.com/open-component-model/ocm/v2/pkg/runtime"
)
type Handler struct {
standard.Handler
opts *Options
spiff spiffing.Spiff
}
func New(opts ...transferhandler.TransferOption) (transferhandler.TransferHandler, error) {
options := &Options{}
err := transferhandler.ApplyOptions(options, opts...)
if err != nil {
return nil, err
}
spiff := spiffing.New().WithFeatures(features.CONTROL, features.INTERPOLATION)
if options.GetScriptFilesystem() != nil {
spiff = spiff.WithFileSystem(options.fs)
}
return &Handler{
Handler: *standard.NewDefaultHandler(&options.Options),
opts: options,
spiff: spiff,
}, nil
}
func (h *Handler) OverwriteVersion(src ocm.ComponentVersionAccess, tgt ocm.ComponentVersionAccess) (bool, error) {
return h.Handler.OverwriteVersion(src, tgt)
}
func (h *Handler) TransferVersion(repo ocm.Repository, src ocm.ComponentVersionAccess, meta *compdesc.ComponentReference, tgt ocm.Repository) (ocm.ComponentVersionAccess, transferhandler.TransferHandler, error) {
if src == nil || h.opts.IsRecursive() {
if h.opts.GetScript() == nil {
return h.Handler.TransferVersion(repo, src, meta, tgt)
}
binding := h.getBinding(src, nil, &meta.ElementMeta, nil, tgt)
result, r, s, err := h.EvalRecursion("componentversion", binding, "process")
if err != nil {
return nil, nil, err
}
if result {
if r != nil {
repo, err = repo.GetContext().RepositoryForConfig(r, runtime.DefaultJSONEncoding)
if err != nil {
return nil, nil, err
}
}
if s == nil {
return h.Handler.TransferVersion(repo, src, meta, tgt)
}
opts := *h.opts
opts.script = s
cv, _, err := h.Handler.TransferVersion(repo, src, meta, tgt)
return cv, &Handler{
Handler: h.Handler,
opts: &opts,
}, err
}
}
return nil, nil, nil
}
func (h *Handler) TransferResource(src ocm.ComponentVersionAccess, a ocm.AccessSpec, r ocm.ResourceAccess) (bool, error) {
if !h.opts.IsResourcesByValue() {
return false, nil
}
if h.opts.GetScript() == nil {
return true, nil
}
binding := h.getBinding(src, a, &r.Meta().ElementMeta, &r.Meta().Type, nil)
return h.EvalBool("resource", binding, "process")
}
func (h *Handler) TransferSource(src ocm.ComponentVersionAccess, a ocm.AccessSpec, r ocm.SourceAccess) (bool, error) {
if !h.opts.IsSourcesByValue() {
return false, nil
}
if h.opts.GetScript() == nil {
return true, nil
}
binding := h.getBinding(src, a, &r.Meta().ElementMeta, &r.Meta().Type, nil)
return h.EvalBool("source", binding, "process")
}
func (h *Handler) getBinding(src ocm.ComponentVersionAccess, a ocm.AccessSpec, m *compdesc.ElementMeta, typ *string, tgt ocm.Repository) map[string]interface{} {
binding := map[string]interface{}{}
if src != nil {
binding["component"] = getCVAttrs(src)
}
if a != nil {
binding["access"] = getData(a)
}
binding["element"] = getData(m)
if typ != nil {
binding["element"].(map[string]interface{})["type"] = *typ
}
if tgt != nil {
binding["target"] = getData(tgt.GetSpecification())
}
return binding
}
func getData(in interface{}) interface{} {
var v interface{}
d, err := json.Marshal(in)
if err != nil {
logrus.Error(err)
}
if err := json.Unmarshal(d, &v); err != nil {
logrus.Error(err)
}
return v
}
func getCVAttrs(cv ocm.ComponentVersionAccess) map[string]interface{} {
provider := map[string]interface{}{}
data, err := json.Marshal(cv.GetDescriptor().Provider)
if err != nil {
logrus.Error(err)
}
json.Unmarshal(data, &provider)
labels := cv.GetDescriptor().Labels.AsMap()
values := map[string]interface{}{}
values["name"] = cv.GetName()
values["version"] = cv.GetVersion()
values["provider"] = provider
values["labels"] = labels
return values
}
func (h *Handler) Eval(binding map[string]interface{}) (spiffing.Node, error) {
spiff, err := h.spiff.WithValues(binding)
if err != nil {
return nil, err
}
node, err := spiff.Unmarshal("script", h.opts.GetScript())
if err != nil {
return nil, err
}
return spiff.Cascade(node, nil)
}
func (h *Handler) EvalBool(mode string, binding map[string]interface{}, key string) (bool, error) {
binding = map[string]interface{}{
"mode": mode,
"values": binding,
}
r, err := h.Eval(binding)
if err != nil {
return false, err
}
return h.evalBool(r, key)
}
func (h *Handler) EvalRecursion(mode string, binding map[string]interface{}, key string) (bool, []byte, []byte, error) {
binding = map[string]interface{}{
"mode": mode,
"values": binding,
}
r, err := h.Eval(binding)
if err != nil {
return false, nil, nil, err
}
valueMap, ok := r.Value().(map[string]spiffing.Node)
if !ok {
return false, nil, nil, errors.ErrUnknown("transfer script field", key)
}
r = valueMap[key]
if r == nil {
return false, nil, nil, errors.ErrUnknown("transfer script field", key)
}
b, err := h.evalBoolValue(r)
if err == nil {
// flat boolean without result structure
return b, nil, nil, nil
}
valueMap, ok = r.Value().(map[string]spiffing.Node)
if !ok {
return false, nil, nil, errors.ErrInvalid("transfer script result field type", dynaml.ExpressionType(r))
}
// now we expect a result structure
// process: bool
// repospec: map
// script: template
b, err = h.evalBool(r, key)
if err != nil || !b {
return false, nil, nil, err
}
var script []byte
v := valueMap["script"]
if v != nil && v.Value() != nil {
if t, ok := v.Value().(dynaml.TemplateValue); ok {
if m, ok := t.Orig.Value().(map[string]spiffing.Node); ok {
delete(m, "<<")
delete(m, "<<<")
} else {
return false, nil, nil, errors.ErrInvalid("script template type", dynaml.ExpressionType(t.Orig))
}
script, err = yaml.Marshal(t.Orig)
if err != nil {
return false, nil, nil, err
}
} else {
return false, nil, nil, errors.ErrInvalid("script type", dynaml.ExpressionType(v))
}
}
var repospec []byte
v = valueMap["repospec"]
if v != nil && v.Value() != nil {
if _, ok := v.Value().(map[string]spiffing.Node); ok {
spec, err := yaml.Normalize(v)
if err == nil {
repospec, err = json.Marshal(spec)
}
if err != nil {
return false, nil, nil, errors.Wrapf(err, "invalid field repospec")
}
} else {
return false, nil, nil, errors.ErrInvalid("repospec type", dynaml.ExpressionType(v))
}
}
return true, repospec, script, nil
}
func (h *Handler) evalBool(r spiffing.Node, key string) (bool, error) {
v := r.Value().(map[string]spiffing.Node)[key]
if v == nil {
return false, errors.ErrUnknown("field", key)
}
return h.evalBoolValue(v)
}
func (h *Handler) evalBoolValue(v spiffing.Node) (bool, error) {
switch b := v.Value().(type) {
case bool:
return b, nil
case int64:
return b != 0, nil
case float64:
return b != 0, nil
case string:
r, err := strconv.ParseBool(b)
if err != nil {
return len(b) > 0, nil
}
return r, nil
default:
return false, errors.ErrInvalid("boolean result", dynaml.ExpressionType(v))
}
}