Skip to content

Commit

Permalink
Merge pull request #385 from Tinyblargon/#384
Browse files Browse the repository at this point in the history
feat: use same mac address case
  • Loading branch information
Tinyblargon authored Nov 27, 2024
2 parents ea532c4 + 68bb40d commit af1f4e8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
24 changes: 16 additions & 8 deletions proxmox/config_qemu_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -138,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))
}
Expand Down Expand Up @@ -186,6 +193,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)
Expand Down
41 changes: 38 additions & 3 deletions proxmox/config_qemu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()}},
Expand Down Expand Up @@ -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)),
Expand All @@ -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)),
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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`,
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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{})}}})},
Expand All @@ -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})}}})},
},
Expand Down

0 comments on commit af1f4e8

Please sign in to comment.