-
Notifications
You must be signed in to change notification settings - Fork 6
/
object_eval.go
101 lines (96 loc) · 2.59 KB
/
object_eval.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
package omcmd
import (
"context"
"fmt"
"github.com/opensvc/om3/core/client"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/core/objectaction"
"github.com/opensvc/om3/core/objectselector"
"github.com/opensvc/om3/core/output"
"github.com/opensvc/om3/core/rawconfig"
"github.com/opensvc/om3/daemon/api"
"github.com/opensvc/om3/util/key"
)
type (
CmdObjectEval struct {
OptsGlobal
Keywords []string
Impersonate string
}
)
func (t *CmdObjectEval) Run(selector, kind string) error {
mergedSelector := mergeSelector(selector, t.ObjectSelector, kind, "")
if t.Local {
return t.doObjectAction(mergedSelector)
}
c, err := client.New()
if err != nil {
return err
}
sel := objectselector.New(mergedSelector, objectselector.WithClient(c))
paths, err := sel.Expand()
if err != nil {
return err
}
l := make(api.KeywordItems, 0)
for _, p := range paths {
params := api.GetObjectConfigGetParams{}
params.Kw = &t.Keywords
v := true
params.Evaluate = &v
if t.Impersonate != "" {
params.Impersonate = &t.Impersonate
}
response, err := c.GetObjectConfigGetWithResponse(context.Background(), p.Namespace, p.Kind, p.Name, ¶ms)
if err != nil {
return err
}
switch response.StatusCode() {
case 200:
l = append(l, response.JSON200.Items...)
case 400:
return fmt.Errorf("%s: %s", p, *response.JSON400)
case 401:
return fmt.Errorf("%s: %s", p, *response.JSON401)
case 403:
return fmt.Errorf("%s: %s", p, *response.JSON403)
case 500:
return fmt.Errorf("%s: %s", p, *response.JSON500)
default:
return fmt.Errorf("%s: unexpected response: %s", p, response.Status())
}
}
output.Renderer{
DefaultOutput: "tab=OBJECT:meta.object,NODE:meta.node,KEYWORD:meta.keyword,VALUE:data.value",
Output: t.Output,
Color: t.Color,
Data: api.KeywordList{Items: l, Kind: "KeywordList"},
Colorize: rawconfig.Colorize,
}.Print()
return nil
}
func (t *CmdObjectEval) doObjectAction(mergedSelector string) error {
return objectaction.New(
objectaction.LocalFirst(),
objectaction.WithLocal(t.Local),
objectaction.WithColor(t.Color),
objectaction.WithOutput(t.Output),
objectaction.WithObjectSelector(mergedSelector),
objectaction.WithLocalFunc(func(ctx context.Context, p naming.Path) (interface{}, error) {
c, err := object.NewConfigurer(p)
if err != nil {
return nil, err
}
for _, s := range t.Keywords {
kw := key.Parse(s)
if t.Impersonate != "" {
return c.EvalAs(kw, t.Impersonate)
} else {
return c.Eval(kw)
}
}
return nil, nil
}),
).Do()
}