Skip to content

Commit 4d10fe2

Browse files
yfodilremyleone
andauthored
feat(instance): get pnic from mac address (#2715)
Co-authored-by: Rémy Léone <rleone@scaleway.com>
1 parent d2eea8d commit 4d10fe2

10 files changed

+3762
-2
lines changed

cmd/scw/testdata/test-all-usage-instance-private-nic-get-usage.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ USAGE:
77

88
ARGS:
99
server-id The server the private NIC is attached to
10-
private-nic-id The private NIC unique ID
10+
private-nic-id The private NIC unique ID or MAC address
1111
[zone=fr-par-1] Zone to target. If none is passed will use default zone from the config (fr-par-1 | fr-par-2 | fr-par-3 | nl-ams-1 | nl-ams-2 | pl-waw-1 | pl-waw-2)
1212

1313
FLAGS:

docs/commands/instance.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ scw instance private-nic get [arg=value ...]
888888
| Name | | Description |
889889
|------|---|-------------|
890890
| server-id | Required | The server the private NIC is attached to |
891-
| private-nic-id | Required | The private NIC unique ID |
891+
| private-nic-id | Required | The private NIC unique ID or MAC address |
892892
| zone | Default: `fr-par-1`<br />One of: `fr-par-1`, `fr-par-2`, `fr-par-3`, `nl-ams-1`, `nl-ams-2`, `pl-waw-1`, `pl-waw-2` | Zone to target. If none is passed will use default zone from the config |
893893

894894

internal/namespaces/instance/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func GetCommands() *core.Commands {
173173
human.RegisterMarshalerFunc(instance.PrivateNICState(""), human.EnumMarshalFunc(privateNICStateMarshalSpecs))
174174

175175
cmds.MustFind("instance", "private-nic", "list").Override(privateNicListBuilder)
176+
cmds.MustFind("instance", "private-nic", "get").Override(privateNicGetBuilder)
176177

177178
return cmds
178179
}

internal/namespaces/instance/v1/custom_privatenics.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package instance
22

33
import (
44
"context"
5+
"net"
56

67
"github.com/fatih/color"
78
"github.com/scaleway/scaleway-cli/v2/internal/core"
@@ -31,3 +32,38 @@ func privateNicListBuilder(c *core.Command) *core.Command {
3132

3233
return c
3334
}
35+
36+
func privateNicGetBuilder(c *core.Command) *core.Command {
37+
c.ArgSpecs.GetByName("private-nic-id").Short = "The private NIC unique ID or MAC address"
38+
39+
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
40+
tmpRequest := argsI.(*instance.GetPrivateNICRequest)
41+
42+
if isMacAddress(tmpRequest.PrivateNicID) {
43+
client := core.ExtractClient(ctx)
44+
api := instance.NewAPI(client)
45+
46+
listPrivateNICs, err := api.ListPrivateNICs(&instance.ListPrivateNICsRequest{
47+
Zone: tmpRequest.Zone,
48+
ServerID: tmpRequest.ServerID,
49+
})
50+
if err != nil {
51+
return nil, err
52+
}
53+
for _, pn := range listPrivateNICs.PrivateNics {
54+
if pn.MacAddress == tmpRequest.PrivateNicID {
55+
tmpRequest.PrivateNicID = pn.ID
56+
}
57+
}
58+
}
59+
60+
return runner(ctx, tmpRequest)
61+
}
62+
63+
return c
64+
}
65+
66+
func isMacAddress(address string) bool {
67+
_, err := net.ParseMAC(address)
68+
return err == nil
69+
}

internal/namespaces/instance/v1/custom_privatenics_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/scaleway/scaleway-cli/v2/internal/core"
7+
"github.com/scaleway/scaleway-cli/v2/internal/namespaces/vpc/v1"
78
)
89

910
func Test_ListNICs(t *testing.T) {
@@ -16,3 +17,34 @@ func Test_ListNICs(t *testing.T) {
1617
),
1718
}))
1819
}
20+
21+
func Test_GetPrivateNIC(t *testing.T) {
22+
cmds := GetCommands()
23+
cmds.Merge(vpc.GetCommands())
24+
25+
t.Run("Get from ID", core.Test(&core.TestConfig{
26+
Commands: cmds,
27+
BeforeFunc: core.BeforeFuncCombine(
28+
createPN(),
29+
createServer("Server"),
30+
createNIC(),
31+
),
32+
Cmd: "scw instance private-nic get server-id={{ .Server.ID }} private-nic-id={{ .NIC.PrivateNic.ID }}",
33+
Check: core.TestCheckCombine(
34+
core.TestCheckGolden(),
35+
),
36+
}))
37+
38+
t.Run("Get from MAC address", core.Test(&core.TestConfig{
39+
Commands: cmds,
40+
BeforeFunc: core.BeforeFuncCombine(
41+
createPN(),
42+
createServer("Server"),
43+
createNIC(),
44+
),
45+
Cmd: "scw instance private-nic get server-id={{ .Server.ID }} private-nic-id={{ .NIC.PrivateNic.MacAddress }}",
46+
Check: core.TestCheckCombine(
47+
core.TestCheckGolden(),
48+
),
49+
}))
50+
}

internal/namespaces/instance/v1/helpers_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,17 @@ func deleteSecurityGroup(metaKey string) core.AfterFunc {
135135
func deleteSnapshot(metaKey string) core.AfterFunc {
136136
return core.ExecAfterCmd("scw instance snapshot delete {{ ." + metaKey + ".Snapshot.ID }}")
137137
}
138+
139+
func createPN() core.BeforeFunc {
140+
return core.ExecStoreBeforeCmd(
141+
"PN",
142+
"scw vpc private-network create",
143+
)
144+
}
145+
146+
func createNIC() core.BeforeFunc {
147+
return core.ExecStoreBeforeCmd(
148+
"NIC",
149+
"scw instance private-nic create server-id={{ .Server.ID }} private-network-id={{ .PN.ID }}",
150+
)
151+
}

internal/namespaces/instance/v1/testdata/test-get-private-nic-get-from-id.cassette.yaml

Lines changed: 1804 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
PrivateNic.ID 528c9810-73cd-4322-bb15-8ae2d9774ed6
4+
PrivateNic.ServerID 72bdf9f6-f398-43fb-94f5-645a7cb2683a
5+
PrivateNic.PrivateNetworkID b2fe1ee6-93ad-474d-b350-b5f3462e1969
6+
PrivateNic.MacAddress 02:00:00:11:e0:f1
7+
PrivateNic.State available
8+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
9+
{
10+
"private_nic": {
11+
"id": "528c9810-73cd-4322-bb15-8ae2d9774ed6",
12+
"server_id": "72bdf9f6-f398-43fb-94f5-645a7cb2683a",
13+
"private_network_id": "b2fe1ee6-93ad-474d-b350-b5f3462e1969",
14+
"mac_address": "02:00:00:11:e0:f1",
15+
"state": "available"
16+
}
17+
}

0 commit comments

Comments
 (0)