Commit 07e596ef authored by Mubeena Ishaq's avatar Mubeena Ishaq
Browse files

Implement POST method for registrations endpoint

parent 0f884311
Loading
Loading
Loading
Loading
+91 −37
Original line number Diff line number Diff line
@@ -622,6 +622,66 @@ func timingCurrentTimeGET(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, string(jsonResponse))
}

func registerAppPost(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	log.Info("applicationsRegistrationPOST")

	// Decode the request body into AppInfo struct
	var appInfo AppInfo
	err := json.NewDecoder(r.Body).Decode(&appInfo)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Validation of mandatory parameters
	if appInfo.AppName == "" {
		log.Error("Mandatory AppName parameter not present")
		errHandlerProblemDetails(w, "Mandatory AppName parameter not present", http.StatusBadRequest)
		return
	}

	if appInfo.AppInstanceId == "" {
		log.Error("Mandatory appInstanceId parameter should be present")
		errHandlerProblemDetails(w, "Mandatory attribute appInstanceId is missing in the request body.", http.StatusBadRequest)
		return
	}

	// Get App instance
	appId, err := getAppInfo(appInfo.AppInstanceId)
	if err != nil {
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

	// Validate App info
	code, problemDetails, err := validateAppInfo(appId)
	if err != nil {
		log.Error(err.Error())
		if problemDetails != "" {
			w.WriteHeader(code)
			fmt.Fprint(w, problemDetails)
		} else {
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}

	var jsonResponse []byte

	jsonResponse, err = setApplicationInfo(appInfo)
	if err != nil {
		log.Error("Unable to store new Registration in redis")
		errHandlerProblemDetails(w, "Unable to store new Registration in redis", http.StatusInternalServerError)
		return
	}

	// On successful registration, return 201 Created with the response body
	w.WriteHeader(http.StatusCreated)
	fmt.Fprint(w, string(jsonResponse))
}

func deleteAppInstance(appId string) {
	log.Info("Deleting App instance: ", appId)

@@ -834,7 +894,7 @@ func refreshApps() error {
func getApp(appId string) (map[string]string, error) {
	appInfo, found := appInfoMap[appId]
	if !found {
		return nil, errors.New("App Instance not found")
		return nil, errors.New("app instance not found")
	}
	return appInfo, nil
}
@@ -849,7 +909,7 @@ func updateApp(appId string) (map[string]string, error) {

	// If MEP instance, ignore non-local apps
	if mepName != globalMepName && app.Node != mepName {
		return nil, errors.New("Ignoring app update on other MEP")
		return nil, errors.New("ignoring app update on other MEP")
	}

	mutex.Lock()
@@ -1011,6 +1071,35 @@ func sendAppRemoveCnf(id string) {
	}
}

/*
* getAppInfo gets application information using its application Instance Id stored in redis
* @param {string} appId application Instance Id used to retrive its information from redis DB
*	@return {map[string]string} appInfo application information
* @return {error} err It returns error if there is no information associated with this appId
 */
func getAppInfo(appId string) (map[string]string, error) {
	var appInfo map[string]string

	// Get app instance from local MEP only
	baseKeyAppEn := dkm.GetKeyRoot(sandboxName) + appEnablementKey + ":mep:" + mepName + ":"
	key := baseKeyAppEn + "app:" + appId + ":info"
	appInfo, err := rc.GetEntry(key)
	if err != nil || len(appInfo) == 0 {
		return nil, errors.New("App Instance not found")
	}
	return appInfo, nil
}

func setApplicationInfo(appInfo AppInfo) ([]byte, error) {
	var jsonResponse []byte
	appInstanceId := appInfo.AppInstanceId
	keyName := baseKey + "appInfo:" + appInstanceId
	_ = rc.JSONSetEntry(keyName, ".", convertAppInfoToJson(&appInfo))
	jsonResponse, err := json.Marshal(appInfo)

	return jsonResponse, err
}

func errHandlerProblemDetails(w http.ResponseWriter, error string, code int) {
	var pd ProblemDetails
	pd.Detail = error
@@ -1021,38 +1110,3 @@ func errHandlerProblemDetails(w http.ResponseWriter, error string, code int) {
	w.WriteHeader(code)
	fmt.Fprint(w, jsonResponse)
}

func registerAppPost(w http.ResponseWriter, r *http.Request) {
	// Decode the request body into AppInfo struct
	var appInfo AppInfo
	err := json.NewDecoder(r.Body).Decode(&appInfo)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	// Validation of mandatory parameters
	if appInfo.AppName == "" {
		log.Error("Mandatory AppName parameter not present")
		errHandlerProblemDetails(w, "Mandatory AppName parameter not present", http.StatusBadRequest)
		return
	}
	// On successful registration, return 201 Created with the response body
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusCreated)

	// Marshal the response message content into JSON format
	responseBody, err := json.Marshal(appInfo)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
	// Send the response message content
	_, err = w.Write(responseBody)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
+11 −0
Original line number Diff line number Diff line
@@ -66,3 +66,14 @@ func convertProblemDetailstoJson(probdetails *ProblemDetails) string {
	}
	return string(jsonInfo)
}

func convertAppInfoToJson(obj *AppInfo) string {

	jsonInfo, err := json.Marshal(*obj)
	if err != nil {
		log.Error(err.Error())
		return ""
	}

	return string(jsonInfo)
}