create validators for each part that would need it

This commit is contained in:
Matthew Stobbs 2024-09-26 13:34:17 -06:00
parent c8ff16ec22
commit 0e4757c7b7
4 changed files with 22 additions and 3 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}