From: jw_wonny.cha Date: Thu, 28 Mar 2019 12:30:15 +0000 (+0900) Subject: - Remove unnecessary rest api (get service / delete service..) X-Git-Tag: submit/tizen/20190409.085658~10^2~8 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f4617b0b5b4559655805ac09b320d1a117c82f9e;p=platform%2Fcore%2Fsystem%2Fedge-orchestration.git - Remove unnecessary rest api (get service / delete service..) - Add notification rest api --- diff --git a/src/restapi/v1/restapi.go b/src/restapi/v1/restapi.go index a6afa65..946d879 100644 --- a/src/restapi/v1/restapi.go +++ b/src/restapi/v1/restapi.go @@ -19,8 +19,6 @@ import ( "os/exec" "servicemgr" "strconv" - - "github.com/gorilla/mux" ) // APIV1DeviceResourceUsageCPUGet function @@ -78,36 +76,17 @@ func APIV1ServicemgrServicesDelete(w http.ResponseWriter, r *http.Request) { func APIV1ServicemgrServicesPost(w http.ResponseWriter, r *http.Request) { log.Printf("[%s] APIV1ServicemgrServicesPost", logPrefix) - serviceParam, err := createServiceHandler(w, r) - - if err == nil { - data := servicemgr.Create(serviceParam) - writeJSONResponse(w, data, http.StatusOK) - } else if err == servicemgr.ErrInvalidService { - data := servicemgr.ServiceCreationResponse{Status: servicemgr.ConstServiceNotFound} - dataBytes, _ := json.Marshal(data) - writeJSONResponse(w, dataBytes, http.StatusNotFound) - } else { - writeJSONResponse(w, nil, http.StatusBadRequest) - } -} - -// APIV1ServicemgrServicesServiceIDDelete function -func APIV1ServicemgrServicesServiceIDDelete(w http.ResponseWriter, r *http.Request) { - log.Printf("[%s] APIV1ServicemgrServicesServiceIDDelete", logPrefix) + distService, err := executeServiceHandler(w, r) - vars := mux.Vars(r) - serviceID := vars["serviceid"] - id, err := strconv.ParseUint(serviceID, 10, 64) - name, _ := servicemgr.GetServiceName(id) + if err != nil { + log.Println(err.Error()) - if len(name) == 0 || err != nil { - smbytes, _ := json.Marshal(servicemgr.ServiceDestroyResponse{Return: servicemgr.ConstServiceNotFound}) + smbytes, _ := json.Marshal(servicemgr.ServiceExecutionResponse{Status: servicemgr.ConstServiceStatusFailed}) writeJSONResponse(w, smbytes, http.StatusBadRequest) } else { - go servicemgr.Delete(id) + go servicemgr.Run(distService) - smbytes, _ := json.Marshal(servicemgr.ServiceDestroyResponse{Return: "OK"}) + smbytes, _ := json.Marshal(servicemgr.ServiceExecutionResponse{Status: servicemgr.ConstServiceStatusStarted}) writeJSONResponse(w, smbytes, http.StatusOK) } } @@ -115,106 +94,19 @@ func APIV1ServicemgrServicesServiceIDDelete(w http.ResponseWriter, r *http.Reque // APIV1ServicemgrEventServiceIDPost function func APIV1ServicemgrEventServiceIDPost(w http.ResponseWriter, r *http.Request) { log.Printf("[%s] APIV1ServicemgrEventServiceIDPost", logPrefix) - writeJSONResponse(w, nil, http.StatusNotImplemented) - - // @ TODO - // vars := mux.Vars(r) - // serviceID := vars["serviceid"] - // id, err := strconv.ParseUint(serviceID, 10, 64) - - // if len(serviceID) == 0 || err != nil { - // writeJSONResponse(http.StatusBadRequest) - // } else { - // servicemgr.DataPathHandler(w, r, id) - - // writeJSONResponse(http.StatusOK) - // } } -// APIV1ServicemgrServicesServiceIDPost function -func APIV1ServicemgrServicesServiceIDPost(w http.ResponseWriter, r *http.Request) { - log.Printf("[%s] APIV1ServicemgrServicesServiceIDPost", logPrefix) - - vars := mux.Vars(r) - serviceID := vars["serviceid"] - id, _ := strconv.ParseUint(serviceID, 10, 64) +// APIV1ServicemgrServicesNotificationServiceIDPost function +func APIV1ServicemgrServicesNotificationServiceIDPost(w http.ResponseWriter, r *http.Request) { + log.Printf("[%s] APIV1ServicemgrServicesNotificationServiceIDPost", logPrefix) - log.Println("[serviceID] :", id) - - exist, _ := servicemgr.GetServiceName(id) - distService, err := executeServiceHandler(w, r, id) - - if len(exist) == 0 || err != nil { - smbytes, _ := json.Marshal(servicemgr.ServiceExecutionResponse{Status: servicemgr.ConstServiceStatusFailed}) - writeJSONResponse(w, smbytes, http.StatusBadRequest) - } else { - go servicemgr.Run(distService, id) - - smbytes, _ := json.Marshal(servicemgr.ServiceExecutionResponse{Status: servicemgr.ConstServiceStatusStarted}) - writeJSONResponse(w, smbytes, http.StatusOK) - } -} - -// APIV1ServicemgrServicesGet function -func APIV1ServicemgrServicesGet(w http.ResponseWriter, r *http.Request) { - log.Printf("[%s] APIV1ServicemgrServicesGet", logPrefix) - - ret, err := servicemgr.ServiceList() + statusNotification, err := notificationHandler(w, r) if err != nil { - writeJSONResponse(w, nil, http.StatusBadRequest) - } - - json, err := json.Marshal(ret) - if err == nil { - writeJSONResponse(w, json, http.StatusOK) + w.WriteHeader(http.StatusBadRequest) } else { - writeJSONResponse(w, nil, http.StatusBadRequest) - } -} - -// APIV1ServicemgrServicesAppnameGet function -func APIV1ServicemgrServicesAppnameGet(w http.ResponseWriter, r *http.Request) { - log.Printf("[%s] APIV1ServicemgrServicesAppnameGet", logPrefix) - - vars := mux.Vars(r) - appName := vars["appname"] - - ret, err := servicemgr.FindServiceByName(appName) - if err != nil { - writeJSONResponse(w, nil, http.StatusBadRequest) - } - - json, err := json.Marshal(ret) - if err == nil { - writeJSONResponse(w, json, http.StatusOK) - } else { - writeJSONResponse(w, nil, http.StatusBadRequest) - } -} - -// APIV1ServicemgrServicesServiceIDGet function -func APIV1ServicemgrServicesServiceIDGet(w http.ResponseWriter, r *http.Request) { - log.Printf("[%s] APIV1ServicemgrServicesServiceIDGet", logPrefix) - - vars := mux.Vars(r) - serviceID := vars["serviceid"] - - id, err := strconv.ParseUint(serviceID, 10, 64) - if err != nil { - writeJSONResponse(w, nil, http.StatusBadRequest) - } - - ret, err := servicemgr.FindServiceByID(id) - if err != nil { - writeJSONResponse(w, nil, http.StatusBadRequest) - } - - json, err := json.Marshal(ret) - if err == nil { - writeJSONResponse(w, json, http.StatusOK) - } else { - writeJSONResponse(w, nil, http.StatusBadRequest) + go servicemgr.HandleNoti(statusNotification) + w.WriteHeader(http.StatusOK) } } @@ -225,38 +117,22 @@ func writeJSONResponse(w http.ResponseWriter, data []byte, status int) { w.Write(data) } -func createServiceHandler(w http.ResponseWriter, req *http.Request) (serviceParam map[string]interface{}, err error) { +func executeServiceHandler(w http.ResponseWriter, req *http.Request) (distService map[string]interface{}, err error) { decoder := json.NewDecoder(req.Body) - - err = decoder.Decode(&serviceParam) + err = decoder.Decode(&distService) if err != nil { - log.Println(err.Error()) return } - binary := serviceParam["ServiceBinPath"].(string) + serviceParam["ServiceName"].(string) - - _, err = exec.LookPath(binary) + _, err = exec.LookPath(distService["ServiceName"].(string)) if err != nil { - log.Println(err.Error()) err = errors.New("It is Invalid Service") - } - - return -} - -func executeServiceHandler(w http.ResponseWriter, req *http.Request, serviceID uint64) (distService map[string]interface{}, err error) { - - decoder := json.NewDecoder(req.Body) - err = decoder.Decode(&distService) - if err != nil { - log.Println(err.Error()) return + } remoteAddr, _, err := net.SplitHostPort(req.RemoteAddr) if err != nil { - log.Println(err.Error()) return } @@ -265,22 +141,9 @@ func executeServiceHandler(w http.ResponseWriter, req *http.Request, serviceID u return } -// @TODO DataPathHandler function (service <-> service) -// func DataPathHandler(w http.ResponseWriter, req *http.Request, serviceID uint64) (err error) { -// msgChan, err := GetMsgChan(serviceID) -// if err != nil { -// return -// } - -// decoder := json.NewDecoder(req.Body) -// msgFormat := MsgFormat{} -// err = decoder.Decode(&msgFormat) - -// if err != nil { -// return -// } - -// msgChan <- msgFormat +func notificationHandler(w http.ResponseWriter, req *http.Request) (statusNoti map[string]interface{}, err error) { + decoder := json.NewDecoder(req.Body) + err = decoder.Decode(&statusNoti) -// return -// } + return +} diff --git a/src/restapi/v1/restapi_test.go b/src/restapi/v1/restapi_test.go index cd3b879..a3d12ab 100644 --- a/src/restapi/v1/restapi_test.go +++ b/src/restapi/v1/restapi_test.go @@ -9,10 +9,8 @@ import ( "net/http" "net/http/httptest" "servicemgr" - "strconv" "strings" "testing" - "time" ) var ( @@ -30,7 +28,8 @@ const ( // @TODO discovery mgr URI // Service mgr URI - ConstServicemgrServices = "/api/v1/servicemgr/services" + ConstServicemgrServices = "/api/v1/servicemgr/services" + ConstServicemgrServicesNoti = "/api/v1/servicemgr/services/notification/" ConstAppName = "AppName" ConstServiceName = "ls" @@ -164,40 +163,20 @@ func testPost(t *testing.T, targetURI string, bodyContents []byte, statusCode in return } -func registerService(t *testing.T) (createResponse servicemgr.ServiceCreationResponse) { +func executeService(t *testing.T) { t.Helper() - - body := servicemgr.ServiceParam{AppName: ConstAppName, ServiceName: ConstServiceName, Count: 1} - t.Log(body) - bdbytes, err := json.Marshal(body) - - if err != nil { - t.Log(err.Error()) - t.Error() - } - targetURI := ConstServicemgrServices - res := []byte(testPost(t, targetURI, bdbytes, http.StatusOK).Content) - err = json.Unmarshal(res, &createResponse) + appInfo := make(map[string]interface{}) - if err != nil { - t.Log(err.Error()) - t.Error() - } + appInfo[servicemgr.ConstKeyServiceID] = 1 + appInfo[servicemgr.ConstKeyServiceName] = ConstServiceName + appInfo[servicemgr.ConstKeyNotiTargetURL] = "URL" - return -} - -func executeService(t *testing.T, serviceID uint64) { - t.Helper() - targetURI := ConstServicemgrServices + "/" + strconv.FormatUint(serviceID, 10) + userArgs := []string{"-ail"} + appInfo[servicemgr.ConstKeyUserArgs] = userArgs - userArgs := make([]string, 1) - userArgs[0] = "-ail" - - body := servicemgr.DistService{UserArgs: userArgs} - bdbytes, err := json.Marshal(body) + bdbytes, err := json.Marshal(appInfo) if err != nil { t.Log(err.Error()) @@ -240,58 +219,23 @@ func TestAPIV1DeviceResourceUsageNetworkGet(t *testing.T) { } func TestAPIV1ServicemgrServicesPost(t *testing.T) { - registerService(t) + executeService(t) } -func TestAPIV1ServicemgrServicesGet(t *testing.T) { - registerService(t) +func TestAPIV1ServicemgrServicesNotificationServiceIDPost(t *testing.T) { + statusNotificationRequest := make(map[string]interface{}) + statusNotificationRequest["ServiceID"] = 1 + statusNotificationRequest["Status"] = "Failed" - targetURI := ConstServicemgrServices - testGet(t, targetURI, http.StatusOK) -} + snbytes, _ := json.Marshal(statusNotificationRequest) -func TestAPIV1ServicemgrServicesGetByAppName(t *testing.T) { - // @TODO add register service - registerService(t) + targetURI := ConstServicemgrServicesNoti - targetURI := ConstServicemgrServices + "/" + ConstAppName - testGet(t, targetURI, http.StatusOK) -} - -func TestAPIV1ServicemgrServicesGetByServiceID(t *testing.T) { - // @TODO add register service - createResponse := registerService(t) - - targetURI := ConstServicemgrServices + "/" + strconv.FormatUint(createResponse.ServiceList[0].ID, 10) - testGet(t, targetURI, http.StatusOK) -} - -func TestAPIV1ServicemgrServicesServiceIDPost(t *testing.T) { - createResponse := registerService(t) - executeService(t, createResponse.ServiceList[0].ID) - - time.Sleep(time.Second * 2) -} - -func TestAPIV1ServicemgrServicesServiceIDDeleteNotFoundService(t *testing.T) { - // createResponse := registerService(t) - // executeService(t, createResponse.ServiceList[0].ID) - - // time.Sleep(time.Second * 1) - // targetURI := ConstServicemgrServices + "/" + strconv.FormatUint(createResponse.ServiceList[0].ID, 10) - - targetURI := ConstServicemgrServices + "/6" - res, err := DoDelete(testURL + targetURI) + res, err := DoPost(testURL+targetURI+"1", snbytes) if err != nil { t.Log(err.Error()) t.Error() } - t.Log(res.Content) - if len(res.Content) == 0 { - t.Error() - } - - AssertEqualInt(t, res.Code, http.StatusBadRequest) - return + AssertEqualInt(t, res.Code, http.StatusOK) } diff --git a/src/restapi/v1/routers.go b/src/restapi/v1/routers.go index d80d7d5..5834c11 100644 --- a/src/restapi/v1/routers.go +++ b/src/restapi/v1/routers.go @@ -101,27 +101,6 @@ var routes = Routes{ APIV1DiscoverymgrDevicesGet, }, - Route{ - "APIV1ServicemgrServicesAppnameGet", - strings.ToUpper("Get"), - "/api/v1/servicemgr/services/{appname}", - APIV1ServicemgrServicesAppnameGet, - }, - - Route{ - "APIV1ServicemgrServicesDelete", - strings.ToUpper("Delete"), - "/api/v1/servicemgr/services", - APIV1ServicemgrServicesDelete, - }, - - Route{ - "APIV1ServicemgrServicesGet", - strings.ToUpper("Get"), - "/api/v1/servicemgr/services", - APIV1ServicemgrServicesGet, - }, - Route{ "APIV1ServicemgrServicesPost", strings.ToUpper("Post"), @@ -130,16 +109,9 @@ var routes = Routes{ }, Route{ - "APIV1ServicemgrServicesServiceIDDelete", - strings.ToUpper("Delete"), - "/api/v1/servicemgr/services/{serviceid}", - APIV1ServicemgrServicesServiceIDDelete, - }, - - Route{ - "APIV1ServicemgrServicesServiceIDPost", + "APIV1ServicemgrServicesNotificationServiceIDPost", strings.ToUpper("Post"), - "/api/v1/servicemgr/services/{serviceid}", - APIV1ServicemgrServicesServiceIDPost, + "/api/v1/servicemgr/services/notification/{serviceid}", + APIV1ServicemgrServicesNotificationServiceIDPost, }, }