From 9f13e0798901aaece6f66705b4746c0834d11ccb Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Thu, 5 Sep 2024 13:33:17 +0200 Subject: [PATCH 1/2] Add new NetworkAdapter attributes New attributes for describing a NetworkAdapter. Currently only relevant for NetworkAdapters in KVM. `MacAddress`, `IsConnected` and `IsPrimary`. Add `Name` attribute for create and edit parameters. Add test for KVM specific Create. --- networkadapters.go | 5 +++++ networkadapters_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/networkadapters.go b/networkadapters.go index 896598b..b06d18a 100644 --- a/networkadapters.go +++ b/networkadapters.go @@ -15,6 +15,9 @@ type NetworkAdapter struct { AdapterType string `json:"adaptertype,omitempty"` Bandwidth int `json:"bandwidth"` ID string `json:"networkadapterid"` + IsConnected bool `json:"isconnected,omitempty"` + IsPrimary bool `json:"isprimary,omitempty"` + MacAddress string `json:"macaddress,omitempty"` Name string `json:"name"` NetworkID string `json:"networkid"` ServerID string `json:"serverid"` @@ -35,6 +38,7 @@ func (na *NetworkAdapter) IsReady() bool { type CreateNetworkAdapterParams struct { AdapterType string `json:"adaptertype,omitempty"` Bandwidth int `json:"bandwidth,omitempty"` + Name string `json:"name,omitempty"` NetworkID string `json:"networkid,omitempty"` ServerID string `json:"serverid"` } @@ -42,6 +46,7 @@ type CreateNetworkAdapterParams struct { // EditNetworkAdapterParams is used when editing an existing network adapter type EditNetworkAdapterParams struct { Bandwidth int `json:"bandwidth,omitempty"` + Name string `json:"name,omitempty"` NetworkID string `json:"networkid,omitempty"` } diff --git a/networkadapters_test.go b/networkadapters_test.go index ae0061f..169af3a 100644 --- a/networkadapters_test.go +++ b/networkadapters_test.go @@ -46,6 +46,32 @@ func TestNetworkAdaptersCreate(t *testing.T) { assert.Equal(t, "mynetwork", networkadapter.NetworkID, "networkadapter Description is correct") } +func TestNetworkAdaptersCreate_KVM(t *testing.T) { + c := &mockClient{body: `{ "response": { "networkadapter": + { "bandwidth": 1000, "name": "Adapter Example", "isprimary": false, "isconnected": true, + "networkid": "79c67265-a9a8-4607-b2b5-7377a6b6ebf7", + "networkadapterid": "ab12cd34-dcba-0123-abcd-abc123456789", + "serverid": "kvm123456" }}}`} + n := NetworkAdapterService{client: c} + + params := CreateNetworkAdapterParams{ + Bandwidth: 1000, + NetworkID: "79c67265-a9a8-4607-b2b5-7377a6b6ebf7", + ServerID: "kvm123456", + Name: "Adapter Example", + } + + networkadapter, _ := n.Create(context.Background(), params) + + assert.Equal(t, "POST", c.lastMethod, "method is used correct") + assert.Equal(t, "networkadapter/create", c.lastPath, "path used is correct") + assert.Equal(t, "kvm123456", networkadapter.ServerID, "networkadapter ServerID is correct") + assert.Equal(t, "Adapter Example", networkadapter.Name, "networkadapter Name is correct") + assert.Equal(t, true, networkadapter.IsConnected, "networkadapter IsConnected is correct") + assert.Equal(t, false, networkadapter.IsPrimary, "networkadapter IsPrimary is correct") + assert.Equal(t, "79c67265-a9a8-4607-b2b5-7377a6b6ebf7", networkadapter.NetworkID, "networkadapter networkID is correct") +} + func TestNetworkAdaptersDestroy(t *testing.T) { c := &mockClient{} n := NetworkAdapterService{client: c} From eb0cce2a39b906d27e53f5d6316ad661e6297125 Mon Sep 17 00:00:00 2001 From: Andreas Eriksson Date: Thu, 5 Sep 2024 13:45:35 +0200 Subject: [PATCH 2/2] Fix NetworkAdapter test typo --- networkadapters_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/networkadapters_test.go b/networkadapters_test.go index 169af3a..0742e86 100644 --- a/networkadapters_test.go +++ b/networkadapters_test.go @@ -43,7 +43,7 @@ func TestNetworkAdaptersCreate(t *testing.T) { assert.Equal(t, "networkadapter/create", c.lastPath, "path used is correct") assert.Equal(t, "wps123456", networkadapter.ServerID, "networkadapter ServerID is correct") assert.Equal(t, "Network Adapter 2", networkadapter.Name, "networkadapter Name is correct") - assert.Equal(t, "mynetwork", networkadapter.NetworkID, "networkadapter Description is correct") + assert.Equal(t, "mynetwork", networkadapter.NetworkID, "networkadapter NetworkID is correct") } func TestNetworkAdaptersCreate_KVM(t *testing.T) {