diff options
author | Ritu Sood <ritu.sood@intel.com> | 2020-03-03 00:44:52 -0800 |
---|---|---|
committer | Eric Multanen <eric.w.multanen@intel.com> | 2020-04-07 09:20:19 -0700 |
commit | ee5748eca6350222051125b5e2313f78da00efbe (patch) | |
tree | 0af1e364a976787a7c640e676a159e72d9be8a3e /src/ncm | |
parent | ec83b3d3bda5501b20e05efae198202b29396c4a (diff) |
Adding function to Query cluster based on label
Add Query endpoint and implements returning list
of clusters with label.
Issue-ID: MULTICLOUD-922
Signed-off-by: Ritu Sood <ritu.sood@intel.com>
Signed-off-by: Eric Multanen <eric.w.multanen@intel.com>
Change-Id: I879b5e9002a0cbc9191abb99f1e164ee2b1c6581
Diffstat (limited to 'src/ncm')
-rw-r--r-- | src/ncm/api/api.go | 1 | ||||
-rw-r--r-- | src/ncm/api/clusterhandler.go | 17 | ||||
-rw-r--r-- | src/ncm/api/clusterhandler_test.go | 9 | ||||
-rw-r--r-- | src/ncm/pkg/module/cluster.go | 32 |
4 files changed, 58 insertions, 1 deletions
diff --git a/src/ncm/api/api.go b/src/ncm/api/api.go index 34b46c67..3ff8671a 100644 --- a/src/ncm/api/api.go +++ b/src/ncm/api/api.go @@ -74,6 +74,7 @@ func NewRouter(testClient interface{}) *mux.Router { router.HandleFunc("/cluster-providers/{name}", clusterHandler.deleteClusterProviderHandler).Methods("DELETE") router.HandleFunc("/cluster-providers/{provider-name}/clusters", clusterHandler.createClusterHandler).Methods("POST") router.HandleFunc("/cluster-providers/{provider-name}/clusters", clusterHandler.getClusterHandler).Methods("GET") + router.HandleFunc("/cluster-providers/{provider-name}/clusters", clusterHandler.getClusterHandler).Queries("label", "{label}") router.HandleFunc("/cluster-providers/{provider-name}/clusters/{name}", clusterHandler.getClusterHandler).Methods("GET") router.HandleFunc("/cluster-providers/{provider-name}/clusters/{name}", clusterHandler.deleteClusterHandler).Methods("DELETE") router.HandleFunc("/cluster-providers/{provider-name}/clusters/{cluster-name}/labels", clusterHandler.createClusterLabelHandler).Methods("POST") diff --git a/src/ncm/api/clusterhandler.go b/src/ncm/api/clusterhandler.go index cb147a8a..8c50f720 100644 --- a/src/ncm/api/clusterhandler.go +++ b/src/ncm/api/clusterhandler.go @@ -194,6 +194,23 @@ func (h clusterHandler) getClusterHandler(w http.ResponseWriter, r *http.Request provider := vars["provider-name"] name := vars["name"] + label := r.URL.Query().Get("label") + if len(label) != 0 { + ret, err := h.client.GetClustersWithLabel(provider, label) + 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 + } + return + } + // handle the get all clusters case - return a list of only the json parts if len(name) == 0 { var retList []moduleLib.Cluster diff --git a/src/ncm/api/clusterhandler_test.go b/src/ncm/api/clusterhandler_test.go index af5bd160..a26c41bd 100644 --- a/src/ncm/api/clusterhandler_test.go +++ b/src/ncm/api/clusterhandler_test.go @@ -43,6 +43,7 @@ type mockClusterManager struct { ClusterContentItems []moduleLib.ClusterContent ClusterLabelItems []moduleLib.ClusterLabel ClusterKvPairsItems []moduleLib.ClusterKvPairs + ClusterList []string Err error } @@ -106,6 +107,14 @@ func (m *mockClusterManager) GetClusters(provider string) ([]moduleLib.Cluster, return m.ClusterItems, nil } +func (m *mockClusterManager) GetClustersWithLabel(provider, label string) ([]string, error) { + if m.Err != nil { + return []string{}, m.Err + } + + return m.ClusterList, nil +} + func (m *mockClusterManager) DeleteCluster(provider, name string) error { return m.Err } diff --git a/src/ncm/pkg/module/cluster.go b/src/ncm/pkg/module/cluster.go index e3260b7e..4ca4e7c8 100644 --- a/src/ncm/pkg/module/cluster.go +++ b/src/ncm/pkg/module/cluster.go @@ -66,6 +66,12 @@ type ClusterLabelKey struct { ClusterLabelName string `json:"label"` } +// LabelKey is the key structure that is used in the database +type LabelKey struct { + ClusterProviderName string `json:"provider"` + ClusterLabelName string `json:"label"` +} + // ClusterKvPairsKey is the key structure that is used in the database type ClusterKvPairsKey struct { ClusterProviderName string `json:"provider"` @@ -73,7 +79,7 @@ type ClusterKvPairsKey struct { ClusterKvPairsName string `json:"kvname"` } -// Manager is an interface exposes the Cluster functionality +// ClusterManager is an interface exposes the Cluster functionality type ClusterManager interface { CreateClusterProvider(pr ClusterProvider) (ClusterProvider, error) GetClusterProvider(name string) (ClusterProvider, error) @@ -83,6 +89,7 @@ type ClusterManager interface { GetCluster(provider, name string) (Cluster, error) GetClusterContent(provider, name string) (ClusterContent, error) GetClusters(provider string) ([]Cluster, error) + GetClustersWithLabel(provider, label string) ([]string, error) DeleteCluster(provider, name string) error CreateClusterLabel(provider, cluster string, pr ClusterLabel) (ClusterLabel, error) GetClusterLabel(provider, cluster, label string) (ClusterLabel, error) @@ -314,6 +321,29 @@ func (v *ClusterClient) GetClusters(provider string) ([]Cluster, error) { return resp, nil } +// GetClustersWithLabel returns all the Clusters with Labels for provider +// Support Query like /cluster-providers/{Provider}/clusters?label={label} +func (v *ClusterClient) GetClustersWithLabel(provider, label string) ([]string, error) { + //Construct key and tag to select the entry + key := LabelKey{ + ClusterProviderName: provider, + ClusterLabelName: label, + } + + values, err := db.DBconn.Find(v.db.storeName, key, "cluster") + if err != nil { + return []string{}, pkgerrors.Wrap(err, "Get Clusters by label") + } + var resp []string + + for _, value := range values { + cp := string(value) + resp = append(resp, cp) + } + + return resp, nil +} + // DeleteCluster the Cluster from database func (v *ClusterClient) DeleteCluster(provider, name string) error { //Construct key and tag to select the entry |