Commit 912ba040 authored by Yann Garcia's avatar Yann Garcia
Browse files

Enhance error management in meep-sandbox-api; Add demo6/run.sh script

parent 2d7face9
Loading
Loading
Loading
Loading

examples/demo6/run.sh

0 → 100755
+9 −0
Original line number Diff line number Diff line
#!/bin/bash

set -e
set +x

docker run -it --rm meep-docker-registry:30001/demo6 /bin/bash

echo ""
echo ">>> Done"
+33 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2024  The AdvantEDGE Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package server

import (
	"encoding/json"
	//"fmt"

	log "github.com/InterDigitalInc/AdvantEDGE/go-packages/meep-logger"
)

func convertProblemDetailstoJson(probdetails *ProblemDetails) string {
	jsonInfo, err := json.Marshal(*probdetails)
	if err != nil {
		log.Error(err.Error())
		return ""
	}
	return string(jsonInfo)
}
+49 −54
Original line number Diff line number Diff line
@@ -251,6 +251,23 @@ func Stop() (err error) {
	return nil
}

/*
 * errHandlerProblemDetails sends an error message
 * @param {struct} HTTP write reference
 * @param {string} error contains the error message
 * @param {int} code contains the error code
 */
func errHandlerProblemDetails(w http.ResponseWriter, error string, code int) {
	var pb ProblemDetails
	pb.Detail = error
	pb.Status = int32(code)

	jsonResponse := convertProblemDetailstoJson(&pb)

	w.WriteHeader(code)
	fmt.Fprint(w, jsonResponse)
}

