From 306608f1e3bcfd2a32c8ca9b1cf3857c4b347475 Mon Sep 17 00:00:00 2001 From: Sergey Kudriavtsev Date: Wed, 8 Dec 2021 07:33:44 +0300 Subject: [PATCH] PTR-record update fixes (record_name field) --- object_manager_ptr-record.go | 62 +++++++++++++++++++----------------- objects.go | 2 +- objects_test.go | 12 ++++++- 3 files changed, 44 insertions(+), 32 deletions(-) diff --git a/object_manager_ptr-record.go b/object_manager_ptr-record.go index 2fedff13..7047daae 100644 --- a/object_manager_ptr-record.go +++ b/object_manager_ptr-record.go @@ -25,7 +25,9 @@ func (objMgr *ObjectManager) CreatePTRRecord( } recordPTR := NewRecordPTR(dnsView, ptrdname, useTtl, ttl, comment, eas) - if ipAddr == "" && cidr != "" { + if recordName != "" { + recordPTR.Name = recordName + } else if ipAddr == "" && cidr != "" { if networkView == "" { networkView = "default" } @@ -51,8 +53,6 @@ func (objMgr *ObjectManager) CreatePTRRecord( } else { recordPTR.Ipv6Addr = ipAddr } - } else if recordName != "" { - recordPTR.Name = recordName } else { return nil, fmt.Errorf("CIDR and network view are required to allocate a next available IP address\n" + "IP address is required to create PTR record in reverse mapping zone\n" + @@ -131,42 +131,44 @@ func (objMgr *ObjectManager) UpdatePTRRecord( recordPTR.Name = name isIPv6, _ := regexp.MatchString(`^record:ptr/.+.ip6.arpa/.+`, ref) - if ipAddr == "" { - if cidr != "" { - ipAddress, _, err := net.ParseCIDR(cidr) - if err != nil { - return nil, fmt.Errorf("cannot parse CIDR value: %s", err.Error()) + if name == "" { + if ipAddr == "" { + if cidr != "" { + ipAddress, _, err := net.ParseCIDR(cidr) + if err != nil { + return nil, fmt.Errorf("cannot parse CIDR value: %s", err.Error()) + } + if netview == "" { + netview = "default" + } + if isIPv6 { + if ipAddress.To4() != nil || ipAddress.To16() == nil { + return nil, fmt.Errorf("CIDR value must be an IPv6 CIDR, not an IPv4 one") + } + recordPTR.Ipv6Addr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview) + } else { + if ipAddress.To4() == nil { + return nil, fmt.Errorf("CIDR value must be an IPv4 CIDR, not an IPv6 one") + } + recordPTR.Ipv4Addr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview) + } } - if netview == "" { - netview = "default" + } else { + ipAddress := net.ParseIP(ipAddr) + if ipAddress == nil { + return nil, fmt.Errorf("IP address for the record is not valid") } if isIPv6 { if ipAddress.To4() != nil || ipAddress.To16() == nil { - return nil, fmt.Errorf("CIDR value must be an IPv6 CIDR, not an IPv4 one") + return nil, fmt.Errorf("IP address must be an IPv6 address, not an IPv4 one") } - recordPTR.Ipv6Addr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview) + recordPTR.Ipv6Addr = ipAddr } else { if ipAddress.To4() == nil { - return nil, fmt.Errorf("CIDR value must be an IPv4 CIDR, not an IPv6 one") + return nil, fmt.Errorf("IP address must be an IPv4 address, not an IPv6 one") } - recordPTR.Ipv4Addr = fmt.Sprintf("func:nextavailableip:%s,%s", cidr, netview) - } - } - } else { - ipAddress := net.ParseIP(ipAddr) - if ipAddress == nil { - return nil, fmt.Errorf("IP address for the record is not valid") - } - if isIPv6 { - if ipAddress.To4() != nil || ipAddress.To16() == nil { - return nil, fmt.Errorf("IP address must be an IPv6 address, not an IPv4 one") + recordPTR.Ipv4Addr = ipAddr } - recordPTR.Ipv6Addr = ipAddr - } else { - if ipAddress.To4() == nil { - return nil, fmt.Errorf("IP address must be an IPv4 address, not an IPv6 one") - } - recordPTR.Ipv4Addr = ipAddr } } reference, err := objMgr.connector.UpdateObject(recordPTR, ref) diff --git a/objects.go b/objects.go index 986f2b01..123a66eb 100644 --- a/objects.go +++ b/objects.go @@ -540,7 +540,7 @@ type RecordPTR struct { func NewEmptyRecordPTR() *RecordPTR { res := RecordPTR{} res.objectType = "record:ptr" - res.returnFields = []string{"extattrs", "ipv4addr", "ipv6addr", "ptrdname", "view", "zone", "comment", "use_ttl", "ttl"} + res.returnFields = []string{"extattrs", "ipv4addr", "ipv6addr", "name", "ptrdname", "view", "zone", "comment", "use_ttl", "ttl"} return &res } diff --git a/objects_test.go b/objects_test.go index 5fbdc36b..ae83912b 100644 --- a/objects_test.go +++ b/objects_test.go @@ -468,7 +468,17 @@ var _ = Describe("Objects", func() { It("should set base fields correctly", func() { Expect(rptr.ObjectType()).To(Equal("record:ptr")) - Expect(rptr.ReturnFields()).To(ConsistOf("extattrs", "ipv4addr", "ipv6addr", "ptrdname", "view", "zone", "comment", "use_ttl", "ttl")) + Expect(rptr.ReturnFields()).To(ConsistOf( + "extattrs", + "ipv4addr", + "ipv6addr", + "name", + "ptrdname", + "view", + "zone", + "comment", + "use_ttl", + "ttl")) }) })