diff options
author | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-08-07 15:45:54 -0700 |
---|---|---|
committer | Kiran Kamineni <kiran.k.kamineni@intel.com> | 2019-08-13 12:48:48 -0700 |
commit | f22ab29dff2b69aaadd748c34c259f25dacec52d (patch) | |
tree | 1790140cbc7baa5e73f3acf862852f5001e9c0a0 /src/k8splugin/internal/app | |
parent | 845cdd2aa5d790e57b0b2999180df4abc6724bf1 (diff) |
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 <kiran.k.kamineni@intel.com>
Diffstat (limited to 'src/k8splugin/internal/app')
-rw-r--r-- | src/k8splugin/internal/app/instance.go | 92 | ||||
-rw-r--r-- | src/k8splugin/internal/app/instance_test.go | 148 |
2 files changed, 85 insertions, 155 deletions
diff --git a/src/k8splugin/internal/app/instance.go b/src/k8splugin/internal/app/instance.go index 5cfdaea1..cf96d50c 100644 --- a/src/k8splugin/internal/app/instance.go +++ b/src/k8splugin/internal/app/instance.go @@ -60,8 +60,8 @@ type InstanceMiniResponse struct { type InstanceManager interface { Create(i InstanceRequest) (InstanceResponse, error) Get(id string) (InstanceResponse, error) - List() ([]InstanceMiniResponse, error) - Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) + List(rbname, rbversion, profilename string) ([]InstanceMiniResponse, error) + Find(rbName string, ver string, profile string, labelKeys map[string]string) ([]InstanceMiniResponse, error) Delete(id string) error } @@ -184,7 +184,7 @@ func (v *InstanceClient) Get(id string) (InstanceResponse, error) { // List returns the instance for corresponding ID // Empty string returns all -func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { +func (v *InstanceClient) List(rbname, rbversion, profilename string) ([]InstanceMiniResponse, error) { dbres, err := db.DBconn.ReadAll(v.storeName, v.tagInst) if err != nil || len(dbres) == 0 { @@ -192,6 +192,7 @@ func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { } var results []InstanceMiniResponse + for key, value := range dbres { //value is a byte array if value != nil { @@ -206,6 +207,21 @@ func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { Request: resp.Request, Namespace: resp.Namespace, } + + //Filter based on the accepted keys + if len(rbname) != 0 && + miniresp.Request.RBName != rbname { + continue + } + if len(rbversion) != 0 && + miniresp.Request.RBVersion != rbversion { + continue + } + if len(profilename) != 0 && + miniresp.Request.ProfileName != profilename { + continue + } + results = append(results, miniresp) } } @@ -218,70 +234,36 @@ func (v *InstanceClient) List() ([]InstanceMiniResponse, error) { // If profile is empty, it will return all instances for a given rbName+version // If labelKeys are provided, the results are filtered based on that. // It is an AND operation for labelkeys. -func (v *InstanceClient) Find(rbName string, version string, profile string, labelKeys map[string]string) ([]InstanceResponse, error) { +func (v *InstanceClient) Find(rbName string, version string, profile string, labelKeys map[string]string) ([]InstanceMiniResponse, error) { if rbName == "" && len(labelKeys) == 0 { - return []InstanceResponse{}, pkgerrors.New("rbName or labelkeys is required and cannot be empty") + return []InstanceMiniResponse{}, pkgerrors.New("rbName or labelkeys is required and cannot be empty") } - values, err := db.DBconn.ReadAll(v.storeName, v.tagInst) - if err != nil || len(values) == 0 { - return []InstanceResponse{}, pkgerrors.Wrap(err, "Find Instance") + responses, err := v.List(rbName, version, profile) + if err != nil { + return []InstanceMiniResponse{}, pkgerrors.Wrap(err, "Listing Instances") } - response := []InstanceResponse{} - //values is a map[string][]byte -InstanceResponseLoop: - for _, value := range values { - resp := InstanceResponse{} - db.DBconn.Unmarshal(value, &resp) - if err != nil { - return []InstanceResponse{}, pkgerrors.Wrap(err, "Unmarshaling Instance Value") - } + ret := []InstanceMiniResponse{} - // Filter by labels provided - if len(labelKeys) != 0 { - for lkey, lvalue := range labelKeys { - //Check if label key exists and get its value - if val, ok := resp.Request.Labels[lkey]; ok { - if lvalue != val { - continue InstanceResponseLoop - } - } else { - continue InstanceResponseLoop - } - } - } + //filter the list by labelKeys now + for _, resp := range responses { - if rbName != "" { - if resp.Request.RBName == rbName { - - //Check if a version is provided and if it matches - if version != "" { - if resp.Request.RBVersion == version { - //Check if a profilename matches or if it is not provided - if profile == "" || resp.Request.ProfileName == profile { - response = append(response, resp) - } - } - } else { - //Append all versions as version is not provided - response = append(response, resp) - } + add := true + for k, v := range labelKeys { + if resp.Request.Labels[k] != v { + add = false + break } - } else { - response = append(response, resp) } - } - - //filter the list by labelKeys now - for _, value := range response { - for _, label := range labelKeys { - if _, ok := value.Request.Labels[label]; ok { - } + // If label was not found in the response, don't add it + if add { + ret = append(ret, resp) } + } - return response, nil + return ret, nil } // Delete the Instance from database diff --git a/src/k8splugin/internal/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go index 3cb62ee1..b79cf388 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -434,7 +434,7 @@ func TestInstanceFind(t *testing.T) { Items: items, } - expected := []InstanceResponse{ + expected := []InstanceMiniResponse{ { ID: "HaKpys8e", Request: InstanceRequest{ @@ -447,22 +447,6 @@ func TestInstanceFind(t *testing.T) { }, }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, { ID: "HaKpys8f", @@ -473,22 +457,6 @@ func TestInstanceFind(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, { ID: "HaKpys8g", @@ -499,22 +467,6 @@ func TestInstanceFind(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, } ic := NewInstanceClient() @@ -541,12 +493,12 @@ func TestInstanceFind(t *testing.T) { } }) - t.Run("Successfully Find Instance By Name Version", func(t *testing.T) { + t.Run("Successfully Find Instance By Name and Label", func(t *testing.T) { db.DBconn = &db.MockDB{ Items: items, } - expected := []InstanceResponse{ + expected := []InstanceMiniResponse{ { ID: "HaKpys8e", Request: InstanceRequest{ @@ -559,23 +511,53 @@ func TestInstanceFind(t *testing.T) { }, }, Namespace: "testnamespace", + }, + } + ic := NewInstanceClient() + name := "test-rbdef" + labels := map[string]string{ + "vf_module_id": "test-vf-module-id", + } + data, err := ic.Find(name, "", "", labels) + if err != nil { + t.Fatalf("TestInstanceFind returned an error (%s)", err) + } - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(data, func(i, j int) bool { + return data[i].ID < data[j].ID + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(expected, func(i, j int) bool { + return expected[i].ID < expected[j].ID + }) + + if !reflect.DeepEqual(expected, data) { + t.Fatalf("TestInstanceFind returned:\n result=%v\n expected=%v", + data, expected) + } + }) + + t.Run("Successfully Find Instance By Name Version", func(t *testing.T) { + db.DBconn = &db.MockDB{ + Items: items, + } + + expected := []InstanceMiniResponse{ + { + ID: "HaKpys8e", + Request: InstanceRequest{ + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + CloudRegion: "region1", + Labels: map[string]string{ + "vf_module_id": "test-vf-module-id", }, }, + Namespace: "testnamespace", }, { ID: "HaKpys8f", @@ -586,23 +568,6 @@ func TestInstanceFind(t *testing.T) { CloudRegion: "region1", }, Namespace: "testnamespace", - - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, } ic := NewInstanceClient() @@ -634,7 +599,7 @@ func TestInstanceFind(t *testing.T) { Items: items, } - expected := []InstanceResponse{ + expected := []InstanceMiniResponse{ { ID: "HaKpys8e", Request: InstanceRequest{ @@ -647,23 +612,6 @@ func TestInstanceFind(t *testing.T) { }, }, Namespace: "testnamespace", - - Resources: []helm.KubernetesResource{ - { - GVK: schema.GroupVersionKind{ - Group: "apps", - Version: "v1", - Kind: "Deployment"}, - Name: "deployment-1", - }, - { - GVK: schema.GroupVersionKind{ - Group: "", - Version: "v1", - Kind: "Service"}, - Name: "service-1", - }, - }, }, } ic := NewInstanceClient() |