Skip to content

Commit

Permalink
Merge pull request #159 from skudriavtsev/bugfixes-2021-12-07
Browse files Browse the repository at this point in the history
PTR-record update fixes (record_name field)
  • Loading branch information
skudriavtsev authored Dec 13, 2021
2 parents 83b1dd1 + 306608f commit a7b7f78
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 32 deletions.
62 changes: 32 additions & 30 deletions object_manager_ptr-record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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" +
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
12 changes: 11 additions & 1 deletion objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
})

Expand Down

0 comments on commit a7b7f78

Please sign in to comment.