Skip to content

Commit

Permalink
Adding full support for txt records
Browse files Browse the repository at this point in the history
  • Loading branch information
shadtimm committed Nov 19, 2021
1 parent c4750eb commit f706b35
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 77 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ This library is compatible with Go 1.2+
* GetNetworkViewByRef
* GetPTRRecordByRef
* GetPTRRecord
* GetTXTRecord
* GetTXTRecordByRef
* GetZoneAuthByRef
* GetZoneDelegated
* GetUpgradeStatus (2.7 or above)
Expand All @@ -125,6 +127,7 @@ This library is compatible with Go 1.2+
* UpdateNetworkContainer
* UpdateNetworkView
* UpdatePTRRecord
* UpdateTXTRecord
* UpdateARecord
* UpdateZoneDelegated

Expand Down
5 changes: 4 additions & 1 deletion object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type IBObjectManager interface {
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)
CreateTXTRecord(recordname string, text string, ttl uint, dnsview string) (*RecordTXT, error)
CreateTXTRecord(recordName string, text string, dnsView string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordTXT, error)
CreateZoneDelegated(fqdn string, delegate_to []NameServer) (*ZoneDelegated, error)
DeleteARecord(ref string) (string, error)
DeleteAAAARecord(ref string) (string, error)
Expand Down Expand Up @@ -58,6 +58,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)
GetTXTRecord(name string) (*RecordTXT, error)
GetTXTRecordByRef(ref string) (*RecordTXT, error)
GetZoneAuthByRef(ref string) (*ZoneAuth, error)
GetZoneDelegated(fqdn string) (*ZoneDelegated, error)
GetCapacityReport(name string) ([]CapacityReport, error)
Expand All @@ -74,6 +76,7 @@ type IBObjectManager interface {
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)
UpdateTXTRecord(ref string, recordName string, text string, useTtl bool, ttl uint32, 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
4 changes: 4 additions & 0 deletions object_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (c *fakeConnector) GetObject(obj IBObject, ref string, qp *QueryParams, res
*res.(*[]RecordAAAA) = c.resultObject.([]RecordAAAA)
case *RecordPTR:
*res.(*[]RecordPTR) = c.resultObject.([]RecordPTR)
case *RecordTXT:
*res.(*[]RecordTXT) = c.resultObject.([]RecordTXT)
case *ZoneDelegated:
*res.(*[]ZoneDelegated) = c.resultObject.([]ZoneDelegated)
case *RecordCNAME:
Expand All @@ -99,6 +101,8 @@ func (c *fakeConnector) GetObject(obj IBObject, ref string, qp *QueryParams, res
**res.(**HostRecord) = *c.resultObject.(*HostRecord)
case *RecordPTR:
**res.(**RecordPTR) = *c.resultObject.(*RecordPTR)
case *RecordTXT:
**res.(**RecordTXT) = *c.resultObject.(*RecordTXT)
case *RecordCNAME:
**res.(**RecordCNAME) = *c.resultObject.(*RecordCNAME)
case *RecordA:
Expand Down
61 changes: 29 additions & 32 deletions object_manager_txt-record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ package ibclient
import "fmt"

// Creates TXT Record. Use TTL of 0 to inherit TTL from the Zone
func (objMgr *ObjectManager) CreateTXTRecord(recordname string, text string, ttl uint, dnsview string) (*RecordTXT, error) {
func (objMgr *ObjectManager) CreateTXTRecord(
recordName string,
text string,
dnsView string,
useTtl bool,
ttl uint32,
comment string,
eas EA) (*RecordTXT, error) {

recordTXT := NewRecordTXT(RecordTXT{
View: dnsview,
Name: recordname,
Text: text,
Ttl: ttl,
})
recordTXT := NewRecordTXT(recordName, text, dnsView, "", useTtl, ttl, comment, eas)

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

func (objMgr *ObjectManager) GetTXTRecordByRef(ref string) (*RecordTXT, error) {
recordTXT := NewRecordTXT(RecordTXT{})
recordTXT := NewEmptyRecordTXT()
err := objMgr.connector.GetObject(
recordTXT, ref, NewQueryParams(false, nil), &recordTXT)
return recordTXT, err
Expand All @@ -30,7 +35,7 @@ func (objMgr *ObjectManager) GetTXTRecord(name string) (*RecordTXT, error) {
}
var res []RecordTXT

recordTXT := NewRecordTXT(RecordTXT{})
recordTXT := NewEmptyRecordTXT()

sf := map[string]string{
"name": name,
Expand All @@ -45,35 +50,27 @@ func (objMgr *ObjectManager) GetTXTRecord(name string) (*RecordTXT, error) {
return &res[0], nil
}

func (objMgr *ObjectManager) UpdateTXTRecord(recordname string, text string) (*RecordTXT, error) {
var res []RecordTXT

recordTXT := NewRecordTXT(RecordTXT{Name: recordname})

sf := map[string]string{
"name": recordname,
}
queryParams := NewQueryParams(false, sf)
err := objMgr.connector.GetObject(recordTXT, "", queryParams, &res)
func (objMgr *ObjectManager) UpdateTXTRecord(
ref string,
recordName string,
text string,
useTtl bool,
ttl uint32,
comment string,
eas EA) (*RecordTXT, error) {

if len(res) == 0 {
return nil, nil
}

res[0].Text = text

res[0].Zone = "" // set the Zone value to "" as its a non writable field

_, err = objMgr.connector.UpdateObject(&res[0], res[0].Ref)
recordTXT := NewRecordTXT(recordName, text, "", "", useTtl, ttl, comment, eas)
recordTXT.Ref = ref

if err != nil || res == nil || len(res) == 0 {
reference, err := objMgr.connector.UpdateObject(recordTXT, ref)
if err != nil {
return nil, err
}

return &res[0], nil
recordTXT, err = objMgr.GetTXTRecordByRef(reference)
return recordTXT, err
}

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

89 changes: 64 additions & 25 deletions object_manager_txt-record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,93 @@ import (
)

var _ = Describe("Object Manager: TXT-record", func() {
Describe("Allocate TXT Record ", func() {
Describe("Create TXT Record ", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
text := "test-text"
dnsView := "default"
recordName := "test"
ttl := uint(30)
useTtl := true
ttl := uint32(70)
comment := "creation test"
eas := EA{"Country": "test"}
fakeRefReturn := fmt.Sprintf("record:txt/ZG5zLmJpbmRfY25h:%s/%20%20", recordName)

aniFakeConnector := &fakeConnector{
createObjectObj: NewRecordTXT(RecordTXT{
Name: recordName,
Text: text,
Ttl: ttl,
View: dnsView,
}),
getObjectRef: fakeRefReturn,
getObjectObj: NewRecordTXT(RecordTXT{
Name: recordName,
Text: text,
View: dnsView,
Ref: fakeRefReturn,
Ttl: ttl,
}),
createObjectObj: NewRecordTXT(recordName, text, dnsView, "", useTtl, ttl, comment, eas),
getObjectRef: fakeRefReturn,
getObjectObj: NewEmptyRecordTXT(),
getObjectQueryParams: NewQueryParams(false, nil),
resultObject: NewRecordTXT(RecordTXT{
Name: recordName,
Text: text,
View: dnsView,
Ttl: ttl,
Ref: fakeRefReturn,
}),
fakeRefReturn: fakeRefReturn,
resultObject: NewRecordTXT(recordName, text, dnsView, "", useTtl, ttl, comment, eas),
fakeRefReturn: fakeRefReturn,
}

objMgr := NewObjectManager(aniFakeConnector, cmpType, tenantID)

var actualRecord *RecordTXT
var err error
It("should pass expected TXT record Object to CreateObject", func() {
actualRecord, err = objMgr.CreateTXTRecord(recordName, text, 30, dnsView)
actualRecord, err = objMgr.CreateTXTRecord(recordName, text, dnsView, useTtl, ttl, comment, eas)
})
It("should return expected TXT record Object", func() {
Expect(actualRecord).To(Equal(aniFakeConnector.resultObject))
Expect(err).To(BeNil())
})
})

Describe("Update TXT record", func() {
var (
err error
objMgr IBObjectManager
conn *fakeConnector
ref string
actualObj *RecordTXT
)

cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
recordName := "test"

It("Updating text, useTtl, ttl, comment and EAs", func() {
ref = fmt.Sprintf("record:txt/ZG5zLmJpbmRfY25h:%s/%20%20", recordName)
initialEas := EA{"Country": "old value"}
initObj := NewRecordTXT(recordName, "old-text", "", "", true, uint32(70), "old comment", initialEas)
initObj.Ref = ref

expectedEas := EA{"Country": "new value"}

updateText := ""
updateComment := "new comment"
updateUseTtl := true
updateTtl := uint32(10)
updatedRef := fmt.Sprintf("record:txt/ZG5zLmJpbmRfY25h:%s/%20%20", recordName)
updateObjIn := NewRecordTXT(recordName, updateText, "", "", updateUseTtl, updateTtl, updateComment, expectedEas)
updateObjIn.Ref = ref

expectedObj := NewRecordTXT(recordName, updateText, "", "", updateUseTtl, updateTtl, updateComment, expectedEas)
expectedObj.Ref = updatedRef

conn = &fakeConnector{
getObjectObj: NewEmptyRecordTXT(),
getObjectQueryParams: NewQueryParams(false, nil),
getObjectRef: updatedRef,
getObjectError: nil,
resultObject: expectedObj,

updateObjectObj: updateObjIn,
updateObjectRef: ref,
updateObjectError: nil,

fakeRefReturn: updatedRef,
}
objMgr = NewObjectManager(conn, cmpType, tenantID)

actualObj, err = objMgr.UpdateTXTRecord(ref, recordName, updateText, updateUseTtl, updateTtl, updateComment, expectedEas)
Expect(err).To(BeNil())
Expect(*actualObj).To(BeEquivalentTo(*expectedObj))
})
})

Describe("Delete TXT Record", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
Expand Down
46 changes: 35 additions & 11 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,25 +729,49 @@ func NewHostRecord(
}

type RecordTXT struct {
IBBase `json:"-"`
Ref string `json:"_ref,omitempty"`
Name string `json:"name,omitempty"`
Text string `json:"text,omitempty"`
Ttl uint `json:"ttl,omitempty"`
View string `json:"view,omitempty"`
Zone string `json:"zone,omitempty"`
Ea EA `json:"extattrs"`
UseTtl bool `json:"use_ttl"`
IBBase `json:"-"`
Ref string `json:"_ref,omitempty"`
Name string `json:"name,omitempty"`
Text string `json:"text,omitempty"`
Ttl uint32 `json:"ttl,omitempty"`
View string `json:"view,omitempty"`
Zone string `json:"zone,omitempty"`
Comment string `json:"comment"`
Ea EA `json:"extattrs,omitempty"`
UseTtl bool `json:"use_ttl,omitempty"`
}

func NewRecordTXT(rt RecordTXT) *RecordTXT {
res := rt
func NewEmptyRecordTXT() *RecordTXT {
res := RecordTXT{}
res.objectType = "record:txt"
res.returnFields = []string{"extattrs", "name", "text", "view", "zone", "ttl", "use_ttl"}

return &res
}

func NewRecordTXT(
recordname string,
text string,
dnsview string,
zone string,
useTtl bool,
ttl uint32,
comment string,
eas EA) *RecordTXT {

res := NewEmptyRecordTXT()
res.Name = recordname
res.Text = text
res.View = dnsview
res.Zone = zone
res.UseTtl = useTtl
res.Ttl = ttl
res.Comment = comment
res.Ea = eas

return res
}

type ZoneAuth struct {
IBBase `json:"-"`
Ref string `json:"_ref,omitempty"`
Expand Down
14 changes: 6 additions & 8 deletions objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,24 +597,22 @@ var _ = Describe("Objects", func() {
name := "txt.domain.com"
text := "this is text string"
view := "default"
zone := "domain.com"
useTtl := true
ttl := uint32(70)
comment := "test client"
eas := EA{"Country": "test"}

rt := NewRecordTXT(RecordTXT{
Name: name,
Text: text,
View: view,
Zone: zone})
rt := NewRecordTXT(name, text, view, "", useTtl, ttl, comment, eas)

It("should set fields correctly", func() {
Expect(rt.Name).To(Equal(name))
Expect(rt.Text).To(Equal(text))
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:txt"))
Expect(rt.ReturnFields()).To(ConsistOf("extattrs", "name", "text", "view", "zone", "ttl", "use_ttl"))
Expect(rt.ReturnFields()).To(ConsistOf("name", "text", "view", "zone", "ttl", "use_ttl", "extattrs"))
})
})

Expand Down

0 comments on commit f706b35

Please sign in to comment.