From: subhash kumar singh Date: Fri, 23 Feb 2024 09:58:00 +0000 (+0000) Subject: Hide the S3Manager details from DbMgr inerface X-Git-Tag: 2.0.0~4 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=3fa7ca4a1111032d4e8ef01be7d62361630a4e61;p=aiml-fw%2Fawmf%2Fmodelmgmtservice.git Hide the S3Manager details from DbMgr inerface Used DbMgr interface to hide the details of S3Maanager in service layer. Also fixed the the close on body if the err is thrown. Change-Id: If113b9bdf9c7c5e43994c459c21995e5e02e331b Signed-off-by: subhash kumar singh --- diff --git a/apis/mmes_apis.go b/apis/mmes_apis.go index c24e905..8d34437 100644 --- a/apis/mmes_apis.go +++ b/apis/mmes_apis.go @@ -34,13 +34,13 @@ type ModelInfo struct { Metainfo map[string]interface{} `json:"meta-info"` } -var s3_manager *core.S3Manager +var dbmgr core.DBMgr func init() { logging.INFO("Starting api server...") - // set up the s3 manager - s3_manager = core.GetS3ManagerInstance() + // set up the db manager + dbmgr = core.GetDBManagerInstance() router := gin.Default() @@ -75,12 +75,12 @@ func RegisterModel(cont *gin.Context) { logging.INFO(modelInfo.ModelName, modelInfo.RAppId, modelInfo.Metainfo) modelInfoBytes, _ := json.Marshal(modelInfo) - s3Err := s3_manager.CreateBucket(modelInfo.ModelName) - if s3Err == nil { - s3_manager.UploadFile(modelInfoBytes, modelInfo.ModelName+os.Getenv("INFO_FILE_POSTFIX"), modelInfo.ModelName) + err := dbmgr.CreateBucket(modelInfo.ModelName) + if err == nil { + dbmgr.UploadFile(modelInfoBytes, modelInfo.ModelName+os.Getenv("INFO_FILE_POSTFIX"), modelInfo.ModelName) } else { returnCode = http.StatusInternalServerError - responseMsg = s3Err.Error() + responseMsg = err.Error() } cont.JSON(returnCode, gin.H{ "code": returnCode, @@ -104,7 +104,7 @@ func GetModelInfo(cont *gin.Context) { model_name := jsonMap["model-name"].(string) logging.INFO("The request model name: ", model_name) - model_info := s3_manager.GetBucketObject(model_name+os.Getenv("INFO_FILE_POSTFIX"), model_name) + model_info := dbmgr.GetBucketObject(model_name+os.Getenv("INFO_FILE_POSTFIX"), model_name) cont.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, @@ -119,7 +119,7 @@ func GetModelInfoByName(cont *gin.Context) { logging.INFO("Get model info by name API ...") modelName := cont.Param("modelName") - model_info := s3_manager.GetBucketObject(modelName+os.Getenv("INFO_FILE_POSTFIX"), modelName) + model_info := dbmgr.GetBucketObject(modelName+os.Getenv("INFO_FILE_POSTFIX"), modelName) cont.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, @@ -142,7 +142,7 @@ func UploadModel(cont *gin.Context) { byteFile, _ := io.ReadAll((file)) logging.INFO("Uploading model : ", modelName) - s3_manager.UploadFile(byteFile, modelName+os.Getenv("MODEL_FILE_POSTFIX"), modelName) + dbmgr.UploadFile(byteFile, modelName+os.Getenv("MODEL_FILE_POSTFIX"), modelName) cont.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, "message": string("Model uploaded successfully.."), @@ -150,14 +150,14 @@ func UploadModel(cont *gin.Context) { } /* -API to download the trained model from s3 bucket +API to download the trained model from bucket Input: model name in path params as "modelName" */ func DownloadModel(cont *gin.Context) { logging.INFO("Download model API ...") modelName := cont.Param("modelName") fileName := modelName + os.Getenv("MODEL_FILE_POSTFIX") - fileByes := s3_manager.GetBucketObject(fileName, modelName) + fileByes := dbmgr.GetBucketObject(fileName, modelName) //Return file in api reponse using byte slice cont.Header("Content-Disposition", "attachment;"+fileName) diff --git a/core/s3_manager.go b/core/s3_manager.go index b54e06b..e787cf4 100644 --- a/core/s3_manager.go +++ b/core/s3_manager.go @@ -54,7 +54,7 @@ type DBMgr interface { } // Singleton for S3Manager -func GetS3ManagerInstance() *S3Manager { +func GetDBManagerInstance() DBMgr { Lock.Lock() defer Lock.Unlock() @@ -64,7 +64,6 @@ func GetS3ManagerInstance() *S3Manager { } else { logging.WARN("S3Manager instance already exists") } - return s3MgrInstance } @@ -123,15 +122,16 @@ func (s3manager *S3Manager) GetBucketObject(objectName string, bucketName string Key: aws.String(objectName), } result, err := s3manager.S3Client.GetObject(getInputs) - defer result.Body.Close() - if err != nil { logging.ERROR("Error, can't get fetch object..") return response } + defer result.Body.Close() logging.INFO("Successfully retrieved object...") - //TODO : Error handling response, err = io.ReadAll(result.Body) + if err != nil { + logging.ERROR("Recived error while reading body:", err) + } return response } @@ -188,5 +188,5 @@ func (s3manager *S3Manager) ListBucket() { } // Return list of objects in the buckets -func GetBucketItems(bucketName string) { +func (S3Manager *S3Manager) GetBucketItems(bucketName string) { }