Skip to content

Commit

Permalink
fix(lb): fix list private network command (scaleway#3537)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil authored and Mia-Cross committed Dec 27, 2023
1 parent 6713cef commit 2f8616f
Show file tree
Hide file tree
Showing 7 changed files with 593 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/core/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ func AfterFuncCombine(afterFuncs ...AfterFunc) AfterFunc {
}
}

func AfterFuncWhenUpdatingCassette(afterFunc AfterFunc) AfterFunc {
return func(ctx *AfterFuncCtx) error {
if *UpdateCassettes {
return afterFunc(ctx)
}
return nil
}
}

// ExecStoreBeforeCmd executes the given before command and register the result
// in the context Meta at metaKey.
func ExecStoreBeforeCmd(metaKey, cmd string) BeforeFunc {
Expand Down
1 change: 1 addition & 0 deletions internal/namespaces/lb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func GetCommands() *core.Commands {
human.RegisterMarshalerFunc(lb.Frontend{}, lbFrontendMarshalerFunc)
human.RegisterMarshalerFunc(lb.Certificate{}, lbCertificateMarshalerFunc)
human.RegisterMarshalerFunc(lb.ACL{}, lbACLMarshalerFunc)
human.RegisterMarshalerFunc([]*lb.PrivateNetwork{}, lbPrivateNetworksMarshalerFunc)

cmds := GetGeneratedCommands()

Expand Down
55 changes: 55 additions & 0 deletions internal/namespaces/lb/v1/custom_private_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package lb

import (
"fmt"
"time"

"github.com/scaleway/scaleway-cli/v2/internal/human"
"github.com/scaleway/scaleway-sdk-go/api/lb/v1"
)

func lbPrivateNetworksMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
privateNetworks, ok := i.([]*lb.PrivateNetwork)
if !ok {
return "", fmt.Errorf("invalid type: expected []*lb.PrivateNetwork")
}

type customPrivateNetwork struct {
IpamIDs []string `json:"ipam_ids,omitempty"`
DHCPConfigIPID *string `json:"dhcp_config_ip_id,omitempty"`
StaticConfigIPAddress *[]string `json:"static_config_ip_address,omitempty"`
PrivateNetworkID string `json:"private_network_id"`
Status lb.PrivateNetworkStatus `json:"status"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
}

customPrivateNetworks := make([]customPrivateNetwork, 0, len(privateNetworks))
for _, pn := range privateNetworks {
if pn == nil {
continue
}

customPN := customPrivateNetwork{
IpamIDs: pn.IpamIDs,
PrivateNetworkID: pn.PrivateNetworkID,
Status: pn.Status,
CreatedAt: pn.CreatedAt,
UpdatedAt: pn.UpdatedAt,
}

//nolint: staticcheck
if pn.DHCPConfig != nil {
customPN.DHCPConfigIPID = pn.DHCPConfig.IPID
}

//nolint: staticcheck
if pn.StaticConfig != nil {
customPN.StaticConfigIPAddress = pn.StaticConfig.IPAddress
}

customPrivateNetworks = append(customPrivateNetworks, customPN)
}

return human.Marshal(customPrivateNetworks, opt)
}
36 changes: 36 additions & 0 deletions internal/namespaces/lb/v1/custom_private_network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lb

import (
"testing"
"time"

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

func Test_ListLBPrivateNetwork(t *testing.T) {
cmds := GetCommands()
cmds.Merge(vpc.GetCommands())

t.Run("Simple", core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
createLB(),
createPN(),
attachPN(),
),
Cmd: "scw lb private-network list {{ .LB.ID }}",
Check: core.TestCheckGolden(),
AfterFunc: core.AfterFuncCombine(
detachPN(),
deleteLB(),
core.AfterFuncWhenUpdatingCassette(
func(ctx *core.AfterFuncCtx) error {
time.Sleep(10 * time.Second)
return nil
},
),
deletePN(),
),
}))
}
23 changes: 23 additions & 0 deletions internal/namespaces/lb/v1/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,26 @@ func retrieveLBID(metaKey string) core.BeforeFunc {
return nil
}
}

func createPN() core.BeforeFunc {
return core.ExecStoreBeforeCmd(
"PN",
"scw vpc private-network create",
)
}

func deletePN() core.AfterFunc {
return core.ExecAfterCmd("scw vpc private-network delete {{ .PN.ID }}")
}

func attachPN() core.BeforeFunc {
return core.ExecBeforeCmd(
"scw lb private-network attach {{ .LB.ID }} private-network-id={{ .PN.ID }}",
)
}

func detachPN() core.AfterFunc {
return core.ExecAfterCmd(
"scw lb private-network detach {{ .LB.ID }} private-network-id={{ .PN.ID }}",
)
}
Loading

0 comments on commit 2f8616f

Please sign in to comment.