2024-09-14 04:21:14 +00:00
|
|
|
package reservation
|
|
|
|
|
|
|
|
import (
|
2024-09-24 22:58:42 +00:00
|
|
|
"log/slog"
|
|
|
|
|
2024-09-14 04:21:14 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/types"
|
2024-09-26 19:34:17 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/validators/ipv4"
|
2024-09-24 22:58:42 +00:00
|
|
|
"git.staur.ca/stobbsm/kea-manage/lib/validators/macaddr"
|
2024-09-26 19:34:17 +00:00
|
|
|
typeValidator "git.staur.ca/stobbsm/kea-manage/lib/validators/types"
|
2024-09-14 04:21:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ReservationV4 struct {
|
2024-09-24 22:58:42 +00:00
|
|
|
IdentifierType types.Identifier
|
|
|
|
SubnetID int
|
|
|
|
Identifier string
|
|
|
|
Ipv4 string
|
|
|
|
Hostname string
|
2024-09-26 19:34:17 +00:00
|
|
|
typeName string
|
2024-09-14 04:21:14 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 22:58:42 +00:00
|
|
|
func ReserveV4MacAddr(identifierType string, identifier string, addr string, hostname string, subnet int) *ReservationV4 {
|
2024-09-14 04:21:14 +00:00
|
|
|
r := &ReservationV4{
|
2024-09-26 19:34:17 +00:00
|
|
|
typeName: identifierType,
|
2024-09-24 23:10:39 +00:00
|
|
|
IdentifierType: types.LookupIdentifier(identifierType),
|
2024-09-24 22:58:42 +00:00
|
|
|
SubnetID: subnet,
|
|
|
|
Identifier: identifier,
|
|
|
|
Ipv4: addr,
|
|
|
|
Hostname: hostname,
|
2024-09-14 04:21:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2024-09-24 22:58:42 +00:00
|
|
|
|
|
|
|
// 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 {
|
2024-09-26 19:34:17 +00:00
|
|
|
if typeValidator.IsValid(r.typeName) {
|
2024-09-24 22:58:42 +00:00
|
|
|
slog.Error("validate", slog.Any("reservationV4.type", "invalid"))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-09-26 19:34:17 +00:00
|
|
|
if !ipv4.IsValid(r.Ipv4) {
|
2024-09-24 22:58:42 +00:00
|
|
|
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
|
|
|
|
}
|