Skip to content

Commit

Permalink
Merge pull request #14 from HewlettPackard/groupsNewFormat
Browse files Browse the repository at this point in the history
DE9792: Groups Code new format
  • Loading branch information
gandharvas authored Oct 7, 2021
2 parents cc3d486 + 942c917 commit 965e542
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 126 deletions.
155 changes: 35 additions & 120 deletions pkg/client/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -170,69 +169,30 @@ Get a Specific Group
*/
func (a *GroupsAPIService) GetASpecificGroup(ctx context.Context, groupID int) (models.GroupResp, error) {
var (
localVarHTTPMethod = strings.ToUpper("Get")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
)

// create path and map variables
localVarPath := fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath,
consts.GroupsPath, groupID)

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := url.Values{}
if groupID < 1 {
return models.GroupResp{}, reportError("groupID must be greater than 1")
}

// to determine the Content-Type header
localVarHTTPContentTypes := []string{}

// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}

// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json"}

// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}

r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody,
localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return models.GroupResp{}, err
}

localVarHTTPResponse, err := a.Client.callAPI(r)
if err != nil || localVarHTTPResponse == nil {
return models.GroupResp{}, err
}

if localVarHTTPResponse.StatusCode >= 300 {
return models.GroupResp{}, ParseError(localVarHTTPResponse)
}

localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return models.GroupResp{}, err
}

var groupResp models.GroupResp
if err = json.Unmarshal(localVarBody, &groupResp); err != nil {
return models.GroupResp{}, err
}

return groupResp, nil
specificGrpResp := models.GroupResp{}

specificGrpRespAPI := &api{
method: "GET",
path: fmt.Sprintf("%s/%s/%s/%d", a.Cfg.Host, consts.VmaasCmpAPIBasePath,
consts.GroupsPath, groupID),
client: a.Client,

jsonParser: func(body []byte) error {
return json.Unmarshal(body, &specificGrpResp)
},
validations: []validationFunc{
func() error {
if groupID < 1 {
return fmt.Errorf("%s", "group id should be greater than or equal to 1")
}

return nil
},
},
}
err := specificGrpRespAPI.do(ctx, nil, nil)

return specificGrpResp, err
}

/*
Expand All @@ -245,66 +205,21 @@ This endpoint retrieves all groups and a list of zones associated with the group
*/
func (a *GroupsAPIService) GetAllGroups(ctx context.Context,
queryParams map[string]string) (models.Groups, error) {
var (
localVarHTTPMethod = strings.ToUpper("Get")
localVarPostBody interface{}
localVarFileName string
localVarFileBytes []byte
)

// create path and map variables
localVarPath := fmt.Sprintf("%s/%s/%s", a.Cfg.Host, consts.VmaasCmpAPIBasePath,
consts.GroupsPath)

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
localVarFormParams := getURLValues(queryParams)

// to determine the Content-Type header
localVarHTTPContentTypes := []string{}

// set Content-Type header
localVarHTTPContentType := selectHeaderContentType(localVarHTTPContentTypes)
if localVarHTTPContentType != "" {
localVarHeaderParams["Content-Type"] = localVarHTTPContentType
}
allGrpResp := models.Groups{}

// to determine the Accept header
localVarHTTPHeaderAccepts := []string{"application/json"}

// set Accept header
localVarHTTPHeaderAccept := selectHeaderAccept(localVarHTTPHeaderAccepts)
if localVarHTTPHeaderAccept != "" {
localVarHeaderParams["Accept"] = localVarHTTPHeaderAccept
}

r, err := a.Client.prepareRequest(ctx, localVarPath, localVarHTTPMethod, localVarPostBody,
localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return models.Groups{}, err
}

localVarHTTPResponse, err := a.Client.callAPI(r)
if err != nil || localVarHTTPResponse == nil {
return models.Groups{}, err
}

if localVarHTTPResponse.StatusCode >= 300 {
return models.Groups{}, ParseError(localVarHTTPResponse)
}

localVarBody, err := ioutil.ReadAll(localVarHTTPResponse.Body)
defer localVarHTTPResponse.Body.Close()
if err != nil {
return models.Groups{}, err
}
allGrpRespAPI := &api{
method: "GET",
path: fmt.Sprintf("%s/%s/%s", a.Cfg.Host, consts.VmaasCmpAPIBasePath,
consts.GroupsPath),
client: a.Client,

var groupsResp models.Groups
if err = json.Unmarshal(localVarBody, &groupsResp); err != nil {
return models.Groups{}, err
jsonParser: func(body []byte) error {
return json.Unmarshal(body, &allGrpResp)
},
}
err := allGrpRespAPI.do(ctx, nil, queryParams)

return groupsResp, nil
return allGrpResp, err
}

/*
Expand Down
12 changes: 6 additions & 6 deletions pkg/client/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ func TestGroupsAPIService_GetAllGroups(t *testing.T) {
}]
}
`)))
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{},
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers,
getURLValues(map[string]string{
"name": templateName,
}), "", nil).Return(req, nil)
}), url.Values{}, "", nil).Return(req, nil)

m.EXPECT().callAPI(req).Return(&http.Response{
StatusCode: 200,
Expand All @@ -187,10 +187,10 @@ func TestGroupsAPIService_GetAllGroups(t *testing.T) {
path := mockHost + "/v1/groups"
method := "GET"
headers := getDefaultHeaders()
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{},
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers,
getURLValues(map[string]string{
"name": templateName,
}), "", nil).Return(nil, errors.New("prepare request error"))
}), url.Values{}, "", nil).Return(nil, errors.New("prepare request error"))
},
want: models.Groups{},
wantErr: true,
Expand All @@ -213,10 +213,10 @@ func TestGroupsAPIService_GetAllGroups(t *testing.T) {
]
}
`)))
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers, url.Values{},
m.EXPECT().prepareRequest(gomock.Any(), path, method, nil, headers,
getURLValues(map[string]string{
"name": templateName,
}), "", nil).Return(req, nil)
}), url.Values{}, "", nil).Return(req, nil)

m.EXPECT().callAPI(req).Return(&http.Response{
StatusCode: 500,
Expand Down

0 comments on commit 965e542

Please sign in to comment.