Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next available IP for recordAAAA, recordHost #244

Merged
merged 10 commits into from
Oct 3, 2024
Merged
3 changes: 3 additions & 0 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ var _ IBObjectManager = new(ObjectManager)
type IBObjectManager interface {
GetDNSView(name string) (*View, error)
AllocateIP(netview string, cidr string, ipAddr string, isIPv6 bool, macOrDuid string, name string, comment string, eas EA) (*FixedAddress, error)
AllocateNextAvailableIp(name string, objectType string, objectParams map[string]string, params map[string][]string, useEaInheritance bool, isIpv6 bool, ea EA, comment string, disable bool, n *int) (interface{}, error)
AllocateNetwork(netview string, cidr string, isIPv6 bool, prefixLen uint, comment string, eas EA) (network *Network, err error)
AllocateNetworkByEA(netview string, isIPv6 bool, comment string, eas EA, eaMap map[string]string, prefixLen int, object string) (network *Network, err error)
AllocateNetworkContainer(netview string, cidr string, isIPv6 bool, prefixLen uint, comment string, eas EA) (netContainer *NetworkContainer, err error)
AllocateNetworkContainerByEA(netview string, isIPv6 bool, comment string, eas EA, eaMap map[string]string, prefixLen int) (*NetworkContainer, error)
CreateARecord(netView string, dnsView string, name string, cidr string, ipAddr string, ttl uint32, useTTL bool, comment string, ea EA) (*RecordA, error)
CreateAAAARecord(netView string, dnsView string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordAAAA, error)
CreateZoneAuth(fqdn string, ea EA) (*ZoneAuth, error)
Expand Down
73 changes: 73 additions & 0 deletions object_manager_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,79 @@ func (objMgr *ObjectManager) AllocateNetwork(
return
}

func (objMgr *ObjectManager) AllocateNextAvailableIp(
name string,
objectType string,
objectParams map[string]string,
params map[string][]string,
useEaInheritance bool,
isIpv6 bool,
ea EA,
comment string,
disable bool, n *int) (interface{}, error) {

networkIp := NewIpNextAvailable(name, objectType, objectParams, params, useEaInheritance, isIpv6, ea, comment, disable, n)

ref, err := objMgr.connector.CreateObject(networkIp)
if err != nil {
return nil, err
}

switch objectType {
case "record:a":
return objMgr.GetARecordByRef(ref)
case "record:aaaa":
return objMgr.GetAAAARecordByRef(ref)
case "record:host":
return objMgr.GetHostRecordByRef(ref)
}

return nil, err
}

func (objMgr *ObjectManager) AllocateNetworkByEA(
netview string, isIPv6 bool, comment string, eas EA, eaMap map[string]string, prefixLen int, object string) (network *Network, err error) {

var (
containerObject string
objectType string
)

objectType = getNetworkObjectType(isIPv6, "network", "ipv6network")

if object == "network" {
containerObject = getNetworkObjectType(isIPv6, "network", "ipv6network")
} else {
containerObject = getNetworkObjectType(isIPv6, "networkcontainer", "ipv6networkcontainer")
}

nextAvailableNetworkInfo := NetworkContainerNextAvailableInfo{
Function: "next_available_network",
ResultField: "networks",
Object: containerObject,
ObjectParams: eaMap,
Params: map[string]uint{"cidr": uint(prefixLen)},
}

nextAvailableNetwork := NetworkContainerNextAvailable{
Network: &nextAvailableNetworkInfo,
objectType: objectType,
Comment: comment,
Ea: eas,
NetviewName: netview,
}

ref, err := objMgr.connector.CreateObject(&nextAvailableNetwork)
if err == nil {
if isIPv6 {
network, err = BuildIPv6NetworkFromRef(ref)
} else {
network, err = BuildNetworkFromRef(ref)
}
}
return
}

func (objMgr *ObjectManager) GetNetwork(netview string, cidr string, isIPv6 bool, ea EA) (*Network, error) {
if netview != "" && cidr != "" {
var res []Network
Expand Down
39 changes: 39 additions & 0 deletions object_manager_network_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,45 @@ func (objMgr *ObjectManager) AllocateNetworkContainer(
}
}

func (objMgr *ObjectManager) AllocateNetworkContainerByEA(
netview string, isIPv6 bool, comment string, eas EA, eaMap map[string]string, prefixLen int) (*NetworkContainer, error) {

//if isIPv6 {
// object = "ipv6networkcontainer"
//} else {
// object = "networkcontainer"
//}
var object string
object = getNetworkObjectType(isIPv6, "networkcontainer", "ipv6networkcontainer")

nextAvailableNetworkInfo := NetworkContainerNextAvailableInfo{
Function: "next_available_network",
ResultField: "networks",
Object: object,
ObjectParams: eaMap,
Params: map[string]uint{"cidr": uint(prefixLen)},
}

net := NetworkContainerNextAvailable{
Network: &nextAvailableNetworkInfo,
objectType: object,
Comment: comment,
Ea: eas,
NetviewName: netview,
}
fmt.Println("net: ", net)
ref, err := objMgr.connector.CreateObject(&net)

if err != nil {
return nil, err
}
if isIPv6 {
return BuildIPv6NetworkContainerFromRef(ref)
} else {
return BuildNetworkContainerFromRef(ref)
}
}

func (objMgr *ObjectManager) DeleteNetworkContainer(ref string) (string, error) {
ncRegExp := regexp.MustCompile("^(ipv6)?networkcontainer\\/.+")
if !ncRegExp.MatchString(ref) {
Expand Down
84 changes: 84 additions & 0 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,90 @@ func (SingleRequest) ObjectType() string {
return "request"
}

type NextavailableIPv4Addrs struct {
NextavailableIPv4Addr IpNextAvailableInfo `json:"ipv4addr,omitempty"`
}

type NextavailableIPv6Addrs struct {
NextavailableIPv6Addr IpNextAvailableInfo `json:"ipv6addr,omitempty"`
}

type IpNextAvailable struct {
IBBase `json:"-"`
objectType string
Name string `json:"name"`
NextAvailableIPv4Addr *IpNextAvailableInfo `json:"ipv4addr,omitempty"`
NextAvailableIPv6Addr *IpNextAvailableInfo `json:"ipv6addr,omitempty"`
NextAvailableIPv4Addrs []NextavailableIPv4Addrs `json:"ipv4addrs,omitempty"`
NextAvailableIPv6Addrs []NextavailableIPv6Addrs `json:"ipv6addrs,omitempty"`
Comment string `json:"comment"`
Ea EA `json:"extattrs"`
Disable bool `json:"disable,omitempty"`
//NetviewName string `json:"network_view,omitempty"`
}

func (ni *IpNextAvailable) ObjectType() string {
return ni.objectType
}

type IpNextAvailableInfo struct {
Function string `json:"_object_function"`
ResultField string `json:"_result_field"`
Object string `json:"_object"`
ObjectParams map[string]string `json:"_object_parameters"`
Params map[string][]string `json:"_parameters"`
NetviewName string `json:"network_view,omitempty"`
UseEaInheritance bool `json:"use_for_ea_inheritance"`
}

func NewIpNextAvailableInfo(objectParams map[string]string, params map[string][]string, useEaInheritance bool, isIpv6 bool) *IpNextAvailableInfo {
nextAvailableIpInfo := IpNextAvailableInfo{
Function: "next_available_ip",
ResultField: "ips",
ObjectParams: objectParams,
Params: params,
UseEaInheritance: useEaInheritance,
}

if isIpv6 {
nextAvailableIpInfo.Object = "ipv6network"
} else {
nextAvailableIpInfo.Object = "network"
}

return &nextAvailableIpInfo
}

func NewIpNextAvailable(name string, objectType string, objectParams map[string]string, params map[string][]string,
useEaInheritance bool, isIpv6 bool, ea EA, comment string, disable bool, n *int) *IpNextAvailable {
nextAvailableIP := IpNextAvailable{
Name: name,
objectType: objectType,
Ea: ea,
Comment: comment,
Disable: disable,
}
if n != nil && *n >= 1 {
ipInfo := make([]IpNextAvailableInfo, *n)
for i := 0; i < *n; i++ {
ipInfo[i] = *NewIpNextAvailableInfo(objectParams, params, useEaInheritance, isIpv6)
if isIpv6 {
nextAvailableIP.NextAvailableIPv6Addrs = append(nextAvailableIP.NextAvailableIPv6Addrs, NextavailableIPv6Addrs{NextavailableIPv6Addr: ipInfo[i]})
} else {
nextAvailableIP.NextAvailableIPv4Addrs = append(nextAvailableIP.NextAvailableIPv4Addrs, NextavailableIPv4Addrs{NextavailableIPv4Addr: ipInfo[i]})
}
}
} else {
if isIpv6 {
nextAvailableIP.NextAvailableIPv6Addr = NewIpNextAvailableInfo(objectParams, params, useEaInheritance, isIpv6)
} else {
nextAvailableIP.NextAvailableIPv4Addr = NewIpNextAvailableInfo(objectParams, params, useEaInheritance, isIpv6)
}
}

return &nextAvailableIP
}

type NetworkContainerNextAvailable struct {
IBBase `json:"-"`
objectType string
Expand Down
7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func BuildNetworkViewFromRef(ref string) *NetworkView {
}
}

func getNetworkObjectType(isIPv6 bool, ipv4Object string, ipv6Object string) string {
if isIPv6 {
return ipv6Object
}
return ipv4Object
}

func BuildNetworkFromRef(ref string) (*Network, error) {
// network/ZG5zLm5ldHdvcmskODkuMC4wLjAvMjQvMjU:89.0.0.0/24/global_view
r := regexp.MustCompile(`network/\w+:(\d+\.\d+\.\d+\.\d+/\d+)/(.+)`)
Expand Down