create validators for each part that would need it
This commit is contained in:
parent
c8ff16ec22
commit
0e4757c7b7
@ -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
|
||||
}
|
||||
|
8
lib/validators/ipv4/ipv4.go
Normal file
8
lib/validators/ipv4/ipv4.go
Normal 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
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
7
lib/validators/types/types.go
Normal file
7
lib/validators/types/types.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user