change APIV1ScoringmgrScoreLibnameGet to handle body 44/206844/2
authordaeken.kwon <daeken.kwon@samsung.com>
Tue, 28 May 2019 00:33:38 +0000 (09:33 +0900)
committerdaeken.kwon <daeken.kwon@samsung.com>
Tue, 28 May 2019 06:27:50 +0000 (15:27 +0900)
Change-Id: Ia974792eb02bb02e661f46ad56092b36f5053a95

src/restapi/httpclient/http_sender.go
src/restapi/httpclient/internal.go
src/restapi/httpclient/types.go
src/restapi/v1/restapi.go
src/restapi/v1/restapi_test.go
src/restapi/v1/routers.go
src/scoringmgr/score.go

index 0035c1f..d9c8159 100755 (executable)
@@ -29,12 +29,28 @@ import (
 )
 
 // DoGet is for get request
-func DoGet(targetURL string) (respBytes []byte, statusCode int, err error) {
-       req, err := http.NewRequest("GET", targetURL, nil)
+func DoGet(targetURL string, pbytes []byte) (respBytes []byte, statusCode int, err error) {
+       if len(pbytes) == 0 {
+               log.Printf("DoGet body length is zero(0) !!")
+       }
+
+       encryptedReqByte, err := securemgr.EncryptByte(pbytes)
        if err != nil {
+               log.Println("data encryption failed !!")
+               respBytes = nil
                return
        }
 
+       buff := bytes.NewBuffer(encryptedReqByte)
+
+       req, err := http.NewRequest("GET", targetURL, buff)
+       if err != nil {
+               return
+       }
+
+       // Content-Type Header
+       req.Header.Add("Content-Type", "application/json")
+
        var netTransport = &http.Transport{
                Dial: (&net.Dialer{
                        Timeout: 5 * time.Second,
@@ -54,6 +70,7 @@ func DoGet(targetURL string) (respBytes []byte, statusCode int, err error) {
 
        defer resp.Body.Close()
 
+       // @TODO: decrypt only if status is OK
        statusCode = resp.StatusCode
        encryptedRespByte, err := ioutil.ReadAll(resp.Body)
        if err != nil {
@@ -114,6 +131,7 @@ func DoPost(targetURL string, pbytes []byte) (respBytes []byte, statusCode int,
 
        defer resp.Body.Close()
 
+       // @TODO: decrypt only if status is OK
        statusCode = resp.StatusCode
        encryptedRespByte, err := ioutil.ReadAll(resp.Body)
        if err != nil {
index abc9d08..cc4c6fd 100644 (file)
@@ -67,9 +67,17 @@ func DoExecuteRemoteDevice(appInfo map[string]interface{}, target string) (err e
 // DoGetScoreRemoteDevice for getting score in remote device
 func DoGetScoreRemoteDevice(appName string, target string) (scoreValue float64, err error) {
        targetURL := ConstPrefixHTTP + target + ":" +
-               strconv.Itoa(ConstWellknownPort) + ConstGetScoreURI + appName
+               strconv.Itoa(ConstWellknownPort) + ConstGetScoreURI
 
-       respBytes, statusCode, err := DoGet(targetURL)
+       appNameInfo := make(map[string]interface{})
+       appNameInfo["appName"] = appName
+
+       reqbytes, err := json.Marshal(appNameInfo)
+       if common.CheckError(err) == true {
+               return
+       }
+
+       respBytes, statusCode, err := DoGet(targetURL, reqbytes)
        if common.CheckError(err) == true {
                return scoreValue, err
        }
index af28d84..4b1951b 100644 (file)
@@ -35,7 +35,7 @@ const (
        ConstServiceExecuteURI = "/api/v1/servicemgr/services"
 
        // ConstGetScoreURI is URI for getting score value
-       ConstGetScoreURI = "/api/v1/scoringmgr/score/"
+       ConstGetScoreURI = "/api/v1/scoringmgr/score"
 
        // ConstServiceStatusNotiURI is URI for notification status of service
        ConstServiceStatusNotiURI = "/api/v1/servicemgr/services/notification/"
index bf09c42..d861e71 100755 (executable)
@@ -36,8 +36,6 @@ import (
        "os/exec"
        "restapi/httpclient"
 
-       "github.com/gorilla/mux"
-
        "discoverymgr"
        "scoringmgr"
        "securemgr"
@@ -154,13 +152,21 @@ func APIV1ServicemgrServicesNotificationServiceIDPost(w http.ResponseWriter, r *
 func APIV1ScoringmgrScoreLibnameGet(w http.ResponseWriter, r *http.Request) {
        log.Printf("[%s] APIV1ScoringmgrScoreLibnameGet", logPrefix)
 
-       vars := mux.Vars(r)
-       libName := vars["libname"]
+       // vars := mux.Vars(r)
+       // libName := vars["libname"]
+       // name := libName
+
+       encryptedReqByte, _ := ioutil.ReadAll(r.Body)
+       appNameInfo, err := securemgr.DecryptByteToJson(encryptedReqByte)
+       if err != nil {
+               return
+       }
+
+       name := appNameInfo["appName"]
 
-       name := libName
        target := httpclient.ConstLocalTarget
 
-       scoreValue, err := scoringmgr.GetScore(target, name)
+       scoreValue, err := scoringmgr.GetScore(target, name.(string))
 
        respInfo := make(map[string]interface{})
        respInfo["ScoreValue"] = scoreValue
index 2803bd0..e556fa3 100755 (executable)
@@ -18,6 +18,7 @@
 package v1
 
 import (
+       "common"
        "encoding/json"
        "log"
        "net/http"
@@ -81,10 +82,10 @@ func init() {
        log.Println("testURL:", testURL)
 }
 
-func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) {
-       targetURI := ConstDiscoverymgrDevices
-       testGet(t, targetURI, http.StatusOK)
-}
+// func TestAPIV1DiscoverymgrDevicesGet(t *testing.T) {
+//     targetURI := ConstDiscoverymgrDevices
+//     testGet(t, targetURI, http.StatusOK, nil)
+// }
 
 func TestAPIV1DiscoverymgrDevicesPost(t *testing.T) {
        getDevice(t, ConstDiscoverymgrDevices, ConstAppName, http.StatusOK)
@@ -108,7 +109,18 @@ func TestAPIV1ServicemgrServicesPostWithNotExistExecutionFile(t *testing.T) {
 
 func TestAPIV1ScoringmgrScoreLibnameGet(t *testing.T) {
        targetURI := ConstScoremgrScore
-       testGet(t, targetURI+"/mysum", http.StatusOK)
+
+       appNameInfoSample := make(map[string]interface{})
+       appNameSample := "/usr/share/mysum"
+       appNameInfoSample["appName"] = appNameSample
+
+       reqbytes, err := json.Marshal(appNameInfoSample)
+
+       if common.CheckError(err) == true {
+               return
+       }
+
+       testGet(t, targetURI, reqbytes)
 }
 
 func TestAPIV1ServicemgrServicesNotificationServiceIDPost(t *testing.T) {
@@ -143,10 +155,10 @@ func AssertEqualInt(t *testing.T, a, b int) {
        }
 }
 
-func testGet(t *testing.T, targetURI string, statusCode int) {
+func testGet(t *testing.T, targetURI string, reqbytes []byte) {
        t.Helper()
 
-       resByte, resStatusCode, err := httpclient.DoGet(testURL + targetURI)
+       resByte, resStatusCode, err := httpclient.DoGet(testURL+targetURI, reqbytes)
        if err != nil {
                t.Log(err.Error())
                t.Error()
@@ -157,7 +169,7 @@ func testGet(t *testing.T, targetURI string, statusCode int) {
                t.Error()
        }
 
-       AssertEqualInt(t, resStatusCode, statusCode)
+       AssertEqualInt(t, resStatusCode, http.StatusOK)
        return
 }
 
index 0f742a8..e912872 100644 (file)
@@ -114,7 +114,7 @@ var routes = Routes{
        Route{
                "APIV1ScoringmgrScoreLibnameGet",
                strings.ToUpper("Get"),
-               "/api/v1/scoringmgr/score/{libname}",
+               "/api/v1/scoringmgr/score",
                APIV1ScoringmgrScoreLibnameGet,
        },
 }
index 9f1978c..45f8c05 100755 (executable)
@@ -33,8 +33,10 @@ func GetScore(target string, name string) (scoreValue float64, err error) {
        }
 
        if strings.Compare(target, outboundIP) == 0 || strings.Compare(target, httpclient.ConstLocalTarget) == 0 {
+               log.Println("getScoreLocalEnv(): ", name, ", IP:", target)
                scoreValue, err = getScoreLocalEnv(name)
        } else {
+               log.Println("DoGetScoreRemoteDevice(): ", name, ", IP:", target)
                scoreValue, err = httpclient.DoGetScoreRemoteDevice(name, target)
        }