Skip to content

Commit

Permalink
CRUD management for TXT record (#89)
Browse files Browse the repository at this point in the history
* CRUD management for TXT record
* Ability to specify TTL during RecordTXT creation
* Catch nil response situation
* Return to the type approach consistent with other methods
* Return on empty res
  • Loading branch information
ytsarev authored Mar 18, 2020
1 parent 43d68f2 commit 4d8e743
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
66 changes: 66 additions & 0 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,72 @@ func (objMgr *ObjectManager) DeleteCNAMERecord(ref string) (string, error) {
return objMgr.connector.DeleteObject(ref)
}

// Creates TXT Record. Use TTL of 0 to inherit TTL from the Zone
func (objMgr *ObjectManager) CreateTXTRecord(recordname string, text string, ttl int, dnsview string) (*RecordTXT, error) {

recordTXT := NewRecordTXT(RecordTXT{
View: dnsview,
Name: recordname,
Text: text,
TTL: ttl,
})

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

func (objMgr *ObjectManager) GetTXTRecordByRef(ref string) (*RecordTXT, error) {
recordTXT := NewRecordTXT(RecordTXT{})
err := objMgr.connector.GetObject(recordTXT, ref, &recordTXT)
return recordTXT, err
}

func (objMgr *ObjectManager) GetTXTRecord(name string) (*RecordTXT, error) {
if name == "" {
return nil, fmt.Errorf("name can not be empty")
}
var res []RecordTXT

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

err := objMgr.connector.GetObject(recordTXT, "", &res)

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

return &res[0], nil
}

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

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

err := objMgr.connector.GetObject(recordTXT, "", &res)

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)

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

return &res[0], nil
}

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

func (objMgr *ObjectManager) CreatePTRRecord(netview string, dnsview string, recordname string, cidr string, ipAddr string, ea EA) (*RecordPTR, error) {

eas := objMgr.extendEA(ea)
Expand Down
71 changes: 71 additions & 0 deletions object_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,53 @@ var _ = Describe("Object Manager", func() {
Expect(err).To(BeNil())
})
})

Describe("Allocate TXT Record ", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
text := "test-text"
dnsView := "default"
recordName := "test"
ttl := 30
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,
}),
resultObject: NewRecordTXT(RecordTXT{
Name: recordName,
Text: text,
View: dnsView,
TTL: ttl,
Ref: fakeRefReturn,
}),
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)
})
It("should return expected TXT record Object", func() {
Expect(actualRecord).To(Equal(aniFakeConnector.resultObject))
Expect(err).To(BeNil())
})
})

Describe("Create EA Definition", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
Expand Down Expand Up @@ -1349,6 +1396,30 @@ var _ = Describe("Object Manager", func() {
})
})

Describe("Delete TXT Record", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
recordName := "test"
deleteRef := fmt.Sprintf("record:txt/ZG5zLmJpbmRfY25h:%s/%20%20", recordName)
fakeRefReturn := deleteRef
nwFakeConnector := &fakeConnector{
deleteObjectRef: deleteRef,
fakeRefReturn: fakeRefReturn,
}

objMgr := NewObjectManager(nwFakeConnector, cmpType, tenantID)

var actualRef string
var err error
It("should pass expected TXT record Ref to DeleteObject", func() {
actualRef, err = objMgr.DeleteTXTRecord(deleteRef)
})
It("should return expected TXT record Ref", func() {
Expect(actualRef).To(Equal(fakeRefReturn))
Expect(err).To(BeNil())
})
})

Describe("BuildNetworkViewFromRef", func() {
netviewName := "default_view"
netviewRef := fmt.Sprintf("networkview/ZG5zLm5ldHdvcmtfdmlldyQyMw:%s/false", netviewName)
Expand Down
1 change: 1 addition & 0 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ type RecordTXT struct {
Ref string `json:"_ref,omitempty"`
Name string `json:"name,omitempty"`
Text string `json:"text,omitempty"`
TTL int `json:"ttl,omitempty"`
View string `json:"view,omitempty"`
Zone string `json:"zone,omitempty"`
Ea EA `json:"extattrs,omitempty"`
Expand Down

0 comments on commit 4d8e743

Please sign in to comment.