From 1a6fa14a409b2cdec7caef8ed2d49eeacc58749c Mon Sep 17 00:00:00 2001 From: Sudarshan Das Date: Tue, 29 Aug 2023 18:00:22 +0530 Subject: [PATCH 1/3] Implemented a function to fetch information about ip address. --- object_manager.go | 26 ++++++++++++++++++++++++++ object_manager_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/object_manager.go b/object_manager.go index 14f4c289..52ed15bc 100644 --- a/object_manager.go +++ b/object_manager.go @@ -91,6 +91,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, delegate_to []NameServer) (*ZoneDelegated, error) + GetIPAddressInfo(ipAddr string) ([]IPv4Address, error) } type ObjectManager struct { @@ -297,3 +298,28 @@ func (objMgr *ObjectManager) UpdateZoneDelegated(ref string, delegate_to []NameS func (objMgr *ObjectManager) DeleteZoneDelegated(ref string) (string, error) { return objMgr.connector.DeleteObject(ref) } + +// GetIPAddressInfo returns information related to an ipv4 address +func (objMgr *ObjectManager) GetIPAddressInfo(ipAddr string) ([]IPv4Address, error) { + if ipAddr == "" { + return nil, fmt.Errorf("'IP address should not be empty") + } + + sf := map[string]string{ + "ip_address": ipAddr, + } + + var res []IPv4Address + ipv4Info := &IPv4Address{} + + err := objMgr.connector.GetObject( + ipv4Info, "", NewQueryParams(false, sf), &res) + + if err != nil { + return nil, err + } else if res == nil || len(res) == 0 { + return nil, NewNotFoundError(fmt.Sprintf("Information for IPv4 address '%s' is not found", ipAddr)) + } + + return res, err +} diff --git a/object_manager_test.go b/object_manager_test.go index 7a487be1..38e481f5 100644 --- a/object_manager_test.go +++ b/object_manager_test.go @@ -88,6 +88,8 @@ func (c *fakeConnector) GetObject(obj IBObject, ref string, qp *QueryParams, res *res.(*[]RecordA) = c.resultObject.([]RecordA) case *RecordMX: *res.(*[]RecordMX) = c.resultObject.([]RecordMX) + case *IPv4Address: + *res.(*[]IPv4Address) = c.resultObject.([]IPv4Address) } } else { switch obj.(type) { @@ -521,4 +523,43 @@ var _ = Describe("Object Manager", func() { Expect(err).To(BeNil()) }) }) + + Describe("Get IPAddressInfo", func() { + cmpType := "Docker" + tenantID := "01234567890abcdef01234567890abcdef" + sf := map[string]string{ + "ip_address": "10.197.38.235", + } + zdFakeConnector := &fakeConnector{ + getObjectQueryParams: NewQueryParams(false, sf), + getObjectObj: &IPv4Address{}, + resultObject: []IPv4Address{{ + IBBase: IBBase{}, + Ref: "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuMTk3LjM4LjIzNS8w:10.197.38.235", + IpAddress: "10.197.38.235", + Names: []string{"custom.example.k8s.org", "infoblox.localdomain"}, + Network: "10.197.38.0/24", + NetworkView: "default", + Objects: []string{ + "record:a/ZG5zLmJpbmRfYSQuMS5vcmcuazhzLmV4YW1wbGUsY3VzdG9tLDEwLjE5Ny4zOC4yMzU:custom.example.k8s.org/non-default", + "record:ptr/ZG5zLmJpbmRfcHRyJC5fZGVmYXVsdC5hcnBhLmluLWFkZHIuMTAuMTk3LjM4LjIzNS5pbmZvYmxveC5sb2NhbGRvbWFpbg:235.38.197.10.in-addr.arpa/default", + "record:ptr/ZG5zLmJpbmRfcHRyJC4xLmFycGEuaW4tYWRkci4xMC4xOTcuMzguMjM1LmN1c3RvbS5leGFtcGxlLms4cy5vcmc:235.38.197.10.in-addr.arpa/non-default", + }, + Status: "USED", + Types: []string{"A", "PTR"}, + Usage: []string{"DNS"}, + }}, + } + objMgr := NewObjectManager(zdFakeConnector, cmpType, tenantID) + + var actualObj []IPv4Address + var err error + It("should pass expected get IPAddressInfo", func() { + actualObj, err = objMgr.GetIPAddressInfo("10.197.38.235") + }) + It("Should return IPv4 Address info", func() { + Expect(actualObj).ToNot(BeNil()) + Expect(err).To(BeNil()) + }) + }) }) From 598be5f19434734a1c6950a5fe39a4ad829068c0 Mon Sep 17 00:00:00 2001 From: Sudarshan Das Date: Thu, 31 Aug 2023 16:18:19 +0530 Subject: [PATCH 2/3] Addressed review comments. --- object_manager.go | 4 ++-- object_manager_test.go | 13 +++++-------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/object_manager.go b/object_manager.go index 52ed15bc..93a342a1 100644 --- a/object_manager.go +++ b/object_manager.go @@ -302,7 +302,7 @@ func (objMgr *ObjectManager) DeleteZoneDelegated(ref string) (string, error) { // GetIPAddressInfo returns information related to an ipv4 address func (objMgr *ObjectManager) GetIPAddressInfo(ipAddr string) ([]IPv4Address, error) { if ipAddr == "" { - return nil, fmt.Errorf("'IP address should not be empty") + return nil, fmt.Errorf("empty ipAddr value is not allowed") } sf := map[string]string{ @@ -318,7 +318,7 @@ func (objMgr *ObjectManager) GetIPAddressInfo(ipAddr string) ([]IPv4Address, err if err != nil { return nil, err } else if res == nil || len(res) == 0 { - return nil, NewNotFoundError(fmt.Sprintf("Information for IPv4 address '%s' is not found", ipAddr)) + return nil, NewNotFoundError(fmt.Sprintf("failed to get IPV4 address object '%s' ", ipAddr)) } return res, err diff --git a/object_manager_test.go b/object_manager_test.go index 38e481f5..203ae441 100644 --- a/object_manager_test.go +++ b/object_manager_test.go @@ -528,17 +528,16 @@ var _ = Describe("Object Manager", func() { cmpType := "Docker" tenantID := "01234567890abcdef01234567890abcdef" sf := map[string]string{ - "ip_address": "10.197.38.235", + "ip_address": "dummy", } zdFakeConnector := &fakeConnector{ getObjectQueryParams: NewQueryParams(false, sf), getObjectObj: &IPv4Address{}, resultObject: []IPv4Address{{ - IBBase: IBBase{}, - Ref: "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuMTk3LjM4LjIzNS8w:10.197.38.235", - IpAddress: "10.197.38.235", + Ref: "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuMTk3LjM4LjIzNS8w:dummy", + IpAddress: "dummy", Names: []string{"custom.example.k8s.org", "infoblox.localdomain"}, - Network: "10.197.38.0/24", + Network: "dummy/24", NetworkView: "default", Objects: []string{ "record:a/ZG5zLmJpbmRfYSQuMS5vcmcuazhzLmV4YW1wbGUsY3VzdG9tLDEwLjE5Ny4zOC4yMzU:custom.example.k8s.org/non-default", @@ -555,9 +554,7 @@ var _ = Describe("Object Manager", func() { var actualObj []IPv4Address var err error It("should pass expected get IPAddressInfo", func() { - actualObj, err = objMgr.GetIPAddressInfo("10.197.38.235") - }) - It("Should return IPv4 Address info", func() { + actualObj, err = objMgr.GetIPAddressInfo("dummy") Expect(actualObj).ToNot(BeNil()) Expect(err).To(BeNil()) }) From 2febf69fd1a0d6b71653dba00e1ad5c20bcc30c5 Mon Sep 17 00:00:00 2001 From: Sudarshan Das Date: Fri, 1 Sep 2023 14:09:03 +0530 Subject: [PATCH 3/3] Addressed review comments. --- object_manager_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/object_manager_test.go b/object_manager_test.go index 203ae441..bfc806f1 100644 --- a/object_manager_test.go +++ b/object_manager_test.go @@ -528,16 +528,16 @@ var _ = Describe("Object Manager", func() { cmpType := "Docker" tenantID := "01234567890abcdef01234567890abcdef" sf := map[string]string{ - "ip_address": "dummy", + "ip_address": "10.38.197.235", } zdFakeConnector := &fakeConnector{ getObjectQueryParams: NewQueryParams(false, sf), getObjectObj: &IPv4Address{}, resultObject: []IPv4Address{{ - Ref: "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuMTk3LjM4LjIzNS8w:dummy", - IpAddress: "dummy", + Ref: "ipv4address/Li5pcHY0X2FkZHJlc3MkMTAuMTk3LjM4LjIzNS8w:10.38.197.235", + IpAddress: "10.38.197.235", Names: []string{"custom.example.k8s.org", "infoblox.localdomain"}, - Network: "dummy/24", + Network: "10.38.197.0/24", NetworkView: "default", Objects: []string{ "record:a/ZG5zLmJpbmRfYSQuMS5vcmcuazhzLmV4YW1wbGUsY3VzdG9tLDEwLjE5Ny4zOC4yMzU:custom.example.k8s.org/non-default", @@ -554,7 +554,7 @@ var _ = Describe("Object Manager", func() { var actualObj []IPv4Address var err error It("should pass expected get IPAddressInfo", func() { - actualObj, err = objMgr.GetIPAddressInfo("dummy") + actualObj, err = objMgr.GetIPAddressInfo("10.38.197.235") Expect(actualObj).ToNot(BeNil()) Expect(err).To(BeNil()) })