Implement get the charts by name 45/9245/1
authorsubhash kumar singh <subh.singh@samsung.com>
Wed, 12 Oct 2022 11:58:52 +0000 (11:58 +0000)
committersubhash kumar singh <subh.singh@samsung.com>
Wed, 12 Oct 2022 11:58:52 +0000 (11:58 +0000)
Implemented the query to get the available charts details
for given xApp name.

Signed-off-by: subhash kumar singh <subh.singh@samsung.com>
Change-Id: I8eedc09c3a9f661a5cdd0a597211878440f8e05e

api/ric-dms-api-2.0.yaml
config/config-test.yaml
config/config.yaml
pkg/charts/chart_manager.go
pkg/config/conf.go
pkg/restful/restful.go
pkg/resthooks/mocks/resp-get-charts-by-name.json [new file with mode: 0644]
pkg/resthooks/resthooks.go
pkg/resthooks/resthooks_test.go

index 5679271..421dbcd 100644 (file)
@@ -87,7 +87,9 @@ paths:
         '200':
           description: Get helm chart
           schema:
-            type: object
+            type: array
+            items:
+              type: object
         '500':
           description: Get helm chart failed
           schema:
index 4e57661..9680096 100644 (file)
@@ -16,4 +16,5 @@ log-level: debug
 onborder-url: "http://127.0.0.1:9191"
 mock-server: "127.0.0.1:9191"
 getCharts-url: "http://127.0.0.1:9191"
-download-charts-url-format: "http://127.0.0.1:9191/helmrepo/charts/%s-%s.tgz"
\ No newline at end of file
+download-charts-url-format: "http://127.0.0.1:9191/helmrepo/charts/%s-%s.tgz"
+getCharts-by-name-url: "http://127.0.0.1:9191/helmrepo/api/charts/%s"
\ No newline at end of file
index f27d413..7f4470e 100644 (file)
@@ -15,4 +15,5 @@
 log-level: debug
 onborder-url: "http://service-ricplt-xapp-onboarder-http.ricplt:8888/api/v1/onboard"
 getCharts-url: "http://service-ricplt-xapp-onboarder-http.ricplt:8080/api/charts"
-download-charts-url-format: "http://service-ricplt-xapp-onboarder-http.ricplt:8080/charts/%s-%s.tgz"
\ No newline at end of file
+download-charts-url-format: "http://service-ricplt-xapp-onboarder-http.ricplt:8080/charts/%s-%s.tgz"
+getCharts-by-name-url: "http://172.17.0.1:9100/helmrepo/api/charts/%s"
\ No newline at end of file
index 75ec338..0f6f7af 100644 (file)
@@ -21,6 +21,7 @@
 package charts
 
 import (
+       "encoding/json"
        "fmt"
        "io"
        "io/ioutil"
@@ -35,6 +36,7 @@ type ChartMgr struct {
 type IChartMgr interface {
        GetCharts() (string, error)
        DownloadChart(string, string) (io.ReadCloser, error)
+       GetChartsByName(name string) ([]map[string]interface{}, error)
 }
 
 func NewChartmgr() IChartMgr {
@@ -78,3 +80,35 @@ func (c *ChartMgr) DownloadChart(chartName string, version string) (io.ReadClose
 
        return resp.Request.Body, nil
 }
+
+func (c *ChartMgr) GetChartsByName(name string) ([]map[string]interface{}, error) {
+       ricdms.Logger.Debug("Get Chart by xApp name is invoked")
+
+       if name == "" {
+               return make([]map[string]interface{}, 0), fmt.Errorf("xAppname is empty")
+       }
+
+       URL := fmt.Sprintf(ricdms.Config.GetChartsByxAppNameURL, name)
+
+       response, err := http.Get(URL)
+       if err != nil {
+               ricdms.Logger.Error("error: %v", err)
+               return make([]map[string]interface{}, 0), err
+       }
+
+       defer response.Body.Close()
+       data, err := ioutil.ReadAll(response.Body)
+
+       if err != nil {
+               ricdms.Logger.Debug("Reading response failed with err : %v", err)
+               return make([]map[string]interface{}, 0), err
+       }
+
+       v := make([]map[string]interface{}, 0)
+       err = json.Unmarshal(data, &v)
+       if err != nil {
+               ricdms.Logger.Debug("Error while parsing res: %v", err)
+               return make([]map[string]interface{}, 0), err
+       }
+       return v, nil
+}
index b241aa0..fe5a980 100644 (file)
@@ -31,6 +31,7 @@ type Conf struct {
        LogLevel               string `yaml:"log-level"`
        OnboardURL             string `yaml:"onborder-url"`
        GetChartsURL           string `yaml:"getCharts-url"`
+       GetChartsByxAppNameURL string `yaml:"getCharts-by-name-url"`
        DownloadChartURLFormat string `yaml:"download-charts-url-format"`
        MockServer             string `yaml:"mock-server"`
 }
index e01d2fc..ca45417 100644 (file)
@@ -81,6 +81,12 @@ func (r *Restful) setupHandler() {
                return resp
        })
 
+       api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder {
+               ricdms.Logger.Debug("==> Get Charts by name is invoked")
+               resp := r.rh.GetChartsByName(param.XAppName)
+               return resp
+       })
+
        r.api = api
 }
 
