From 4d8e743354a68ddcf79685aa47afdd092f680855 Mon Sep 17 00:00:00 2001 From: Yury Tsarev Date: Wed, 18 Mar 2020 15:46:57 +0100 Subject: [PATCH] CRUD management for TXT record (#89) * 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 --- object_manager.go | 66 +++++++++++++++++++++++++++++++++++++++ object_manager_test.go | 71 ++++++++++++++++++++++++++++++++++++++++++ objects.go | 1 + 3 files changed, 138 insertions(+) diff --git a/object_manager.go b/object_manager.go index ae8aba4b..d03c85da 100644 --- a/object_manager.go +++ b/object_manager.go @@ -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) diff --git a/object_manager_test.go b/object_manager_test.go index e0b3c3c4..c1e4b4a5 100644 --- a/object_manager_test.go +++ b/object_manager_test.go @@ -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" @@ -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) diff --git a/objects.go b/objects.go index 9bea2ce2..c0b1dc48 100644 --- a/objects.go +++ b/objects.go @@ -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"`