|
| 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 | + "github.com/jedib0t/go-pretty/v6/text" |
| 9 | +) |
| 10 | + |
| 11 | +// ShowCommand creates the "loadbalancer show" command |
| 12 | +func ShowCommand() commands.Command { |
| 13 | + return &showCommand{ |
| 14 | + BaseCommand: commands.New( |
| 15 | + "show", |
| 16 | + "Show load balancer details", |
| 17 | + "upctl loadbalancer show 55199a44-4751-4e27-9394-7c7661910be3", |
| 18 | + "upctl loadbalancer show my-load-balancer", |
| 19 | + ), |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +type showCommand struct { |
| 24 | + *commands.BaseCommand |
| 25 | + // resolver.CachingLoadBalancer |
| 26 | + // completion.LoadBalancer |
| 27 | +} |
| 28 | + |
| 29 | +// Execute implements commands.MultipleArgumentCommand |
| 30 | +func (s *showCommand) Execute(exec commands.Executor, uuid string) (output.Output, error) { |
| 31 | + svc := exec.All() |
| 32 | + lb, err := svc.GetLoadBalancer(&request.GetLoadBalancerRequest{UUID: uuid}) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + |
| 37 | + var networkName string |
| 38 | + if network, err := svc.GetNetworkDetails(&request.GetNetworkDetailsRequest{UUID: lb.NetworkUUID}); err != nil { |
| 39 | + networkName = text.FgHiBlack.Sprint("Unknown") |
| 40 | + } else { |
| 41 | + networkName = network.Name |
| 42 | + } |
| 43 | + |
| 44 | + backEndRows := []output.TableRow{} |
| 45 | + for _, backEnd := range lb.Backends { |
| 46 | + resolver := text.FgHiBlack.Sprint("None") |
| 47 | + if backEnd.Resolver != "" { |
| 48 | + resolver = backEnd.Resolver |
| 49 | + } |
| 50 | + |
| 51 | + backEndRows = append(backEndRows, output.TableRow{ |
| 52 | + backEnd.Name, |
| 53 | + resolver, |
| 54 | + len(backEnd.Members), |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + frontEndRows := []output.TableRow{} |
| 59 | + for _, frontEnd := range lb.Frontends { |
| 60 | + frontEndRows = append(frontEndRows, output.TableRow{ |
| 61 | + frontEnd.Name, |
| 62 | + frontEnd.Mode, |
| 63 | + frontEnd.Port, |
| 64 | + len(frontEnd.TLSConfigs), |
| 65 | + frontEnd.DefaultBackend, |
| 66 | + len(frontEnd.Rules), |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + // For JSON and YAML output, passthrough API response |
| 71 | + return output.MarshaledWithHumanOutput{ |
| 72 | + Value: lb, |
| 73 | + Output: output.Combined{ |
| 74 | + output.CombinedSection{ |
| 75 | + Contents: output.Details{ |
| 76 | + Sections: []output.DetailSection{ |
| 77 | + { |
| 78 | + Title: "Overview:", |
| 79 | + Rows: []output.DetailRow{ |
| 80 | + {Title: "UUID:", Value: lb.UUID, Colour: ui.DefaultUUUIDColours}, |
| 81 | + {Title: "Name:", Value: lb.Name}, |
| 82 | + {Title: "Plan:", Value: lb.Plan}, |
| 83 | + {Title: "Zone:", Value: lb.Zone}, |
| 84 | + {Title: "DNS name", Value: lb.DNSName}, |
| 85 | + {Title: "Network name", Value: networkName}, |
| 86 | + {Title: "Network UUID", Value: lb.NetworkUUID}, |
| 87 | + {Title: "Operational state:", Value: lb.OperationalState, Colour: commands.LoadBalancerOperationalStateColour(lb.OperationalState)}, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + output.CombinedSection{ |
| 94 | + Title: "Backends:", |
| 95 | + Contents: output.Table{ |
| 96 | + Columns: []output.TableColumn{ |
| 97 | + {Key: "name", Header: "Name"}, |
| 98 | + {Key: "resolver", Header: "Resolver"}, |
| 99 | + {Key: "members", Header: "Members"}, |
| 100 | + }, |
| 101 | + Rows: backEndRows, |
| 102 | + }, |
| 103 | + }, |
| 104 | + output.CombinedSection{ |
| 105 | + Title: "Frontends:", |
| 106 | + Contents: output.Table{ |
| 107 | + Columns: []output.TableColumn{ |
| 108 | + {Key: "name", Header: "Name"}, |
| 109 | + {Key: "mode", Header: "Mode"}, |
| 110 | + {Key: "port", Header: "Port"}, |
| 111 | + {Key: "tls_configs", Header: "TLS configs"}, |
| 112 | + {Key: "default_backend", Header: "Default Backend"}, |
| 113 | + {Key: "rules", Header: "Rules"}, |
| 114 | + }, |
| 115 | + Rows: frontEndRows, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + }, nil |
| 120 | +} |
0 commit comments