-
Notifications
You must be signed in to change notification settings - Fork 20
/
instance_snapshot_list.go
111 lines (87 loc) · 2.94 KB
/
instance_snapshot_list.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
package cmd
import (
"fmt"
"os"
"strings"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
type instanceSnapshotListItemOutput struct {
ID string `json:"id"`
CreationDate string `json:"creation_date"`
Instance string `json:"instance"`
State string `json:"state"`
Zone string `json:"zone"`
}
type instanceSnapshotListOutput []instanceSnapshotListItemOutput
func (o *instanceSnapshotListOutput) toJSON() { outputJSON(o) }
func (o *instanceSnapshotListOutput) toText() { outputText(o) }
func (o *instanceSnapshotListOutput) toTable() { outputTable(o) }
type instanceSnapshotListCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"list"`
Zone string `cli-short:"z" cli-usage:"zone to filter results to"`
}
func (c *instanceSnapshotListCmd) cmdAliases() []string { return nil }
func (c *instanceSnapshotListCmd) cmdShort() string { return "List Compute instance snapshots" }
func (c *instanceSnapshotListCmd) cmdLong() string {
return fmt.Sprintf(`This command lists existing Compute instance snapshots.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&instanceSnapshotListOutput{}), ", "))
}
func (c *instanceSnapshotListCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *instanceSnapshotListCmd) cmdRun(_ *cobra.Command, _ []string) error {
var zones []string
if c.Zone != "" {
zones = []string{c.Zone}
} else {
zones = allZones
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, zones[0]))
out := make(instanceSnapshotListOutput, 0)
res := make(chan instanceSnapshotListItemOutput)
defer close(res)
instances := make(map[string]*egoscale.Instance) // For caching
go func() {
for dt := range res {
out = append(out, dt)
}
}()
err := forEachZone(zones, func(zone string) error {
list, err := cs.ListSnapshots(ctx, zone)
if err != nil {
return fmt.Errorf("unable to list Compute instance snapshots in zone %s: %v", zone, err)
}
for _, s := range list {
instance, cached := instances[*s.InstanceID]
if !cached {
instance, err = cs.GetInstance(ctx, zone, *s.InstanceID)
if err != nil {
return fmt.Errorf("unable to retrieve Compute instance %q: %s", *s.InstanceID, err)
}
instances[*s.InstanceID] = instance
}
res <- instanceSnapshotListItemOutput{
ID: *s.ID,
CreationDate: s.CreatedAt.String(),
Instance: *instance.Name,
State: *s.State,
Zone: zone,
}
}
return nil
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr,
"warning: errors during listing, results might be incomplete.\n%s\n", err) // nolint:golint
}
return output(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(computeInstanceSnapshotCmd, &instanceSnapshotListCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}