From c9098a3aa35e6f5be0f6b441c5dab536b196b48e Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 16 May 2016 13:08:49 +0300 Subject: [PATCH] started destination removal feature --- apier/v1/apier.go | 9 ++++++ engine/destinations.go | 3 +- engine/storage_interface.go | 1 + engine/storage_map.go | 2 +- engine/storage_mongo_datadb.go | 2 +- engine/storage_redis.go | 59 ++++++++++++++++++++++++++++++++-- 6 files changed, 71 insertions(+), 5 deletions(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 26b18a152..87ba8e175 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -63,6 +63,15 @@ func (self *ApierV1) GetDestination(dstId string, reply *engine.Destination) err return nil } +type AttrRemoveDestination struct { + DestinationIDs []string + Prefixes []string +} + +func (self *ApierV1) RemoveDestination(attr AttrRemoveDestination, reply *string) error { + +} + func (apier *ApierV1) GetSharedGroup(sgId string, reply *engine.SharedGroup) error { if sg, err := apier.RatingDb.GetSharedGroup(sgId, false); err != nil && err != utils.ErrNotFound { // Not found is not an error here return err diff --git a/engine/destinations.go b/engine/destinations.go index 5c65bd257..ae60cf187 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -63,12 +63,13 @@ func (d *Destination) AddPrefix(pfx string) { } // history record method -func (d *Destination) GetHistoryRecord() history.Record { +func (d *Destination) GetHistoryRecord(deleted bool) history.Record { js, _ := json.Marshal(d) return history.Record{ Id: d.Id, Filename: history.DESTINATIONS_FN, Payload: js, + Deleted: deleted, } } diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 327be197f..7d88886b0 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -50,6 +50,7 @@ type RatingStorage interface { RemoveRatingProfile(string) error GetDestination(string) (*Destination, error) SetDestination(*Destination) error + RemoveDestination(string) error GetLCR(string, bool) (*LCR, error) SetLCR(*LCR) error SetCdrStats(*CdrStats) error diff --git a/engine/storage_map.go b/engine/storage_map.go index f9a747a5b..6b8ebc6bc 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -436,7 +436,7 @@ func (ms *MapStorage) SetDestination(dest *Destination) (err error) { ms.dict[utils.DESTINATION_PREFIX+dest.Id] = b.Bytes() response := 0 if historyScribe != nil { - go historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(), &response) + go historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(false), &response) } return } diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 70fc91dc4..ae6779919 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -883,7 +883,7 @@ func (ms *MongoStorage) SetDestination(dest *Destination) (err error) { }{Key: dest.Id, Value: b.Bytes()}) if err == nil && historyScribe != nil { var response int - historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(), &response) + historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(false), &response) } return } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index be591ff48..553a90105 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -545,7 +545,7 @@ func (rs *RedisStorage) GetDestination(key string) (dest *Destination, err error cache2go.Push(utils.DESTINATION_PREFIX+p, dest.Id) } } else { - return nil, errors.New("not found") + return nil, utils.ErrNotFound } return } @@ -562,11 +562,66 @@ func (rs *RedisStorage) SetDestination(dest *Destination) (err error) { err = rs.db.Cmd("SET", utils.DESTINATION_PREFIX+dest.Id, b.Bytes()).Err if err == nil && historyScribe != nil { response := 0 - go historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(), &response) + go historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(false), &response) } return } +func (rs *RedisStorage) RemoveDestination(destID string) (err error) { + conn, err := rs.db.Get() + if err != nil { + return err + } + var dest *Destination + defer rs.db.Put(conn) + if values, err = conn.Cmd("GET", key).Bytes(); len(values) > 0 && err == nil { + b := bytes.NewBuffer(values) + r, err := zlib.NewReader(b) + if err != nil { + return err + } + out, err := ioutil.ReadAll(r) + if err != nil { + return err + } + r.Close() + dest = new(Destination) + err = rs.ms.Unmarshal(out, dest) + } else { + return utils.ErrNotFound + } + key := utils.DESTINATION_PREFIX + destID + if err = conn.Cmd("DEL", key).Err; err != nil { + return err + } + if dest != nil { + for _, prefix := range dest.Prefixes { + changed := false + if idIDs, err := cache2go.Get(utils.DESTINATION_PREFIX + prefix); err == nil { + dIDs := idIDs.(map[interface{}]struct{}) + if len(dIDs) == 1 { + // remove de prefix from cache + cache2go.RemKey(utils.DESTINATION_PREFIX + prefix) + } else { + // delete the destination from list and put the new list in chache + delete(dIDs, searchedDID) + changed = true + } + } + if changed { + cache2go.Cache(utils.DESTINATION_PREFIX+prefix, dIDs) + } + } + } + dest := &Destination{Id: key} + if historyScribe != nil { + response := 0 + go historyScribe.Call("HistoryV1.Record", dest.GetHistoryRecord(true), &response) + } + + return +} + func (rs *RedisStorage) GetActions(key string, skipCache bool) (as Actions, err error) { key = utils.ACTION_PREFIX + key if !skipCache {