-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #289 from telenornms/rabbitmq_queue
Rabbitmq queue
- Loading branch information
Showing
8 changed files
with
468 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* skogul, rabbitmq-receiver | ||
* | ||
* Copyright (c) 2023 Telenor Norge AS | ||
* Author(s): | ||
* - Kamil Oracz <kamil.oracz@telenor.no> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301 USA | ||
*/ | ||
|
||
package receiver | ||
|
||
import ( | ||
"fmt" | ||
|
||
amqp "github.com/rabbitmq/amqp091-go" | ||
"github.com/telenornms/skogul" | ||
) | ||
|
||
type Rabbitmq struct { | ||
Username skogul.Secret `doc:"Username for rabbitmq instance"` | ||
Password skogul.Secret `doc:"Password for rabbitmq instance"` | ||
Host string `doc:"Hostname for rabbitmq instance. Fallback is localhost"` | ||
Port string `doc:"Port for rabbitmq instance. Fallback is 5672"` | ||
Queue string `doc:"Queue to read from"` | ||
Handler *skogul.HandlerRef `doc:"Handler used to parse, transform and send data. Default skogul."` | ||
} | ||
|
||
func (r *Rabbitmq) Start() error { | ||
if r.Port == "" { | ||
r.Port = "5672" | ||
} | ||
|
||
if r.Host == "" { | ||
r.Host = "localhost" | ||
} | ||
|
||
conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%s/", r.Username.Expose(), r.Password.Expose(), r.Host, r.Port)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ch, err := conn.Channel() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = ch.QueueDeclare( | ||
r.Queue, | ||
false, | ||
false, | ||
false, | ||
false, | ||
nil, | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
msgs, err := ch.Consume( | ||
r.Queue, | ||
"", | ||
true, | ||
false, | ||
false, | ||
false, | ||
nil, | ||
) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
for message := range msgs { | ||
container, err := r.Handler.H.Parse(message.Body) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
err = r.Handler.H.TransformAndSend(container) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *Rabbitmq) Verify() error { | ||
if r.Handler.Name == "" { | ||
return skogul.MissingArgument("Handler") | ||
} | ||
|
||
if r.Username.Expose() == "" { | ||
return skogul.MissingArgument("Username") | ||
} | ||
|
||
if r.Password.Expose() == "" { | ||
return skogul.MissingArgument("Password") | ||
} | ||
|
||
if r.Queue == "" { | ||
return skogul.MissingArgument("Queue") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* skogul, rabbitmq-receiver test | ||
* | ||
* Copyright (c) 2023 Telenor Norge AS | ||
* Author(s): | ||
* - Kamil Oracz <kamil.oracz@telenor.no> | ||
* | ||
* This library is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 2.1 of the License, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301 USA | ||
*/ | ||
|
||
package receiver_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/telenornms/skogul/config" | ||
"github.com/telenornms/skogul/receiver" | ||
) | ||
|
||
func TestRabbitmq(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("Short test: Not connecting to a Rabbitmq instance") | ||
} | ||
|
||
sconf := fmt.Sprintf(` | ||
{ | ||
"receivers": { | ||
"x": { | ||
"type": "rabbitmq", | ||
"handler": "kek", | ||
"username":"guest", | ||
"password":"guest", | ||
"queue":"test-queue" | ||
} | ||
}, | ||
"handlers": { | ||
"kek": { | ||
"parser": "skogulmetric", | ||
"transformers": [ | ||
"now" | ||
], | ||
"sender": "test" | ||
} | ||
}, | ||
"senders": { | ||
"test": { | ||
"type": "test" | ||
} | ||
} | ||
}`) | ||
|
||
conf, err := config.Bytes([]byte(sconf)) | ||
|
||
if err != nil { | ||
t.Errorf("Failed to load config: %v", err) | ||
return | ||
} | ||
|
||
rcv := conf.Receivers["x"].Receiver.(*receiver.Rabbitmq) | ||
|
||
err = rcv.Start() | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.