Skip to content

Commit 8002bf3

Browse files
authored
feat(vpc): add support to see all servers in a given private network (#1426)
1 parent 1f77c25 commit 8002bf3

8 files changed

Lines changed: 1667 additions & 1 deletion

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/mattn/go-colorable v0.1.4
2323
github.com/mattn/go-isatty v0.0.11
2424
github.com/pkg/errors v0.9.1 // indirect
25-
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200911150340-00656aa3a030
25+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200914115748-b34ecf5eaa4a
2626
github.com/sergi/go-diff v1.0.0 // indirect
2727
github.com/spf13/cobra v0.0.5
2828
github.com/spf13/pflag v1.0.5

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200908180425-0a4ce2d8f2ad
6666
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200908180425-0a4ce2d8f2ad/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
6767
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200911150340-00656aa3a030 h1:v4Ge/Hm1UnrDP83PG7Spt2dYJT/3+ppruwdYbJ658A4=
6868
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200911150340-00656aa3a030/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
69+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200914115748-b34ecf5eaa4a h1:CHGQgSORmr4wK9EbIu5EWHv3ky0bqG0iBgHGMmIZR1o=
70+
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.6.0.20200914115748-b34ecf5eaa4a/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
6971
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
7072
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
7173
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=

internal/namespaces/vpc/v1/custom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ import (
77
func GetCommands() *core.Commands {
88
cmds := GetGeneratedCommands()
99

10+
cmds.MustFind("vpc", "private-network", "get").Override(privateNetworkGetBuilder)
11+
1012
return cmds
1113
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package vpc
2+
3+
import (
4+
"context"
5+
6+
"github.com/scaleway/scaleway-cli/internal/core"
7+
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
8+
"github.com/scaleway/scaleway-sdk-go/api/vpc/v1"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
func privateNetworkGetBuilder(c *core.Command) *core.Command {
13+
type customServer struct {
14+
ID string `json:"id"`
15+
Name string `json:"name"`
16+
State instance.ServerState `json:"state"`
17+
NicID string `json:"nic_id"`
18+
MacAddress string `json:"mac"`
19+
}
20+
21+
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
22+
getPNResp, err := runner(ctx, argsI)
23+
if err != nil {
24+
return getPNResp, err
25+
}
26+
pn := getPNResp.(*vpc.PrivateNetwork)
27+
28+
client := core.ExtractClient(ctx)
29+
instanceAPI := instance.NewAPI(client)
30+
listServers, err := instanceAPI.ListServers(&instance.ListServersRequest{
31+
PrivateNetwork: &pn.ID,
32+
}, scw.WithAllPages())
33+
if err != nil {
34+
return getPNResp, err
35+
}
36+
37+
customServers := []customServer{}
38+
for _, server := range listServers.Servers {
39+
for _, nic := range server.PrivateNics {
40+
if nic.PrivateNetworkID == pn.ID {
41+
customServers = append(customServers, customServer{
42+
NicID: nic.ID,
43+
ID: nic.ServerID,
44+
MacAddress: nic.MacAddress,
45+
Name: server.Name,
46+
State: server.State,
47+
})
48+
}
49+
}
50+
}
51+
52+
return &struct {
53+
*vpc.PrivateNetwork
54+
Servers []customServer `json:"servers"`
55+
}{
56+
pn,
57+
customServers,
58+
}, nil
59+
}
60+
61+
c.View = &core.View{
62+
Sections: []*core.ViewSection{
63+
{FieldName: "Servers", Title: "Servers"},
64+
},
65+
}
66+
67+
return c
68+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package vpc
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/internal/core"
7+
"github.com/scaleway/scaleway-cli/internal/namespaces/instance/v1"
8+
)
9+
10+
func Test_GetPrivateNetwork(t *testing.T) {
11+
cmds := GetCommands()
12+
cmds.Merge(instance.GetCommands())
13+
14+
t.Run("Simple", core.Test(&core.TestConfig{
15+
Commands: cmds,
16+
BeforeFunc: core.BeforeFuncCombine(
17+
createPN(),
18+
createInstance(),
19+
createNIC(),
20+
),
21+
Cmd: "scw vpc private-network get {{ .PN.ID }}",
22+
Check: core.TestCheckGolden(),
23+
AfterFunc: core.AfterFuncCombine(
24+
deleteInstance(),
25+
deletePN(),
26+
),
27+
}))
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package vpc
2+
3+
import (
4+
"github.com/scaleway/scaleway-cli/internal/core"
5+
)
6+
7+
func createInstance() core.BeforeFunc {
8+
return core.ExecStoreBeforeCmd(
9+
"Instance",
10+
"scw instance server create stopped=true image=ubuntu_focal",
11+
)
12+
}
13+
14+
func deleteInstance() core.AfterFunc {
15+
return core.ExecAfterCmd("scw instance server delete {{ .Instance.ID }}")
16+
}
17+
18+
func createPN() core.BeforeFunc {
19+
return core.ExecStoreBeforeCmd(
20+
"PN",
21+
"scw vpc private-network create",
22+
)
23+
}
24+
25+
func deletePN() core.AfterFunc {
26+
return core.ExecAfterCmd("scw vpc private-network delete {{ .PN.ID }}")
27+
}
28+
29+
func createNIC() core.BeforeFunc {
30+
return core.ExecStoreBeforeCmd(
31+
"NIC",
32+
"scw instance private-nic create server-id={{ .Instance.ID }} private-network-id={{ .PN.ID }}",
33+
)
34+
}

0 commit comments

Comments
 (0)