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

Ns record/implementation #284

Open
wants to merge 7 commits 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
189 changes: 189 additions & 0 deletions e2e_tests/wapi_framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3581,3 +3581,192 @@ var _ = Describe("DTC LBDN and server object", func() {
})

})

var _ = Describe("NS Record", func() {
var connector *ConnectorFacadeE2E

BeforeEach(func() {
hostConfig := ibclient.HostConfig{
Host: os.Getenv("INFOBLOX_SERVER"),
Version: os.Getenv("WAPI_VERSION"),
Port: os.Getenv("PORT"),
}

authConfig := ibclient.AuthConfig{
Username: os.Getenv("INFOBLOX_USERNAME"),
Password: os.Getenv("INFOBLOX_PASSWORD"),
}

transportConfig := ibclient.NewTransportConfig("false", 20, 10)
requestBuilder := &ibclient.WapiRequestBuilder{}
requestor := &ibclient.WapiHttpRequestor{}
ibClientConnector, err := ibclient.NewConnector(hostConfig, authConfig, transportConfig, requestBuilder, requestor)
Expect(err).To(BeNil())
connector = &ConnectorFacadeE2E{*ibClientConnector, make([]string, 0)}

zones := ibclient.ZoneAuth{
Fqdn: "wapi_test.com",
}
zoneRef, err := connector.CreateObject(&zones)
Expect(err).To(BeNil())
zones.Ref = zoneRef
})
AfterEach(func() {
err := connector.SweepObjects()
Expect(err).To(BeNil())
})

It("Should create a NS Record", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Nameserver: utils.StringPtr("ns1.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
ref, err := connector.CreateObject(&nsRecord)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))
})

It("Should update the NS Record", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Nameserver: utils.StringPtr("ns1.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
ref, err := connector.CreateObject(&nsRecord)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))

// Update the DTC Lbdn object
nsRecordUpdate := ibclient.RecordNS{
Nameserver: utils.StringPtr("ns2.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}

var res []ibclient.RecordNS
search := &ibclient.RecordNS{}
err = connector.GetObject(search, "", nil, &res)
ref, err = connector.UpdateObject(&nsRecordUpdate, ref)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))
})

It("Should get the NS Record", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Nameserver: utils.StringPtr("ns1.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
ref, err := connector.CreateObject(&nsRecord)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))

var res []ibclient.RecordNS
search := &ibclient.RecordNS{}
search.SetReturnFields(append(search.ReturnFields(), "addresses"))
qp := ibclient.NewQueryParams(false, map[string]string{
"nameserver": "ns1.wapi_test.com",
})
errCode := connector.GetObject(search, "", qp, &res)
Expect(errCode).To(BeNil())
Expect(res[0].Nameserver).To(Equal(utils.StringPtr("ns1.wapi_test.com")))
Expect(res[0].Name).To(Equal("wapi_test.com"))
Expect(res[0].Addresses[0].Address).To(Equal("2.3.4.5"))
Expect(res[0].Addresses[0].AutoCreatePtr).To(Equal(true))
Expect(res[0].Ref).To(MatchRegexp("record:ns/*"))
})
It("Should delete a NS Record", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Nameserver: utils.StringPtr("ns1.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
ref, err := connector.CreateObject(&nsRecord)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))
delRef, err := connector.DeleteObject(ref)
Expect(err).To(BeNil())
Expect(delRef).To(MatchRegexp("record:ns/*"))
})

It("Should fail to create a NS Record object", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
_, err := connector.CreateObject(&nsRecord)
Expect(err).NotTo(BeNil())
})

