AgentRequest in agents for more flexibility in filters and processing agent requests

This commit is contained in:
DanB
2018-06-14 19:15:28 +02:00
parent 072c14d76f
commit 5d2de6b644
6 changed files with 44 additions and 23 deletions

View File

@@ -21,7 +21,6 @@ package agents
import (
"fmt"
"net/http"
"strconv"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
@@ -58,13 +57,11 @@ func (ha *HTTPAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) {
utils.HTTPAgent, err.Error()))
return
}
agReq := newAgentRequest(dcdr)
var processed bool
procVars := make(processorVars)
rpl := engine.NewNavigableMap(nil)
for _, reqProcessor := range ha.reqProcessors {
var lclProcessed bool
if lclProcessed, err = ha.processRequest(reqProcessor, dcdr,
procVars, rpl); lclProcessed {
if lclProcessed, err = ha.processRequest(reqProcessor, agReq); lclProcessed {
processed = lclProcessed
}
if err != nil ||
@@ -74,13 +71,13 @@ func (ha *HTTPAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}
if err != nil {
utils.Logger.Warning(
fmt.Sprintf("<%s> error: %s processing request: %s, process vars: %+v",
utils.HTTPAgent, err.Error(), utils.ToJSON(req), procVars))
fmt.Sprintf("<%s> error: %s processing request: %s",
utils.HTTPAgent, err.Error(), utils.ToJSON(req)))
return // FixMe with returning some error on HTTP level
} else if !processed {
utils.Logger.Warning(
fmt.Sprintf("<%s> no request processor enabled, ignoring request %s, process vars: %+v",
utils.HTTPAgent, utils.ToJSON(req), procVars))
fmt.Sprintf("<%s> no request processor enabled, ignoring request %s",
utils.HTTPAgent, utils.ToJSON(req)))
return // FixMe with returning some error on HTTP level
}
encdr, err := newHAReplyEncoder(ha.rplyPayload, w)
@@ -90,33 +87,28 @@ func (ha *HTTPAgent) ServeHTTP(w http.ResponseWriter, req *http.Request) {
utils.HTTPAgent, err.Error()))
return
}
if err = encdr.encode(rpl); err != nil {
if err = encdr.encode(agReq.Reply); err != nil {
utils.Logger.Warning(
fmt.Sprintf("<%s> error: %s encoding out %s",
utils.HTTPAgent, err.Error(), utils.ToJSON(rpl)))
utils.HTTPAgent, err.Error(), utils.ToJSON(agReq.Reply)))
return
}
}
// processRequest represents one processor processing the request
func (ha *HTTPAgent) processRequest(reqProcessor *config.HttpAgntProcCfg,
dP engine.DataProvider, procVars processorVars,
reply *engine.NavigableMap) (processed bool, err error) {
tnt, err := dP.FieldAsString([]string{utils.Tenant})
agReq *AgentRequest) (processed bool, err error) {
tnt, err := agReq.Request.FieldAsString([]string{utils.Tenant})
if err != nil {
return false, err
}
if pass, err := ha.filterS.Pass(tnt, reqProcessor.Filters, dP); err != nil {
if pass, err := ha.filterS.Pass(tnt, reqProcessor.Filters, agReq); err != nil {
return false, err
} else if !pass {
return false, nil
}
for k, v := range reqProcessor.Flags { // update procVars with flags from processor
procVars[k] = strconv.FormatBool(v)
}
if reqProcessor.DryRun {
utils.Logger.Info(fmt.Sprintf("<%s> DRY_RUN, HTTP request: %s", utils.HTTPAgent, dP))
utils.Logger.Info(fmt.Sprintf("<%s> DRY_RUN, process variables: %+v", utils.HTTPAgent, procVars))
utils.Logger.Info(fmt.Sprintf("<%s> DRY_RUN, HTTP request: %s", utils.HTTPAgent, agReq))
}
/*
ev, err := radReqAsCGREvent(req, procVars, reqProcessor.Flags, reqProcessor.RequestFields)

View File

@@ -25,7 +25,7 @@ import (
"github.com/cgrates/cgrates/engine"
)
// newHAReqDecoder produces decoders
// newHADataProvider constructs a DataProvider
func newHADataProvider(dpType string,
req *http.Request) (dP engine.DataProvider, err error) {
switch dpType {

View File

@@ -24,8 +24,8 @@ import (
// DataProvider is a data source from multiple formats
type DataProvider interface {
String() string // printable version of data
FieldAsInterface(fldPath []string) (interface{}, error)
FieldAsString(fldPath []string) (string, error)
String() string // printable versin of data
AsNavigableMap([]*config.CfgCdrField) (*NavigableMap, error)
}

View File

@@ -543,6 +543,9 @@ const (
MetaUrl = "*url"
MetaXml = "*xml"
ApiKey = "apikey"
MetaRequest = "*request"
MetaVars = "*vars"
MetaReply = "*reply"
)
// Migrator Action

View File

@@ -75,3 +75,11 @@ func PrefixSliceItems(slc []string, prfx string) (out []string) {
}
return
}
// StripSlicePrefix will strip a number of items from the beginning of the slice
func StripSlicePrefix(slc []string, nrItems int) []string {
if len(slc) < nrItems {
return []string{}
}
return slc[nrItems:]
}

View File

@@ -17,7 +17,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package utils
import "testing"
import (
"reflect"
"testing"
)
func TestAvg(t *testing.T) {
values := []float64{1, 2, 3}
@@ -36,3 +39,18 @@ func TestAvgEmpty(t *testing.T) {
t.Errorf("Wrong Avg: expected %v got %v", expected, result)
}
}
func TestStripSlicePrefix(t *testing.T) {
eSlc := make([]string, 0)
if retSlc := StripSlicePrefix([]string{}, 2); !reflect.DeepEqual(eSlc, retSlc) {
t.Errorf("expecting: %+v, received: %+v", eSlc, retSlc)
}
eSlc = []string{"1", "2"}
if retSlc := StripSlicePrefix([]string{"0", "1", "2"}, 1); !reflect.DeepEqual(eSlc, retSlc) {
t.Errorf("expecting: %+v, received: %+v", eSlc, retSlc)
}
eSlc = []string{}
if retSlc := StripSlicePrefix([]string{"0", "1", "2"}, 3); !reflect.DeepEqual(eSlc, retSlc) {
t.Errorf("expecting: %+v, received: %+v", eSlc, retSlc)
}
}