Compare commits

..

4 Commits

Author SHA1 Message Date
Gibheer c5f0a86886 add endpoints for ipblocks/containers 2021-05-21 17:51:35 +02:00
Gibheer 08808fe984 add IPVersion type to properly parse a 4 or 6 2021-05-21 17:51:35 +02:00
Gibheer d746ad7ac0 add function to get subnets to schema
This also adds a view to get a list of all containers and their free
space in between.
This is needed for ippool_list to get a nice overview over everything.

The code of the function is based on
https://pkg.go.dev/inet.af/netaddr#IPRange.Prefixes

Many thanks to Brad Fitzpatrick for the awesome code and changelog to
make it possible to understand what is going on.
2021-05-21 17:50:31 +02:00
Gibheer c2bf09fd9e add view to get container hierarchy 2021-05-21 17:50:19 +02:00
2 changed files with 21 additions and 1 deletions

View File

@ -68,7 +68,11 @@ func main() {
s.Register("layer3domain_list", layer3DomainList)
s.Register("layer3domain_get_attr", layer3DomainGetAttr)
s.Register("layer3domain_set_attr", layer3DomainSetAttr)
s.Register("ipblock_create", ipblockCreate)
s.Register("ipblock_create", containerCreate)
s.Register("ipblock_remove", containerDelete)
s.Register("ipblock_list", containerList)
s.Register("ipblock_set_attr", containerSetAttr)
s.Register("ipblock_get_attr", containerGetAttr)
s.Register("ippool_create", PoolCreate)
s.Register("ippool_delete", PoolDelete)
s.Register("ippool_list", PoolList)

View File

@ -5,6 +5,7 @@ import (
"database/sql/driver"
"fmt"
"net"
"strconv"
)
type (
@ -12,6 +13,8 @@ type (
Subnet net.IPNet
// IP is used to parse an IP parameter.
IP net.IP
// IPVersion represents the two IP versions currently in use.
IPVersion int
)
// UnmarshalJSON parses a value into a subnet.
@ -69,3 +72,16 @@ func (i *IP) UnmarshalJSON(in []byte) error {
*i = IP(ip)
return nil
}
// UnmarshalJSON parses the incoming version from json.
func (v *IPVersion) UnmarshalJSON(in []byte) error {
raw, err := strconv.Atoi(string(in))
if err != nil {
return fmt.Errorf("can't parse ip version: %#v", err)
}
if raw != 4 && raw != 6 {
return fmt.Errorf("only version 4 and 6 are supported")
}
*v = IPVersion(raw)
return nil
}