-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
75 lines (64 loc) · 1.67 KB
/
list.go
File metadata and controls
75 lines (64 loc) · 1.67 KB
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
package debugcmd
import (
"context"
"encoding/json"
)
type ListRequest struct {
TwinID uint32 `json:"twin_id"` // optional, if not provided lists for all twins
}
type ListWorkload struct {
Type string `json:"type"`
Name string `json:"name"`
State string `json:"state"`
}
type ListDeployment struct {
TwinID uint32 `json:"twin_id"`
ContractID uint64 `json:"contract_id"`
Workloads []ListWorkload `json:"workloads"`
}
type ListResponse struct {
Deployments []ListDeployment `json:"deployments"`
}
func ParseListRequest(payload []byte) (ListRequest, error) {
if len(payload) == 0 {
return ListRequest{}, nil
}
var req ListRequest
if err := json.Unmarshal(payload, &req); err != nil {
return ListRequest{}, err
}
return req, nil
}
func List(ctx context.Context, deps Deps, req ListRequest) (ListResponse, error) {
twins := []uint32{req.TwinID}
if req.TwinID == 0 {
var err error
twins, err = deps.Storage.GetTwins(ctx)
if err != nil {
return ListResponse{}, err
}
}
deployments := make([]ListDeployment, 0)
for _, twin := range twins {
deploymentList, err := deps.Storage.GetDeployments(ctx, twin)
if err != nil {
return ListResponse{}, err
}
for _, d := range deploymentList {
workloads := make([]ListWorkload, 0, len(d.Workloads))
for _, wl := range d.Workloads {
workloads = append(workloads, ListWorkload{
Type: string(wl.Type),
Name: string(wl.Name),
State: string(wl.Result.State),
})
}
deployments = append(deployments, ListDeployment{
TwinID: d.TwinID,
ContractID: d.ContractID,
Workloads: workloads,
})
}
}
return ListResponse{Deployments: deployments}, nil
}