Seperate Definitions by Sub System

This commit is contained in:
Samuel Lorch 2023-04-13 17:03:11 +02:00
parent 14c8da64cc
commit 72bf96295d
31 changed files with 120 additions and 108 deletions

View file

@ -0,0 +1,91 @@
package network
import (
"encoding/json"
"nfsense.net/nfsense/internal/definitions/common"
)
type Interface struct {
Alias string `json:"alias,omitempty" validate:"min=0,max=3"`
Type InterfaceType `json:"type" validate:"min=0,max=3"`
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"`
Address *common.IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
HardwareDevice *string `json:"hardware_device,omitempty"`
// TODO fix Validator for int pointers with min=0,max=4094
VlanID *uint `json:"vlan_id,omitempty"`
VlanParent *string `json:"vlan_parent,omitempty"`
BondMembers *[]string `json:"bond_members,omitempty"`
BridgeMembers *[]string `json:"bridge_members,omitempty"`
Comment string `json:"comment,omitempty"`
}
type InterfaceType int
const (
Hardware InterfaceType = iota
Vlan
Bond
Bridge
)
func (t InterfaceType) String() string {
return [...]string{"hardware", "vlan", "bond", "bridge"}[t]
}
func (t *InterfaceType) FromString(input string) InterfaceType {
return map[string]InterfaceType{
"hardware": Hardware,
"vlan": Vlan,
"bond": Bond,
"bridge": Bridge,
}[input]
}
func (t InterfaceType) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *InterfaceType) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*t = t.FromString(s)
return nil
}
type InterfaceAddressingMode int
const (
None InterfaceAddressingMode = iota
Static
Dhcp
)
func (t InterfaceAddressingMode) String() string {
return [...]string{"none", "static", "dhcp"}[t]
}
func (t *InterfaceAddressingMode) FromString(input string) InterfaceAddressingMode {
return map[string]InterfaceAddressingMode{
"none": None,
"static": Static,
"dhcp": Dhcp,
}[input]
}
func (t InterfaceAddressingMode) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
func (t *InterfaceAddressingMode) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*t = t.FromString(s)
return nil
}

View file

@ -0,0 +1,6 @@
package network
type Network struct {
Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"`
StaticRoutes []StaticRoute `json:"static_routes" validate:"required,dive"`
}

View file

@ -0,0 +1,15 @@
package network
import (
"net/netip"
"nfsense.net/nfsense/internal/definitions/common"
)
type StaticRoute struct {
Name string `json:"name,omitempty"`
Interface string `json:"interface,omitempty"`
Gateway netip.Addr `json:"gateway,omitempty"`
Destination common.IPNet `json:"destination,omitempty"`
Metric uint `json:"metric,omitempty"`
}