From d8d204f3ca9ebfd256043f6a0de526887fdcace9 Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Wed, 12 Oct 2022 18:07:44 +0000 Subject: [PATCH] Implement get the charts by name and version Implemented the query to get the available charts details for given xApp name and version. Signed-off-by: subhash kumar singh Change-Id: I9eda1f0375130c9cdb42e3cb61c818f2f929440b --- api/ric-dms-api-2.0.yaml | 7 ++--- config/config-test.yaml | 3 +- config/config.yaml | 3 +- pkg/charts/chart_manager.go | 34 ++++++++++++++++++++++ pkg/config/conf.go | 13 +++++---- pkg/restful/restful.go | 6 ++++ .../mocks/resp-get-charts-by-name-and-ver.json | 12 ++++++++ pkg/resthooks/resthooks.go | 11 +++++++ pkg/resthooks/resthooks_test.go | 28 ++++++++++++++++++ 9 files changed, 105 insertions(+), 12 deletions(-) create mode 100644 pkg/resthooks/mocks/resp-get-charts-by-name-and-ver.json diff --git a/api/ric-dms-api-2.0.yaml b/api/ric-dms-api-2.0.yaml index 421dbcd..ff55bf9 100644 --- a/api/ric-dms-api-2.0.yaml +++ b/api/ric-dms-api-2.0.yaml @@ -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: diff --git a/config/config-test.yaml b/config/config-test.yaml index 9680096..b2b2461 100644 --- a/config/config-test.yaml +++ b/config/config-test.yaml @@ -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 diff --git a/config/config.yaml b/config/config.yaml index 7f4470e..861912d 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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 diff --git a/pkg/charts/chart_manager.go b/pkg/charts/chart_manager.go index 0f6f7af..c0308dd 100644 --- a/pkg/charts/chart_manager.go +++ b/pkg/charts/chart_manager.go @@ -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 +} diff --git a/pkg/config/conf.go b/pkg/config/conf.go index fe5a980..c24500f 100644 --- a/pkg/config/conf.go +++ b/pkg/config/conf.go @@ -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) { diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index ca45417..9c9e861 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -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 index 0000000..dde2dc8 --- /dev/null +++ b/pkg/resthooks/mocks/resp-get-charts-by-name-and-ver.json @@ -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 diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index b461344..519e636 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -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) +} diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index c8deeb2..8e2dfcc 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -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 } -- 2.16.6