From f22ab29dff2b69aaadd748c34c259f25dacec52d Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Wed, 7 Aug 2019 15:45:54 -0700 Subject: Add query parameters support to instance API Add support for query parameters to find instances with rb-name, rb-version, profile-name as query parameters eg: curl IP:PORT/v1/instance?rb-name=test&rb-version=v1 Issue-ID: MULTICLOUD-662 Change-Id: If68c87885c8b8dd097cbb3860e8c7320310a6ef7 Signed-off-by: Kiran Kamineni --- src/k8splugin/api/instancehandler_test.go | 49 +++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'src/k8splugin/api/instancehandler_test.go') diff --git a/src/k8splugin/api/instancehandler_test.go b/src/k8splugin/api/instancehandler_test.go index 83fa3d2b..418054ec 100644 --- a/src/k8splugin/api/instancehandler_test.go +++ b/src/k8splugin/api/instancehandler_test.go @@ -60,7 +60,7 @@ func (m *mockInstanceClient) Get(id string) (app.InstanceResponse, error) { return m.items[0], nil } -func (m *mockInstanceClient) List() ([]app.InstanceMiniResponse, error) { +func (m *mockInstanceClient) List(rbname, rbversion, profilename string) ([]app.InstanceMiniResponse, error) { if m.err != nil { return []app.InstanceMiniResponse{}, m.err } @@ -68,12 +68,12 @@ func (m *mockInstanceClient) List() ([]app.InstanceMiniResponse, error) { return m.miniitems, nil } -func (m *mockInstanceClient) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]app.InstanceResponse, error) { +func (m *mockInstanceClient) Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]app.InstanceMiniResponse, error) { if m.err != nil { return nil, m.err } - return m.items, nil + return m.miniitems, nil } func (m *mockInstanceClient) Delete(id string) error { @@ -312,6 +312,8 @@ func TestInstanceListHandler(t *testing.T) { label string input string expectedCode int + queryParams bool + queryParamsMap map[string]string expectedResponse []app.InstanceMiniResponse instClient *mockInstanceClient }{ @@ -373,11 +375,52 @@ func TestInstanceListHandler(t *testing.T) { }, }, }, + { + label: "List Instances Based on Query Parameters", + queryParams: true, + queryParamsMap: map[string]string{ + "rb-name": "test-rbdef1", + }, + expectedCode: http.StatusOK, + expectedResponse: []app.InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: app.InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", + }, + }, + instClient: &mockInstanceClient{ + miniitems: []app.InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: app.InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + }, + Namespace: "testnamespace", + }, + }, + }, + }, } for _, testCase := range testCases { t.Run(testCase.label, func(t *testing.T) { request := httptest.NewRequest("GET", "/v1/instance", nil) + if testCase.queryParams { + q := request.URL.Query() + for k, v := range testCase.queryParamsMap { + q.Add(k, v) + } + request.URL.RawQuery = q.Encode() + } resp := executeRequest(request, NewRouter(nil, nil, testCase.instClient, nil, nil, nil)) if testCase.expectedCode != resp.StatusCode { -- cgit 1.2.3-korg