Fix Address Type

This commit is contained in:
Samuel Lorch 2023-04-08 01:36:20 +02:00
parent 2c1df2a4ad
commit d9ecfae454
2 changed files with 33 additions and 1 deletions

View file

@ -8,7 +8,7 @@ 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 *IPNet `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
Address *IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
HardwareInterface *string `json:"hardware_interface,omitempty"`
// TODO fix Validator for int pointers with min=0,max=4094
VlanID *uint `json:"vlan_id,omitempty"`

View file

@ -0,0 +1,32 @@
package definitions
import (
"encoding/json"
"net"
)
// IPCIDR is IP Address with the mask in CIDR format
type IPCIDR struct {
net.IPNet
}
// MarshalJSON for IPCIDR
func (i IPCIDR) MarshalJSON() ([]byte, error) {
return json.Marshal(i.String())
}
// UnmarshalJSON for IPCIDR
func (i *IPCIDR) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
ip, ipnet, err := net.ParseCIDR(s)
if err != nil {
return err
}
i.IPNet = *ipnet
i.IPNet.IP = ip
return nil
}