From 2fe100b35c1586dba1d7e9856fdec68091c22df0 Mon Sep 17 00:00:00 2001 From: Juha Hyttinen Date: Tue, 31 Jan 2023 13:03:36 +0200 Subject: [PATCH] xapp-frame go imrpovements xapp registration error handling won't work fully as it won't return error when status code in response is wrong. This will stop app registeration retries. Config file get rest functionality tries to read config file from hard coded path, which is wrong assumtion. xapp-frame reads config file given as argument "-f" or from CFG_FILE env variable. Thou config file rest api should try read file that is really used. Signed-off-by: Juha Hyttinen Change-Id: Ib5f4cce4cbad3a84c0c412c9b9b03f2cd69605ca --- pkg/xapp/restapi.go | 22 +++++++++++++++------- pkg/xapp/xapp.go | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 54e18ea..2cdee59 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -316,15 +316,25 @@ func appconfigHandler(w http.ResponseWriter, r *http.Request) { metadata.XappName = &name metadata.ConfigType = &configtype - configFile, err := os.Open("/opt/ric/config/config-file.json") - if err != nil { - Logger.Error("Cannot open config file: %v", err) + // Read config-files + cfiles := []string{viper.ConfigFileUsed(), "/opt/ric/config/config-file.json"} + + var err error + var configFile *os.File + for _, cfile := range cfiles { + configFile, err = os.Open(cfile) + if err != nil { + configFile = nil + Logger.Error("Cannot open config file: %s err: %v", cfile, err) + } + } + if err != nil || configFile == nil { + Logger.Error("Cannot open any of listed config files: %v", cfiles) respondWithJSON(w, http.StatusInternalServerError, nil) - // return nil,errors.New("Could Not parse the config file") + return } body, err := ioutil.ReadAll(configFile) - defer configFile.Close() xappconfig.Metadata = &metadata @@ -333,6 +343,4 @@ func appconfigHandler(w http.ResponseWriter, r *http.Request) { appconfig = append(appconfig, &xappconfig) respondWithJSON(w, http.StatusOK, appconfig) - - //return appconfig,nil } diff --git a/pkg/xapp/xapp.go b/pkg/xapp/xapp.go index 12f1628..d08e1cf 100755 --- a/pkg/xapp/xapp.go +++ b/pkg/xapp/xapp.go @@ -152,9 +152,21 @@ func getPltNamespace(envName, defVal string) string { func doPost(pltNs, url string, msg []byte, status int) error { resp, err := http.Post(fmt.Sprintf(url, pltNs, pltNs), "application/json", bytes.NewBuffer(msg)) if err != nil || resp == nil || resp.StatusCode != status { - Logger.Info("http.Post to '%s' failed with error: %v", fmt.Sprintf(url, pltNs, pltNs), err) - return err + logdesc := fmt.Sprintf("http.Post to '%s' failed with", fmt.Sprintf(url, pltNs, pltNs)) + if resp != nil { + logdesc += fmt.Sprintf(" status: %d != %d", resp.StatusCode, status) + } else { + logdesc += fmt.Sprintf(" resp: nil") + } + if err != nil { + logdesc += fmt.Sprintf(" err: %s", err.Error()) + } else { + logdesc += fmt.Sprintf(" err: nil") + } + Logger.Info(logdesc) + return fmt.Errorf(logdesc) } + Logger.Info("Post to '%s' done, status:%v", fmt.Sprintf(url, pltNs, pltNs), resp.Status) return err -- 2.16.6