From 1640bcb484a80611b48e2d58528b40a7aa1eed2e Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Wed, 27 Nov 2024 00:11:21 +0100 Subject: [PATCH 1/2] feat: use same mac address case when the binary value of the mac address does not change re-use the current string representation. --- proxmox/config_qemu_network.go | 20 ++++++++++++----- proxmox/config_qemu_test.go | 41 +++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/proxmox/config_qemu_network.go b/proxmox/config_qemu_network.go index 656a93ec..10665d6f 100644 --- a/proxmox/config_qemu_network.go +++ b/proxmox/config_qemu_network.go @@ -57,6 +57,7 @@ type QemuNetworkInterface struct { RateLimitKBps *QemuNetworkRate `json:"rate,omitempty"` NativeVlan *Vlan `json:"native_vlan,omitempty"` TaggedVlans *Vlans `json:"tagged_vlans,omitempty"` + mac string } func (config QemuNetworkInterface) mapToApi(current *QemuNetworkInterface) (settings string) { @@ -70,12 +71,20 @@ func (config QemuNetworkInterface) mapToApi(current *QemuNetworkInterface) (sett } builder.WriteString(model) if config.MAC != nil { - mac = config.MAC.String() + mac = config.MAC.String() // Returns a lowercase MAC address + if mac == strings.ToLower(current.mac) { + mac = current.mac + } else { + mac = strings.ToUpper(mac) + } + builder.WriteString("=" + mac) } else if current.MAC != nil { - mac = current.MAC.String() - } - if mac != "" { - builder.WriteString("=" + strings.ToUpper(mac)) + if current.mac != "" { + mac = current.mac + } else { + mac = strings.ToUpper(current.MAC.String()) + } + builder.WriteString("=" + mac) } if config.Bridge != nil { builder.WriteString(",bridge=" + *config.Bridge) @@ -186,6 +195,7 @@ func (QemuNetworkInterface) mapToSDK(rawParams string) (config QemuNetworkInterf model = QemuNetworkModel(modelAndMac[0]) config.Model = &model mac, _ := net.ParseMAC(modelAndMac[1]) + config.mac = modelAndMac[1] config.MAC = &mac } params := splitStringOfSettings(rawParams) diff --git a/proxmox/config_qemu_test.go b/proxmox/config_qemu_test.go index affe4a68..2ff6dfc1 100644 --- a/proxmox/config_qemu_test.go +++ b/proxmox/config_qemu_test.go @@ -3642,9 +3642,21 @@ func Test_ConfigQemu_mapToAPI(t *testing.T) { currentConfig: ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID29: networkInterface()}}, output: map[string]interface{}{"net29": "virtio=52:54:00:12:34:56,bridge=vmbr0,link_down=1,mtu=1500,queues=5,rate=0.045,tag=23,trunks=12;23;45"}}, {name: `MAC replace`, - config: &ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: QemuNetworkInterface{MAC: util.Pointer(net.HardwareAddr(parseMAC("00:11:22:33:44:55")))}}}, + config: &ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: QemuNetworkInterface{MAC: util.Pointer(net.HardwareAddr(parseMAC("BC:24:11:C2:75:20")))}}}, currentConfig: ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: networkInterface()}}, - output: map[string]interface{}{"net28": "virtio=00:11:22:33:44:55,bridge=vmbr0,firewall=1,link_down=1,mtu=1500,queues=5,rate=0.045,tag=23,trunks=12;23;45"}}, + output: map[string]interface{}{"net28": "virtio=BC:24:11:C2:75:20,bridge=vmbr0,firewall=1,link_down=1,mtu=1500,queues=5,rate=0.045,tag=23,trunks=12;23;45"}}, + {name: `MAC replace binary match`, + config: &ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: QemuNetworkInterface{MAC: util.Pointer(net.HardwareAddr(parseMAC("BC:24:11:C2:75:20")))}}}, + currentConfig: ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: QemuNetworkInterface{ + MAC: util.Pointer(net.HardwareAddr(parseMAC("bc:24:11:C2:75:20"))), + mac: "bc:24:11:C2:75:20"}}}, + output: map[string]interface{}{"net28": "=bc:24:11:C2:75:20"}}, + {name: `MAC no update mixed case`, + config: &ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: QemuNetworkInterface{}}}, + currentConfig: ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID28: QemuNetworkInterface{ + MAC: util.Pointer(net.HardwareAddr(parseMAC("bc:24:11:C2:75:20"))), + mac: "bc:24:11:C2:75:20"}}}, + output: map[string]interface{}{"net28": "=bc:24:11:C2:75:20"}}, {name: `MTU replace`, config: &ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID27: QemuNetworkInterface{MTU: util.Pointer(QemuMTU{Value: MTU(1400)})}}}, currentConfig: ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID27: networkInterface()}}, @@ -6459,12 +6471,13 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { {category: `Networks`, tests: []test{ {name: `all e1000`, - input: map[string]interface{}{"net0": "e1000=BC:24:11:E1:BB:5D,bridge=vmbr0,mtu=1395,firewall=1,link_down=1,queues=23,rate=1.53,tag=12,trunks=34;18;25"}, + input: map[string]interface{}{"net0": "e1000=BC:24:11:E1:BB:5d,bridge=vmbr0,mtu=1395,firewall=1,link_down=1,queues=23,rate=1.53,tag=12,trunks=34;18;25"}, output: baseConfig(ConfigQemu{Networks: QemuNetworkInterfaces{QemuNetworkInterfaceID0: QemuNetworkInterface{ Bridge: util.Pointer("vmbr0"), Connected: util.Pointer(false), Firewall: util.Pointer(true), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5d", // MTU is only supported for virtio Model: util.Pointer(QemuNetworkModelE1000), MultiQueue: util.Pointer(QemuNetworkQueue(23)), @@ -6478,6 +6491,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(false), Firewall: util.Pointer(true), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", MTU: util.Pointer(QemuMTU{Value: 1395}), Model: util.Pointer(QemuNetworkModelVirtIO), MultiQueue: util.Pointer(QemuNetworkQueue(23)), @@ -6491,6 +6505,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Model and Mac`, @@ -6499,6 +6514,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Connected false`, @@ -6507,6 +6523,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(false), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Connected true`, @@ -6515,6 +6532,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Connected unset`, @@ -6523,6 +6541,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Firwall true`, @@ -6531,6 +6550,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(true), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Firwall false`, @@ -6539,6 +6559,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `Firwall unset`, @@ -6547,6 +6568,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `MTU value`, @@ -6555,6 +6577,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", MTU: &QemuMTU{Value: 1500}, Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6564,6 +6587,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", MTU: &QemuMTU{Inherit: true}, Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6573,6 +6597,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `MultiQueue enable`, @@ -6582,6 +6607,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Firewall: util.Pointer(false), MultiQueue: util.Pointer(QemuNetworkQueue(1)), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `RateLimitKBps disable`, @@ -6590,6 +6616,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{})}}})}, {name: `RateLimitKBps 0.001`, @@ -6598,6 +6625,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), RateLimitKBps: util.Pointer(QemuNetworkRate(1)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6607,6 +6635,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), RateLimitKBps: util.Pointer(QemuNetworkRate(10)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6616,6 +6645,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), RateLimitKBps: util.Pointer(QemuNetworkRate(100)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6625,6 +6655,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), RateLimitKBps: util.Pointer(QemuNetworkRate(1000)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6634,6 +6665,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), RateLimitKBps: util.Pointer(QemuNetworkRate(1264)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6643,6 +6675,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), RateLimitKBps: util.Pointer(QemuNetworkRate(15264)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6652,6 +6685,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), NativeVlan: util.Pointer(Vlan(1)), TaggedVlans: util.Pointer(Vlans{})}}})}, @@ -6661,6 +6695,7 @@ func Test_ConfigQemu_mapToStruct(t *testing.T) { Connected: util.Pointer(true), Firewall: util.Pointer(false), MAC: util.Pointer(parseMAC("BC:24:11:E1:BB:5D")), + mac: "BC:24:11:E1:BB:5D", Model: util.Pointer(QemuNetworkModelVirtIO), TaggedVlans: util.Pointer(Vlans{1, 63, 21})}}})}, }, From 68bb40da69ffd28a406de326ada2f77b8f1020da Mon Sep 17 00:00:00 2001 From: Tinyblargon <76069640+Tinyblargon@users.noreply.github.com> Date: Wed, 27 Nov 2024 22:02:14 +0100 Subject: [PATCH 2/2] refactor: remove extra check for `model` --- proxmox/config_qemu_network.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/proxmox/config_qemu_network.go b/proxmox/config_qemu_network.go index 10665d6f..508443b4 100644 --- a/proxmox/config_qemu_network.go +++ b/proxmox/config_qemu_network.go @@ -147,12 +147,10 @@ func (config QemuNetworkInterface) mapToApi(current *QemuNetworkInterface) (sett // Create if config.Model != nil { model = config.Model.String() + builder.WriteString(config.Model.String()) } if config.MAC != nil { mac = config.MAC.String() - } - if model != "" { - builder.WriteString(model) if mac != "" { builder.WriteString("=" + strings.ToUpper(mac)) }