diff --git a/pkg/resthooks/mocks/resp-get-charts-by-name.json b/pkg/resthooks/mocks/resp-get-charts-by-name.json
new file mode 100644 (file)
index 0000000..d2f224b
--- /dev/null
@@ -0,0 +1,14 @@
+[
+  {
+    "name": "hw-go",
+    "version": "1.0.0",
+    "description": "Standard xApp Helm Chart",
+    "apiVersion": "v1",
+    "appVersion": "1.0",
+    "urls": [
+      "charts/hw-go-1.0.0.tgz"
+    ],
+    "created": "2022-09-19T08:12:55.54057053Z",
+    "digest": "f155d6c356bdcd1e3bcc1dd3df080e25a62b6e8ee32c289c5f20dec1af9c96de"
+  }
+]
\ No newline at end of file
index 622bcaf..b461344 100644 (file)
@@ -70,3 +70,20 @@ func (rh *Resthook) DownloadChart(chartname, version string) (resp middleware.Re
 
        return charts.NewDownloadHelmChartOK().WithPayload(reader)
 }
+
+func (rh *Resthook) GetChartsByName(name string) middleware.Responder {
+       ricdms.Logger.Debug("GetChartByName: invoked")
+       res, err := rh.ChartMgr.GetChartsByName(name)
+
+       if err != nil {
+               ricdms.Logger.Error("error: %v", err)
+               return charts.NewGetChartInternalServerError()
+       }
+
+       response := make([]interface{}, 0)
+       for _, item := range res {
+               response = append(response, item)
+       }
+
+       return charts.NewGetChartOK().WithPayload(response)
+}
index b18b164..c8deeb2 100644 (file)
@@ -28,6 +28,7 @@ import (
        "net/http"
        "net/http/httptest"
        "os"
+       "path/filepath"
        "strings"
        "testing"
 
@@ -144,6 +145,34 @@ func TestDownloadChart(t *testing.T) {
        assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
 }
 
+func TestGetChartsByName(t *testing.T) {
+       svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               ricdms.Logger.Debug("request received by mock to get chart by name")
+               path, _ := filepath.Abs("./mocks/resp-get-charts-by-name.json")
+               file, err := os.Open(path)
+
+               if err != nil {
+                       ricdms.Logger.Error("error in reading file: %v", err)
+               }
+
+               jsonData, err := io.ReadAll(file)
+               if err != nil {
+                       ricdms.Logger.Error("Error in rading file: %v", err)
+               }
+               w.Write(jsonData)
+       }))
+
+       svr.Listener.Close()
+       svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
+
+       svr.Start()
+       defer svr.Close()
+
+       resp := rh.GetChartsByName("TEST_NAME")
+       ricdms.Logger.Debug("resp Data: %s", resp.(*charts.GetChartOK).Payload...)
+       assert.IsType(t, &charts.GetChartOK{}, resp, "response did not match type")
+}
+
 type HealthCheckerMock struct {
        mock.Mock
 }