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

Add RecordNS support #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type IBObjectManager interface {
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)
CreateNSRecord(dnsview string, name string, ns string, addr []string) (*RecordNS, error)
CreatePTRRecord(networkView string, dnsView string, ptrdname string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordPTR, 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)
Expand All @@ -34,6 +35,7 @@ type IBObjectManager interface {
DeleteNetwork(ref string) (string, error)
DeleteNetworkContainer(ref string) (string, error)
DeleteNetworkView(ref string) (string, error)
DeleteNSRecord(ref string) (string, error)
DeletePTRRecord(ref string) (string, error)
DeleteTXTRecord(ref string) (string, error)
DeleteZoneDelegated(ref string) (string, error)
Expand Down
23 changes: 23 additions & 0 deletions object_manager_ns-record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ibclient

func (objMgr *ObjectManager) CreateNSRecord(dnsview string, recordname string, ns string, addr []string) (*RecordNS, error) {

var addresses []ZoneNameServer
for _, addr := range addr {
addresses = append(addresses, ZoneNameServer{Address: addr})
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @k0da , Can you please add some validations on errors that might rise?
Ex: if recordname == "" {
return nil, fmt.Errorf("recordname must not be empty")
}

recordNS := NewRecordNS(RecordNS{
View: dnsview,
Name: recordname,
NS: ns,
Addresses: addresses,
})

ref, err := objMgr.connector.CreateObject(recordNS)
recordNS.Ref = ref
return recordNS, err
}

func (objMgr *ObjectManager) DeleteNSRecord(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}
83 changes: 83 additions & 0 deletions object_manager_ns-record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ibclient

import (
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Object Manager: NS-record", func() {
Describe("Allocate NS Record ", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
ns := "ns.example.com"
dnsView := "default"
recordName := "test.example.com"
addr := []string{"10.0.0.1"}
resultAddr := []ZoneNameServer{{Address: addr[0]}}

fakeRefReturn := fmt.Sprintf("record:ns/ZG5zLmhvc3RjkuMC4xLg:%s/%s/default", ns, recordName)

aniFakeConnector := &fakeConnector{
createObjectObj: NewRecordNS(RecordNS{
Name: recordName,
NS: ns,
Addresses: resultAddr,
View: dnsView,
}),
getObjectRef: fakeRefReturn,
getObjectObj: NewRecordNS(RecordNS{
Name: recordName,
NS: ns,
Addresses: resultAddr,
View: dnsView,
Ref: fakeRefReturn,
}),
resultObject: NewRecordNS(RecordNS{
Name: recordName,
NS: ns,
Addresses: resultAddr,
View: dnsView,
Ref: fakeRefReturn,
}),
fakeRefReturn: fakeRefReturn,
}

objMgr := NewObjectManager(aniFakeConnector, cmpType, tenantID)

var actualRecord *RecordNS
var err error
It("should pass expected NS record Object to CreateObject", func() {
actualRecord, err = objMgr.CreateNSRecord(dnsView, recordName, ns, addr)
})
It("should return expected NS record Object", func() {
Expect(actualRecord).To(Equal(aniFakeConnector.resultObject))
Expect(err).To(BeNil())
})
})
Describe("Delete NS Record", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
recordName := "test.example.com"
ns := "ns1.example.com"
deleteRef := fmt.Sprintf("record:ns/ZG5zLmJpbmRfY25h:%s/%s", ns, recordName)
fakeRefReturn := deleteRef
nwFakeConnector := &fakeConnector{
deleteObjectRef: deleteRef,
fakeRefReturn: fakeRefReturn,
}

objMgr := NewObjectManager(nwFakeConnector, cmpType, tenantID)

var actualRef string
var err error
It("should pass expected NS record Ref to DeleteObject", func() {
actualRef, err = objMgr.DeleteTXTRecord(deleteRef)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @k0da , Please change this DeleteTXTRecord to DeleteNSRecord.

})
It("should return expected NS record Ref", func() {
Expect(actualRef).To(Equal(fakeRefReturn))
Expect(err).To(BeNil())
})
})
})
23 changes: 23 additions & 0 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,29 @@ func NewZoneDelegated(za ZoneDelegated) *ZoneDelegated {
return &res
}

type RecordNS struct {
IBBase `json:"-"`
Ref string `json:"_ref,omitempty"`
Addresses []ZoneNameServer `json:"addresses,omitempty"`
Name string `json:"name,omitempty"`
NS string `json:"nameserver,omitempty"`
View string `json:"view,omitempty"`
Zone string `json:"zone,omitempty"`
Ea EA `json:"extattrs,omitempty"`
}

func NewRecordNS(rc RecordNS) *RecordNS {
res := rc
res.objectType = "record:ns"
res.returnFields = []string{"extattrs", "addresses", "nameserver", "name", "view", "zone"}

return &res
}

type ZoneNameServer struct {
Address string `json:"address,omitempty"`
}

func (ea EA) Count() int {
return len(ea)
}
Expand Down
29 changes: 29 additions & 0 deletions objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,35 @@ var _ = Describe("Objects", func() {

})

Context("RecordNS object", func() {
name := "ns.domain.com"
ns := "ns.domain.com"
addr := []ZoneNameServer{{
Address: "10.0.0.1"}}
view := "default"
zone := "domain.com"

rt := NewRecordNS(RecordNS{
Name: name,
NS: ns,
Addresses: addr,
View: view,
Zone: zone})

It("should set fields correctly", func() {
Expect(rt.Name).To(Equal(name))
Expect(rt.NS).To(Equal(ns))
Expect(rt.Addresses).To(Equal(addr))
Expect(rt.View).To(Equal(view))
Expect(rt.Zone).To(Equal(zone))
})

It("should set base fields correctly", func() {
Expect(rt.ObjectType()).To(Equal("record:ns"))
Expect(rt.ReturnFields()).To(ConsistOf("extattrs", "name", "nameserver", "addresses", "view", "zone"))
})
})

Context("Unmarshalling malformed JSON", func() {
Context("for EA", func() {
badJSON := `""`
Expand Down
23 changes: 0 additions & 23 deletions record_ns.go

This file was deleted.