From 412d02f7bd53a9e810be2c17d1c391c9bc6dda13 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 18 Jul 2019 14:49:25 -0700 Subject: Add list api for profiles Add a list api for profiles for a specific definition and version. GET /v1/rb/definition/name/version/profile will list all the profiles. Issue-ID: MULTICLOUD-730 Change-Id: If1b8e6910c276a0f7139ab13340721c6ec8a49e8 Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/rb/profile.go | 34 ++++++++++ src/k8splugin/internal/rb/profile_test.go | 104 +++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 1 deletion(-) (limited to 'src/k8splugin/internal') diff --git a/src/k8splugin/internal/rb/profile.go b/src/k8splugin/internal/rb/profile.go index 64449ebd..49768d4b 100644 --- a/src/k8splugin/internal/rb/profile.go +++ b/src/k8splugin/internal/rb/profile.go @@ -20,6 +20,7 @@ import ( "bytes" "encoding/base64" "encoding/json" + "log" "path/filepath" "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" @@ -44,6 +45,7 @@ type Profile struct { type ProfileManager interface { Create(def Profile) (Profile, error) Get(rbName, rbVersion, prName string) (Profile, error) + List(rbName, rbVersion string) ([]Profile, error) Delete(rbName, rbVersion, prName string) error Upload(rbName, rbVersion, prName string, inp []byte) error } @@ -148,6 +150,38 @@ func (v *ProfileClient) Get(rbName, rbVersion, prName string) (Profile, error) { return Profile{}, pkgerrors.New("Error getting Resource Bundle Profile") } +// List returns the Resource Bundle Profile for corresponding ID +func (v *ProfileClient) List(rbName, rbVersion string) ([]Profile, error) { + + //Get all profiles + dbres, err := db.DBconn.ReadAll(v.storeName, v.tagMeta) + if err != nil || len(dbres) == 0 { + return []Profile{}, pkgerrors.Wrap(err, "No Profiles Found") + } + + var results []Profile + for key, value := range dbres { + //value is a byte array + if value != nil { + pr := Profile{} + err = db.DBconn.Unmarshal(value, &pr) + if err != nil { + log.Printf("[Profile] Error: %s Unmarshaling value for: %s", err.Error(), key) + continue + } + if pr.RBName == rbName && pr.RBVersion == rbVersion { + results = append(results, pr) + } + } + } + + if len(results) == 0 { + return results, pkgerrors.New("No Profiles Found for Definition and Version") + } + + return results, nil +} + // Delete the Resource Bundle Profile from database func (v *ProfileClient) Delete(rbName, rbVersion, prName string) error { key := ProfileKey{ diff --git a/src/k8splugin/internal/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go index 263c48ab..26b0161d 100644 --- a/src/k8splugin/internal/rb/profile_test.go +++ b/src/k8splugin/internal/rb/profile_test.go @@ -18,11 +18,13 @@ package rb import ( "bytes" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" "reflect" + "sort" "strings" "testing" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/db" + pkgerrors "github.com/pkg/errors" ) @@ -187,6 +189,106 @@ func TestGetProfile(t *testing.T) { } } +func TestListProfile(t *testing.T) { + + testCases := []struct { + label string + name string + rbdef string + version string + expectedError string + mockdb *db.MockDB + expected []Profile + }{ + { + label: "List Resource Bundle Profile", + name: "testresourcebundle", + rbdef: "testresourcebundle", + version: "v1", + expected: []Profile{ + { + ProfileName: "testprofile1", + ReleaseName: "testprofilereleasename", + Namespace: "testnamespace", + KubernetesVersion: "1.12.3", + RBName: "testresourcebundle", + RBVersion: "v1", + }, + { + ProfileName: "testprofile2", + ReleaseName: "testprofilereleasename2", + Namespace: "testnamespace2", + KubernetesVersion: "1.12.3", + RBName: "testresourcebundle", + RBVersion: "v1", + }, + }, + expectedError: "", + mockdb: &db.MockDB{ + Items: map[string]map[string][]byte{ + ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { + "profilemetadata": []byte( + "{\"profile-name\":\"testprofile1\"," + + "\"release-name\":\"testprofilereleasename\"," + + "\"namespace\":\"testnamespace\"," + + "\"rb-name\":\"testresourcebundle\"," + + "\"rb-version\":\"v1\"," + + "\"kubernetes-version\":\"1.12.3\"}"), + }, + ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile2"}.String(): { + "profilemetadata": []byte( + "{\"profile-name\":\"testprofile2\"," + + "\"release-name\":\"testprofilereleasename2\"," + + "\"namespace\":\"testnamespace2\"," + + "\"rb-name\":\"testresourcebundle\"," + + "\"rb-version\":\"v1\"," + + "\"kubernetes-version\":\"1.12.3\"}"), + }, + }, + }, + }, + { + label: "List Error", + expectedError: "DB Error", + mockdb: &db.MockDB{ + Err: pkgerrors.New("DB Error"), + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + db.DBconn = testCase.mockdb + impl := NewProfileClient() + got, err := impl.List(testCase.rbdef, testCase.version) + if err != nil { + if testCase.expectedError == "" { + t.Fatalf("List returned an unexpected error %s", err) + } + if strings.Contains(err.Error(), testCase.expectedError) == false { + t.Fatalf("List returned an unexpected error %s", err) + } + } else { + // Since the order of returned slice is not guaranteed + // Check both and return error if both don't match + sort.Slice(got, func(i, j int) bool { + return got[i].ProfileName < got[j].ProfileName + }) + // Sort both as it is not expected that testCase.expected + // is sorted + sort.Slice(testCase.expected, func(i, j int) bool { + return testCase.expected[i].ProfileName < testCase.expected[j].ProfileName + }) + + if reflect.DeepEqual(testCase.expected, got) == false { + t.Errorf("List Resource Bundle returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestDeleteProfile(t *testing.T) { testCases := []struct { -- cgit 1.2.3-korg