Skip to content

Commit 6b27e67

Browse files
committed
feat(server): add option to filter server list IP addresses by access type
1 parent 87bd4c3 commit 6b27e67

1 file changed

Lines changed: 22 additions & 14 deletions

File tree

internal/commands/server/list.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"sync"
88

99
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
10-
"github.com/UpCloudLtd/upcloud-cli/internal/config"
1110
"github.com/UpCloudLtd/upcloud-cli/internal/output"
1211
"github.com/UpCloudLtd/upcloud-cli/internal/service"
1312
"github.com/UpCloudLtd/upcloud-cli/internal/ui"
@@ -20,7 +19,13 @@ import (
2019
// ListCommand creates the "server list" command
2120
func ListCommand() commands.Command {
2221
return &listCommand{
23-
BaseCommand: commands.New("list", "List current servers", "upctl server list"),
22+
BaseCommand: commands.New(
23+
"list",
24+
"List current servers",
25+
"upctl server list",
26+
"upctl server list --show-ip-addresses",
27+
"upctl server list --show-ip-addresses=public",
28+
),
2429
}
2530
}
2631

@@ -38,13 +43,14 @@ type listServerIpaddresses struct {
3843

3944
type listCommand struct {
4045
*commands.BaseCommand
41-
showIPAddresses config.OptionalBoolean
46+
showIPAddresses string
4247
}
4348

4449
// InitCommand implements Command.InitCommand
4550
func (ls *listCommand) InitCommand() {
4651
flags := &pflag.FlagSet{}
47-
config.AddToggleFlag(flags, &ls.showIPAddresses, "show-ip-addresses", false, "Show IP addresses of the servers in the output.")
52+
flags.StringVar(&ls.showIPAddresses, "show-ip-addresses", "none", "Show servers IP addresses of specified access type in the output or all ip addresses if argument value is \"all\" or no argument is specified.")
53+
flags.Lookup("show-ip-addresses").NoOptDefVal = "all"
4854
ls.AddFlags(flags)
4955
}
5056

@@ -83,8 +89,8 @@ func (ls *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.O
8389
{Key: "state", Header: "State"},
8490
}
8591

86-
if ls.showIPAddresses.Value() {
87-
ipaddressMap, err := getIPAddressesByServerUUID(servers, svc)
92+
if ls.showIPAddresses != "none" {
93+
ipaddressMap, err := getIPAddressesByServerUUID(servers, ls.showIPAddresses, svc)
8894
if err != nil {
8995
return nil, err
9096
}
@@ -115,15 +121,15 @@ func (ls *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.O
115121
}
116122

117123
// getIPAddressesByServerUUID returns IP addresses grouped by server UUID. This function will be removed when server end-point response includes IP addresses.
118-
func getIPAddressesByServerUUID(servers *upcloud.Servers, svc service.AllServices) (map[string]listServerIpaddresses, error) {
124+
func getIPAddressesByServerUUID(servers *upcloud.Servers, accessType string, svc service.AllServices) (map[string]listServerIpaddresses, error) {
119125
returnChan := make(chan listServerIpaddresses)
120126
var wg sync.WaitGroup
121127

122128
for _, server := range servers.Servers {
123129
wg.Add(1)
124130
go func(server upcloud.Server) {
125131
defer wg.Done()
126-
ipaddresses, err := getServerIPAddresses(server.UUID, svc)
132+
ipaddresses, err := getServerIPAddresses(server.UUID, accessType, svc)
127133
returnChan <- listServerIpaddresses{
128134
ServerUUID: server.UUID,
129135
IPAddresses: ipaddresses,
@@ -145,7 +151,7 @@ func getIPAddressesByServerUUID(servers *upcloud.Servers, svc service.AllService
145151
return ipaddressMap, nil
146152
}
147153

148-
func getServerIPAddresses(uuid string, svc service.AllServices) ([]listIPAddress, error) {
154+
func getServerIPAddresses(uuid, accessType string, svc service.AllServices) ([]listIPAddress, error) {
149155
server, err := svc.GetServerNetworks(&request.GetServerNetworksRequest{ServerUUID: uuid})
150156
if err != nil {
151157
return nil, err
@@ -154,11 +160,13 @@ func getServerIPAddresses(uuid string, svc service.AllServices) ([]listIPAddress
154160
var ipaddresses []listIPAddress
155161
for _, iface := range server.Interfaces {
156162
for _, ipa := range iface.IPAddresses {
157-
ipaddresses = append(ipaddresses, listIPAddress{
158-
Access: iface.Type,
159-
Address: ipa.Address,
160-
Floating: ipa.Floating.Bool(),
161-
})
163+
if accessType == "all" || iface.Type == accessType {
164+
ipaddresses = append(ipaddresses, listIPAddress{
165+
Access: iface.Type,
166+
Address: ipa.Address,
167+
Floating: ipa.Floating.Bool(),
168+
})
169+
}
162170
}
163171
}
164172

0 commit comments

Comments
 (0)