From 045ce10b7c48136dee24761e23ea0156f6255521 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 18 Jul 2019 15:28:34 -0700 Subject: Make definition tags specific Tags used in db such as metadata are too generic. Instead we are using specific ones with the type prefixed. Issue-ID: MULTICLOUD-730 Change-Id: Ice76cf743f0782a3d7e76747cde1f0e0b116b14e Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/app/instance_test.go | 4 ++-- src/k8splugin/internal/rb/definition.go | 4 ++-- src/k8splugin/internal/rb/definition_test.go | 22 +++++++++++----------- src/k8splugin/internal/rb/profile_test.go | 8 ++++---- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/k8splugin/internal/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go index 24558a44..8e817f0e 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -84,13 +84,13 @@ func TestInstanceCreate(t *testing.T) { "yJ66WPQwcHBwcHBwcHBwcHBwcHBwcHhm8Q/mTHqWgAoAAA="), }, rb.DefinitionKey{RBName: "test-rbdef", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"test-rbdef\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"vault-consul-dev\"," + "\"description\":\"testresourcebundle\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/vault-consul-dev - "content": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + + "defcontent": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + "QYujdJehatb+V4czPnOmnPk9bO2Gk7nbaTgUhIxpgiGAK0o3P9QPca92S3C5AU9GXZiax" + "c7rA/LJEAFovdxX4AK1/RIlGNSKSySBoxuzp4sn1oAgx6Pf0JsPipv7c63XZ70O61W4Mn" + "zVZ7MGg9Ib1HoGUJCqloTsiTXAh1V79N7V8oXC3K/+iC5iqY0kmytTlQwP1ud538W51Wf" + diff --git a/src/k8splugin/internal/rb/definition.go b/src/k8splugin/internal/rb/definition.go index 476e40ee..6998eedf 100644 --- a/src/k8splugin/internal/rb/definition.go +++ b/src/k8splugin/internal/rb/definition.go @@ -79,8 +79,8 @@ type DefinitionClient struct { func NewDefinitionClient() *DefinitionClient { return &DefinitionClient{ storeName: "rbdef", - tagMeta: "metadata", - tagContent: "content", + tagMeta: "defmetadata", + tagContent: "defcontent", } } diff --git a/src/k8splugin/internal/rb/definition_test.go b/src/k8splugin/internal/rb/definition_test.go index 054da2cd..0140b459 100644 --- a/src/k8splugin/internal/rb/definition_test.go +++ b/src/k8splugin/internal/rb/definition_test.go @@ -113,14 +113,14 @@ func TestListDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"testchart\"}"), }, DefinitionKey{RBName: "testresourcebundle", RBVersion: "v2"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle_version2\"," + "\"rb-version\":\"v2\"," + @@ -196,7 +196,7 @@ func TestGetDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -326,7 +326,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"}"), @@ -362,7 +362,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -400,7 +400,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -438,7 +438,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -459,7 +459,7 @@ func TestUploadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -551,12 +551,12 @@ func TestDownloadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"firewall\"}"), - "content": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + + "defcontent": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + "QEtLE40igAUtSC+2IHt9IEVwIpYtShP/bvGFmFk/SLI08Re3IVCG077Rn" + "b75zYZ2yztVV8N7XP9vWSWmzZ6mP+yxx0lrF7pJzjkN/Sz//1u5/6ppKG" + "R/jVLrT0VUAAAAAAAAAAAAAAAAAABu8ALXoSvkAKAAA"), @@ -572,7 +572,7 @@ func TestDownloadDefinition(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + diff --git a/src/k8splugin/internal/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go index 29efb506..7c9deac3 100644 --- a/src/k8splugin/internal/rb/profile_test.go +++ b/src/k8splugin/internal/rb/profile_test.go @@ -56,7 +56,7 @@ func TestCreateProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -79,7 +79,7 @@ func TestCreateProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ DefinitionKey{RBName: "testresourcebundle", RBVersion: "v2"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"description\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + @@ -545,13 +545,13 @@ func TestResolveProfile(t *testing.T) { "yJ66WPQwcHBwcHBwcHBwcHBwcHBwcHhm8Q/mTHqWgAoAAA="), }, DefinitionKey{RBName: "testresourcebundle", RBVersion: "v1"}.String(): { - "metadata": []byte( + "defmetadata": []byte( "{\"rb-name\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"chart-name\":\"vault-consul-dev\"," + "\"description\":\"testresourcebundle\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/vault-consul-dev - "content": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + + "defcontent": []byte("H4sICEetS1wAA3ZhdWx0LWNvbnN1bC1kZXYudGFyAO0c7XLbNjK/+R" + "QYujdJehatb+V4czPnOmnPk9bO2Gk7nbaTgUhIxpgiGAK0o3P9QPca92S3C5AU9GXZiax" + "c7rA/LJEAFovdxX4AK1/RIlGNSKSySBoxuzp4sn1oAgx6Pf0JsPipv7c63XZ70O61W4Mn" + "zVZ7MGg9Ib1HoGUJCqloTsiTXAh1V79N7V8oXC3K/+iC5iqY0kmytTlQwP1ud538W51Wf" + -- cgit 1.2.3-korg From d605586c571f5bc059486d3b41a9709b5d9c10e3 Mon Sep 17 00:00:00 2001 From: Kiran Kamineni Date: Thu, 18 Jul 2019 16:05:33 -0700 Subject: Make profile db tags specific Tags used in profile such as metadata are too generic. Instead we are using specific ones with the type prefixed. Issue-ID: MULTICLOUD-730 Change-Id: I2f189d35bc202f4a71e0a92ca5b7188ebfd1cef2 Signed-off-by: Kiran Kamineni --- src/k8splugin/internal/app/instance_test.go | 4 ++-- src/k8splugin/internal/rb/profile.go | 4 ++-- src/k8splugin/internal/rb/profile_test.go | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/k8splugin/internal/app/instance_test.go b/src/k8splugin/internal/app/instance_test.go index 8e817f0e..3cb62ee1 100644 --- a/src/k8splugin/internal/app/instance_test.go +++ b/src/k8splugin/internal/app/instance_test.go @@ -51,7 +51,7 @@ func TestInstanceCreate(t *testing.T) { Items: map[string]map[string][]byte{ rb.ProfileKey{RBName: "test-rbdef", RBVersion: "v1", ProfileName: "profile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"profile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -59,7 +59,7 @@ func TestInstanceCreate(t *testing.T) { "\"rb-version\":\"v1\"," + "\"kubernetesversion\":\"1.12.3\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/profile - "content": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + + "profilecontent": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + "78K78lLMsxY5gRxmqIYhoKWaJsYJWokZdfo+r/vSFmunCZNBtQJ1vF7sXX36e54vDN5T" + "knGFlTpcEtS3jgO2ohBr2c/EXc/29Gg1+h0e1F32Ol1B1Gj3Ymifr8B7SPFc4BCaSIBG" + "lII/SXeY/r/KIIg8NZUKiayEaw7nt7mdOQBrAkvqBqBL1ArWULflRJbJz4SYpEt2FJSJ" + diff --git a/src/k8splugin/internal/rb/profile.go b/src/k8splugin/internal/rb/profile.go index 37e9aba8..64449ebd 100644 --- a/src/k8splugin/internal/rb/profile.go +++ b/src/k8splugin/internal/rb/profile.go @@ -78,8 +78,8 @@ type ProfileClient struct { func NewProfileClient() *ProfileClient { return &ProfileClient{ storeName: "rbdef", - tagMeta: "metadata", - tagContent: "content", + tagMeta: "profilemetadata", + tagContent: "profilecontent", manifestName: "manifest.yaml", } } diff --git a/src/k8splugin/internal/rb/profile_test.go b/src/k8splugin/internal/rb/profile_test.go index 7c9deac3..263c48ab 100644 --- a/src/k8splugin/internal/rb/profile_test.go +++ b/src/k8splugin/internal/rb/profile_test.go @@ -145,7 +145,7 @@ func TestGetProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -265,7 +265,7 @@ func TestUploadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -306,7 +306,7 @@ func TestUploadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile2"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -330,7 +330,7 @@ func TestUploadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -425,14 +425,14 @@ func TestDownloadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + "\"rb-name\":\"testresourcebundle\"," + "\"rb-version\":\"v1\"," + "\"kubernetesversion\":\"1.12.3\"}"), - "content": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + + "profilecontent": []byte("H4sICLBr9FsAA3Rlc3QudGFyAO3OQQrCMBCF4aw9RU5" + "QEtLE40igAUtSC+2IHt9IEVwIpYtShP/bvGFmFk/SLI08Re3IVCG077Rn" + "b75zYZ2yztVV8N7XP9vWSWmzZ6mP+yxx0lrF7pJzjkN/Sz//1u5/6ppKG" + "R/jVLrT0VUAAAAAAAAAAAAAAAAAABu8ALXoSvkAKAAA"), @@ -449,7 +449,7 @@ func TestDownloadProfile(t *testing.T) { mockdb: &db.MockDB{ Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "testprofile2"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"testprofile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -512,7 +512,7 @@ func TestResolveProfile(t *testing.T) { Items: map[string]map[string][]byte{ ProfileKey{RBName: "testresourcebundle", RBVersion: "v1", ProfileName: "profile1"}.String(): { - "metadata": []byte( + "profilemetadata": []byte( "{\"profile-name\":\"profile1\"," + "\"release-name\":\"testprofilereleasename\"," + "\"namespace\":\"testnamespace\"," + @@ -520,7 +520,7 @@ func TestResolveProfile(t *testing.T) { "\"rb-version\":\"v1\"," + "\"kubernetesversion\":\"1.12.3\"}"), // base64 encoding of vagrant/tests/vnfs/testrb/helm/profile - "content": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + + "profilecontent": []byte("H4sICLmjT1wAA3Byb2ZpbGUudGFyAO1Y32/bNhD2s/6Kg/KyYZZsy" + "78K78lLMsxY5gRxmqIYhoKWaJsYJWokZdfo+r/vSFmunCZNBtQJ1vF7sXX36e54vDN5T" + "knGFlTpcEtS3jgO2ohBr2c/EXc/29Gg1+h0e1F32Ol1B1Gj3Ymifr8B7SPFc4BCaSIBG" + "lII/SXeY/r/KIIg8NZUKiayEaw7nt7mdOQBrAkvqBqBL1ArWULflRJbJz4SYpEt2FJSJ" + -- cgit 1.2.3-korg 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/api/api.go | 1 + src/k8splugin/api/profilehandler.go | 25 ++++++- src/k8splugin/api/profilehandler_test.go | 101 +++++++++++++++++++++++++++++ src/k8splugin/internal/rb/profile.go | 34 ++++++++++ src/k8splugin/internal/rb/profile_test.go | 104 +++++++++++++++++++++++++++++- 5 files changed, 263 insertions(+), 2 deletions(-) diff --git a/src/k8splugin/api/api.go b/src/k8splugin/api/api.go index 353972a1..a6cdfc7a 100644 --- a/src/k8splugin/api/api.go +++ b/src/k8splugin/api/api.go @@ -78,6 +78,7 @@ func NewRouter(defClient rb.DefinitionManager, } profileHandler := rbProfileHandler{client: profileClient} resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.createHandler).Methods("POST") + resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile", profileHandler.listHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}/content", profileHandler.uploadHandler).Methods("POST") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.getHandler).Methods("GET") resRouter.HandleFunc("/definition/{rbname}/{rbversion}/profile/{prname}", profileHandler.deleteHandler).Methods("DELETE") diff --git a/src/k8splugin/api/profilehandler.go b/src/k8splugin/api/profilehandler.go index adb9249b..68ab77a4 100644 --- a/src/k8splugin/api/profilehandler.go +++ b/src/k8splugin/api/profilehandler.go @@ -20,9 +20,10 @@ import ( "encoding/json" "io" "io/ioutil" - "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" "net/http" + "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" + "github.com/gorilla/mux" ) @@ -119,6 +120,28 @@ func (h rbProfileHandler) getHandler(w http.ResponseWriter, r *http.Request) { } } +// getHandler handles GET operations on a particular ids +// Returns a rb.Definition +func (h rbProfileHandler) listHandler(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + rbName := vars["rbname"] + rbVersion := vars["rbversion"] + + ret, err := h.client.List(rbName, rbVersion) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + err = json.NewEncoder(w).Encode(ret) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } +} + // deleteHandler handles DELETE operations on a particular bundle definition id func (h rbProfileHandler) deleteHandler(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) diff --git a/src/k8splugin/api/profilehandler_test.go b/src/k8splugin/api/profilehandler_test.go index e81fb262..4dae377c 100644 --- a/src/k8splugin/api/profilehandler_test.go +++ b/src/k8splugin/api/profilehandler_test.go @@ -23,6 +23,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "sort" "testing" "github.com/onap/multicloud-k8s/src/k8splugin/internal/rb" @@ -57,6 +58,14 @@ func (m *mockRBProfile) Get(rbname, rbversion, prname string) (rb.Profile, error return m.Items[0], nil } +func (m *mockRBProfile) List(rbname, rbversion string) ([]rb.Profile, error) { + if m.Err != nil { + return []rb.Profile{}, m.Err + } + + return m.Items, nil +} + func (m *mockRBProfile) Delete(rbname, rbversion, prname string) error { return m.Err } @@ -210,6 +219,98 @@ func TestRBProfileGetHandler(t *testing.T) { } } +func TestRBProfileListHandler(t *testing.T) { + + testCases := []struct { + def string + version string + label string + expected []rb.Profile + expectedCode int + rbProClient *mockRBProfile + }{ + { + def: "test-rbdef", + version: "v1", + label: "List Profiles", + expectedCode: http.StatusOK, + expected: []rb.Profile{ + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + ReleaseName: "testprofilereleasename", + Namespace: "ns1", + KubernetesVersion: "1.12.3", + }, + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + ReleaseName: "testprofilereleasename", + Namespace: "ns2", + KubernetesVersion: "1.12.3", + }, + }, + rbProClient: &mockRBProfile{ + // list of Profiles that will be returned by the mockclient + Items: []rb.Profile{ + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile1", + ReleaseName: "testprofilereleasename", + Namespace: "ns1", + KubernetesVersion: "1.12.3", + }, + { + RBName: "test-rbdef", + RBVersion: "v1", + ProfileName: "profile2", + ReleaseName: "testprofilereleasename", + Namespace: "ns2", + KubernetesVersion: "1.12.3", + }, + }, + }, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.label, func(t *testing.T) { + request := httptest.NewRequest("GET", "/v1/rb/definition/"+testCase.def+"/"+testCase.version+"/profile", nil) + resp := executeRequest(request, NewRouter(nil, testCase.rbProClient, nil, nil, nil, nil)) + + //Check returned code + if resp.StatusCode != testCase.expectedCode { + t.Fatalf("Expected %d; Got: %d", testCase.expectedCode, resp.StatusCode) + } + + //Check returned body only if statusOK + if resp.StatusCode == http.StatusOK { + got := []rb.Profile{} + json.NewDecoder(resp.Body).Decode(&got) + + // 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("listHandler returned unexpected body: got %v;"+ + " expected %v", got, testCase.expected) + } + } + }) + } +} + func TestRBProfileDeleteHandler(t *testing.T) { testCases := []struct { 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