-
Notifications
You must be signed in to change notification settings - Fork 20
/
affinitygroup_show.go
68 lines (56 loc) · 1.7 KB
/
affinitygroup_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"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type affinityGroupShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Instances []string `json:"instances"`
}
func (o *affinityGroupShowOutput) Type() string { return "Anti-Affinity Group" }
func (o *affinityGroupShowOutput) toJSON() { outputJSON(o) }
func (o *affinityGroupShowOutput) toText() { outputText(o) }
func (o *affinityGroupShowOutput) toTable() { outputTable(o) }
func init() {
affinitygroupCmd.AddCommand(&cobra.Command{
Use: "show NAME|ID",
Short: "Show affinity group details",
Long: fmt.Sprintf(`This command shows an Anti-Affinity Group details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&affinityGroupShowOutput{}), ", ")),
Aliases: gShowAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
ag, err := getAntiAffinityGroupByNameOrID(args[0])
if err != nil {
return err
}
return output(showAffinityGroup(ag))
},
})
}
func showAffinityGroup(ag *egoscale.AffinityGroup) (outputter, error) {
out := affinityGroupShowOutput{
ID: ag.ID.String(),
Name: ag.Name,
Description: ag.Description,
Instances: make([]string, len(ag.VirtualMachineIDs)),
}
if len(ag.VirtualMachineIDs) > 0 {
resp, err := cs.ListWithContext(gContext, &egoscale.ListVirtualMachines{IDs: ag.VirtualMachineIDs})
if err != nil {
return nil, err
}
for i, r := range resp {
vm := r.(*egoscale.VirtualMachine)
out.Instances[i] = vm.Name
}
}
return &out, nil
}