Use HardwareAddress (MAC) for Identifing Hardware Interfaces

This commit is contained in:
Samuel Lorch 2023-04-08 12:54:42 +02:00
parent d9ecfae454
commit 15de5fb5bb
4 changed files with 37 additions and 7 deletions

View file

@ -44,7 +44,7 @@ export const editTypes: { [key: string]: { [key: string]: any } } = {
fields: [ fields: [
{ key: "name", label: "Name", as: "TextBox", default: "placeholder" }, { key: "name", label: "Name", as: "TextBox", default: "placeholder" },
{ key: "type", label: "Type", as: "PillBar", props: { options: [{ name: 'Hardware', key: 'hardware' }, { name: 'VLAN', key: 'vlan' }, { name: 'Bond', key: 'bond' }, { name: 'Bridge', key: 'bridge' }] } }, { key: "type", label: "Type", as: "PillBar", props: { options: [{ name: 'Hardware', key: 'hardware' }, { name: 'VLAN', key: 'vlan' }, { name: 'Bond', key: 'bond' }, { name: 'Bridge', key: 'bridge' }] } },
{ key: "hardware_interface", label: "Hardware Interface", as: "TextBox", enabled: (values: any) => (values["type"] == 'hardware') }, { key: "hardware_address", label: "Hardware Address", as: "TextBox", enabled: (values: any) => (values["type"] == 'hardware') },
{ key: "vlan_parent", label: "VLAN Parent", as: "TextBox", enabled: (values: any) => (values["type"] == 'vlan') }, { key: "vlan_parent", label: "VLAN Parent", as: "TextBox", enabled: (values: any) => (values["type"] == 'vlan') },
{ key: "vlan_id", label: "VLAN ID", as: "NumberBox", props: { min: 1, max: 4094 }, enabled: (values: any) => (values["type"] == 'vlan') }, { key: "vlan_id", label: "VLAN ID", as: "NumberBox", props: { min: 1, max: 4094 }, enabled: (values: any) => (values["type"] == 'vlan') },
{ key: "bond_members", label: "Bond Members", as: "TextBox", enabled: (values: any) => (values["type"] == 'bond') }, { key: "bond_members", label: "Bond Members", as: "TextBox", enabled: (values: any) => (values["type"] == 'bond') },

View file

@ -0,0 +1,30 @@
package definitions
import (
"encoding/json"
"net"
)
type HardwareAddress struct {
net.HardwareAddr
}
// MarshalJSON for IPCIDR
func (i HardwareAddress) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
// UnmarshalJSON for IPCIDR
func (i *HardwareAddress) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
mac, err := net.ParseMAC(s)
if err != nil {
return err
}
i.HardwareAddr = mac
return nil
}

View file

@ -5,11 +5,11 @@ import (
) )
type Interface struct { type Interface struct {
Alias string `json:"alias,omitempty" validate:"min=0,max=3"` Alias string `json:"alias,omitempty" validate:"min=0,max=3"`
Type InterfaceType `json:"type" validate:"min=0,max=3"` Type InterfaceType `json:"type" validate:"min=0,max=3"`
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"` AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"`
Address *IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"` Address *IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
HardwareInterface *string `json:"hardware_interface,omitempty"` HardwareAddress *HardwareAddress `json:"hardware_address,omitempty"`
// TODO fix Validator for int pointers with min=0,max=4094 // TODO fix Validator for int pointers with min=0,max=4094
VlanID *uint `json:"vlan_id,omitempty"` VlanID *uint `json:"vlan_id,omitempty"`
VlanParent *string `json:"vlan_parent,omitempty"` VlanParent *string `json:"vlan_parent,omitempty"`

View file

@ -1,5 +1,5 @@
[Match] [Match]
OriginalName= {{ .HardwareInterface }} PermanentMACAddress= {{ .HardwareAddress }}
[Link] [Link]
Name= {{ .Name }} Name= {{ .Name }}