Skip to content

Commit e90c9fd

Browse files
committed
feat(lb): add loadbalancer list command
1 parent a23e82b commit e90c9fd

5 files changed

Lines changed: 86 additions & 1 deletion

File tree

internal/commands/all/all.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/UpCloudLtd/upcloud-cli/internal/commands/account"
66
"github.com/UpCloudLtd/upcloud-cli/internal/commands/database"
77
"github.com/UpCloudLtd/upcloud-cli/internal/commands/ipaddress"
8+
"github.com/UpCloudLtd/upcloud-cli/internal/commands/loadbalancer"
89
"github.com/UpCloudLtd/upcloud-cli/internal/commands/network"
910
"github.com/UpCloudLtd/upcloud-cli/internal/commands/networkinterface"
1011
"github.com/UpCloudLtd/upcloud-cli/internal/commands/root"
@@ -106,6 +107,10 @@ func BuildCommands(rootCmd *cobra.Command, conf *config.Config) {
106107
commands.BuildCommand(database.TypesCommand(), databaseCommand.Cobra(), conf)
107108
commands.BuildCommand(database.PlansCommand(), databaseCommand.Cobra(), conf)
108109

110+
// LoadBalancers
111+
loadbalancerCommand := commands.BuildCommand(loadbalancer.BaseLoadBalancerCommand(), rootCmd, conf)
112+
commands.BuildCommand(loadbalancer.ListCommand(), loadbalancerCommand.Cobra(), conf)
113+
109114
// Misc
110115
commands.BuildCommand(
111116
&root.CompletionCommand{

internal/commands/database/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
55
)
66

7-
// BaseDatabaseCommand creates the base "zone" command
7+
// BaseDatabaseCommand creates the base "database" command
88
func BaseDatabaseCommand() commands.Command {
99
return &databaseCommand{
1010
commands.New("database", "Manage databases"),
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package loadbalancer
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
5+
"github.com/UpCloudLtd/upcloud-cli/internal/output"
6+
"github.com/UpCloudLtd/upcloud-cli/internal/ui"
7+
"github.com/UpCloudLtd/upcloud-go-api/v4/upcloud/request"
8+
)
9+
10+
// ListCommand creates the "loadbalancer list" command
11+
func ListCommand() commands.Command {
12+
return &listCommand{
13+
BaseCommand: commands.New("list", "List current load balancers", "upctl loadbalancer list"),
14+
}
15+
}
16+
17+
type listCommand struct {
18+
*commands.BaseCommand
19+
}
20+
21+
// ExecuteWithoutArguments implements commands.NoArgumentCommand
22+
func (s *listCommand) ExecuteWithoutArguments(exec commands.Executor) (output.Output, error) {
23+
svc := exec.All()
24+
loadbalancers, err := svc.GetLoadBalancers(&request.GetLoadBalancersRequest{})
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
rows := []output.TableRow{}
30+
for _, lb := range loadbalancers {
31+
coloredState := commands.LoadBalancerOperationalStateColour(lb.OperationalState).Sprint(lb.OperationalState)
32+
33+
rows = append(rows, output.TableRow{
34+
lb.UUID,
35+
lb.Name,
36+
lb.Plan,
37+
lb.Zone,
38+
coloredState,
39+
})
40+
}
41+
42+
return output.Table{
43+
Columns: []output.TableColumn{
44+
{Key: "uuid", Header: "UUID", Colour: ui.DefaultUUUIDColours},
45+
{Key: "name", Header: "Name"},
46+
{Key: "plan", Header: "Plan"},
47+
{Key: "zone", Header: "Zone"},
48+
{Key: "state", Header: "State"},
49+
},
50+
Rows: rows,
51+
}, nil
52+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package loadbalancer
2+
3+
import (
4+
"github.com/UpCloudLtd/upcloud-cli/internal/commands"
5+
)
6+
7+
// BaseLoadBalancerCommand creates the base "loadbalancer" command
8+
func BaseLoadBalancerCommand() commands.Command {
9+
return &loadbalancerCommand{
10+
commands.New("loadbalancer", "Manage load balancers"),
11+
}
12+
}
13+
14+
type loadbalancerCommand struct {
15+
*commands.BaseCommand
16+
}

internal/commands/util.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ func DatabaseStateColour(state upcloud.ManagedDatabaseState) text.Colors {
8484
}
8585
}
8686

87+
// LoadBalancerOperationalStateColour maps load balancer states to colours
88+
func LoadBalancerOperationalStateColour(state upcloud.LoadBalancerOperationalState) text.Colors {
89+
switch state {
90+
case upcloud.LoadBalancerOperationalStateRunning:
91+
return text.Colors{text.FgGreen}
92+
case upcloud.LoadBalancerOperationalStateCheckup, upcloud.LoadBalancerOperationalStatePending, upcloud.LoadBalancerOperationalStateSetupAgent, upcloud.LoadBalancerOperationalStateSetupDNS, upcloud.LoadBalancerOperationalStateSetupLB, upcloud.LoadBalancerOperationalStateSetupNetwork, upcloud.LoadBalancerOperationalStateSetupServer:
93+
return text.Colors{text.FgYellow}
94+
default:
95+
return text.Colors{text.FgHiBlack}
96+
}
97+
}
98+
8799
// ServerStateColour is a helper mapping server states to colours
88100
func ServerStateColour(state string) text.Colors {
89101
switch state {

0 commit comments

Comments
 (0)