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 Oct 19, 2021
1 parent b339be9 commit 21de0f2
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 82 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ This library is compatible with Go 1.2+
* GetNetworkViewByRef
* GetPTRRecordByRef
* GetPTRRecord
* GetTXTRecord
* GetTXTRecordByRef
* GetZoneAuthByRef
* GetZoneDelegated
* GetUpgradeStatus (2.7 or above)
Expand All @@ -124,6 +126,7 @@ This library is compatible with Go 1.2+
* UpdateNetworkContainer
* UpdateNetworkView
* UpdatePTRRecord
* UpdateTXTRecord
* UpdateARecord
* UpdateZoneDelegated

Expand Down
2 changes: 1 addition & 1 deletion connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (whr *WapiHttpRequestor) Init(cfg TransportConfig) {
RootCAs: cfg.certPool,
Renegotiation: tls.RenegotiateOnceAsClient},
MaxIdleConnsPerHost: cfg.HttpPoolConnections,
Proxy: http.ProxyFromEnvironment,
Proxy: http.ProxyFromEnvironment,
}

if cfg.ProxyUrl != nil {
Expand Down
67 changes: 34 additions & 33 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 @@ -61,6 +61,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 @@ -77,6 +79,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 Expand Up @@ -216,7 +219,7 @@ func (objMgr *ObjectManager) UpdateNetworkView(ref string, name string, comment
if cleanName != "" {
nv.Name = cleanName
}
nv.Comment = comment
nv.Comment = comment
nv.Ea = setEas

updatedRef, err := objMgr.connector.UpdateObject(nv, ref)
Expand Down Expand Up @@ -1241,22 +1244,27 @@ func (objMgr *ObjectManager) UpdateCNAMERecord(
}

// 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 @@ -1268,7 +1276,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 @@ -1283,32 +1291,25 @@ 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)

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
func (objMgr *ObjectManager) UpdateTXTRecord(
ref string,
recordName string,
text string,
useTtl bool,
ttl uint32,
comment string,
eas EA) (*RecordTXT, error) {

_, 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) {
Expand Down
93 changes: 68 additions & 25 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 Expand Up @@ -3108,54 +3112,93 @@ var _ = Describe("Object Manager", 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("Create EA Definition", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
Expand Down
54 changes: 39 additions & 15 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ func (obj *IBBase) EaSearch() EASearch {
}

type NetworkView struct {
IBBase `json:"-"`
Ref string `json:"_ref,omitempty"`
Name string `json:"name,omitempty"`
IBBase `json:"-"`
Ref string `json:"_ref,omitempty"`
Name string `json:"name,omitempty"`
Comment string `json:"comment"`
Ea EA `json:"extattrs"`
Ea EA `json:"extattrs"`
}

func NewEmptyNetworkView() *NetworkView {
Expand Down Expand Up @@ -728,25 +728,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,omitempty"`
UseTtl bool `json:"use_ttl,omitempty"`
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 21de0f2

Please sign in to comment.