xapp-frame go imrpovements 22/10922/1
authorJuha Hyttinen <juha.hyttinen@nokia.com>
Tue, 31 Jan 2023 11:03:36 +0000 (13:03 +0200)
committerJuha Hyttinen <juha.hyttinen@nokia.com>
Mon, 15 May 2023 05:58:49 +0000 (08:58 +0300)
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 <juha.hyttinen@nokia.com>
Change-Id: Ib5f4cce4cbad3a84c0c412c9b9b03f2cd69605ca

pkg/xapp/restapi.go
pkg/xapp/xapp.go

index 54e18ea..2cdee59 100755 (executable)
@@ -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
 }
index 12f1628..d08e1cf 100755 (executable)
@@ -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