diff --git a/ipv4/reservation/reservation.go b/ipv4/reservation/reservation.go index 5e9f60d..cb4854c 100644 --- a/ipv4/reservation/reservation.go +++ b/ipv4/reservation/reservation.go @@ -2,10 +2,11 @@ package reservation import ( "log/slog" - "net" "git.staur.ca/stobbsm/kea-manage/lib/types" + "git.staur.ca/stobbsm/kea-manage/lib/validators/ipv4" "git.staur.ca/stobbsm/kea-manage/lib/validators/macaddr" + typeValidator "git.staur.ca/stobbsm/kea-manage/lib/validators/types" ) type ReservationV4 struct { @@ -14,10 +15,12 @@ type ReservationV4 struct { Identifier string Ipv4 string Hostname string + typeName string } func ReserveV4MacAddr(identifierType string, identifier string, addr string, hostname string, subnet int) *ReservationV4 { r := &ReservationV4{ + typeName: identifierType, IdentifierType: types.LookupIdentifier(identifierType), SubnetID: subnet, Identifier: identifier, @@ -33,12 +36,12 @@ func ReserveV4MacAddr(identifierType string, identifier string, addr string, hos // This does not validate against the database, just validates that the written configuration // is correct func (r *ReservationV4) Validate() bool { - if r.IdentifierType == 255 { + if typeValidator.IsValid(r.typeName) { slog.Error("validate", slog.Any("reservationV4.type", "invalid")) return false } - if net.ParseIP(r.Ipv4) == nil { + if !ipv4.IsValid(r.Ipv4) { slog.Error("validate", slog.Any("reservationV4.ipv4", r.Ipv4)) return false } diff --git a/lib/validators/ipv4/ipv4.go b/lib/validators/ipv4/ipv4.go new file mode 100644 index 0000000..e1a2481 --- /dev/null +++ b/lib/validators/ipv4/ipv4.go @@ -0,0 +1,8 @@ +package ipv4 + +import "net" + +// IsValid return true if the given IP is a valid IP, false otherwise +func IsValid(ipv4 string) bool { + return net.ParseIP(ipv4) != nil +} diff --git a/lib/validators/macaddr/macaddr.go b/lib/validators/macaddr/macaddr.go index dc8fba4..97a7569 100644 --- a/lib/validators/macaddr/macaddr.go +++ b/lib/validators/macaddr/macaddr.go @@ -4,6 +4,7 @@ import "regexp" const rxValidMac = `^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$` +// IsValid returns true if the macaddress appears valid, false otherwise func IsValid(mac string) bool { return regexp.MustCompile(rxValidMac).MatchString(mac) } diff --git a/lib/validators/types/types.go b/lib/validators/types/types.go new file mode 100644 index 0000000..3c8dee4 --- /dev/null +++ b/lib/validators/types/types.go @@ -0,0 +1,7 @@ +package types + +import "git.staur.ca/stobbsm/kea-manage/lib/types" + +func IsValid(identifierType string) bool { + return types.LookupIdentifier(identifierType) != 255 +}