-
Notifications
You must be signed in to change notification settings - Fork 18
/
privnet_show.go
90 lines (75 loc) · 2.15 KB
/
privnet_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cmd
import (
"fmt"
"strings"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
)
type privnetShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Zone string `json:"zone"`
DHCP string `json:"dhcp"`
Instances []string `json:"instances,omitempty"`
}
func (o *privnetShowOutput) Type() string { return "Private Network" }
func (o *privnetShowOutput) ToJSON() { output.JSON(o) }
func (o *privnetShowOutput) ToText() { output.Text(o) }
func (o *privnetShowOutput) ToTable() { output.Table(o) }
func init() {
privnetCmd.AddCommand(&cobra.Command{
Use: "show NAME|ID",
Short: "Show a Private Network details",
Long: fmt.Sprintf(`This command shows a Private Network details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&privnetShowOutput{}), ", ")),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return cmd.Usage()
}
privnet, err := getNetwork(args[0], nil)
if err != nil {
return err
}
return printOutput(showPrivnet(privnet))
},
})
}
func showPrivnet(privnet *egoscale.Network) (output.Outputter, error) {
out := privnetShowOutput{
ID: privnet.ID.String(),
Name: privnet.Name,
Description: privnet.DisplayText,
Zone: privnet.ZoneName,
DHCP: dhcpRange(*privnet),
}
vms, err := privnetDetails(privnet)
if err != nil {
return nil, err
}
out.Instances = make([]string, len(vms))
for i := range vms {
out.Instances[i] = vms[i].Name
}
return &out, nil
}
func privnetDetails(network *egoscale.Network) ([]egoscale.VirtualMachine, error) {
vms, err := globalstate.EgoscaleClient.ListWithContext(gContext, &egoscale.VirtualMachine{
ZoneID: network.ZoneID,
})
if err != nil {
return nil, err
}
var vmsRes []egoscale.VirtualMachine
for _, v := range vms {
vm := v.(*egoscale.VirtualMachine)
nic := vm.NicByNetworkID(*network.ID)
if nic != nil {
vmsRes = append(vmsRes, *vm)
}
}
return vmsRes, nil
}