Пример #1
0
func apiServiceCall(call otto.FunctionCall) otto.Value {
	svc := call.Argument(0).String()

	interfaceValue, err := call.Argument(1).Export()
	if err != nil {
		logrus.Errorf("Plugins: rules: javascript supplied invalid parameters")
		return otto.UndefinedValue()
	}
	params := interfaceValue.(map[string]interface{})

	future := service.CallService(svc, params)
	result := <-future.Result

	var res otto.Value

	if _, ok := result.(error); ok {
		res, err = otto.ToValue(result.(error).Error())
	} else {
		res, err = otto.ToValue(result)
	}
	if err != nil {
		logrus.Errorf("Plugins: rules: failed to convert service result to javascript")
		return otto.UndefinedValue()
	}
	return res
}
Пример #2
0
func (vm *RuleVM) PrepareEnv() error {

	Service := func(call otto.FunctionCall) otto.Value {
		svc := call.Argument(0).String()

		interfaceValue, err := call.Argument(1).Export()
		if err != nil {
			logrus.Errorf("Plugins: rules: javascript supplied invalid parameters")
			return otto.UndefinedValue()
		}
		params := interfaceValue.(map[string]interface{})

		future := service.CallService(svc, params)
		result := <-future.Result

		var res otto.Value

		if _, ok := result.(error); ok {
			res, err = otto.ToValue(result.(error).Error())
		} else {
			res, err = otto.ToValue(result)
		}
		if err != nil {
			logrus.Errorf("Plugins: rules: failed to convert service result to javascript")
			return otto.UndefinedValue()
		}
		return res
	}

	Log := func(call otto.FunctionCall) otto.Value {
		logrus.Infof("Plugins: jsrule: %s Log: %s", vm.Name, call.Argument(0).String())
		return otto.Value{}
	}

	err := vm.otto.Set("Service", Service)
	if err != nil {
		return err
	}

	err = vm.otto.Set("Log", Log)
	if err != nil {
		return err
	}

	return nil
}
Пример #3
0
func callService(w rest.ResponseWriter, r *rest.Request) {
	svc := r.PathParam("backend") + "." + r.PathParam("service")
	var params interface{}
	r.DecodeJsonPayload(&params)

	if _, ok := params.(map[string]interface{}); !ok {
		w.WriteJson(map[string]string{"Error": "Call parameters must be a JSON object"})
		return
	}
	future := service.CallService(svc, params.(map[string]interface{}))

	result := <-future.Result

	switch r := result.(type) {
	case error:
		rest.Error(
			w,
			r.Error(),
			http.StatusInternalServerError,
		)
	default:
		w.WriteJson(r)
	}
}