-
Notifications
You must be signed in to change notification settings - Fork 20
/
snapshot_show.go
68 lines (58 loc) · 1.83 KB
/
snapshot_show.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
package cmd
import (
"fmt"
"strings"
humanize "github.com/dustin/go-humanize"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type snapshotShowOutput struct {
ID string `json:"id"`
Date string `json:"date"`
InstanceID string `json:"instance_id"`
InstanceName string `json:"instance_name"`
State string `json:"state"`
Size string `json:"size"`
TemplateID string `json:"template_id"`
TemplateName string `json:"template_name"`
}
func (o *snapshotShowOutput) Type() string { return "Snapshot" }
func (o *snapshotShowOutput) toJSON() { outputJSON(o) }
func (o *snapshotShowOutput) toText() { outputText(o) }
func (o *snapshotShowOutput) toTable() { outputTable(o) }
func init() {
snapshotCmd.AddCommand(&cobra.Command{
Use: "show ID",
Short: "Show a snapshot details",
Long: fmt.Sprintf(`This command shows a snapshot details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&snapshotShowOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
snapshot, err := getSnapshotByNameOrID(args[0])
if err != nil {
return err
}
return output(showSnapshot(snapshot))
},
})
}
func showSnapshot(snapshot *egoscale.Snapshot) (outputter, error) {
resp, err := cs.GetWithContext(gContext, &egoscale.Volume{ID: snapshot.VolumeID})
if err != nil {
return nil, err
}
volume := resp.(*egoscale.Volume)
return &snapshotShowOutput{
ID: snapshot.ID.String(),
InstanceID: volume.VirtualMachineID.String(),
InstanceName: volume.VMName,
Date: snapshot.Created,
State: snapshot.State,
Size: humanize.IBytes(uint64(snapshot.Size)),
TemplateID: volume.TemplateID.String(),
TemplateName: volume.TemplateName,
}, nil
}