Added extra configurable fields in RemoteHost struct in rpcconn.go. Tested them as well.

This commit is contained in:
nickolasdaniel
2021-07-13 14:23:43 +03:00
committed by Dan Christian Bogos
parent 781fd95193
commit fd312255bc
2 changed files with 372 additions and 109 deletions

View File

@@ -19,6 +19,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package config
import (
"time"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
)
@@ -154,14 +156,21 @@ func (rC RPCConn) Clone() (cln *RPCConn) {
// RemoteHost connection config
type RemoteHost struct {
ID string
Address string
Transport string
Synchronous bool
TLS bool
ID string
Address string
Transport string
Synchronous bool
TLS bool
KeyPath string
CertPath string
CaPath string
ConnAttempts int
Reconnects int
ConnectTimeout time.Duration
ReplyTimeout time.Duration
}
func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) {
func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) (err error) {
if jsnCfg == nil {
return
}
@@ -183,6 +192,32 @@ func (rh *RemoteHost) loadFromJSONCfg(jsnCfg *RemoteHostJson) {
if jsnCfg.Tls != nil {
rh.TLS = *jsnCfg.Tls
}
if jsnCfg.Key_path != nil {
rh.KeyPath = *jsnCfg.Key_path
}
if jsnCfg.Cert_path != nil {
rh.CertPath = *jsnCfg.Cert_path
}
if jsnCfg.Ca_path != nil {
rh.CaPath = *jsnCfg.Ca_path
}
if jsnCfg.Conn_attempts != nil {
rh.ConnAttempts = *jsnCfg.Conn_attempts
}
if jsnCfg.Reconnects != nil {
rh.Reconnects = *jsnCfg.Reconnects
}
if jsnCfg.Connect_timeout != nil {
if rh.ConnectTimeout, err = utils.ParseDurationWithNanosecs(*jsnCfg.Connect_timeout); err != nil {
return err
}
}
if jsnCfg.Reply_timeout != nil {
if rh.ReplyTimeout, err = utils.ParseDurationWithNanosecs(*jsnCfg.Reply_timeout); err != nil {
return err
}
}
return
}
// AsMapInterface returns the config as a map[string]interface{}
@@ -198,7 +233,28 @@ func (rh *RemoteHost) AsMapInterface() (mp map[string]interface{}) {
mp[utils.SynchronousCfg] = rh.Synchronous
}
if rh.TLS {
mp[utils.TLS] = rh.TLS
mp[utils.TLSNoCaps] = rh.TLS
}
if rh.KeyPath != utils.EmptyString {
mp[utils.KeyPathCgr] = rh.KeyPath
}
if rh.CertPath != utils.EmptyString {
mp[utils.CertPathCgr] = rh.CertPath
}
if rh.CaPath != utils.EmptyString {
mp[utils.CAPathCgr] = rh.CaPath
}
if rh.ConnAttempts != 0 {
mp[utils.ConnectAttemptsCfg] = rh.ConnAttempts
}
if rh.Reconnects != 0 {
mp[utils.ReconnectsCfg] = rh.Reconnects
}
if rh.ConnectTimeout != 0 {
mp[utils.ConnectTimeoutCfg] = rh.ConnectTimeout
}
if rh.ReplyTimeout != 0 {
mp[utils.ReplyTimeoutCfg] = rh.ReplyTimeout
}
return
}
@@ -206,11 +262,18 @@ func (rh *RemoteHost) AsMapInterface() (mp map[string]interface{}) {
// Clone returns a deep copy of RemoteHost
func (rh RemoteHost) Clone() (cln *RemoteHost) {
return &RemoteHost{
ID: rh.ID,
Address: rh.Address,
Transport: rh.Transport,
Synchronous: rh.Synchronous,
TLS: rh.TLS,
ID: rh.ID,
Address: rh.Address,
Transport: rh.Transport,
Synchronous: rh.Synchronous,
TLS: rh.TLS,
KeyPath: rh.KeyPath,
CertPath: rh.CertPath,
CaPath: rh.CaPath,
ConnAttempts: rh.ConnAttempts,
Reconnects: rh.Reconnects,
ConnectTimeout: rh.ConnectTimeout,
ReplyTimeout: rh.ReplyTimeout,
}
}
@@ -229,6 +292,13 @@ func UpdateRPCCons(rpcConns RPCConns, newHosts map[string]*RemoteHost) (connIDs
rh.Transport = newHost.Transport
rh.Synchronous = newHost.Synchronous
rh.TLS = newHost.TLS
rh.KeyPath = newHost.KeyPath
rh.CertPath = newHost.CertPath
rh.CaPath = newHost.CaPath
rh.ConnAttempts = newHost.ConnAttempts
rh.Reconnects = newHost.Reconnects
rh.ConnectTimeout = newHost.ConnectTimeout
rh.ReplyTimeout = newHost.ReplyTimeout
}
}
return
@@ -248,6 +318,13 @@ func RemoveRPCCons(rpcConns RPCConns, hosts utils.StringSet) (connIDs utils.Stri
rh.Transport = ""
rh.Synchronous = false
rh.TLS = false
rh.KeyPath = ""
rh.CertPath = ""
rh.CaPath = ""
rh.ConnAttempts = 0
rh.Reconnects = 0
rh.ConnectTimeout = 0
rh.ReplyTimeout = 0
}
}
return
@@ -255,11 +332,18 @@ func RemoveRPCCons(rpcConns RPCConns, hosts utils.StringSet) (connIDs utils.Stri
// Represents one connection instance towards a rater/cdrs server
type RemoteHostJson struct {
Id *string
Address *string
Transport *string
Synchronous *bool
Tls *bool
Id *string
Address *string
Transport *string
Synchronous *bool
Tls *bool
Key_path *string
Cert_path *string
Ca_path *string
Conn_attempts *int
Reconnects *int
Connect_timeout *string
Reply_timeout *string
}
func diffRemoteHostJson(v1, v2 *RemoteHost) (d *RemoteHostJson) {
@@ -279,6 +363,27 @@ func diffRemoteHostJson(v1, v2 *RemoteHost) (d *RemoteHostJson) {
if v1.TLS != v2.TLS {
d.Tls = utils.BoolPointer(v2.TLS)
}
if v1.KeyPath != v2.KeyPath {
d.Key_path = utils.StringPointer(v2.KeyPath)
}
if v1.CertPath != v2.CertPath {
d.Cert_path = utils.StringPointer(v2.CertPath)
}
if v1.CaPath != v2.CaPath {
d.Ca_path = utils.StringPointer(v2.CaPath)
}
if v1.ConnAttempts != v2.ConnAttempts {
d.Conn_attempts = utils.IntPointer(v2.ConnAttempts)
}
if v1.Reconnects != v2.Reconnects {
d.Reconnects = utils.IntPointer(v2.Reconnects)
}
if v1.ConnectTimeout != v2.ConnectTimeout {
d.Connect_timeout = utils.StringPointer(v2.ConnectTimeout.String())
}
if v1.ReplyTimeout != v2.ReplyTimeout {
d.Reply_timeout = utils.StringPointer(v2.ReplyTimeout.String())
}
return
}
@@ -318,7 +423,14 @@ func equalsRemoteHosts(v1, v2 []*RemoteHost) bool {
v1[i].Address != v2[i].Address ||
v1[i].Transport != v2[i].Transport ||
v1[i].Synchronous != v2[i].Synchronous ||
v1[i].TLS != v2[i].TLS {
v1[i].TLS != v2[i].TLS ||
v1[i].KeyPath != v2[i].KeyPath ||
v1[i].CertPath != v2[i].CertPath ||
v1[i].CaPath != v2[i].CaPath ||
v1[i].ConnAttempts != v2[i].ConnAttempts ||
v1[i].Reconnects != v2[i].Reconnects ||
v1[i].ConnectTimeout != v2[i].ConnectTimeout ||
v1[i].ReplyTimeout != v2[i].ReplyTimeout {
return false
}
}

View File

@@ -21,6 +21,7 @@ package config
import (
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/utils"
"github.com/cgrates/rpcclient"
@@ -32,10 +33,17 @@ func TestRPCConnsloadFromJsonCfgCase1(t *testing.T) {
PoolSize: utils.IntPointer(1),
Conns: &[]*RemoteHostJson{
{
Address: utils.StringPointer("127.0.0.1:2012"),
Transport: utils.StringPointer("*json"),
Synchronous: utils.BoolPointer(false),
Tls: utils.BoolPointer(false),
Address: utils.StringPointer("127.0.0.1:2012"),
Transport: utils.StringPointer("*json"),
Synchronous: utils.BoolPointer(false),
Tls: utils.BoolPointer(false),
Key_path: utils.StringPointer("key_path"),
Cert_path: utils.StringPointer("cert_path"),
Ca_path: utils.StringPointer("ca_path"),
Conn_attempts: utils.IntPointer(5),
Reconnects: utils.IntPointer(2),
Connect_timeout: utils.StringPointer("1m"),
Reply_timeout: utils.StringPointer("1m"),
},
},
}
@@ -78,10 +86,17 @@ func TestRPCConnsloadFromJsonCfgCase1(t *testing.T) {
PoolSize: 1,
Conns: []*RemoteHost{
{
Address: "127.0.0.1:2012",
Transport: "*json",
Synchronous: false,
TLS: false,
Address: "127.0.0.1:2012",
Transport: "*json",
Synchronous: false,
TLS: false,
KeyPath: "key_path",
CertPath: "cert_path",
CaPath: "ca_path",
ConnAttempts: 5,
Reconnects: 2,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
},
},
@@ -135,10 +150,17 @@ func TestRPCConnsloadFromJsonCfgCase2(t *testing.T) {
PoolSize: 0,
Conns: []*RemoteHost{
{
Address: "127.0.0.1:2012",
Transport: "*json",
Synchronous: false,
TLS: false,
Address: "127.0.0.1:2012",
Transport: "*json",
Synchronous: false,
TLS: false,
KeyPath: "",
CertPath: "",
CaPath: "",
ConnAttempts: 0,
Reconnects: 0,
ConnectTimeout: 0 * time.Minute,
ReplyTimeout: 0 * time.Minute,
},
},
},
@@ -154,7 +176,22 @@ func TestRPCConnsAsMapInterface(t *testing.T) {
cfgJSONStr := `{
"rpc_conns": {
"*localhost": {
"conns": [{"address": "127.0.0.1:2012", "transport":"*json"}],
"conns": [
{
"address": "127.0.0.1:2012",
"transport":"*json",
"id": "id_example",
"synchronous": true,
"tls": true,
"key_path": "path_to_key",
"cert_path": "path_to_cert",
"ca_path": "path_to_ca",
"connect_attempts": 5,
"reconnects": 3,
"connect_timeout": "1m",
"reply_timeout": "1m"
}
],
},
},
}`
@@ -174,8 +211,17 @@ func TestRPCConnsAsMapInterface(t *testing.T) {
utils.StrategyCfg: utils.MetaFirst,
utils.Conns: []map[string]interface{}{
{
utils.AddressCfg: "127.0.0.1:2012",
utils.TransportCfg: "*json",
utils.AddressCfg: "127.0.0.1:2012",
utils.TransportCfg: "*json",
utils.IDCfg: "id_example",
utils.SynchronousCfg: true,
utils.TLSNoCaps: true,
utils.KeyPathCgr: "path_to_key",
utils.CertPathCgr: "path_to_cert",
utils.CAPathCgr: "path_to_ca",
utils.ReconnectsCfg: 3,
utils.ConnectTimeoutCfg: 1 * time.Minute,
utils.ReplyTimeoutCfg: 1 * time.Minute,
},
},
},
@@ -212,7 +258,7 @@ func TestRpcConnAsMapInterface1(t *testing.T) {
"rpc_conns": {
"*localhost": {
"conns": [
{"address": "127.0.0.1:2018", "TLS": true, "synchronous": true, "transport": "*json"},
{"address": "127.0.0.1:2018", "tls": true, "synchronous": true, "transport": "*json"},
],
"poolSize": 2,
},
@@ -252,7 +298,7 @@ func TestRpcConnAsMapInterface1(t *testing.T) {
utils.MetaLocalHost: map[string]interface{}{
utils.Conns: []map[string]interface{}{
{
utils.TLS: true,
utils.TLSNoCaps: true,
utils.AddressCfg: "127.0.0.1:2018",
utils.SynchronousCfg: true,
utils.TransportCfg: "*json",
@@ -288,10 +334,17 @@ func TestRPCConnsClone(t *testing.T) {
PoolSize: 1,
Conns: []*RemoteHost{
{
Address: "127.0.0.1:2012",
Transport: "*json",
Synchronous: false,
TLS: false,
Address: "127.0.0.1:2012",
Transport: "*json",
Synchronous: false,
TLS: false,
KeyPath: "",
CertPath: "",
CaPath: "",
ConnAttempts: 0,
Reconnects: 0,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
},
},
@@ -331,11 +384,18 @@ func TestUpdateRPCCons(t *testing.T) {
newHosts := map[string]*RemoteHost{
"RPC1": {
ID: "RPC1",
Address: utils.MetaInternal,
Transport: utils.EmptyString,
Synchronous: true,
TLS: true,
ID: "RPC1",
Address: utils.MetaInternal,
Transport: utils.EmptyString,
Synchronous: true,
TLS: true,
KeyPath: "key",
CertPath: "cert",
CaPath: "ca",
ConnAttempts: 2,
Reconnects: 2,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
}
expectedID := utils.StringSet{utils.MetaInternal: {}}
@@ -345,11 +405,18 @@ func TestUpdateRPCCons(t *testing.T) {
PoolSize: 0,
Conns: []*RemoteHost{
{
ID: "RPC1",
Address: utils.MetaInternal,
Transport: utils.EmptyString,
Synchronous: true,
TLS: true,
ID: "RPC1",
Address: utils.MetaInternal,
Transport: utils.EmptyString,
Synchronous: true,
TLS: true,
KeyPath: "key",
CertPath: "cert",
CaPath: "ca",
ConnAttempts: 2,
Reconnects: 2,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
{
ID: "RPC2",
@@ -383,11 +450,18 @@ func TestRemoveRPCCons(t *testing.T) {
TLS: false,
},
{
ID: "RPC2",
Address: utils.MetaInternal,
Transport: utils.EmptyString,
Synchronous: false,
TLS: false,
ID: "RPC2",
Address: utils.MetaInternal,
Transport: utils.EmptyString,
Synchronous: false,
TLS: false,
KeyPath: "key",
CertPath: "cert",
CaPath: "ca",
ConnAttempts: 2,
Reconnects: 2,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
},
},
@@ -425,11 +499,18 @@ func TestDiffRPCConnJson(t *testing.T) {
PoolSize: 2,
Conns: []*RemoteHost{
{
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
KeyPath: "key1",
CertPath: "path1",
CaPath: "ca_path1",
ConnAttempts: 2,
Reconnects: 5,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
},
}
@@ -439,11 +520,18 @@ func TestDiffRPCConnJson(t *testing.T) {
PoolSize: 3,
Conns: []*RemoteHost{
{
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
KeyPath: "key2",
CertPath: "path2",
CaPath: "ca_path2",
ConnAttempts: 3,
Reconnects: 4,
ConnectTimeout: 2 * time.Minute,
ReplyTimeout: 2 * time.Minute,
},
},
}
@@ -453,11 +541,18 @@ func TestDiffRPCConnJson(t *testing.T) {
PoolSize: utils.IntPointer(3),
Conns: &[]*RemoteHostJson{
{
Id: utils.StringPointer("host2_ID"),
Address: utils.StringPointer("0.0.0.0:8080"),
Transport: utils.StringPointer("udp"),
Synchronous: utils.BoolPointer(true),
Tls: utils.BoolPointer(true),
Id: utils.StringPointer("host2_ID"),
Address: utils.StringPointer("0.0.0.0:8080"),
Transport: utils.StringPointer("udp"),
Synchronous: utils.BoolPointer(true),
Tls: utils.BoolPointer(true),
Key_path: utils.StringPointer("key2"),
Cert_path: utils.StringPointer("path2"),
Ca_path: utils.StringPointer("ca_path2"),
Conn_attempts: utils.IntPointer(3),
Reconnects: utils.IntPointer(4),
Connect_timeout: utils.StringPointer("2m0s"),
Reply_timeout: utils.StringPointer("2m0s"),
},
},
}
@@ -471,11 +566,18 @@ func TestDiffRPCConnJson(t *testing.T) {
expected = &RPCConnJson{
Conns: &[]*RemoteHostJson{
{
Id: utils.StringPointer("host2_ID"),
Address: utils.StringPointer("0.0.0.0:8080"),
Transport: utils.StringPointer("udp"),
Synchronous: utils.BoolPointer(true),
Tls: utils.BoolPointer(true),
Id: utils.StringPointer("host2_ID"),
Address: utils.StringPointer("0.0.0.0:8080"),
Transport: utils.StringPointer("udp"),
Synchronous: utils.BoolPointer(true),
Tls: utils.BoolPointer(true),
Key_path: utils.StringPointer("key2"),
Cert_path: utils.StringPointer("path2"),
Ca_path: utils.StringPointer("ca_path2"),
Conn_attempts: utils.IntPointer(3),
Reconnects: utils.IntPointer(4),
Connect_timeout: utils.StringPointer("2m0s"),
Reply_timeout: utils.StringPointer("2m0s"),
},
},
}
@@ -488,21 +590,35 @@ func TestDiffRPCConnJson(t *testing.T) {
func TestEqualsRemoteHosts(t *testing.T) {
v1 := []*RemoteHost{
{
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
KeyPath: "key1",
CertPath: "path1",
CaPath: "ca_path1",
ConnAttempts: 2,
Reconnects: 5,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
}
v2 := []*RemoteHost{
{
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
KeyPath: "key2",
CertPath: "path2",
CaPath: "ca_path2",
ConnAttempts: 3,
Reconnects: 4,
ConnectTimeout: 2 * time.Minute,
ReplyTimeout: 2 * time.Minute,
},
}
@@ -527,11 +643,18 @@ func TestEqualsRPCConn(t *testing.T) {
PoolSize: 2,
Conns: []*RemoteHost{
{
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
KeyPath: "key1",
CertPath: "path1",
CaPath: "ca_path1",
ConnAttempts: 2,
Reconnects: 5,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
},
}
@@ -541,11 +664,18 @@ func TestEqualsRPCConn(t *testing.T) {
PoolSize: 3,
Conns: []*RemoteHost{
{
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
KeyPath: "key2",
CertPath: "path2",
CaPath: "ca_path2",
ConnAttempts: 3,
Reconnects: 4,
ConnectTimeout: 2 * time.Minute,
ReplyTimeout: 2 * time.Minute,
},
},
}
@@ -574,11 +704,18 @@ func TestDiffRPCConnsJson(t *testing.T) {
PoolSize: 2,
Conns: []*RemoteHost{
{
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
ID: "host1_ID",
Address: "127.0.0.1:8080",
Transport: "tcp",
Synchronous: false,
TLS: false,
KeyPath: "key1",
CertPath: "path1",
CaPath: "ca_path1",
ConnAttempts: 2,
Reconnects: 5,
ConnectTimeout: 1 * time.Minute,
ReplyTimeout: 1 * time.Minute,
},
},
},
@@ -590,11 +727,18 @@ func TestDiffRPCConnsJson(t *testing.T) {
PoolSize: 3,
Conns: []*RemoteHost{
{
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
ID: "host2_ID",
Address: "0.0.0.0:8080",
Transport: "udp",
Synchronous: true,
TLS: true,
KeyPath: "key2",
CertPath: "path2",
CaPath: "ca_path2",
ConnAttempts: 3,
Reconnects: 4,
ConnectTimeout: 2 * time.Minute,
ReplyTimeout: 2 * time.Minute,
},
},
},
@@ -606,11 +750,18 @@ func TestDiffRPCConnsJson(t *testing.T) {
PoolSize: utils.IntPointer(3),
Conns: &[]*RemoteHostJson{
{
Id: utils.StringPointer("host2_ID"),
Address: utils.StringPointer("0.0.0.0:8080"),
Transport: utils.StringPointer("udp"),
Synchronous: utils.BoolPointer(true),
Tls: utils.BoolPointer(true),
Id: utils.StringPointer("host2_ID"),
Address: utils.StringPointer("0.0.0.0:8080"),
Transport: utils.StringPointer("udp"),
Synchronous: utils.BoolPointer(true),
Tls: utils.BoolPointer(true),
Key_path: utils.StringPointer("key2"),
Cert_path: utils.StringPointer("path2"),
Ca_path: utils.StringPointer("ca_path2"),
Conn_attempts: utils.IntPointer(3),
Reconnects: utils.IntPointer(4),
Connect_timeout: utils.StringPointer("2m0s"),
Reply_timeout: utils.StringPointer("2m0s"),
},
},
},