Skip to content

Commit

Permalink
new record types: MX and SRV
Browse files Browse the repository at this point in the history
  • Loading branch information
skudriavtsev committed Mar 10, 2023
1 parent b91e06d commit 6775107
Show file tree
Hide file tree
Showing 10 changed files with 1,051 additions and 2 deletions.
10 changes: 10 additions & 0 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ type IBObjectManager interface {
CreateDefaultNetviews(globalNetview string, localNetview string) (globalNetviewRef string, localNetviewRef string, err error)
CreateEADefinition(eadef EADefinition) (*EADefinition, error)
CreateHostRecord(enabledns bool, enabledhcp bool, recordName string, netview string, dnsview string, ipv4cidr string, ipv6cidr string, ipv4Addr string, ipv6Addr string, macAddr string, duid string, useTtl bool, ttl uint32, comment string, eas EA, aliases []string) (*HostRecord, error)
CreateMXRecord(dnsView string, fqdn string, mx string, preference uint32, ttl uint32, useTtl bool, comment string, eas EA) (*RecordMX, error)
CreateNetwork(netview string, cidr string, isIPv6 bool, comment string, eas EA) (*Network, error)
CreateNetworkContainer(netview string, cidr string, isIPv6 bool, comment string, eas EA) (*NetworkContainer, error)
CreateNetworkView(name string, comment string, setEas EA) (*NetworkView, error)
CreatePTRRecord(networkView string, dnsView string, ptrdname string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordPTR, error)
CreateSRVRecord(dnsView string, name string, priority uint32, weight uint32, port uint32, target string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordSRV, error)
CreateTXTRecord(dnsView string, recordName string, text string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordTXT, error)
CreateZoneDelegated(fqdn string, delegate_to []NameServer) (*ZoneDelegated, error)
DeleteARecord(ref string) (string, error)
Expand All @@ -33,10 +35,12 @@ type IBObjectManager interface {
DeleteCNAMERecord(ref string) (string, error)
DeleteFixedAddress(ref string) (string, error)
DeleteHostRecord(ref string) (string, error)
DeleteMXRecord(ref string) (string, error)
DeleteNetwork(ref string) (string, error)
DeleteNetworkContainer(ref string) (string, error)
DeleteNetworkView(ref string) (string, error)
DeletePTRRecord(ref string) (string, error)
DeleteSRVRecord(ref string) (string, error)
DeleteTXTRecord(ref string) (string, error)
DeleteZoneDelegated(ref string) (string, error)
GetARecordByRef(ref string) (*RecordA, error)
Expand All @@ -52,6 +56,8 @@ type IBObjectManager interface {
SearchHostRecordByAltId(internalId string, ref string, eaNameForInternalId string) (*HostRecord, error)
GetHostRecordByRef(ref string) (*HostRecord, error)
GetIpAddressFromHostRecord(host HostRecord) (string, error)
GetMXRecord(dnsView string, fqdn string, mx string) (*RecordMX, error)
GetMXRecordByRef(ref string) (*RecordMX, error)
GetNetwork(netview string, cidr string, isIPv6 bool, ea EA) (*Network, error)
GetNetworkByRef(ref string) (*Network, error)
GetNetworkContainer(netview string, cidr string, isIPv6 bool, eaSearch EA) (*NetworkContainer, error)
Expand All @@ -60,6 +66,8 @@ type IBObjectManager interface {
GetNetworkViewByRef(ref string) (*NetworkView, error)
GetPTRRecord(dnsview string, ptrdname string, recordName string, ipAddr string) (*RecordPTR, error)
GetPTRRecordByRef(ref string) (*RecordPTR, error)
GetSRVRecord(dnsView string, name string, target string, port uint32) (*RecordSRV, error)
GetSRVRecordByRef(ref string) (*RecordSRV, error)
GetTXTRecord(dnsview string, name string) (*RecordTXT, error)
GetTXTRecordByRef(ref string) (*RecordTXT, error)
GetZoneAuthByRef(ref string) (*ZoneAuth, error)
Expand All @@ -74,10 +82,12 @@ type IBObjectManager interface {
UpdateCNAMERecord(ref string, canonical string, recordName string, useTtl bool, ttl uint32, comment string, setEas EA) (*RecordCNAME, error)
UpdateFixedAddress(fixedAddrRef string, netview string, name string, cidr string, ipAddr string, matchclient string, macOrDuid string, comment string, eas EA) (*FixedAddress, error)
UpdateHostRecord(hostRref string, enabledns bool, enabledhcp bool, name string, netview string, dnsView string, ipv4cidr string, ipv6cidr string, ipv4Addr string, ipv6Addr string, macAddress string, duid string, useTtl bool, ttl uint32, comment string, eas EA, aliases []string) (*HostRecord, error)
UpdateMXRecord(ref string, dnsView string, fqdn string, mx string, preference uint32, ttl uint32, useTtl bool, comment string, eas EA) (*RecordMX, error)
UpdateNetwork(ref string, setEas EA, comment string) (*Network, error)
UpdateNetworkContainer(ref string, setEas EA, comment string) (*NetworkContainer, error)
UpdateNetworkView(ref string, name string, comment string, setEas EA) (*NetworkView, error)
UpdatePTRRecord(ref string, netview string, ptrdname string, name string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, setEas EA) (*RecordPTR, error)
UpdateSRVRecord(ref string, name string, priority uint32, weight uint32, port uint32, target string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordSRV, error)
UpdateTXTRecord(ref string, recordName string, text string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordTXT, error)
UpdateARecord(ref string, name string, ipAddr string, cidr string, netview string, ttl uint32, useTTL bool, comment string, eas EA) (*RecordA, error)
UpdateZoneDelegated(ref string, delegate_to []NameServer) (*ZoneDelegated, error)
Expand Down
136 changes: 136 additions & 0 deletions object_manager_mx_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package ibclient

import "fmt"

func (objMgr *ObjectManager) CreateMXRecord(
dnsView string,
fqdn string,
mx string,
preference uint32,
ttl uint32,
useTtl bool,
comment string,
eas EA) (*RecordMX, error) {

if dnsView == "" {
dnsView = "default"
}

if fqdn == "" || mx == "" {
return nil, fmt.Errorf("'fqdn' and 'mail_exchanger' fields must not be empty")
}

if preference < 0 || preference > 65535 {
return nil, fmt.Errorf("'preference' is not in range 0 to 65535")
}

recordMx := NewRecordMX(RecordMX{
View: dnsView,
Fqdn: fqdn,
MX: mx,
Preference: preference,
Ttl: ttl,
UseTtl: useTtl,
Comment: comment,
Ea: eas,
})

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

func (objMgr *ObjectManager) GetMXRecordByRef(ref string) (*RecordMX, error) {
recordMX := NewEmptyRecordMX()
err := objMgr.connector.GetObject(recordMX, ref, NewQueryParams(false, nil), &recordMX)

return recordMX, err
}

func (objMgr *ObjectManager) GetMXRecord(dnsView string, fqdn string, mx string) (*RecordMX, error) {
if dnsView == "" || fqdn == "" {
return nil, fmt.Errorf("'DNS view' and 'fqdn' are required to retrieve a unique mx record")
}
var res []RecordMX

recordMX := NewEmptyRecordMX()
sf := map[string]string{
"view": dnsView,
"name": fqdn,
"mail_exchanger": mx,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(recordMX, "", queryParams, &res)

if err != nil {
return nil, err
}

if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf(
"MX record with name '%s' and MX '%s' in DNS view '%s' is not found",
fqdn, mx, dnsView))
}

return &res[0], nil
}

func (objMgr *ObjectManager) UpdateMXRecord(
ref string,
dnsView string,
fqdn string,
mx string,
preference uint32,
ttl uint32,
useTtl bool,
comment string,
eas EA) (*RecordMX, error) {

res, err := objMgr.GetMXRecordByRef(ref)

if err != nil {
return nil, err
}

if dnsView != res.View {
return nil, fmt.Errorf("changing 'dns_view' field after object creation is not allowed")
}

if preference < 0 || preference > 65535 {
return nil, fmt.Errorf("'preference' is not in range 0 to 65535")
}

if mx == "" {
return nil, fmt.Errorf("'mail_exchanger' field must not be empty")
}
recordMx := NewRecordMX(RecordMX{
View: dnsView,
Fqdn: fqdn,
MX: mx,
Preference: preference,
Ttl: ttl,
UseTtl: useTtl,
Comment: comment,
Ea: eas,
})

recordMx.Ref = ref

nw_ref, err := objMgr.connector.UpdateObject(recordMx, ref)

if err != nil {
return nil, err
}

recordMx.Ref = nw_ref

return recordMx, err
}

func (objMgr *ObjectManager) DeleteMXRecord(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}
Loading

0 comments on commit 6775107

Please sign in to comment.