-
Notifications
You must be signed in to change notification settings - Fork 6
/
get_object.go
95 lines (89 loc) · 2.89 KB
/
get_object.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
package daemonapi
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/opensvc/om3/core/instance"
"github.com/opensvc/om3/core/naming"
"github.com/opensvc/om3/core/object"
"github.com/opensvc/om3/daemon/api"
)
func (a *DaemonAPI) GetObjects(ctx echo.Context, params api.GetObjectsParams) error {
logName := "GetObjects"
log := LogHandler(ctx, logName)
if l, err := a.getObjects(ctx, params.Path); err != nil {
log.Errorf("%s: %s", logName, err)
return JSONProblem(ctx, http.StatusInternalServerError, "Server error", "expand selection")
} else {
return ctx.JSON(http.StatusOK, api.ObjectList{Kind: "ObjectList", Items: l})
}
}
func (a *DaemonAPI) GetObject(ctx echo.Context, namespace string, kind naming.Kind, name string) error {
logName := "GetObject"
log := LogHandler(ctx, logName)
p, err := naming.NewPath(namespace, kind, name)
if err != nil {
return err
}
s := p.FQN()
if l, err := a.getObjects(ctx, &s); err != nil {
log.Errorf("%s: %s", logName, err)
return JSONProblem(ctx, http.StatusInternalServerError, "Server error", "expand selection")
} else if len(l) == 0 {
return JSONProblem(ctx, http.StatusNotFound, "", "")
} else {
return ctx.JSON(http.StatusOK, l[0])
}
}
func (a *DaemonAPI) getObjects(ctx echo.Context, pathSelector *string) (api.ObjectItems, error) {
meta := Meta{
Context: ctx,
Path: pathSelector,
}
if err := meta.Expand(); err != nil {
return nil, err
}
l := make(api.ObjectItems, 0)
for _, p := range meta.Paths() {
ostat := object.StatusData.Get(p)
if ostat == nil {
continue
}
d := api.ObjectItem{
Kind: "ObjectItem",
Meta: api.ObjectMeta{
Object: p.String(),
},
Data: api.ObjectData{
Avail: api.Status(ostat.Avail.String()),
FlexMax: ostat.FlexMax,
FlexMin: ostat.FlexMin,
FlexTarget: ostat.FlexTarget,
Frozen: ostat.Frozen,
Instances: make(map[string]api.Instance),
Orchestrate: api.Orchestrate(ostat.Orchestrate),
Overall: api.Status(ostat.Overall.String()),
PlacementPolicy: api.PlacementPolicy(ostat.PlacementPolicy.String()),
PlacementState: api.PlacementState(ostat.PlacementState.String()),
Pool: ostat.Pool,
Priority: int(ostat.Priority),
Provisioned: api.Provisioned(ostat.Provisioned.String()),
Scope: append([]string{}, ostat.Scope...),
Size: ostat.Size,
Topology: api.Topology(ostat.Topology.String()),
UpInstancesCount: ostat.UpInstancesCount,
UpdatedAt: ostat.UpdatedAt.String(),
},
}
for nodename, config := range instance.ConfigData.GetByPath(p) {
monitor := instance.MonitorData.Get(p, nodename)
status := instance.StatusData.Get(p, nodename)
d.Data.Instances[nodename] = api.Instance{
Config: config,
Monitor: monitor,
Status: status,
}
}
l = append(l, d)
}
return l, nil
}