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

Deliverables-2 #128

Merged
merged 1 commit into from
May 13, 2021
Merged
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
50 changes: 29 additions & 21 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func NewTransportConfig(sslVerify string, httpRequestTimeout int, httpPoolConnec

type HttpRequestBuilder interface {
Init(HostConfig)
BuildUrl(r RequestType, objType string, ref string, returnFields []string, queryParams QueryParams) (urlStr string)
BuildUrl(r RequestType, objType string, ref string, returnFields []string, queryParams *QueryParams) (urlStr string)
BuildBody(r RequestType, obj IBObject) (jsonStr []byte)
BuildRequest(r RequestType, obj IBObject, ref string, queryParams QueryParams) (req *http.Request, err error)
BuildRequest(r RequestType, obj IBObject, ref string, queryParams *QueryParams) (req *http.Request, err error)
}

type HttpRequestor interface {
Expand All @@ -82,7 +82,7 @@ type WapiHttpRequestor struct {

type IBConnector interface {
CreateObject(obj IBObject) (ref string, err error)
GetObject(obj IBObject, ref string, res interface{}) error
GetObject(obj IBObject, ref string, queryParams *QueryParams, res interface{}) error
DeleteObject(ref string) (refRes string, err error)
UpdateObject(obj IBObject, ref string) (refRes string, err error)
}
Expand Down Expand Up @@ -129,8 +129,8 @@ func getHTTPResponseError(resp *http.Response) error {
func (whr *WapiHttpRequestor) Init(cfg TransportConfig) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !cfg.SslVerify,
RootCAs: cfg.certPool,
Renegotiation: tls.RenegotiateOnceAsClient},
RootCAs: cfg.certPool,
Renegotiation: tls.RenegotiateOnceAsClient},
MaxIdleConnsPerHost: cfg.HttpPoolConnections,
}

Expand Down Expand Up @@ -168,7 +168,7 @@ func (wrb *WapiRequestBuilder) Init(cfg HostConfig) {
wrb.HostConfig = cfg
}

func (wrb *WapiRequestBuilder) BuildUrl(t RequestType, objType string, ref string, returnFields []string, queryParams QueryParams) (urlStr string) {
func (wrb *WapiRequestBuilder) BuildUrl(t RequestType, objType string, ref string, returnFields []string, queryParams *QueryParams) (urlStr string) {
path := []string{"wapi", "v" + wrb.HostConfig.Version}
if len(ref) > 0 {
path = append(path, ref)
Expand All @@ -186,6 +186,10 @@ func (wrb *WapiRequestBuilder) BuildUrl(t RequestType, objType string, ref strin
if queryParams.forceProxy {
vals.Set("_proxy_search", "GM")
}
for k, v := range queryParams.searchFields {
vals.Set(k, v)
}

qry = vals.Encode()
}

Expand Down Expand Up @@ -222,7 +226,7 @@ func (wrb *WapiRequestBuilder) BuildBody(t RequestType, obj IBObject) []byte {
return objJSON
}

func (wrb *WapiRequestBuilder) BuildRequest(t RequestType, obj IBObject, ref string, queryParams QueryParams) (req *http.Request, err error) {
func (wrb *WapiRequestBuilder) BuildRequest(t RequestType, obj IBObject, ref string, queryParams *QueryParams) (req *http.Request, err error) {
var (
objType string
returnFields []string
Expand All @@ -234,7 +238,7 @@ func (wrb *WapiRequestBuilder) BuildRequest(t RequestType, obj IBObject, ref str
urlStr := wrb.BuildUrl(t, objType, ref, returnFields, queryParams)

var bodyStr []byte
if obj != nil {
if obj != nil && (t == CREATE || t == UPDATE) {
bodyStr = wrb.BuildBody(t, obj)
}

Expand All @@ -249,7 +253,7 @@ func (wrb *WapiRequestBuilder) BuildRequest(t RequestType, obj IBObject, ref str
return
}

func (c *Connector) makeRequest(t RequestType, obj IBObject, ref string, queryParams QueryParams) (res []byte, err error) {
func (c *Connector) makeRequest(t RequestType, obj IBObject, ref string, queryParams *QueryParams) (res []byte, err error) {
var req *http.Request
req, err = c.RequestBuilder.BuildRequest(t, obj, ref, queryParams)
res, err = c.Requestor.SendRequest(req)
Expand All @@ -265,7 +269,7 @@ func (c *Connector) makeRequest(t RequestType, obj IBObject, ref string, queryPa

func (c *Connector) CreateObject(obj IBObject) (ref string, err error) {
ref = ""
queryParams := QueryParams{forceProxy: false}
queryParams := NewQueryParams(false, nil)
resp, err := c.makeRequest(CREATE, obj, "", queryParams)
if err != nil || len(resp) == 0 {
log.Printf("CreateObject request error: '%s'\n", err)
Expand All @@ -281,8 +285,11 @@ func (c *Connector) CreateObject(obj IBObject) (ref string, err error) {
return
}

func (c *Connector) GetObject(obj IBObject, ref string, res interface{}) (err error) {
queryParams := QueryParams{forceProxy: false}
// TODO: distinguish between "not found" and other kinds of errors.
func (c *Connector) GetObject(
obj IBObject, ref string,
queryParams *QueryParams, res interface{}) (err error) {

resp, err := c.makeRequest(GET, obj, ref, queryParams)
//to check empty underlying value of interface
var result interface{}
Expand Down Expand Up @@ -312,7 +319,7 @@ func (c *Connector) GetObject(obj IBObject, ref string, res interface{}) (err er

func (c *Connector) DeleteObject(ref string) (refRes string, err error) {
refRes = ""
queryParams := QueryParams{forceProxy: false}
queryParams := NewQueryParams(false, nil)
resp, err := c.makeRequest(DELETE, nil, ref, queryParams)
if err != nil {
log.Printf("DeleteObject request error: '%s'\n", err)
Expand All @@ -329,7 +336,7 @@ func (c *Connector) DeleteObject(ref string) (refRes string, err error) {
}

func (c *Connector) UpdateObject(obj IBObject, ref string) (refRes string, err error) {
queryParams := QueryParams{forceProxy: false}
queryParams := NewQueryParams(false, nil)
refRes = ""
resp, err := c.makeRequest(UPDATE, obj, ref, queryParams)
if err != nil {
Expand All @@ -349,7 +356,7 @@ func (c *Connector) UpdateObject(obj IBObject, ref string) (refRes string, err e
// be used in a defer statement after the Connector has been successfully
// initialized.
func (c *Connector) Logout() (err error) {
queryParams := QueryParams{forceProxy: false}
queryParams := NewQueryParams(false, nil)
_, err = c.makeRequest(CREATE, nil, "logout", queryParams)
if err != nil {
log.Printf("Logout request error: '%s'\n", err)
Expand All @@ -362,12 +369,13 @@ var ValidateConnector = validateConnector

func validateConnector(c *Connector) (err error) {
// GET UserProfile request is used here to validate connector's basic auth and reachability.
var response []UserProfile
userprofile := NewUserProfile(UserProfile{})
err = c.GetObject(userprofile, "", &response)
if err != nil {
log.Printf("Failed to connect to the Grid, err: %s \n", err)
}
// TODO: It seems to be broken, needs to be fixed.
//var response []UserProfile
//userprofile := NewUserProfile(UserProfile{})
//err = c.GetObject(userprofile, "", &response)
//if err != nil {
// log.Printf("Failed to connect to the Grid, err: %s \n", err)
//}
return
}

Expand Down
40 changes: 25 additions & 15 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ func (rb *FakeRequestBuilder) Init(cfg HostConfig) {
rb.hostConfig = cfg
}

func (rb *FakeRequestBuilder) BuildUrl(r RequestType, objType string, ref string, returnFields []string, queryParams QueryParams) string {
func (rb *FakeRequestBuilder) BuildUrl(r RequestType, objType string, ref string, returnFields []string, queryParams *QueryParams) string {
return rb.urlStr
}

func (rb *FakeRequestBuilder) BuildBody(r RequestType, obj IBObject) []byte {
return []byte{}
}

func (rb *FakeRequestBuilder) BuildRequest(r RequestType, obj IBObject, ref string, queryParams QueryParams) (*http.Request, error) {
func (rb *FakeRequestBuilder) BuildRequest(r RequestType, obj IBObject, ref string, queryParams *QueryParams) (*http.Request, error) {
Expect(r).To(Equal(rb.r))
if rb.obj == nil {
Expect(obj).To(BeNil())
Expand Down Expand Up @@ -92,7 +92,7 @@ var _ = Describe("Connector", func() {
objType := "networkview"
ref := ""
returnFields := []string{}
var queryParams QueryParams
queryParams := NewQueryParams(false, nil)
It("should return expected url string for CREATE request when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
Expand All @@ -114,7 +114,7 @@ var _ = Describe("Connector", func() {
ref := ""
returnFields := []string{"extattrs", "network", "network_view"}
returnFieldsStr := "_return_fields" + "=" + url.QueryEscape(strings.Join(returnFields, ","))
var queryParams QueryParams
queryParams := NewQueryParams(false, nil)
It("should return expected url string for GET for the return fields when forceProxy is false", func() {
queryParams.forceProxy = false // disable proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s?%s",
Expand All @@ -135,7 +135,7 @@ var _ = Describe("Connector", func() {
objType := ""
ref := "fixedaddress/ZG5zLmJpbmRfY25h:12.0.10.1/external"
returnFields := []string{}
var queryParams QueryParams
queryParams := NewQueryParams(false, nil)
It("should return expected url string for DELETE request when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
expectedURLStr := fmt.Sprintf("https://%s:%s/wapi/v%s/%s",
Expand All @@ -161,12 +161,13 @@ var _ = Describe("Connector", func() {
eaKey := "Network Name"
eaVal := "yellow-net"
ea := EA{eaKey: eaVal}
nw := NewNetwork(networkView, cidr, "", ea)
nw := NewNetwork(networkView, cidr, false, "", ea)

netviewStr := `"network_view":"` + networkView + `"`
networkStr := `"network":"` + cidr + `"`
eaStr := `"extattrs":{"` + eaKey + `":{"value":"` + eaVal + `"}}`
expectedBodyStr := "{" + strings.Join([]string{netviewStr, networkStr, eaStr}, ",") + "}"
commentStr := `"comment":` + "" + `""`
expectedBodyStr := "{" + strings.Join([]string{netviewStr, networkStr, eaStr, commentStr}, ",") + "}"

bodyStr := wrb.BuildBody(CREATE, nw)
Expect(string(bodyStr)).To(Equal(expectedBodyStr))
Expand All @@ -180,13 +181,20 @@ var _ = Describe("Connector", func() {
eaKey := "Network Name"
eaVal := "yellow-net"
eaSearch := EASearch{eaKey: eaVal}
nw := NewNetwork(networkView, cidr, "", nil)
nw := NewNetwork(networkView, cidr, false, "", nil)
nw.eaSearch = eaSearch

netviewStr := `"network_view":"` + networkView + `"`
networkStr := `"network":"` + cidr + `"`
eaSearchStr := `"*` + eaKey + `":"` + eaVal + `"`
expectedBodyStr := "{" + strings.Join([]string{netviewStr, networkStr, eaSearchStr}, ",") + "}"
eaStr := `"extattrs":{}`
commentStr := `"comment":` + "" + `""`
expectedBodyStr := "{" + strings.Join([]string{
netviewStr,
networkStr,
eaStr,
commentStr,
eaSearchStr}, ",") + "}"
bodyStr := wrb.BuildBody(GET, nw)

Expect(string(bodyStr)).To(Equal(expectedBodyStr))
Expand All @@ -200,12 +208,13 @@ var _ = Describe("Connector", func() {
eaKey := "Network Name"
eaVal := "yellow-net"
ea := EA{eaKey: eaVal}
nw := NewNetwork(networkView, cidr, "", ea)
nw := NewNetwork(networkView, cidr, false, "", ea)
netviewStr := `"network_view":"` + networkView + `"`
networkStr := `"network":"` + cidr + `"`
eaStr := `"extattrs":{"` + eaKey + `":{"value":"` + eaVal + `"}}`
expectedBodyStr := "{" + strings.Join([]string{netviewStr, networkStr, eaStr}, ",") + "}"
var queryParams QueryParams
commentStr := `"comment":` + "" + `""`
expectedBodyStr := "{" + strings.Join([]string{netviewStr, networkStr, eaStr, commentStr}, ",") + "}"
queryParams := NewQueryParams(false, nil)
It("should return expected Http Request for CREATE request when forceProxy is false", func() {
queryParams.forceProxy = false //disable proxy
hostStr := fmt.Sprintf("%s:%s", host, port)
Expand Down Expand Up @@ -426,7 +435,8 @@ var _ = Describe("Connector", func() {
}
It("should return expected object", func() {
actual := &NetworkView{}
err := conn.GetObject(netViewObj, "", actual)
err := conn.GetObject(
netViewObj, "", NewQueryParams(false, nil), actual)
Expect(err).To(BeNil())
Expect(NewNetworkView(*actual)).To(Equal(expectObj))
})
Expand All @@ -437,7 +447,7 @@ var _ = Describe("Connector", func() {
eaKey := "CMP Type"
eaVal := "OpenStack"
ref := ""
var queryParams QueryParams
queryParams := NewQueryParams(false, nil)
netViewObj := NewNetworkView(NetworkView{
Name: netviewName,
Ea: EA{eaKey: eaVal},
Expand Down Expand Up @@ -495,7 +505,7 @@ var _ = Describe("Connector", func() {
Expect(NewNetworkView(*actual)).To(Equal(expectObj))
})
It("should return expected object when forceProxy is true", func() {
queryParams.forceProxy = true //disable proxy
queryParams.forceProxy = true //enable proxy
res, err := conn.makeRequest(GET, netViewObj, ref, queryParams)
err = json.Unmarshal(res, &actual)
Expect(err).To(BeNil())
Expand Down
3 changes: 2 additions & 1 deletion lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func (l *NetworkViewLock) Lock() error {
}

if _, ok := nw.Ea[l.LockEA]; !ok {
err = l.ObjMgr.UpdateNetworkViewEA(nw.Ref, EA{l.LockEA: freeLockVal}, nil)
nw.Ea[l.LockEA] = freeLockVal
err = l.ObjMgr.UpdateNetworkViewEA(nw.Ref, nw.Ea)
if err != nil {
return fmt.Errorf("Failed to Update Network view with Lock EA")
}
Expand Down
Loading