-
Notifications
You must be signed in to change notification settings - Fork 19
/
nlb_show.go
116 lines (99 loc) · 2.93 KB
/
nlb_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
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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
apiv2 "github.com/exoscale/egoscale/api/v2"
"github.com/exoscale/cli/table"
)
type nlbShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreationDate string `json:"created_at"`
Zone string `json:"zone"`
IPAddress string `json:"ip_address"`
State string `json:"state"`
Services []nlbServiceShowOutput `json:"services"`
}
func (o *nlbShowOutput) toJSON() { outputJSON(o) }
func (o *nlbShowOutput) toText() { outputText(o) }
func (o *nlbShowOutput) toTable() {
t := table.NewTable(os.Stdout)
defer t.Render()
t.SetHeader([]string{"Network Load Balancer"})
t.Append([]string{"ID", o.ID})
t.Append([]string{"Name", o.Name})
t.Append([]string{"Zone", o.Zone})
t.Append([]string{"IP Address", o.IPAddress})
t.Append([]string{"Description", o.Description})
t.Append([]string{"Services", func() string {
if len(o.Services) > 0 {
return strings.Join(
func() []string {
services := make([]string, len(o.Services))
for i := range o.Services {
services[i] = fmt.Sprintf("%s | %s",
o.Services[i].ID,
o.Services[i].Name)
}
return services
}(),
"\n")
}
return "n/a"
}()})
t.Append([]string{"State", o.State})
}
var nlbShowCmd = &cobra.Command{
Use: "show <name | ID>",
Short: "Show a Network Load Balancer details",
Long: fmt.Sprintf(`This command shows a Network Load Balancer details.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&nlbShowOutput{}), ", ")),
Aliases: gShowAlias,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
cmdSetZoneFlagFromDefault(cmd)
return cmdCheckRequiredFlags(cmd, []string{"zone"})
},
RunE: func(cmd *cobra.Command, args []string) error {
zone, err := cmd.Flags().GetString("zone")
if err != nil {
return err
}
return output(showNLB(zone, args[0]))
},
}
func showNLB(zone, ref string) (outputter, error) {
ctx := apiv2.WithEndpoint(gContext, apiv2.NewReqEndpoint(gCurrentAccount.Environment, zone))
nlb, err := lookupNLB(ctx, zone, ref)
if err != nil {
return nil, err
}
svcOut := make([]nlbServiceShowOutput, 0)
for _, svc := range nlb.Services {
svcOut = append(svcOut, nlbServiceShowOutput{
ID: svc.ID,
Name: svc.Name,
})
}
out := nlbShowOutput{
ID: nlb.ID,
Name: nlb.Name,
Description: nlb.Description,
CreationDate: nlb.CreatedAt.String(),
Zone: zone,
IPAddress: nlb.IPAddress.String(),
State: nlb.State,
Services: svcOut,
}
return &out, nil
}
func init() {
nlbShowCmd.Flags().StringP("zone", "z", "", "Network Load Balancer zone")
nlbCmd.AddCommand(nlbShowCmd)
}