Implement get the charts by name and version 49/9249/1
authorsubhash kumar singh <subh.singh@samsung.com>
Wed, 12 Oct 2022 18:07:44 +0000 (18:07 +0000)
committersubhash kumar singh <subh.singh@samsung.com>
Wed, 12 Oct 2022 18:07:44 +0000 (18:07 +0000)
Implemented the query to get the available charts details
for given xApp name and version.

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

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-and-ver.json [new file with mode: 0644]
pkg/resthooks/resthooks.go
pkg/resthooks/resthooks_test.go

index 421dbcd..ff55bf9 100644 (file)
@@ -128,7 +128,6 @@ paths:
     get:
       produces:
         - application/json
-        - application/gzip
       parameters:
         - in: path
           name: xApp_name
@@ -140,11 +139,11 @@ paths:
           type: string
       responses:
         '200':
-          description: Get helm chart package OK
+          description: Get helm chart details OK
           schema:
-            type: file
+            type: object
         '500':
-          description: Get helm chart package failed
+          description: Get helm chart details failed
           schema:
             $ref: '#/definitions/error_message'
       tags:
index 9680096..b2b2461 100644 (file)
@@ -17,4 +17,5 @@ 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"
-getCharts-by-name-url: "http://127.0.0.1:9191/helmrepo/api/charts/%s"
\ No newline at end of file
+getCharts-by-name-url: "http://127.0.0.1:9191/helmrepo/api/charts/%s"
+getCharts-by-name-and-version-url: "http://127.0.0.1:9191/helmrepo/api/charts/%s/%s"
\ No newline at end of file
index 7f4470e..861912d 100644 (file)
@@ -16,4 +16,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"
-getCharts-by-name-url: "http://172.17.0.1:9100/helmrepo/api/charts/%s"
\ No newline at end of file
+getCharts-by-name-url: "http://172.17.0.1:9100/helmrepo/api/charts/%s"
+getCharts-by-name-and-version-url: "http://172.17.0.1:9100/helmrepo/api/charts/%s/%s"
\ No newline at end of file
index 0f6f7af..c0308dd 100644 (file)
@@ -37,6 +37,7 @@ type IChartMgr interface {
        GetCharts() (string, error)
        DownloadChart(string, string) (io.ReadCloser, error)
        GetChartsByName(name string) ([]map[string]interface{}, error)
+       GetChartsByNameAndVersion(name, version string) (map[string]interface{}, error)
 }
 
 func NewChartmgr() IChartMgr {
@@ -112,3 +113,36 @@ func (c *ChartMgr) GetChartsByName(name string) ([]map[string]interface{}, error
        }
        return v, nil
 }
+
+func (c *ChartMgr) GetChartsByNameAndVersion(name, version string) (map[string]interface{}, error) {
+       ricdms.Logger.Debug("Get Charts by name and version is invoked")
+
+       if name == "" || version == "" {
+               return make(map[string]interface{}, 0), fmt.Errorf("name or version is not provided")
+       }
+
+       URL := fmt.Sprintf(ricdms.Config.GetChartsByNameAndVersionURL, name, version)
+
+       response, err := http.Get(URL)
+       if err != nil {
+               ricdms.Logger.Debug("error in retrieving chart: %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("error in reading response: %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 in parsing response: %v", err)
+               return make(map[string]interface{}, 0), err
+       }
+
+       return v, nil
+}
index fe5a980..c24500f 100644 (file)
@@ -28,12 +28,13 @@ import (
 )
 
 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"`
+       LogLevel                     string `yaml:"log-level"`
+       OnboardURL                   string `yaml:"onborder-url"`
+       GetChartsURL                 string `yaml:"getCharts-url"`
+       GetChartsByxAppNameURL       string `yaml:"getCharts-by-name-url"`
+       GetChartsByNameAndVersionURL string `yaml:"getCharts-by-name-and-version-url"`
+       DownloadChartURLFormat       string `yaml:"download-charts-url-format"`
+       MockServer                   string `yaml:"mock-server"`
 }
 
 func ReadConfiguration(configFile string) (c *Conf, err error) {
index ca45417..9c9e861 100644 (file)
@@ -87,6 +87,12 @@ func (r *Restful) setupHandler() {
                return resp
        })
 
+       api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder {
+               ricdms.Logger.Debug("==> Get Charts by name and version is invoked")
+               resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version)
+               return resp
+       })
+
        r.api = api
 }
 
diff --git a/pkg/resthooks/mocks/resp-get-charts-by-name-and-ver.json b/pkg/resthooks/mocks/resp-get-charts-by-name-and-ver.json
new file mode 100644 (file)
index 0000000..dde2dc8
--- /dev/null
@@ -0,0 +1,12 @@
+{
+    "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 b461344..519e636 100644 (file)
@@ -87,3 +87,14 @@ func (rh *Resthook) GetChartsByName(name string) middleware.Responder {
 
        return charts.NewGetChartOK().WithPayload(response)
 }
+
+func (rh *Resthook) GetChartByNameAndVersion(name, version string) middleware.Responder {
+       ricdms.Logger.Debug("GetChartByNameAndVersion is invoked")
+       resp, err := rh.ChartMgr.GetChartsByNameAndVersion(name, version)
+
+       if err != nil {
+               return charts.NewGetChartsFetcherInternalServerError()
+       }
+
+       return charts.NewGetChartsFetcherOK().WithPayload(resp)
+}
index c8deeb2..8e2dfcc 100644 (file)
@@ -173,6 +173,34 @@ func TestGetChartsByName(t *testing.T) {
        assert.IsType(t, &charts.GetChartOK{}, resp, "response did not match type")
 }
 
+func TestGetChartsByNameAndVersion(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 and version")
+               path, _ := filepath.Abs("./mocks/resp-get-charts-by-name-and-ver.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.GetChartByNameAndVersion("Test", "1.0.0")
+       ricdms.Logger.Debug("resp data: %s", resp.(*charts.GetChartsFetcherOK).Payload)
+       assert.IsType(t, &charts.GetChartsFetcherOK{}, resp, "response did not match type")
+}
+
 type HealthCheckerMock struct {
        mock.Mock
 }