From 10ef469fdb6a938bfc20858b0e14f3377c869f52 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 6 Jul 2015 10:07:01 +0300 Subject: [PATCH] added API and console command for destination set fixes #106 --- apier/v1/apier.go | 23 ++++++++++--- console/destination_set.go | 66 ++++++++++++++++++++++++++++++++++++++ utils/apitpdata.go | 6 ++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 console/destination_set.go diff --git a/apier/v1/apier.go b/apier/v1/apier.go index acabc2dda..cf18d59ba 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -69,10 +69,25 @@ func (apier *ApierV1) GetSharedGroup(sgId string, reply *engine.SharedGroup) err return nil } -type AttrSetDestination struct { //ToDo - Id string - Prefixes []string - Overwrite bool +func (self *ApierV1) SetDestination(attrs utils.AttrSetDestination, reply *string) error { + if missing := utils.MissingStructFields(&attrs, []string{"Id", "Prefixes"}); len(missing) != 0 { + return utils.NewErrMandatoryIeMissing(missing...) + } + + if !attrs.Overwrite { + if exists, err := self.RatingDb.HasData(utils.DESTINATION_PREFIX, attrs.Id); err != nil { + return utils.NewErrServerError(err) + } else if exists { + return utils.ErrExists + } + } + dest := &engine.Destination{Id: attrs.Id, Prefixes: attrs.Prefixes} + if err := self.RatingDb.SetDestination(dest); err != nil { + return utils.NewErrServerError(err) + } + self.RatingDb.CachePrefixValues(map[string][]string{utils.DESTINATION_PREFIX: []string{dest.Id}}) + *reply = OK + return nil } func (self *ApierV1) GetRatingPlan(rplnId string, reply *engine.RatingPlan) error { diff --git a/console/destination_set.go b/console/destination_set.go new file mode 100644 index 000000000..42d8f0cf1 --- /dev/null +++ b/console/destination_set.go @@ -0,0 +1,66 @@ +/* +Rating system designed to be used in VoIP Carriers World +Copyright (C) 2012-2015 ITsysCOM + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +import "github.com/cgrates/cgrates/utils" + +func init() { + c := &CmdSetDestination{ + name: "destination_set", + rpcMethod: "ApierV1.SetDestination", + } + commands[c.Name()] = c + c.CommandExecuter = &CommandExecuter{c} +} + +// Commander implementation +type CmdSetDestination struct { + name string + rpcMethod string + rpcParams *utils.AttrSetDestination + rpcResult string + *CommandExecuter +} + +func (self *CmdSetDestination) Name() string { + return self.name +} + +func (self *CmdSetDestination) RpcMethod() string { + return self.rpcMethod +} + +func (self *CmdSetDestination) RpcParams(ptr bool) interface{} { + if self.rpcParams == nil { + self.rpcParams = &utils.AttrSetDestination{} + } + if ptr { + return self.rpcParams + } + return *self.rpcParams +} + +func (self *CmdSetDestination) PostprocessRpcParams() error { + return nil +} + +func (self *CmdSetDestination) RpcResult() interface{} { + var s string + return &s +} diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 383ce5682..f06d74924 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -252,6 +252,12 @@ func FallbackSubjKeys(direction, tenant, tor, fallbackSubjects string) []string return sslice } +type AttrSetDestination struct { //ToDo + Id string + Prefixes []string + Overwrite bool +} + type AttrTPRatingProfileIds struct { TPid string // Tariff plan id Tenant string // Tenant's Id