-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhealth_list.go
More file actions
103 lines (94 loc) · 3.23 KB
/
health_list.go
File metadata and controls
103 lines (94 loc) · 3.23 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
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
package health
import (
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-cli/generated/stackstate_api"
"github.com/stackvista/stackstate-cli/internal/common"
"github.com/stackvista/stackstate-cli/internal/di"
"github.com/stackvista/stackstate-cli/internal/printer"
)
type ListArgs struct {
Urn string
}
func HealthListCommand(cli *di.Deps) *cobra.Command {
args := &ListArgs{}
cmd := &cobra.Command{
Use: "list",
Short: "List health synchronization streams or sub-streams",
Long: "List all health synchronization streams. If a stream URN is provided, lists the sub-streams within that stream instead.",
Example: `# list all health streams
sts health list
# list sub-streams within a specific stream
sts health list --urn urn:health:my-stream`,
RunE: cli.CmdRunEWithApi(RunHealthListCommand(args)),
}
common.AddUrnFlagVar(cmd, &args.Urn, "URN of the health synchronization stream to list sub-streams for")
return cmd
}
func RunHealthListCommand(args *ListArgs) di.CmdWithApiFn {
return func(
cmd *cobra.Command,
cli *di.Deps,
api *stackstate_api.APIClient,
serverInfo *stackstate_api.ServerInfo,
) common.CLIError {
if args.Urn != "" {
subList, resp, err := api.HealthSynchronizationApi.GetHealthSynchronizationSubStreamOverview(cli.Context, args.Urn).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
printSubStreamList(cli, subList)
} else {
streamList, resp, err := api.HealthSynchronizationApi.GetHealthSynchronizationStreamsOverview(cli.Context).Execute()
if err != nil {
return common.NewResponseError(err, resp)
}
printStreamList(cli, streamList)
}
return nil
}
}
func printSubStreamList(cli *di.Deps, subList *stackstate_api.SubStreamList) {
data := make([][]interface{}, 0)
jsonMap := make([]map[string]interface{}, 0)
for _, v := range subList.GetSubStreams() {
data = append(data, []interface{}{v.GetSubStreamId(), v.GetCheckStateCount()})
jsonMap = append(jsonMap, map[string]interface{}{
"sub_stream_id": v.GetSubStreamId(),
"check_state_count": v.GetCheckStateCount(),
})
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"sub-stream": jsonMap,
})
} else {
cli.Printer.Table(printer.TableData{
Header: []string{"Sub stream id", "Check state count"},
Data: data,
MissingTableDataMsg: printer.NotFoundMsg{Types: "health sub-stream"},
})
}
}
func printStreamList(cli *di.Deps, streamList *stackstate_api.StreamList) {
data := make([][]interface{}, 0)
jsonMap := make([]map[string]interface{}, 0)
for _, v := range streamList.GetItems() {
data = append(data, []interface{}{v.GetUrn(), v.GetConsistencyModel(), v.GetSubStreams()})
jsonMap = append(jsonMap, map[string]interface{}{
"stream_urn": v.GetUrn(),
"stream_consistency_model": v.GetConsistencyModel(),
"sub_stream_count": v.GetSubStreams(),
})
}
if cli.IsJson() {
cli.Printer.PrintJson(map[string]interface{}{
"streams": jsonMap,
})
} else {
cli.Printer.Table(printer.TableData{
Header: []string{"Stream urn", "Stream consistency model", "Sub stream count"},
Data: data,
MissingTableDataMsg: printer.NotFoundMsg{Types: "health stream"},
})
}
}