Implementation for downloading charts 30/9230/1
authorsubhash kumar singh <subh.singh@samsung.com>
Mon, 10 Oct 2022 18:21:51 +0000 (18:21 +0000)
committersubhash kumar singh <subh.singh@samsung.com>
Mon, 10 Oct 2022 18:21:51 +0000 (18:21 +0000)
Implemented API to download helm chart for specified chartname
and version.

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

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/resthooks.go
pkg/resthooks/resthooks_test.go

index b8c8a99..5679271 100644 (file)
@@ -140,7 +140,7 @@ paths:
         '200':
           description: Get helm chart package OK
           schema:
-            type: object
+            type: file
         '500':
           description: Get helm chart package failed
           schema:
index df5b0dc..4e57661 100644 (file)
@@ -15,4 +15,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"
\ No newline at end of file
+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
index a7aecaf..f27d413 100644 (file)
@@ -14,4 +14,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"
\ No newline at end of file
+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
index 252b2dc..75ec338 100644 (file)
@@ -21,6 +21,8 @@
 package charts
 
 import (
+       "fmt"
+       "io"
        "io/ioutil"
        "net/http"
 
@@ -32,6 +34,7 @@ type ChartMgr struct {
 
 type IChartMgr interface {
        GetCharts() (string, error)
+       DownloadChart(string, string) (io.ReadCloser, error)
 }
 
 func NewChartmgr() IChartMgr {
@@ -58,3 +61,20 @@ func (c *ChartMgr) GetCharts() (string, error) {
        ricdms.Logger.Debug("response : %+v", string(respByte))
        return string(respByte), nil
 }
+
+func (c *ChartMgr) DownloadChart(chartName string, version string) (io.ReadCloser, error) {
+       ricdms.Logger.Debug("Download Charts invoked")
+
+       if chartName == "" || version == "" {
+               return nil, fmt.Errorf("chartname or version is empty")
+       }
+
+       ChartURL := fmt.Sprintf(ricdms.Config.DownloadChartURLFormat, chartName, version)
+
+       resp, err := http.Get(ChartURL)
+       if err != nil {
+               return nil, err
+       }
+
+       return resp.Request.Body, nil
+}
index 1ac577a..b241aa0 100644 (file)
@@ -28,10 +28,11 @@ import (
 )
 
 type Conf struct {
-       LogLevel     string `yaml:"log-level"`
-       OnboardURL   string `yaml:"onborder-url"`
-       GetChartsURL string `yaml:"getCharts-url"`
-       MockServer   string `yaml:"mock-server"`
+       LogLevel               string `yaml:"log-level"`
+       OnboardURL             string `yaml:"onborder-url"`
+       GetChartsURL           string `yaml:"getCharts-url"`
+       DownloadChartURLFormat string `yaml:"download-charts-url-format"`
+       MockServer             string `yaml:"mock-server"`
 }
 
 func ReadConfiguration(configFile string) (c *Conf, err error) {
index acfd65a..e01d2fc 100644 (file)
@@ -75,6 +75,12 @@ func (r *Restful) setupHandler() {
                return resp
        })
 
+       api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
+               ricdms.Logger.Debug("==> Download helm chart")
+               resp := r.rh.DownloadChart(param.XAppName, param.Version)
+               return resp
+       })
+
        r.api = api
 }
 
index cc144d4..622bcaf 100644 (file)
@@ -58,3 +58,15 @@ func (rh *Resthook) GetCharts() (resp middleware.Responder) {
        }
        return charts.NewGetChartsListOK().WithPayload(chartList)
 }
+
+func (rh *Resthook) DownloadChart(chartname, version string) (resp middleware.Responder) {
+       ricdms.Logger.Debug("DownloadCharts: invoked")
+       reader, err := rh.ChartMgr.DownloadChart(chartname, version)
+
+       if err != nil {
+               ricdms.Logger.Error("Error : %v", err)
+               return charts.NewDownloadHelmChartInternalServerError()
+       }
+
+       return charts.NewDownloadHelmChartOK().WithPayload(reader)
+}
index 517fb11..b18b164 100644 (file)
@@ -22,11 +22,13 @@ package resthooks
 import (
        "encoding/json"
        "fmt"
+       "io"
        "io/ioutil"
        "net"
        "net/http"
        "net/http/httptest"
        "os"
+       "strings"
        "testing"
 
        ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
@@ -124,6 +126,24 @@ func TestGetCharts(t *testing.T) {
        assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
 }
 
+func TestDownloadChart(t *testing.T) {
+       svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+               ricdms.Logger.Debug("request received by mock to download chart")
+               reader := strings.NewReader("SAMPLE_RESPONSE")
+               data, _ := io.ReadAll(reader)
+               ricdms.Logger.Debug("writing : %+v", data)
+               w.Write(data)
+       }))
+       svr.Listener.Close()
+       svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
+
+       svr.Start()
+       defer svr.Close()
+
+       resp := rh.DownloadChart("CHART_NAME", "VERSION")
+       assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
+}
+
 type HealthCheckerMock struct {
        mock.Mock
 }