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(vpc): add support to see all servers in a given private network #1426

Merged
merged 5 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/mattn/go-colorable v0.1.4
github.com/mattn/go-isatty v0.0.11
github.com/pkg/errors v0.9.1 // indirect
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200911150340-00656aa3a030
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200914115748-b34ecf5eaa4a
github.com/sergi/go-diff v1.0.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200908180425-0a4ce2d8f2ad
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200908180425-0a4ce2d8f2ad/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200911150340-00656aa3a030 h1:v4Ge/Hm1UnrDP83PG7Spt2dYJT/3+ppruwdYbJ658A4=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200911150340-00656aa3a030/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200914115748-b34ecf5eaa4a h1:CHGQgSORmr4wK9EbIu5EWHv3ky0bqG0iBgHGMmIZR1o=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200914115748-b34ecf5eaa4a/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
Expand Down
2 changes: 2 additions & 0 deletions internal/namespaces/vpc/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ import (
func GetCommands() *core.Commands {
cmds := GetGeneratedCommands()

cmds.MustFind("vpc", "private-network", "get").Override(privateNetworkGetBuilder)

return cmds
}
68 changes: 68 additions & 0 deletions internal/namespaces/vpc/v1/custom_private_network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package vpc

import (
"context"

"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
"github.com/scaleway/scaleway-sdk-go/api/vpc/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func privateNetworkGetBuilder(c *core.Command) *core.Command {
type customServer struct {
ID string `json:"id"`
Name string `json:"name"`
State instance.ServerState `json:"state"`
NicID string `json:"nic_id"`
MacAddress string `json:"mac"`
}

c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
getPNResp, err := runner(ctx, argsI)
if err != nil {
return getPNResp, err
}
pn := getPNResp.(*vpc.PrivateNetwork)

client := core.ExtractClient(ctx)
instanceAPI := instance.NewAPI(client)
listServers, err := instanceAPI.ListServers(&instance.ListServersRequest{
PrivateNetwork: &pn.ID,
}, scw.WithAllPages())
if err != nil {
return getPNResp, err
}

customServers := []customServer{}
for _, server := range listServers.Servers {
for _, nic := range server.PrivateNics {
if nic.PrivateNetworkID == pn.ID {
customServers = append(customServers, customServer{
NicID: nic.ID,
ID: nic.ServerID,
MacAddress: nic.MacAddress,
Name: server.Name,
State: server.State,
})
}
}
}

return &struct {
*vpc.PrivateNetwork
Servers []customServer `json:"servers"`
}{
pn,
customServers,
}, nil
}

c.View = &core.View{
Sections: []*core.ViewSection{
{FieldName: "Servers", Title: "Servers"},
},
}

return c
}
28 changes: 28 additions & 0 deletions internal/namespaces/vpc/v1/custom_private_network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package vpc

import (
"testing"

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

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

t.Run("Simple", core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
createPN(),
createInstance(),
createNIC(),
),
Cmd: "scw vpc private-network get {{ .PN.ID }}",
Check: core.TestCheckGolden(),
AfterFunc: core.AfterFuncCombine(
deleteInstance(),
deletePN(),
),
}))
}
34 changes: 34 additions & 0 deletions internal/namespaces/vpc/v1/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package vpc

import (
"github.com/scaleway/scaleway-cli/internal/core"
)

func createInstance() core.BeforeFunc {
return core.ExecStoreBeforeCmd(
"Instance",
"scw instance server create stopped=true image=ubuntu_focal",
)
}

func deleteInstance() core.AfterFunc {
return core.ExecAfterCmd("scw instance server delete {{ .Instance.ID }}")
}

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 createNIC() core.BeforeFunc {
return core.ExecStoreBeforeCmd(
"NIC",
"scw instance private-nic create server-id={{ .Instance.ID }} private-network-id={{ .PN.ID }}",
)
}
Loading