From f8dc81bc7dd198da9fcb17bef2e164dc71c39e49 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Mon, 10 Oct 2022 18:21:51 +0000 Subject: [PATCH] Implementation for downloading charts Implemented API to download helm chart for specified chartname and version. Signed-off-by: subhash kumar singh Change-Id: I343dc18870c81d182a728677e3b21e5fc50d84af --- api/ric-dms-api-2.0.yaml | 2 +- config/config-test.yaml | 3 ++- config/config.yaml | 3 ++- pkg/charts/chart_manager.go | 20 ++++++++++++++++++++ pkg/config/conf.go | 9 +++++---- pkg/restful/restful.go | 6 ++++++ pkg/resthooks/resthooks.go | 12 ++++++++++++ pkg/resthooks/resthooks_test.go | 20 ++++++++++++++++++++ 8 files changed, 68 insertions(+), 7 deletions(-) diff --git a/api/ric-dms-api-2.0.yaml b/api/ric-dms-api-2.0.yaml index b8c8a99..5679271 100644 --- a/api/ric-dms-api-2.0.yaml +++ b/api/ric-dms-api-2.0.yaml @@ -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: diff --git a/config/config-test.yaml b/config/config-test.yaml index df5b0dc..4e57661 100644 --- a/config/config-test.yaml +++ b/config/config-test.yaml @@ -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 diff --git a/config/config.yaml b/config/config.yaml index a7aecaf..f27d413 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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 diff --git a/pkg/charts/chart_manager.go b/pkg/charts/chart_manager.go index 252b2dc..75ec338 100644 --- a/pkg/charts/chart_manager.go +++ b/pkg/charts/chart_manager.go @@ -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 +} diff --git a/pkg/config/conf.go b/pkg/config/conf.go index 1ac577a..b241aa0 100644 --- a/pkg/config/conf.go +++ b/pkg/config/conf.go @@ -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) { diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index acfd65a..e01d2fc 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -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 } diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index cc144d4..622bcaf 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -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) +} diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index 517fb11..b18b164 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -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 } -- 2.16.6