From: subhash kumar singh Date: Wed, 12 Oct 2022 11:58:52 +0000 (+0000) Subject: Implement get the charts by name X-Git-Tag: 2.0.0~13 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F45%2F9245%2F1;p=ric-plt%2Fricdms.git Implement get the charts by name Implemented the query to get the available charts details for given xApp name. Signed-off-by: subhash kumar singh Change-Id: I8eedc09c3a9f661a5cdd0a597211878440f8e05e --- diff --git a/api/ric-dms-api-2.0.yaml b/api/ric-dms-api-2.0.yaml index 5679271..421dbcd 100644 --- a/api/ric-dms-api-2.0.yaml +++ b/api/ric-dms-api-2.0.yaml @@ -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: diff --git a/config/config-test.yaml b/config/config-test.yaml index 4e57661..9680096 100644 --- a/config/config-test.yaml +++ b/config/config-test.yaml @@ -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 diff --git a/config/config.yaml b/config/config.yaml index f27d413..7f4470e 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -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 diff --git a/pkg/charts/chart_manager.go b/pkg/charts/chart_manager.go index 75ec338..0f6f7af 100644 --- a/pkg/charts/chart_manager.go +++ b/pkg/charts/chart_manager.go @@ -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 +} diff --git a/pkg/config/conf.go b/pkg/config/conf.go index b241aa0..fe5a980 100644 --- a/pkg/config/conf.go +++ b/pkg/config/conf.go @@ -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"` } diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index e01d2fc..ca45417 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -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 index 0000000..d2f224b --- /dev/null +++ b/pkg/resthooks/mocks/resp-get-charts-by-name.json @@ -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 diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index 622bcaf..b461344 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -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) +} diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index b18b164..c8deeb2 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -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 }