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

adding wrapper method for Range object #222

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions object_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type IBObjectManager interface {
UpdateDnsStatus(ref string, status bool) (Dns, error)
GetDhcpMember(ref string) ([]Dhcp, error)
UpdateDhcpStatus(ref string, status bool) (Dhcp, error)
CreateRange(netView, name, startAddr, endAddr, comment string, disable bool, exclude []*Exclusionrange) (*Range, error)
UpdateRange(ref, name, startAddr, endAddr, comment string, disable bool, exclude []*Exclusionrange) (*Range, error)
GetRange(ref string) (*Range, error)
DeleteRange(ref string) error
}

type ObjectManager struct {
Expand Down
74 changes: 74 additions & 0 deletions object_manager_range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package ibclient

import (
"fmt"
)

func (objMgr *ObjectManager) CreateRange(netViewName, name, startAddr, endAddr, comment string, disable bool, exclude []*Exclusionrange) (*Range, error) {
newRange := NewEmptyRange()
newRange.Name = &name
newRange.StartAddr = &startAddr
newRange.EndAddr = &endAddr
newRange.Comment = &comment
newRange.NetworkView = &netViewName
newRange.Exclude = exclude
newRange.Disable = &disable

refCreatedObj, err := objMgr.connector.CreateObject(newRange)
if err != nil {
return nil, err
}

return objMgr.GetRange(refCreatedObj)
}

func (objMgr *ObjectManager) UpdateRange(ref, name, startAddr, endAddr, comment string, disable bool, exclude []*Exclusionrange) (*Range, error) {
r := &Range{
Comment: &comment,
Disable: &disable,
}

if len(exclude) > 0 {
r.Exclude = exclude
}

if len(startAddr) > 0 {
r.StartAddr = &startAddr
}

if len(endAddr) > 0 {
r.EndAddr = &endAddr
}

if len(name) > 0 {
r.Name = &name
}

updateRef, err := objMgr.connector.UpdateObject(r, ref)
if err != nil {
return nil, err
}

return objMgr.GetRange(updateRef)
}

func (objMgr *ObjectManager) GetRange(ref string) (*Range, error) {
var res []Range
search := &Range{Ref: ref}
qp := NewQueryParams(false, nil)
err := objMgr.connector.GetObject(search, ref, qp, &res)
if err != nil {
return nil, err
} else if res == nil || len(res) == 0 {
return nil, NewNotFoundError(
fmt.Sprintf(
"could not find range object"))
}

return &res[0], nil
}

func (objMgr *ObjectManager) DeleteRange(ref string) error {
_, err := objMgr.connector.DeleteObject(ref)
return err
}
61 changes: 61 additions & 0 deletions object_manager_range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ibclient

import (
"fmt"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Object Manager: range create", func() {
Describe("Create IP Range", func() {
cmpType := "Docker"
tenantID := "01234567890abcdef01234567890abcdef"
netviewName := "private"
comment := "test"

startAddr := "10.10.0.1"
endAddr := "10.10.0.100"
rangeName := "rangeObj"
disable := false

fakeRefReturn := fmt.Sprintf("range/ZG5zLmJpbmRfY25h:%s/%s/%s", startAddr, endAddr, netviewName)

rangeObj := NewEmptyRange()
rangeObj.StartAddr = &startAddr
rangeObj.EndAddr = &endAddr
rangeObj.Name = &rangeName
rangeObj.Comment = &comment
rangeObj.NetworkView = &netviewName
rangeObj.Disable = &disable

resultObj := NewEmptyRange()
resultObj.StartAddr = &startAddr
resultObj.EndAddr = &endAddr
resultObj.Name = &rangeName
resultObj.NetworkView = &netviewName
resultObj.Comment = &comment
resultObj.Disable = &disable

getRangeObj := NewEmptyRange()
getRangeObj.Ref = fakeRefReturn

conn := &fakeConnector{
createObjectObj: rangeObj,
getObjectObj: getRangeObj,
getObjectRef: fakeRefReturn,
getObjectQueryParams: NewQueryParams(false, nil),
resultObject: []Range{*resultObj},
fakeRefReturn: fakeRefReturn,
}

objMgr := NewObjectManager(conn, cmpType, tenantID)

It("should pass expected Range to CreateObject", func() {
createdRange, err := objMgr.CreateRange(netviewName, rangeName, startAddr, endAddr, comment, disable, nil)
Expect(err).To(BeNil())
Expect(createdRange.EndAddr).To(Equal(resultObj.EndAddr))
Expect(createdRange.NetworkView).To(Equal(resultObj.NetworkView))
})
})
})
4 changes: 4 additions & 0 deletions object_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 *Range:
*res.(*[]Range) = c.resultObject.([]Range)
}
} else {
switch obj.(type) {
Expand Down Expand Up @@ -121,6 +123,8 @@ func (c *fakeConnector) GetObject(obj IBObject, ref string, qp *QueryParams, res
*res.(*[]Dns) = c.resultObject.([]Dns)
case *Dhcp:
*res.(*[]Dhcp) = c.resultObject.([]Dhcp)
case *Range:
*res.(*[]Range) = c.resultObject.([]Range)
}
}

Expand Down
4 changes: 4 additions & 0 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,7 @@ func NewDhcp(dhcp Dhcp) *Dhcp {
result.returnFields = returnFields
return &result
}

func NewEmptyRange() *Range {
return &Range{}
}