forked from canonical/lxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarning.go
384 lines (305 loc) · 8.98 KB
/
warning.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package main
import (
"fmt"
"sort"
"strings"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
"github.com/lxc/lxd/lxc/utils"
"github.com/lxc/lxd/shared/api"
cli "github.com/lxc/lxd/shared/cmd"
"github.com/lxc/lxd/shared/i18n"
)
type warningColumn struct {
Name string
Data func(api.Warning) string
}
type cmdWarning struct {
global *cmdGlobal
}
func (c *cmdWarning) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("warning")
cmd.Short = i18n.G("Manage warnings")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Manage warnings`))
// List
warningListCmd := cmdWarningList{global: c.global, warning: c}
cmd.AddCommand(warningListCmd.Command())
// Acknowledge
warningAcknowledgeCmd := cmdWarningAcknowledge{global: c.global, warning: c}
cmd.AddCommand(warningAcknowledgeCmd.Command())
// Show
warningShowCmd := cmdWarningShow{global: c.global, warning: c}
cmd.AddCommand(warningShowCmd.Command())
// Delete
warningDeleteCmd := cmdWarningDelete{global: c.global, warning: c}
cmd.AddCommand(warningDeleteCmd.Command())
// Workaround for subcommand usage errors. See: https://github.com/spf13/cobra/issues/706
cmd.Args = cobra.NoArgs
cmd.Run = func(cmd *cobra.Command, args []string) { _ = cmd.Usage() }
return cmd
}
// List
type cmdWarningList struct {
global *cmdGlobal
warning *cmdWarning
flagColumns string
flagFormat string
flagAll bool
}
const defaultWarningColumns = "utSscpLl"
func (c *cmdWarningList) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("list", i18n.G("[<remote>:]"))
cmd.Aliases = []string{"ls"}
cmd.Short = i18n.G("List warnings")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`List warnings
The -c option takes a (optionally comma-separated) list of arguments
that control which warning attributes to output when displaying in table
or csv format.
Default column layout is: utSscpLl
Column shorthand chars:
c - Count
l - Last seen
L - Location
f - First seen
p - Project
s - Severity
S - Status
u - UUID
t - Type`))
cmd.Flags().StringVarP(&c.flagColumns, "columns", "c", defaultWarningColumns, i18n.G("Columns")+"``")
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().BoolVarP(&c.flagAll, "all", "a", false, i18n.G("List all warnings")+"``")
cmd.RunE = c.Run
return cmd
}
func (c *cmdWarningList) Run(cmd *cobra.Command, args []string) error {
// Parse remote
remote := ""
if len(args) > 0 {
remote = args[0]
}
remoteName, _, err := c.global.conf.ParseRemote(remote)
if err != nil {
return err
}
remoteServer, err := c.global.conf.GetInstanceServer(remoteName)
if err != nil {
return err
}
allWarnings, err := remoteServer.GetWarnings()
if err != nil {
return err
}
// Per default, acknowledged and resolved warnings are not shown. Using the --all flag will show
// those as well.
var warnings []api.Warning
if c.flagAll {
warnings = allWarnings
} else {
for _, warning := range allWarnings {
if warning.Status == "acknowledged" || warning.Status == "resolved" {
continue
}
warnings = append(warnings, warning)
}
}
// Process the columns
columns, err := c.parseColumns(remoteServer.IsClustered())
if err != nil {
return err
}
// Render the table
data := [][]string{}
for _, warning := range warnings {
row := []string{}
for _, column := range columns {
row = append(row, column.Data(warning))
}
data = append(data, row)
}
sort.Sort(utils.StringList(data))
rawData := make([]*api.Warning, len(warnings))
for i := range warnings {
rawData[i] = &warnings[i]
}
headers := []string{}
for _, column := range columns {
headers = append(headers, column.Name)
}
return utils.RenderTable(c.flagFormat, headers, data, rawData)
}
func (c *cmdWarningList) countColumnData(warning api.Warning) string {
return fmt.Sprintf("%d", warning.Count)
}
func (c *cmdWarningList) firstSeenColumnData(warning api.Warning) string {
return warning.FirstSeenAt.UTC().Format("Jan 2, 2006 at 3:04pm (MST)")
}
func (c *cmdWarningList) lastSeenColumnData(warning api.Warning) string {
return warning.LastSeenAt.UTC().Format("Jan 2, 2006 at 3:04pm (MST)")
}
func (c *cmdWarningList) locationColumnData(warning api.Warning) string {
return warning.Location
}
func (c *cmdWarningList) projectColumnData(warning api.Warning) string {
return warning.Project
}
func (c *cmdWarningList) severityColumnData(warning api.Warning) string {
return strings.ToUpper(warning.Severity)
}
func (c *cmdWarningList) statusColumnData(warning api.Warning) string {
return strings.ToUpper(warning.Status)
}
func (c *cmdWarningList) typeColumnData(warning api.Warning) string {
return warning.Type
}
func (c *cmdWarningList) uuidColumnData(warning api.Warning) string {
return warning.UUID
}
func (c *cmdWarningList) parseColumns(clustered bool) ([]warningColumn, error) {
columnsShorthandMap := map[rune]warningColumn{
'c': {i18n.G("COUNT"), c.countColumnData},
'f': {i18n.G("FIRST SEEN"), c.firstSeenColumnData},
'l': {i18n.G("LAST SEEN"), c.lastSeenColumnData},
'p': {i18n.G("PROJECT"), c.projectColumnData},
's': {i18n.G("SEVERITY"), c.severityColumnData},
'S': {i18n.G("STATUS"), c.statusColumnData},
't': {i18n.G("TYPE"), c.typeColumnData},
'u': {i18n.G("UUID"), c.uuidColumnData},
}
if clustered {
columnsShorthandMap['L'] = warningColumn{i18n.G("LOCATION"), c.locationColumnData}
} else {
if c.flagColumns != defaultWarningColumns {
if strings.ContainsAny(c.flagColumns, "L") {
return nil, fmt.Errorf(i18n.G("Can't specify column L when not clustered"))
}
}
c.flagColumns = strings.Replace(c.flagColumns, "L", "", -1)
}
columnList := strings.Split(c.flagColumns, ",")
columns := []warningColumn{}
for _, columnEntry := range columnList {
if columnEntry == "" {
return nil, fmt.Errorf(i18n.G("Empty column entry (redundant, leading or trailing command) in '%s'"), c.flagColumns)
}
for _, columnRune := range columnEntry {
if column, ok := columnsShorthandMap[columnRune]; ok {
columns = append(columns, column)
} else {
return nil, fmt.Errorf(i18n.G("Unknown column shorthand char '%c' in '%s'"), columnRune, columnEntry)
}
}
}
return columns, nil
}
// Acknowledge
type cmdWarningAcknowledge struct {
global *cmdGlobal
warning *cmdWarning
}
func (c *cmdWarningAcknowledge) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("acknowledge", i18n.G("[<remote>:]<warning-uuid>"))
cmd.Aliases = []string{"ack"}
cmd.Short = i18n.G("Acknowledge warning")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Acknowledge warning`))
cmd.RunE = c.Run
return cmd
}
func (c *cmdWarningAcknowledge) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}
// Parse remote
remoteName, UUID, err := c.global.conf.ParseRemote(args[0])
if err != nil {
return err
}
remoteServer, err := c.global.conf.GetInstanceServer(remoteName)
if err != nil {
return err
}
warning := api.WarningPut{Status: "acknowledged"}
return remoteServer.UpdateWarning(UUID, warning, "")
}
// Show
type cmdWarningShow struct {
global *cmdGlobal
warning *cmdWarning
}
func (c *cmdWarningShow) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("show", i18n.G("[<remote>:]<warning-uuid>"))
cmd.Short = i18n.G("Show warning")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Show warning`))
cmd.RunE = c.Run
return cmd
}
func (c *cmdWarningShow) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}
// Parse remote
remoteName, UUID, err := c.global.conf.ParseRemote(args[0])
if err != nil {
return err
}
remoteServer, err := c.global.conf.GetInstanceServer(remoteName)
if err != nil {
return err
}
warning, _, err := remoteServer.GetWarning(UUID)
if err != nil {
return err
}
data, err := yaml.Marshal(&warning)
if err != nil {
return err
}
fmt.Printf("%s", data)
return nil
}
// Delete
type cmdWarningDelete struct {
global *cmdGlobal
warning *cmdWarning
flagAll bool
}
func (c *cmdWarningDelete) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = usage("delete", i18n.G("[<remote>:]<warning-uuid>"))
cmd.Aliases = []string{"rm"}
cmd.Short = i18n.G("Delete warning")
cmd.Long = cli.FormatSection(i18n.G("Description"), i18n.G(
`Delete warning`))
cmd.Flags().BoolVarP(&c.flagAll, "all", "a", false, i18n.G("Delete all warnings")+"``")
cmd.RunE = c.Run
return cmd
}
func (c *cmdWarningDelete) Run(cmd *cobra.Command, args []string) error {
// Quick checks.
exit, err := c.global.CheckArgs(cmd, args, 1, 1)
if exit {
return err
}
// Parse remote
remoteName, UUID, err := c.global.conf.ParseRemote(args[0])
if err != nil {
return err
}
remoteServer, err := c.global.conf.GetInstanceServer(remoteName)
if err != nil {
return err
}
return remoteServer.DeleteWarning(UUID)
}