From 7e308dca52ddf28938bfb8e2efb69e5b44f00682 Mon Sep 17 00:00:00 2001 From: DanB Date: Wed, 3 Jul 2013 23:01:37 +0200 Subject: [PATCH] Adding Apier.SetTPDestination --- apier/tpdestinations.go | 27 +++++++++++++++---- .../mysql/create_tariffplan_tables.sql | 2 +- rater/storage_interface.go | 1 + rater/storage_map.go | 4 +++ rater/storage_mongo.go | 5 ++++ rater/storage_redis.go | 5 ++++ rater/storage_sql.go | 10 +++++++ 7 files changed, 48 insertions(+), 6 deletions(-) diff --git a/apier/tpdestinations.go b/apier/tpdestinations.go index 16ef2d22f..2120dda70 100644 --- a/apier/tpdestinations.go +++ b/apier/tpdestinations.go @@ -25,17 +25,17 @@ import ( "github.com/cgrates/cgrates/utils" ) -type AttrGetTPDestinations struct { +type AttrGetTPDestination struct { TPid string - DestinationsTag string + DestinationId string } // Return destinations profile for a destination tag received as parameter -func (self *Apier) GetTPDestinations(attrs AttrGetTPDestinations, reply *rater.Destination) error { - if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationsTag"}); len(missing) != 0 { //Params missing +func (self *Apier) GetTPDestination(attrs AttrGetTPDestination, reply *rater.Destination) error { + if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId"}); len(missing) != 0 { //Params missing return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) } - if dst, err := self.StorDb.GetTPDestination(attrs.TPid, attrs.DestinationsTag); err != nil { + if dst, err := self.StorDb.GetTPDestination(attrs.TPid, attrs.DestinationId); err != nil { return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) } else if dst == nil { return errors.New(utils.ERR_NOT_FOUND) @@ -44,3 +44,20 @@ func (self *Apier) GetTPDestinations(attrs AttrGetTPDestinations, reply *rater.D } return nil } + +type AttrSetTPDestination struct { + TPid string + DestinationId string + Prefixes []string +} + +func (self *Apier) SetTPDestination(attrs AttrSetTPDestination, reply *string) error { + if missing := utils.MissingStructFields(&attrs, []string{"TPid", "DestinationId", "Prefixes"}); len(missing) != 0 { //Params missing + return fmt.Errorf("%s:%v", utils.ERR_MANDATORY_IE_MISSING, missing) + } + if err := self.StorDb.SetTPDestination(attrs.TPid, &rater.Destination{attrs.DestinationId, attrs.Prefixes}); err != nil { + return fmt.Errorf("%s:%s", utils.ERR_SERVER_ERROR, err.Error()) + } + *reply = "OK" + return nil +} diff --git a/data/storage/mysql/create_tariffplan_tables.sql b/data/storage/mysql/create_tariffplan_tables.sql index e7f0d1f32..f57301477 100644 --- a/data/storage/mysql/create_tariffplan_tables.sql +++ b/data/storage/mysql/create_tariffplan_tables.sql @@ -71,7 +71,7 @@ CREATE TABLE `tp_rate_profiles` ( `subject` varchar(64) NOT NULL, `rates_fallback_subject` varchar(64), `rates_timing_tag` varchar(24) NOT NULL, - `activation_time` char(3) NOT NULL, ???? char(3) + `activation_time` char(20) NOT NULL, PRIMARY KEY (`id`), KEY `tpid` (`tpid`) ); diff --git a/rater/storage_interface.go b/rater/storage_interface.go index cb6019a26..b4efccd07 100644 --- a/rater/storage_interface.go +++ b/rater/storage_interface.go @@ -58,6 +58,7 @@ type DataStorage interface { GetDestination(string) (*Destination, error) SetDestination(*Destination) error GetTPDestination(string, string) (*Destination, error) + SetTPDestination(string, *Destination) error GetActions(string) (Actions, error) SetActions(string, Actions) error GetUserBalance(string) (*UserBalance, error) diff --git a/rater/storage_map.go b/rater/storage_map.go index a326111f3..d6e7934bd 100644 --- a/rater/storage_map.go +++ b/rater/storage_map.go @@ -78,6 +78,10 @@ func (ms *MapStorage) GetTPDestination(tpid, destTag string) (*Destination, erro return nil, nil } +func (ms *MapStorage) SetTPDestination(tpid string, dest *Destination) error { + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (ms *MapStorage) GetActions(key string) (as Actions, err error) { if values, ok := ms.dict[ACTION_PREFIX+key]; ok { err = ms.ms.Unmarshal(values, &as) diff --git a/rater/storage_mongo.go b/rater/storage_mongo.go index 4a4290a88..c4628eb28 100644 --- a/rater/storage_mongo.go +++ b/rater/storage_mongo.go @@ -152,6 +152,11 @@ func (ms *MongoStorage) GetTPDestination(tpid, destTag string) (*Destination, er return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) } +func (ms *MongoStorage) SetTPDestination(tpid string, dest *Destination) error { + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + + func (ms *MongoStorage) GetActions(key string) (as Actions, err error) { result := AcKeyValue{} err = ms.db.C("actions").Find(bson.M{"key": key}).One(&result) diff --git a/rater/storage_redis.go b/rater/storage_redis.go index 144750a52..a459864e1 100644 --- a/rater/storage_redis.go +++ b/rater/storage_redis.go @@ -107,6 +107,11 @@ func (rs *RedisStorage) GetTPDestination(tpid, destTag string) (*Destination, er return nil, errors.New(utils.ERR_NOT_IMPLEMENTED) } +func (rs *RedisStorage) SetTPDestination(tpid string, dest *Destination) error { + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + + func (rs *RedisStorage) GetActions(key string) (as Actions, err error) { var values string if values, err = rs.db.Get(ACTION_PREFIX + key); err == nil { diff --git a/rater/storage_sql.go b/rater/storage_sql.go index 746d771b1..62a985a14 100644 --- a/rater/storage_sql.go +++ b/rater/storage_sql.go @@ -23,6 +23,7 @@ import ( "encoding/json" "fmt" "github.com/cgrates/cgrates/utils" + "errors" ) type SQLStorage struct { @@ -78,6 +79,15 @@ func (sql *SQLStorage) GetTPDestination(tpid, destTag string) (*Destination, err return d, nil } +func (sql *SQLStorage) SetTPDestination(tpid string, dest *Destination) error { + for _,prefix := range dest.Prefixes { + if _,err := sql.Db.Exec(fmt.Sprintf("INSERT INTO tp_destinations (tpid, tag, prefix) VALUES( '%s','%s','%s')",tpid, dest.Id, prefix));err!=nil { + return err + } + } + return errors.New(utils.ERR_NOT_IMPLEMENTED) +} + func (sql *SQLStorage) GetActions(string) (as Actions, err error) { return }