kea-manage/ipv4/reservation/reservation.go
Matthew Stobbs 4d6d785924 add stringer to generate constant strings
- doing work on validation for each possible field
2024-09-24 16:58:42 -06:00

53 lines
1.3 KiB
Go

package reservation
import (
"log/slog"
"net"
"git.staur.ca/stobbsm/kea-manage/lib/types"
"git.staur.ca/stobbsm/kea-manage/lib/validators/macaddr"
)
type ReservationV4 struct {
IdentifierType types.Identifier
SubnetID int
Identifier string
Ipv4 string
Hostname string
}
func ReserveV4MacAddr(identifierType string, identifier string, addr string, hostname string, subnet int) *ReservationV4 {
r := &ReservationV4{
IdentifierType: types.Lookup(identifierType),
SubnetID: subnet,
Identifier: identifier,
Ipv4: addr,
Hostname: hostname,
}
return r
}
// Validate implements the Validator interface. Returns true when valid, false when not.
// Will print message to the slog Error channel when an error is found to provide more information
// This does not validate against the database, just validates that the written configuration
// is correct
func (r *ReservationV4) Validate() bool {
if r.IdentifierType == 255 {
slog.Error("validate", slog.Any("reservationV4.type", "invalid"))
return false
}
if net.ParseIP(r.Ipv4) == nil {
slog.Error("validate", slog.Any("reservationV4.ipv4", r.Ipv4))
return false
}
if !macaddr.IsValid(r.Identifier) {
slog.Error("validate", slog.Any("reservationV4.identifier", r.Identifier))
return false
}
return true
}