// update Dtc server, -ve scenario
It("Should fail to update a NS Record with wrong reference", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Nameserver: utils.StringPtr("ns3.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
ref, err := connector.CreateObject(&nsRecord)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))
_, err = connector.UpdateObject(&nsRecord, "nonexistent_ref")
Expect(err).NotTo(BeNil())
})
It("Should fail to update a NS record", func() {
nsRecord := ibclient.RecordNS{
Name: "wapi_test.com",
Nameserver: utils.StringPtr("ns3.wapi_test.com"),
Addresses: []*ibclient.ZoneNameServer{
{
Address: "2.3.4.5",
AutoCreatePtr: true,
},
},
}
ref, err := connector.CreateObject(&nsRecord)
Expect(err).To(BeNil())
Expect(ref).To(MatchRegexp("record:ns/*"))
nsRecordUpdate := ibclient.RecordNS{
Name: "wapi_test2.com",
}
var res []ibclient.RecordNS
search := &ibclient.RecordNS{}
err = connector.GetObject(search, "", nil, &res)
ref, err = connector.UpdateObject(&nsRecordUpdate, res[0].Ref)
Expect(err).NotTo(BeNil())
})
})
5 changes: 5 additions & 0 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type IBObjectManager interface {
CreateAAAARecord(netView string, dnsView string, recordName string, cidr string, ipAddr string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordAAAA, error)
CreateDtcPool(comment string, name string, lbPreferredMethod string, lbDynamicRatioPreferred map[string]interface{}, servers []*DtcServerLink, monitors []Monitor, lbPreferredTopology *string, lbAlternateMethod string, lbAlternateTopology *string, lbDynamicRatioAlternate map[string]interface{}, eas EA, autoConsolidatedMonitors bool, userMonitors []map[string]interface{}, availability string, ttl uint32, useTTL bool, disable bool, quorum uint32) (*DtcPool, error)
CreateDtcServer(comment string, name string, host string, autoCreateHostRecord bool, disable bool, ea EA, monitors []map[string]interface{}, sniHostname string, useSniHostname bool) (*DtcServer, error)
CreateNSRecord(name string, nameServer string, view string, addresses []*ZoneNameServer, msDelegationName string) (*RecordNS, error)
CreateZoneAuth(fqdn string, ea EA) (*ZoneAuth, error)
CreateCNAMERecord(dnsview string, canonical string, recordname string, useTtl bool, ttl uint32, comment string, eas EA) (*RecordCNAME, error)
CreateDefaultNetviews(globalNetview string, localNetview string) (globalNetviewRef string, localNetviewRef string, err error)
Expand All @@ -41,6 +42,7 @@ type IBObjectManager interface {
CreateTXTRecord(dnsView string, recordName string, text string, ttl uint32, useTtl bool, comment string, eas EA) (*RecordTXT, error)
CreateZoneDelegated(fqdn string, delegateTo NullableNameServers, comment string, disable bool, locked bool, nsGroup string, delegatedTtl uint32, useDelegatedTtl bool, ea EA, view string, zoneFormat string) (*ZoneDelegated, error)
DeleteARecord(ref string) (string, error)
DeleteNSRecord(ref string) (string, error)
DeleteAAAARecord(ref string) (string, error)
DeleteDtcLbdn(ref string) (string, error)
DeleteDtcPool(ref string) (string, error)
Expand All @@ -64,6 +66,8 @@ type IBObjectManager interface {
GetAAAARecordByRef(ref string) (*RecordAAAA, error)
GetCNAMERecord(dnsview string, canonical string, recordName string) (*RecordCNAME, error)
GetCNAMERecordByRef(ref string) (*RecordCNAME, error)
GetNSRecordByRef(ref string) (*RecordNS, error)
GetAllRecordNS(queryParams *QueryParams) ([]RecordNS, error)
GetAllDtcPool(queryParams *QueryParams) ([]DtcPool, error)
GetDtcPool(name string) (*DtcPool, error)
GetAllDtcServer(queryParams *QueryParams) ([]DtcServer, error)
Expand Down Expand Up @@ -124,6 +128,7 @@ type IBObjectManager interface {
UpdateTXTRecord(ref string, recordName string, text string, ttl uint32, useTtl bool, 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, delegateTo NullableNameServers, comment string, disable bool, locked bool, nsGroup string, delegatedTtl uint32, useDelegatedTtl bool, ea EA) (*ZoneDelegated, error)
UpdateNSRecord(ref string, name string, nameServer string, view string, addresses []*ZoneNameServer, msDelegationName string) (*RecordNS, error)
UpdateZoneForward(ref string, comment string, disable bool, eas EA, forwardTo NullableNameServers, forwardersOnly bool, forwardingServers *NullableForwardingServers, nsGroup string, externalNsGroup string) (*ZoneForward, error)
GetDnsMember(ref string) ([]Dns, error)
UpdateDnsStatus(ref string, status bool) (Dns, error)
Expand Down
84 changes: 84 additions & 0 deletions object_manager_ns_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package ibclient

import (
"encoding/json"
"fmt"
)

func (z ZoneNameServer) MarshalJSON() ([]byte, error) {
type Alias ZoneNameServer
return json.Marshal(&struct {
*Alias
AutoCreatePtr bool `json:"auto_create_ptr"`
}{
Alias: (*Alias)(&z),
AutoCreatePtr: z.AutoCreatePtr,
})
}
func NewRecordNS(name string, nameServer string, view string, addresses []*ZoneNameServer, msDelegationName string) *RecordNS {
res := NewEmptyRecordNS()
res.Name = name
res.Nameserver = &nameServer
res.View = view
res.Addresses = addresses
res.MsDelegationName = &msDelegationName
return res
}

func NewEmptyRecordNS() *RecordNS {
res := &RecordNS{}
res.SetReturnFields(append(res.returnFields, "addresses", "cloud_info", "creator", "dns_name", "last_queried", "ms_delegation_name", "name", "nameserver", "policy", "view", "zone"))
return res
}

func (objMgr *ObjectManager) CreateNSRecord(name string, nameServer string, view string, addresses []*ZoneNameServer, msDelegationName string) (*RecordNS, error) {
if name == "" || nameServer == "" || len(addresses) == 0 {
fmt.Errorf("name, nameserver and addresses are required to create NS record")
}
if view == "" {
view = "default"
}
nsRecord := NewRecordNS(name, nameServer, view, addresses, msDelegationName)
ref, err := objMgr.connector.CreateObject(nsRecord)
if err != nil {
return nil, err
}
nsRecord.Ref = ref
return nsRecord, nil
}

func (objMgr *ObjectManager) GetNSRecordByRef(ref string) (*RecordNS, error) {
recordNS := NewEmptyRecordNS()
err := objMgr.connector.GetObject(
recordNS, ref, NewQueryParams(false, nil), &recordNS)
return recordNS, err
}

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

func (objMgr *ObjectManager) UpdateNSRecord(ref string, name string, nameServer string, view string, addresses []*ZoneNameServer, msDelegationName string) (*RecordNS, error) {
nsRecord := NewRecordNS(name, nameServer, view, addresses, msDelegationName)
nsRecord.Ref = ref

ref, err := objMgr.connector.UpdateObject(nsRecord, ref)
if err != nil {
return nil, err
}
newRec, err := objMgr.GetNSRecordByRef(ref)
if err != nil {
return nil, err
}
return newRec, nil
}

func (objMgr *ObjectManager) GetAllRecordNS(queryParams *QueryParams) ([]RecordNS, error) {
var res []RecordNS
recordNS := NewEmptyRecordNS()
err := objMgr.connector.GetObject(recordNS, "", queryParams, &res)
if err != nil {
return nil, fmt.Errorf("error getting Record NS object, err: %s", err)
}
return res, nil
}
Loading