func login(w http.ResponseWriter, r *http.Request) {
	log.Info(">>> Login: ", r)

@@ -268,8 +285,9 @@ func login(w http.ResponseWriter, r *http.Request) {
		}
	} // End of 'for' statement
	if !found {
		log.Error("Wrong query parameter")
		w.WriteHeader(http.StatusBadRequest)
		err := errors.New("Wrong parameters: provider")
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	provider := q.Get("provider")
@@ -292,8 +310,7 @@ func login(w http.ResponseWriter, r *http.Request) {
	if err != nil {
		log.Error(err.Error())
		metricSessionFail.WithLabelValues("Session").Inc()
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprint(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -376,8 +393,9 @@ func logout(w http.ResponseWriter, r *http.Request) {
		}
	} // End of 'for' statement
	if !found {
		log.Error("Wrong query parameter")
		w.WriteHeader(http.StatusBadRequest)
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	sandbox_name := q.Get("sandbox_name")
@@ -407,8 +425,7 @@ func sandboxNetworkScenariosGET(w http.ResponseWriter, r *http.Request) {
	if q["sandbox_name"] == nil || len(q["sandbox_name"]) == 0 {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	sandbox_name := q["sandbox_name"][0]
@@ -417,8 +434,7 @@ func sandboxNetworkScenariosGET(w http.ResponseWriter, r *http.Request) {
	if sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name] == nil {
		err := errors.New("Sandbox not found")
		log.Error(err.Error())
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

@@ -440,7 +456,7 @@ func sandboxNetworkScenariosGET(w http.ResponseWriter, r *http.Request) {
	jsonResponse, err := json.Marshal(l)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("jsonResponse: ", string(jsonResponse))
@@ -464,8 +480,7 @@ func sandboxIndividualNetworkScenariosGET(w http.ResponseWriter, r *http.Request
	if q["network_scenario_id"] == nil || len(q["network_scenario_id"]) == 0 {
		err := errors.New("Wrong parameters: network_scenario_id")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	network_scenario_id := q["network_scenario_id"][0]
@@ -478,8 +493,7 @@ func sandboxIndividualNetworkScenariosGET(w http.ResponseWriter, r *http.Request
	if sandbox_name == "" {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandbox_name: ", sandbox_name)
@@ -487,7 +501,7 @@ func sandboxIndividualNetworkScenariosGET(w http.ResponseWriter, r *http.Request
	scenario, resp, err := sandboxApiConnectors.pfmCtrlClient.ScenarioConfigurationApi.GetScenario(context.TODO(), network_scenario_id)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandboxIndividualNetworkScenariosGET: resp: ", resp)
@@ -500,7 +514,7 @@ func sandboxIndividualNetworkScenariosGET(w http.ResponseWriter, r *http.Request
	jsonResponse, err := json.Marshal(l)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("jsonResponse: ", string(jsonResponse))
@@ -524,8 +538,7 @@ func sandboxNetworkScenarioPOST(w http.ResponseWriter, r *http.Request) {
	if q["network_scenario_id"] == nil || len(q["network_scenario_id"]) == 0 {
		err := errors.New("Wrong parameters: network_scenario_id")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	network_scenario_id := q["network_scenario_id"][0]
@@ -538,8 +551,7 @@ func sandboxNetworkScenarioPOST(w http.ResponseWriter, r *http.Request) {
	if sandbox_name == "" {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandbox_name: ", sandbox_name)
@@ -547,16 +559,14 @@ func sandboxNetworkScenarioPOST(w http.ResponseWriter, r *http.Request) {
	if sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name] == nil {
		err := errors.New("Sandbox not found")
		log.Error(err.Error())
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

	_, err := sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name].ActiveScenarioApi.ActivateScenario(context.TODO(), network_scenario_id, nil)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -579,8 +589,7 @@ func sandboxNetworkScenarioDELETE(w http.ResponseWriter, r *http.Request) {
	if sandbox_name == "" {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandbox_name: ", sandbox_name)
@@ -588,16 +597,14 @@ func sandboxNetworkScenarioDELETE(w http.ResponseWriter, r *http.Request) {
	if sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name] == nil {
		err := errors.New("Sandbox not found")
		log.Error(err.Error())
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

	_, err := sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name].ActiveScenarioApi.TerminateScenario(context.TODO())
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

@@ -677,8 +684,7 @@ func sandboxAppInstancesGET(w http.ResponseWriter, r *http.Request) {
	if sandbox_name == "" {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandbox_name: ", sandbox_name)
@@ -686,16 +692,14 @@ func sandboxAppInstancesGET(w http.ResponseWriter, r *http.Request) {
	if sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name] == nil {
		err := errors.New("Sandbox not found")
		log.Error(err.Error())
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

	appInstancesList, resp, err := sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name].ApplicationsApi.ApplicationsGET(context.TODO(), nil)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandboxAppInstancesGET: resp: ", resp)
@@ -705,8 +709,7 @@ func sandboxAppInstancesGET(w http.ResponseWriter, r *http.Request) {
	jsonResponse, err := json.Marshal(appInstancesList)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("jsonResponse: ", string(jsonResponse))
@@ -731,8 +734,7 @@ func sandboxAppInstancesPOST(w http.ResponseWriter, r *http.Request) {
	if sandbox_name == "" {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandbox_name: ", sandbox_name)
@@ -740,8 +742,7 @@ func sandboxAppInstancesPOST(w http.ResponseWriter, r *http.Request) {
	if sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name] == nil {
		err := errors.New("Sandbox not found")
		log.Error(err.Error())
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

@@ -751,8 +752,7 @@ func sandboxAppInstancesPOST(w http.ResponseWriter, r *http.Request) {
	err := json.Unmarshal(bodyBytes, &applicationInfo)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}
	log.Info("sandboxAppInstancesPOST: bodyBytes: ", string(bodyBytes))
@@ -804,8 +804,7 @@ func sandboxAppInstancesDELETE(w http.ResponseWriter, r *http.Request) {
	if sandbox_name == "" {
		err := errors.New("Wrong parameters: sandbox_name")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("sandbox_name: ", sandbox_name)
@@ -813,17 +812,14 @@ func sandboxAppInstancesDELETE(w http.ResponseWriter, r *http.Request) {
	if sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name] == nil {
		err := errors.New("Sandbox not found")
		log.Error(err.Error())
		w.WriteHeader(http.StatusNotFound)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

	appInstanceId := vars["app_instance_id"]
	if appInstanceId == "" {
		err := errors.New("Wrong parameters: app_instance_id")
		log.Error(err.Error())
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}
	log.Info("app_instance_id: ", appInstanceId)
@@ -831,8 +827,7 @@ func sandboxAppInstancesDELETE(w http.ResponseWriter, r *http.Request) {
	_, err := sandboxApiConnectors.sandboxCtrlAppClient[sandbox_name].ApplicationsApi.ApplicationsAppInstanceIdDELETE(context.TODO(), appInstanceId)
	if err != nil {
		log.Error(err.Error())
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintf(w, err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		return
	}

+12 −0
Original line number Diff line number Diff line
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=