-
Notifications
You must be signed in to change notification settings - Fork 7
/
show.go
274 lines (250 loc) · 7.53 KB
/
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package server
import (
"fmt"
"strings"
"sync"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/completion"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/format"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/labels"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/output"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/resolver"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/ui"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
// ShowCommand creates the "server show" command
func ShowCommand() commands.Command {
return &showCommand{
BaseCommand: commands.New(
"show",
"Show server details",
"upctl server show 21aeb3b7-cd89-4123-a376-559b0e75be8b",
"upctl server show 21aeb3b7-cd89-4123-a376-559b0e75be8b 0053a6f5-e6d1-4b0b-b9dc-b90d0894e8d0",
"upctl server show myhostname",
"upctl server show my_server1 my_server2",
),
}
}
type showCommand struct {
*commands.BaseCommand
resolver.CachingServer
completion.Server
}
func (s *showCommand) InitCommand() {
}
// Execute implements commands.MultipleArgumentCommand
func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) {
var (
wg sync.WaitGroup
fwRuleErr error
)
serverSvc := exec.Server()
firewallSvc := exec.Firewall()
wg.Add(1)
var firewallRules *upcloud.FirewallRules
go func() {
defer wg.Done()
firewallRules, fwRuleErr = firewallSvc.GetFirewallRules(exec.Context(), &request.GetFirewallRulesRequest{ServerUUID: uuid})
}()
server, err := serverSvc.GetServerDetails(exec.Context(), &request.GetServerDetailsRequest{UUID: uuid})
if err != nil {
return nil, err
}
wg.Wait()
if fwRuleErr != nil {
return nil, fwRuleErr
}
planOutput := server.Plan
if planOutput == "custom" {
memory := server.MemoryAmount / 1024
planOutput = fmt.Sprintf("%dxCPU-%dGB (custom)", server.CoreNumber, memory)
}
// Storage details
storageRows := []output.TableRow{}
for _, storage := range server.StorageDevices {
var flags []string
if storage.PartOfPlan == "yes" {
flags = append(flags, "P")
}
if storage.BootDisk == 1 {
flags = append(flags, "B")
}
storageRows = append(storageRows, output.TableRow{
storage.UUID,
storage.Title,
storage.Type,
storage.Address,
storage.Size,
storage.Encrypted,
strings.Join(flags, " "),
})
}
// Network details
nicRows := []output.TableRow{}
for _, nic := range server.Networking.Interfaces {
var flags []string
if nic.SourceIPFiltering.Bool() {
flags = append(flags, "S")
}
if nic.Bootable.Bool() {
flags = append(flags, "B")
}
nicRows = append(nicRows, output.TableRow{
nic.Index,
nic.Type,
nic.IPAddresses,
nic.MAC,
nic.Network,
strings.Join(flags, " "),
})
}
combined := output.Combined{
output.CombinedSection{
Contents: output.Details{
Sections: []output.DetailSection{
{
Title: "Common",
Rows: []output.DetailRow{
{Title: "UUID:", Key: "uuid", Value: server.UUID, Colour: ui.DefaultUUUIDColours},
{Title: "Hostname:", Key: "hostname", Value: server.Hostname},
{Title: "Title:", Key: "title", Value: server.Title},
{Title: "Plan:", Key: "plan", Value: planOutput},
{Title: "Zone:", Key: "zone", Value: server.Zone},
{Title: "State:", Key: "state", Value: server.State, Format: format.ServerState},
{Title: "Simple Backup:", Key: "simple_backup", Value: server.SimpleBackup},
{Title: "Licence:", Key: "licence", Value: server.License},
{Title: "Metadata:", Key: "metadata", Value: server.Metadata.String()},
{Title: "Timezone:", Key: "timezone", Value: server.Timezone},
{Title: "Host ID:", Key: "host_id", Value: server.Host},
{Title: "Server Group:", Key: "server_group", Value: server.ServerGroup},
{Title: "Tags:", Key: "tags", Value: strings.Join(server.Tags, ",")},
},
},
},
},
},
labels.GetLabelsSection(server.Labels),
output.CombinedSection{
Key: "storage",
Title: "Storage: (Flags: B = bootdisk, P = part of plan)",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
{Key: "title", Header: "Title"},
{Key: "type", Header: "Type"},
{Key: "address", Header: "Address"},
{Key: "size", Header: "Size (GiB)"},
{Key: "encrypted", Header: "Encrypted", Format: format.Boolean},
{Key: "flags", Header: "Flags"},
},
Rows: storageRows,
},
},
output.CombinedSection{
Key: "nics",
Title: "NICs: (Flags: S = source IP filtering, B = bootable)",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "id", Header: "#"},
{Key: "type", Header: "Type"},
{Key: "ip_addresses", Header: "IP Address", Format: formatShowIPAddresses},
{Key: "mac_address", Header: "MAC Address"},
{Key: "network", Header: "Network", Colour: ui.DefaultUUUIDColours},
{Key: "flags", Header: "Flags"},
},
Rows: nicRows,
},
},
}
// Firewall rules
if server.Firewall == "on" {
fwRows := []output.TableRow{}
for _, rule := range firewallRules.FirewallRules {
fwRows = append(fwRows, output.TableRow{
rule.Position,
rule.Direction,
rule.Action,
ui.FormatRange(
rule.SourceAddressStart,
rule.SourceAddressEnd,
),
ui.FormatRange(
rule.DestinationAddressStart,
rule.DestinationAddressEnd,
),
ui.FormatRange(
rule.SourcePortStart,
rule.SourcePortEnd,
),
ui.FormatRange(
rule.DestinationPortStart,
rule.DestinationPortEnd,
),
ui.ConcatStrings(rule.Family, rule.Protocol, rule.ICMPType),
})
}
combined = append(combined, output.CombinedSection{
Key: "firewall",
Title: "Firewall Rules:",
Contents: output.Table{
Columns: []output.TableColumn{
{Key: "position", Header: "#"},
{Key: "direction", Header: "Direction"},
{Key: "action", Header: "Action"},
{Key: "src_ipaddress", Header: "Src IPAddress", Colour: ui.DefaultAddressColours},
{Key: "dest_ipaddress", Header: "Dest IPAddress", Colour: ui.DefaultAddressColours},
{Key: "src_port", Header: "Src Port"},
{Key: "dest_port", Header: "Dest Port"},
{Key: "protocol", Header: "Protocol"},
},
Rows: fwRows,
},
})
}
// Remote access
if server.RemoteAccessEnabled.Bool() {
combined = append(combined, output.CombinedSection{
Contents: output.Details{
Sections: []output.DetailSection{
{
Title: "Remote Access",
Rows: []output.DetailRow{
{Title: "Type:", Key: "type", Value: server.RemoteAccessType},
{Title: "Host:", Key: "host", Value: server.RemoteAccessHost},
{Title: "Port:", Key: "port", Value: server.RemoteAccessPort},
{Title: "Password:", Key: "password", Value: server.RemoteAccessPassword},
},
},
},
},
})
}
return output.MarshaledWithHumanOutput{
Value: server,
Output: combined,
}, nil
}
func formatShowIPAddresses(val interface{}) (text.Colors, string, error) {
addresses, ok := val.(upcloud.IPAddressSlice)
if !ok {
return nil, "", fmt.Errorf("cannot parse IP addresses from %T, expected upcloud.IPAddressSlice", val)
}
var strs []string
for _, ipa := range addresses {
var floating string
if ipa.Floating.Bool() {
floating = " (f)"
}
strs = append(
strs,
fmt.Sprintf(
"%v: %s%s",
ipa.Family,
ui.DefaultAddressColours.Sprint(ipa.Address),
floating),
)
}
return nil, strings.Join(strs, "\n"), nil
}