-
Notifications
You must be signed in to change notification settings - Fork 20
/
nlb_show.go
157 lines (131 loc) · 4.04 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cmd
import (
"bytes"
"fmt"
"os"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/exoscale/cli/pkg/account"
"github.com/exoscale/cli/pkg/globalstate"
"github.com/exoscale/cli/pkg/output"
"github.com/exoscale/cli/table"
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
)
type nlbShowOutput struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
CreationDate string `json:"creation_date"`
Zone string `json:"zone"`
IPAddress string `json:"ip_address"`
State string `json:"state"`
Services []nlbServiceShowOutput `json:"services"`
Labels map[string]string `json:"labels"`
}
func (o *nlbShowOutput) ToJSON() { output.JSON(o) }
func (o *nlbShowOutput) ToText() { output.Text(o) }
func (o *nlbShowOutput) ToTable() {
t := table.NewTable(os.Stdout)
t.SetHeader([]string{"Network Load Balancer"})
defer t.Render()
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{"Creation Date", o.CreationDate})
t.Append([]string{"State", o.State})
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{"Labels", func() string {
sortedKeys := func() []string {
keys := make([]string, 0)
for k := range o.Labels {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}()
buf := bytes.NewBuffer(nil)
at := table.NewEmbeddedTable(buf)
at.SetHeader([]string{" "})
for _, k := range sortedKeys {
at.Append([]string{k, o.Labels[k]})
}
at.Render()
return buf.String()
}()})
}
type nlbShowCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"show"`
NetworkLoadBalancer string `cli-arg:"#" cli-usage:"NAME|ID"`
Zone string `cli-short:"z" cli-usage:"Network Load Balancer zone"`
}
func (c *nlbShowCmd) cmdAliases() []string { return gShowAlias }
func (c *nlbShowCmd) cmdShort() string { return "Show a Network Load Balancer details" }
func (c *nlbShowCmd) cmdLong() string {
return fmt.Sprintf(`This command shows a Network Load Balancer details.
Supported output template annotations: %s`,
strings.Join(output.TemplateAnnotations(&nlbShowOutput{}), ", "))
}
func (c *nlbShowCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *nlbShowCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(account.CurrentAccount.Environment, c.Zone))
nlb, err := globalstate.EgoscaleClient.FindNetworkLoadBalancer(ctx, c.Zone, c.NetworkLoadBalancer)
if err != nil {
return 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: utils.DefaultString(nlb.Description, ""),
CreationDate: nlb.CreatedAt.String(),
Zone: c.Zone,
IPAddress: utils.DefaultIP(nlb.IPAddress, ""),
State: *nlb.State,
Services: svcOut,
Labels: func() (v map[string]string) {
if nlb.Labels != nil {
v = *nlb.Labels
}
return
}(),
}
return c.outputFunc(&out, nil)
}
func init() {
cobra.CheckErr(registerCLICommand(nlbCmd, &nlbShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
// FIXME: remove this someday.
cobra.CheckErr(registerCLICommand(deprecatedNLBCmd, &nlbShowCmd{
cliCommandSettings: defaultCLICmdSettings(),
}))
}