Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lb): improve backend get output #2575

Merged
merged 12 commits into from
Nov 21, 2022
2 changes: 2 additions & 0 deletions internal/namespaces/lb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ func GetCommands() *core.Commands {
human.RegisterMarshalerFunc(lb.ACLActionType(""), human.EnumMarshalFunc(aclMarshalSpecs))
human.RegisterMarshalerFunc(lb.BackendServerStatsHealthCheckStatus(""), human.EnumMarshalFunc(backendServerStatsHealthCheckStatusMarshalSpecs))
human.RegisterMarshalerFunc(lb.BackendServerStatsServerState(""), human.EnumMarshalFunc(backendServerStatsServerStateMarshalSpecs))
human.RegisterMarshalerFunc(lb.LB{}, lbMarshalerFunc)
human.RegisterMarshalerFunc(lb.Backend{}, lbBackendMarshalerFunc)

cmds := GetGeneratedCommands()

Expand Down
45 changes: 45 additions & 0 deletions internal/namespaces/lb/v1/custom_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,48 @@ var (
lb.BackendServerStatsServerStateStopping: &human.EnumMarshalSpec{Attribute: color.FgBlue, Value: "stopping"},
}
)

func lbMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp lb.LB
loadbalancer := tmp(i.(lb.LB))

opt.Sections = []*human.MarshalSection{
{
FieldName: "IP",
},
{
FieldName: "Instances",
},
}

str, err := human.Marshal(loadbalancer, opt)
if err != nil {
return "", err
}

return str, nil
}

func lbBackendMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp lb.Backend
backend := tmp(i.(lb.Backend))

opt.Sections = []*human.MarshalSection{
{
FieldName: "HealthCheck",
},
{
FieldName: "Pool",
},
{
FieldName: "LB",
},
}

str, err := human.Marshal(backend, opt)
if err != nil {
return "", err
}

return str, nil
}
29 changes: 29 additions & 0 deletions internal/namespaces/lb/v1/custom_backend_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lb

import (
"testing"

"github.com/scaleway/scaleway-cli/v2/internal/core"
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/instance/v1"
)

func Test_GetBackend(t *testing.T) {
cmds := GetCommands()
cmds.Merge(instance.GetCommands())

t.Run("Simple", core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
createLB(),
createInstance(),
createBackend(80),
addIP2Backend("{{ .Instance.PublicIP.Address }}"),
),
Cmd: "scw lb backend get {{ .Backend.ID }}",
Check: core.TestCheckGolden(),
AfterFunc: core.AfterFuncCombine(
deleteLB(),
deleteInstance(),
),
}))
}
Loading