From 7bc0209948d58b0af7705352d1f7b5122034198f Mon Sep 17 00:00:00 2001 From: Maxime Caruchet Date: Fri, 3 Aug 2018 14:12:07 +0200 Subject: [PATCH 1/2] chore: update gitignore file --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3e15620..eae8106 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ api/bin api/api /api/tests_results.xml -.env +.env* From 2c801ea91bce12d1632d3952d0ae9555dd25d4e2 Mon Sep 17 00:00:00 2001 From: Maxime Caruchet Date: Fri, 3 Aug 2018 14:13:37 +0200 Subject: [PATCH 2/2] feat: handle multiple XMPP servers and keys --- api/hook/xmpp.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/api/hook/xmpp.go b/api/hook/xmpp.go index ea0f0a2..d8980bb 100644 --- a/api/hook/xmpp.go +++ b/api/hook/xmpp.go @@ -1,6 +1,9 @@ package hook import ( + "fmt" + "strings" + log "github.com/Sirupsen/logrus" "github.com/ovh/tat" "github.com/spf13/viper" @@ -19,5 +22,27 @@ func sendXMPP(hook *tat.HookJSON, path string, topic tat.Topic) error { } log.Debugf("sendXMPP: enter for post XMPP via tat2XMPP setted on topic %s", topic.Topic) - return sendWebHook(hook, viper.GetString("tat2xmpp_url")+"/hook", topic, tat.HookTat2XMPPHeaderKey, viper.GetString("tat2xmpp_key")) + // We split the XMPP servers and keys (comma separated lists) + tat2xmppServers := strings.Split(viper.GetString("tat2xmpp_url"), ",") + tat2xmppKeys := strings.Split(viper.GetString("tat2xmpp_key"), ",") + + // We must have the same number of servers and keys: one key for one server + if len(tat2xmppServers) != len(tat2xmppKeys) { + return fmt.Errorf("the number of XMPP servers differs from the number of provided keys (%v servers and %v keys)", len(tat2xmppServers), len(tat2xmppKeys)) + } + + // Go through the servers and send the hook with the right key + // The right key for the right server is determined by the declaration order: + // the first server goes with the first key, the second server goes with the second key... + for index, tat2xmppServer := range tat2xmppServers { + errSendWebHook := sendWebHook(hook, tat2xmppServer+"/hook", topic, tat.HookTat2XMPPHeaderKey, tat2xmppKeys[index]) + if errSendWebHook != nil { + // If an error is encountered, abort everything and return the error because we should not encounter any error + // even if we are sending the wrong destination to the wrong server (tat2xmpp will handle that and return no error) + // So an error is not normal and we should return it immediately + return errSendWebHook + } + } + + return nil }