diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index 1a4ed1693..e0ff852c5 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -667,7 +667,7 @@ func TestLoadDispatcherHosts(t *testing.T) { Synchronous: false, ConnectAttempts: 1, Reconnects: 3, - ConnectTimeout: 2 * time.Minute, + ConnectTimeout: 1 * time.Minute, ReplyTimeout: 2 * time.Minute, TLS: true, ClientKey: "key2", diff --git a/engine/model_helpers.go b/engine/model_helpers.go index 58b513fff..16ff6baaf 100644 --- a/engine/model_helpers.go +++ b/engine/model_helpers.go @@ -1604,30 +1604,30 @@ func (tps DispatcherHostMdls) AsTPDispatcherHosts() (result []*utils.TPDispatche if len(tp.Transport) == 0 { tp.Transport = utils.MetaJSON } - hostsMap[utils.ConcatenatedKey(tp.Tenant, tp.ID)] = &utils.TPDispatcherHost{ + tntId := utils.ConcatenatedKey(tp.Tenant, tp.ID) // Made separate variable in order to call ConcatenatedKey only once per TP + hostsMap[tntId] = &utils.TPDispatcherHost{ TPid: tp.Tpid, Tenant: tp.Tenant, ID: tp.ID, Conn: &utils.TPDispatcherHostConn{ - Address: tp.Address, - Transport: tp.Transport, - Synchronous: tp.Synchronous, - TLS: tp.TLS, - ConnectAttempts: tp.ConnAttempts, - // ConnectTimeout: tp.ConnectTimeout, - // ReplyTimeout: tp.ReplyTimeout, - ClientKey: tp.KeyPath, - ClientCertificate: tp.CertPath, - CaCertificate: tp.CaPath, + Address: tp.Address, + Transport: tp.Transport, + Synchronous: tp.Synchronous, + TLS: tp.TLS, + ConnectAttempts: tp.ConnectAttempts, + Reconnects: tp.Reconnects, + ClientKey: tp.ClientKey, + ClientCertificate: tp.ClientCertificate, + CaCertificate: tp.CaCertificate, }, } if tp.ConnectTimeout != utils.EmptyString { - if hostsMap[utils.ConcatenatedKey(tp.Tenant, tp.ID)].Conn.ConnectTimeout, err = utils.ParseDurationWithNanosecs(tp.ConnectTimeout); err != nil { + if hostsMap[tntId].Conn.ConnectTimeout, err = utils.ParseDurationWithNanosecs(tp.ConnectTimeout); err != nil { return nil, err } } if tp.ReplyTimeout != utils.EmptyString { - if hostsMap[utils.ConcatenatedKey(tp.Tenant, tp.ID)].Conn.ReplyTimeout, err = utils.ParseDurationWithNanosecs(tp.ReplyTimeout); err != nil { + if hostsMap[tntId].Conn.ReplyTimeout, err = utils.ParseDurationWithNanosecs(tp.ReplyTimeout); err != nil { return nil, err } } @@ -1644,20 +1644,20 @@ func APItoModelTPDispatcherHost(tpDPH *utils.TPDispatcherHost) (mdls *Dispatcher return } return &DispatcherHostMdl{ - Tpid: tpDPH.TPid, - Tenant: tpDPH.Tenant, - ID: tpDPH.ID, - Address: tpDPH.Conn.Address, - Transport: tpDPH.Conn.Transport, - Synchronous: tpDPH.Conn.Synchronous, - ConnAttempts: tpDPH.Conn.ConnectAttempts, - Reconnects: tpDPH.Conn.Reconnects, - ConnectTimeout: tpDPH.Conn.ConnectTimeout, - ReplyTimeout: tpDPH.Conn.ReplyTimeout, - TLS: tpDPH.Conn.TLS, - KeyPath: tpDPH.Conn.ClientKey, - CertPath: tpDPH.Conn.ClientCertificate, - CaPath: tpDPH.Conn.CaCertificate, + Tpid: tpDPH.TPid, + Tenant: tpDPH.Tenant, + ID: tpDPH.ID, + Address: tpDPH.Conn.Address, + Transport: tpDPH.Conn.Transport, + Synchronous: tpDPH.Conn.Synchronous, + ConnectAttempts: tpDPH.Conn.ConnectAttempts, + Reconnects: tpDPH.Conn.Reconnects, + ConnectTimeout: tpDPH.Conn.ConnectTimeout.String(), + ReplyTimeout: tpDPH.Conn.ReplyTimeout.String(), + TLS: tpDPH.Conn.TLS, + ClientKey: tpDPH.Conn.ClientKey, + ClientCertificate: tpDPH.Conn.ClientCertificate, + CaCertificate: tpDPH.Conn.CaCertificate, } } @@ -1668,18 +1668,18 @@ func APItoDispatcherHost(tpDPH *utils.TPDispatcherHost) (dpp *DispatcherHost) { return &DispatcherHost{ Tenant: tpDPH.Tenant, RemoteHost: &config.RemoteHost{ - ID: tpDPH.ID, - Address: tpDPH.Conn.Address, - Transport: tpDPH.Conn.Transport, - Synchronous: tpDPH.Conn.Synchronous, - ConnAttempts: tpDPH.Conn.ConnectAttempts, - Reconnects: tpDPH.Conn.Reconnects, - ConnectTimeout: tpDPH.Conn.ConnectTimeout, - ReplyTimeout: tpDPH.Conn.ReplyTimeout, - TLS: tpDPH.Conn.TLS, - KeyPath: tpDPH.Conn.ClientKey, - CertPath: tpDPH.Conn.ClientCertificate, - CaPath: tpDPH.Conn.CaCertificate, + ID: tpDPH.ID, + Address: tpDPH.Conn.Address, + Transport: tpDPH.Conn.Transport, + Synchronous: tpDPH.Conn.Synchronous, + ConnectAttempts: tpDPH.Conn.ConnectAttempts, + Reconnects: tpDPH.Conn.Reconnects, + ConnectTimeout: tpDPH.Conn.ConnectTimeout, + ReplyTimeout: tpDPH.Conn.ReplyTimeout, + TLS: tpDPH.Conn.TLS, + ClientKey: tpDPH.Conn.ClientKey, + ClientCertificate: tpDPH.Conn.ClientCertificate, + CaCertificate: tpDPH.Conn.CaCertificate, }, } } @@ -1692,14 +1692,14 @@ func DispatcherHostToAPI(dph *DispatcherHost) (tpDPH *utils.TPDispatcherHost) { Address: dph.Address, Transport: dph.Transport, Synchronous: dph.Synchronous, - ConnectAttempts: dph.ConnAttempts, + ConnectAttempts: dph.ConnectAttempts, Reconnects: dph.Reconnects, ConnectTimeout: dph.ConnectTimeout, ReplyTimeout: dph.ReplyTimeout, TLS: dph.TLS, - ClientKey: dph.KeyPath, - ClientCertificate: dph.CertPath, - CaCertificate: dph.CaPath, + ClientKey: dph.ClientKey, + ClientCertificate: dph.ClientCertificate, + CaCertificate: dph.CaCertificate, }, } } diff --git a/engine/model_helpers_test.go b/engine/model_helpers_test.go index 0b6dd1b71..0a7bcff2d 100644 --- a/engine/model_helpers_test.go +++ b/engine/model_helpers_test.go @@ -1773,7 +1773,9 @@ func TestTPDispatcherHostsCSVHeader(t *testing.T) { func TestTPDispatcherHostsAsTPDispatcherHosts(t *testing.T) { tps := &DispatcherHostMdls{} - if rcv := tps.AsTPDispatcherHosts(); rcv != nil { + if rcv, err := tps.AsTPDispatcherHosts(); err != nil { + t.Error(err) + } else if rcv != nil { t.Errorf("Expecting: nil,\nReceived: %+v", utils.ToJSON(rcv)) } @@ -1782,28 +1784,50 @@ func TestTPDispatcherHostsAsTPDispatcherHosts(t *testing.T) { ID: "ID1", Tenant: "Tenant1", }} - if rcv := tps.AsTPDispatcherHosts(); rcv != nil { + if rcv, err := tps.AsTPDispatcherHosts(); err != nil { + t.Error(err) + } else if rcv != nil { t.Errorf("Expecting: nil,\nReceived: %+v", utils.ToJSON(rcv)) } tps = &DispatcherHostMdls{ &DispatcherHostMdl{ - Address: "Address1", - ID: "ID1", - Tenant: "Tenant1", - Transport: utils.EmptyString, + ID: "ID1", + Tenant: "Tenant1", + Address: "localhost:6012", + Transport: "*json", + Synchronous: true, + ConnectAttempts: 2, + Reconnects: 5, + ConnectTimeout: "2m", + ReplyTimeout: "1m", + TLS: true, + ClientKey: "client_key", + ClientCertificate: "client_certificate", + CaCertificate: "ca_certificate", }} eOut := []*utils.TPDispatcherHost{ { Tenant: "Tenant1", ID: "ID1", Conn: &utils.TPDispatcherHostConn{ - Address: "Address1", - Transport: "*json", + Address: "localhost:6012", + Transport: "*json", + Synchronous: true, + ConnectAttempts: 2, + Reconnects: 5, + ConnectTimeout: 2 * time.Minute, + ReplyTimeout: 1 * time.Minute, + TLS: true, + ClientKey: "client_key", + ClientCertificate: "client_certificate", + CaCertificate: "ca_certificate", }, }, } - if rcv := tps.AsTPDispatcherHosts(); !reflect.DeepEqual(rcv, eOut) { + if rcv, err := tps.AsTPDispatcherHosts(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rcv, eOut) { t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) } @@ -1824,7 +1848,9 @@ func TestTPDispatcherHostsAsTPDispatcherHosts(t *testing.T) { }, }, } - if rcv := tps.AsTPDispatcherHosts(); !reflect.DeepEqual(rcv, eOut) { + if rcv, err := tps.AsTPDispatcherHosts(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rcv, eOut) { t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) } @@ -1846,7 +1872,9 @@ func TestTPDispatcherHostsAsTPDispatcherHosts(t *testing.T) { }, }, } - if rcv := tps.AsTPDispatcherHosts(); !reflect.DeepEqual(rcv, eOut) { + if rcv, err := tps.AsTPDispatcherHosts(); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rcv, eOut) { t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) } @@ -1868,7 +1896,10 @@ func TestTPDispatcherHostsAsTPDispatcherHosts(t *testing.T) { }, }, } - rcv := tps.AsTPDispatcherHosts() + rcv, err := tps.AsTPDispatcherHosts() + if err != nil { + t.Error(err) + } sort.Slice(rcv, func(i, j int) bool { return strings.Compare(rcv[i].ID, rcv[j].ID) < 0 }) if !reflect.DeepEqual(rcv, eOut) { t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) @@ -1885,15 +1916,33 @@ func TestAPItoModelTPDispatcherHost(t *testing.T) { Tenant: "Tenant", ID: "ID", Conn: &utils.TPDispatcherHostConn{ - Address: "Address1", - Transport: "*json", + Address: "Address1", + Transport: "*json", + Synchronous: true, + ConnectAttempts: 3, + Reconnects: 5, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 2 * time.Minute, + TLS: true, + ClientKey: "client_key", + ClientCertificate: "client_certificate", + CaCertificate: "ca_certificate", }, } eOut := &DispatcherHostMdl{ - Address: "Address1", - Transport: "*json", - Tenant: "Tenant", - ID: "ID", + Address: "Address1", + Transport: "*json", + Tenant: "Tenant", + ID: "ID", + Synchronous: true, + ConnectAttempts: 3, + Reconnects: 5, + ConnectTimeout: "1m0s", + ReplyTimeout: "2m0s", + TLS: true, + ClientKey: "client_key", + ClientCertificate: "client_certificate", + CaCertificate: "ca_certificate", } if rcv := APItoModelTPDispatcherHost(tpDPH); !reflect.DeepEqual(eOut, rcv) { t.Errorf("Expecting: %+v,\nReceived: %+v", utils.ToJSON(eOut), utils.ToJSON(rcv)) @@ -1911,17 +1960,35 @@ func TestAPItoDispatcherHost(t *testing.T) { Tenant: "Tenant1", ID: "ID1", Conn: &utils.TPDispatcherHostConn{ - Address: "Address1", - Transport: "*json", + Address: "localhost:6012", + Transport: "*json", + Synchronous: true, + ConnectAttempts: 3, + Reconnects: 5, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 2 * time.Minute, + TLS: true, + ClientKey: "client_key", + ClientCertificate: "client_certificate", + CaCertificate: "ca_certificate", }, } eOut := &DispatcherHost{ Tenant: "Tenant1", RemoteHost: &config.RemoteHost{ - ID: "ID1", - Address: "Address1", - Transport: "*json", + ID: "ID1", + Address: "localhost:6012", + Transport: "*json", + Synchronous: true, + Reconnects: 5, + ConnectTimeout: 1 * time.Minute, + ReplyTimeout: 2 * time.Minute, + TLS: true, + ClientKey: "client_key", + ClientCertificate: "client_certificate", + CaCertificate: "ca_certificate", + ConnectAttempts: 3, }, } if rcv := APItoDispatcherHost(tpDPH); !reflect.DeepEqual(eOut, rcv) { diff --git a/engine/models.go b/engine/models.go index a8313c236..168be1da2 100644 --- a/engine/models.go +++ b/engine/models.go @@ -274,22 +274,22 @@ func (DispatcherProfileMdl) TableName() string { } type DispatcherHostMdl struct { - PK uint `gorm:"primary_key"` - Tpid string // - Tenant string `index:"0" re:""` - ID string `index:"1" re:""` - Address string `index:"2" re:""` - Transport string `index:"3" re:""` - Synchronous bool `index:"4" re:""` - ConnAttempts int `index:"5" re:""` - Reconnects int `index:"6" re:""` - ConnectTimeout string `index:"7" re:""` - ReplyTimeout string `index:"8" re:""` - TLS bool `index:"9" re:""` - KeyPath string `index:"10" re:""` - CertPath string `index:"11" re:""` - CaPath string `index:"12" re:""` - CreatedAt time.Time + PK uint `gorm:"primary_key"` + Tpid string // + Tenant string `index:"0" re:""` + ID string `index:"1" re:""` + Address string `index:"2" re:""` + Transport string `index:"3" re:""` + Synchronous bool `index:"4" re:""` + ConnectAttempts int `index:"5" re:""` + Reconnects int `index:"6" re:""` + ConnectTimeout string `index:"7" re:""` + ReplyTimeout string `index:"8" re:""` + TLS bool `index:"9" re:""` + ClientKey string `index:"10" re:""` + ClientCertificate string `index:"11" re:""` + CaCertificate string `index:"12" re:""` + CreatedAt time.Time } func (DispatcherHostMdl) TableName() string { diff --git a/engine/storage_csv.go b/engine/storage_csv.go index e26f6f6c0..301c897e9 100644 --- a/engine/storage_csv.go +++ b/engine/storage_csv.go @@ -413,7 +413,11 @@ func (csvs *CSVStorage) GetTPDispatcherHosts(tpid, tenant, id string) ([]*utils. }); err != nil { return nil, err } - return tpDDHs.AsTPDispatcherHosts(), nil + result, err := tpDDHs.AsTPDispatcherHosts() + if err != nil { + return nil, err + } + return result, nil } func (csvs *CSVStorage) GetTPRateProfiles(tpid, tenant, id string) ([]*utils.TPRateProfile, error) { diff --git a/engine/storage_sql.go b/engine/storage_sql.go index 8d4f16b72..1511dd263 100644 --- a/engine/storage_sql.go +++ b/engine/storage_sql.go @@ -971,7 +971,10 @@ func (sqls *SQLStorage) GetTPDispatcherHosts(tpid, tenant, id string) ([]*utils. if err := q.Find(&dpps).Error; err != nil { return nil, err } - arls := dpps.AsTPDispatcherHosts() + arls, err := dpps.AsTPDispatcherHosts() + if err != nil { + return nil, err + } if len(arls) == 0 { return arls, utils.ErrNotFound } diff --git a/loaders/loader.go b/loaders/loader.go index 06634148d..5779e2c53 100644 --- a/loaders/loader.go +++ b/loaders/loader.go @@ -539,7 +539,11 @@ func (ldr *Loader) storeLoadedData(ctx *context.Context, loaderType string, return } } - for _, tpDsp := range dispModels.AsTPDispatcherHosts() { + tpDsps, err := dispModels.AsTPDispatcherHosts() + if err != nil { + return err + } + for _, tpDsp := range tpDsps { dsp := engine.APItoDispatcherHost(tpDsp) if ldr.dryRun { utils.Logger.Info( diff --git a/loaders/loader_test.go b/loaders/loader_test.go index 097721b3e..f49ba8be7 100644 --- a/loaders/loader_test.go +++ b/loaders/loader_test.go @@ -1294,14 +1294,14 @@ func TestLoaderProcessDispatcheHosts(t *testing.T) { eDispHost := &engine.DispatcherHost{ Tenant: "cgrates.org", RemoteHost: &config.RemoteHost{ - ID: "ALL1", - Address: "127.0.0.1:2012", + ID: "ALL", + Address: "127.0.0.1:6012", Transport: utils.MetaJSON, - TLS: true, + TLS: false, }, } - rcv, err := ldr.dm.GetDispatcherHost(context.TODO(), "cgrates.org", "ALL1", + rcv, err := ldr.dm.GetDispatcherHost(context.TODO(), "cgrates.org", "ALL", true, false, utils.NonTransactional) if err != nil { t.Fatal(err)