Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Commit

Permalink
Refactored naming convention + README
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip committed Mar 3, 2017
1 parent ea157c3 commit f3d5c7e
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ RUN apk --update upgrade && \
apk add curl ca-certificates && \
update-ca-certificates && \
rm -rf /var/cache/apk/*
ADD config /config
RUN mkdir config && touch /config/mapping.json
ADD rabbit-amazon-forwarder /
CMD ["/rabbit-amazon-forwarder"]
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build:
release: build
docker build -t airhelp/rabbit-amazon-forwarder -f Dockerfile .

push: release
push: test release
docker push airhelp/rabbit-amazon-forwarder

fmt:
Expand Down
46 changes: 38 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Application to forward messages from RabbitMQ to different Amazon services.

## Configuration

### Environment variables

Export environment variables:
```bash
export MAPPING_FILE=/config/mapping.json
Expand All @@ -16,30 +18,58 @@ export AWS_ACCESS_KEY_ID=access_key
export AWS_SECRET_ACCESS_KEY=secret_key
```

## Mapping
### Mapping file

Definition of forwarder->consumer pairs should be placed inside mapping file. Samples are located in `tests` directory.
Definition of forwarder->consumer pairs should be placed inside mapping file.
Sample of RabbitMQ -> SNS mapping file. All fields are required.
```json
[
{
"source" : {
"type" : "RabbitMQ",
"name" : "test-rabbit",
"connection" : "amqp://guest:guest@localhost:5672/",
"topic" : "amq.topic",
"queue" : "test-queue",
"routing" : "#"
},
"destination" : {
"type" : "SNS",
"name" : "test-sns",
"target" : "arn:aws:sns:eu-west-1:XXXXXXXX:test-forwarder"
}
}
]
```
Samples are located in `examples` directory.

## Build
## Build docker image

```bash
make release
```

# Release
## Run

Using docker:
```bash
make push
docker tag airhelp/rabbit-amazon-forwarder airhelp/rabbit-amazon-forwarder:$VERSION
docker push airhelp/rabbit-amazon-forwarder:$VERSION

```

## Run

Using docker-compose:
```bash
docker-compose up
```

# Release

```bash
make push
docker tag airhelp/rabbit-amazon-forwarder airhelp/rabbit-amazon-forwarder:$VERSION
docker push airhelp/rabbit-amazon-forwarder:$VERSION
```

## Supervisor

Supervisor is a module which starts the consumer->forwarder pairs.
Expand Down
14 changes: 0 additions & 14 deletions common/common.go

This file was deleted.

23 changes: 23 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package config

const (
// MappingFile mapping file environment variable
MappingFile = "MAPPING_FILE"
)

// RabbitEntry RabbitMQ mapping entry
type RabbitEntry struct {
Type string `json:"type"`
Name string `json:"name"`
ConnectionURL string `json:"connection"`
ExchangeName string `json:"topic"`
QueueName string `json:"queue"`
RoutingKey string `json:"routing"`
}

// AmazonEntry SQS/SNS mapping entry
type AmazonEntry struct {
Type string `json:"type"`
Name string `json:"name"`
Target string `json:"target"`
}
2 changes: 0 additions & 2 deletions config/mapping.json

This file was deleted.

17 changes: 17 additions & 0 deletions examples/rabbit_to_sns.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"source" : {
"type" : "RabbitMQ",
"name" : "test-rabbit",
"connection" : "amqp://hrhisghq:[email protected]/hrhisghq",
"topic" : "amq.topic",
"queue" : "test-queue",
"routing" : "#"
},
"destination" : {
"type" : "SNS",
"name" : "test-sns",
"target" : "arn:aws:sns:eu-west-1:XXXXXXXX:test-forwarder"
}
}
]
17 changes: 17 additions & 0 deletions examples/rabbit_to_sqs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"source" : {
"type" : "RabbitMQ",
"name" : "test-rabbit",
"connection" : "amqp://guest:guest@localhost:5672/",
"topic" : "amq.topic",
"queue" : "test-queue",
"routing" : "#"
},
"destination" : {
"type" : "SQS",
"name" : "test-queue",
"target" : "https://sqs.eu-west-1.amazonaws.com/XXXXXXXXX/test-queue"
}
}
]
Binary file modified img/rabbit-amazon-forwarder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 15 additions & 15 deletions mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log"
"os"

"github.com/AirHelp/rabbit-amazon-forwarder/common"
"github.com/AirHelp/rabbit-amazon-forwarder/config"
"github.com/AirHelp/rabbit-amazon-forwarder/consumer"
"github.com/AirHelp/rabbit-amazon-forwarder/forwarder"
"github.com/AirHelp/rabbit-amazon-forwarder/rabbitmq"
Expand All @@ -17,8 +17,8 @@ import (
type pairs []pair

type pair struct {
Source common.Item `json:"source"`
Destination common.Item `json:"destination"`
Source config.RabbitEntry `json:"source"`
Destination config.AmazonEntry `json:"destination"`
}

// Client mapping client
Expand All @@ -28,8 +28,8 @@ type Client struct {

// Helper interface for creating consumers and forwaders
type Helper interface {
createConsumer(item common.Item) consumer.Client
createForwarder(item common.Item) forwarder.Client
createConsumer(entry config.RabbitEntry) consumer.Client
createForwarder(entry config.AmazonEntry) forwarder.Client
}

type helperImpl struct{}
Expand Down Expand Up @@ -65,27 +65,27 @@ func (c Client) Load() (map[consumer.Client]forwarder.Client, error) {
}

func (c Client) loadFile() ([]byte, error) {
filePath := os.Getenv(common.MappingFile)
filePath := os.Getenv(config.MappingFile)
log.Print("Loading mapping file: ", filePath)
return ioutil.ReadFile(filePath)
}

func (h helperImpl) createConsumer(item common.Item) consumer.Client {
log.Printf("Creating consumer: [%s, %s]", item.Type, item.Name)
switch item.Type {
func (h helperImpl) createConsumer(entry config.RabbitEntry) consumer.Client {
log.Printf("Creating consumer: [%s, %s]", entry.Type, entry.Name)
switch entry.Type {
case rabbitmq.Type:
return rabbitmq.CreateConsumer(item)
return rabbitmq.CreateConsumer(entry)
}
return nil
}

func (h helperImpl) createForwarder(item common.Item) forwarder.Client {
log.Printf("Creating forwarder: [%s, %s]", item.Type, item.Name)
switch item.Type {
func (h helperImpl) createForwarder(entry config.AmazonEntry) forwarder.Client {
log.Printf("Creating forwarder: [%s, %s]", entry.Type, entry.Name)
switch entry.Type {
case sns.Type:
return sns.CreateForwarder(item)
return sns.CreateForwarder(entry)
case sqs.Type:
return sqs.CreateForwarder(item)
return sqs.CreateForwarder(entry)
}
return nil
}
46 changes: 21 additions & 25 deletions mapping/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

"github.com/AirHelp/rabbit-amazon-forwarder/common"
"github.com/AirHelp/rabbit-amazon-forwarder/config"
"github.com/AirHelp/rabbit-amazon-forwarder/consumer"
"github.com/AirHelp/rabbit-amazon-forwarder/forwarder"
"github.com/AirHelp/rabbit-amazon-forwarder/rabbitmq"
Expand All @@ -19,7 +19,7 @@ const (
)

func TestLoad(t *testing.T) {
os.Setenv(common.MappingFile, "../tests/rabbit_to_sns.json")
os.Setenv(config.MappingFile, "../tests/rabbit_to_sns.json")
client := New(MockMappingHelper{})
var consumerForwarderMap map[consumer.Client]forwarder.Client
var err error
Expand All @@ -32,7 +32,7 @@ func TestLoad(t *testing.T) {
}

func TestLoadFile(t *testing.T) {
os.Setenv(common.MappingFile, "../tests/rabbit_to_sns.json")
os.Setenv(config.MappingFile, "../tests/rabbit_to_sns.json")
client := New()
data, err := client.loadFile()
if err != nil {
Expand All @@ -46,13 +46,13 @@ func TestLoadFile(t *testing.T) {
func TestCreateConsumer(t *testing.T) {
client := New()
consumerName := "test-rabbit"
item := common.Item{Type: "RabbitMQ",
entry := config.RabbitEntry{Type: "RabbitMQ",
Name: consumerName,
ConnectionURL: "url",
ExchangeName: "topic",
QueueName: "test-queue",
RoutingKey: "#"}
consumer := client.helper.createConsumer(item)
consumer := client.helper.createConsumer(entry)
if consumer.Name() != consumerName {
t.Errorf("wrong consumer name, expected %s, found %s", consumerName, consumer.Name())
}
Expand All @@ -61,13 +61,11 @@ func TestCreateConsumer(t *testing.T) {
func TestCreateForwarderSNS(t *testing.T) {
client := New(MockMappingHelper{})
forwarderName := "test-sns"
item := common.Item{Type: "SNS",
Name: forwarderName,
ConnectionURL: "",
ExchangeName: "topic",
QueueName: "",
RoutingKey: "#"}
forwarder := client.helper.createForwarder(item)
entry := config.AmazonEntry{Type: "SNS",
Name: forwarderName,
Target: "arn",
}
forwarder := client.helper.createForwarder(entry)
if forwarder.Name() != forwarderName {
t.Errorf("wrong forwarder name, expected %s, found %s", forwarderName, forwarder.Name())
}
Expand All @@ -76,13 +74,11 @@ func TestCreateForwarderSNS(t *testing.T) {
func TestCreateForwarderSQS(t *testing.T) {
client := New(MockMappingHelper{})
forwarderName := "test-sqs"
item := common.Item{Type: "SQS",
Name: forwarderName,
ConnectionURL: "",
ExchangeName: "",
QueueName: "test-queue",
RoutingKey: "#"}
forwarder := client.helper.createForwarder(item)
entry := config.AmazonEntry{Type: "SQS",
Name: forwarderName,
Target: "arn",
}
forwarder := client.helper.createForwarder(entry)
if forwarder.Name() != forwarderName {
t.Errorf("wrong forwarder name, expected %s, found %s", forwarderName, forwarder.Name())
}
Expand All @@ -103,18 +99,18 @@ type MockSQSForwarder struct {

type ErrorForwarder struct{}

func (h MockMappingHelper) createConsumer(item common.Item) consumer.Client {
if item.Type != rabbitmq.Type {
func (h MockMappingHelper) createConsumer(entry config.RabbitEntry) consumer.Client {
if entry.Type != rabbitmq.Type {
return nil
}
return MockRabbitConsumer{}
}
func (h MockMappingHelper) createForwarder(item common.Item) forwarder.Client {
switch item.Type {
func (h MockMappingHelper) createForwarder(entry config.AmazonEntry) forwarder.Client {
switch entry.Type {
case sns.Type:
return MockSNSForwarder{item.Name}
return MockSNSForwarder{entry.Name}
case sqs.Type:
return MockSQSForwarder{item.Name}
return MockSQSForwarder{entry.Name}
}
return ErrorForwarder{}
}
Expand Down
6 changes: 3 additions & 3 deletions rabbitmq/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/AirHelp/rabbit-amazon-forwarder/common"
"github.com/AirHelp/rabbit-amazon-forwarder/config"
"github.com/AirHelp/rabbit-amazon-forwarder/consumer"
"github.com/AirHelp/rabbit-amazon-forwarder/forwarder"
"github.com/streadway/amqp"
Expand Down Expand Up @@ -34,8 +34,8 @@ type workerParams struct {
}

// CreateConsumer creates conusmer from string map
func CreateConsumer(item common.Item) consumer.Client {
return Consumer{item.Name, item.ConnectionURL, item.ExchangeName, item.QueueName, item.RoutingKey}
func CreateConsumer(entry config.RabbitEntry) consumer.Client {
return Consumer{entry.Name, entry.ConnectionURL, entry.ExchangeName, entry.QueueName, entry.RoutingKey}
}

// Name consumer name
Expand Down
6 changes: 3 additions & 3 deletions sns/forwader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sns
import (
"log"

"github.com/AirHelp/rabbit-amazon-forwarder/common"
"github.com/AirHelp/rabbit-amazon-forwarder/config"
"github.com/AirHelp/rabbit-amazon-forwarder/forwarder"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -21,9 +21,9 @@ type Forwarder struct {
}

// CreateForwarder creates instance of forwarder
func CreateForwarder(item common.Item) forwarder.Client {
func CreateForwarder(entry config.AmazonEntry) forwarder.Client {
client := awsClient()
forwarder := Forwarder{item.Name, client, item.ExchangeName}
forwarder := Forwarder{entry.Name, client, entry.Target}
log.Print("Created forwarder: ", forwarder.Name())
return forwarder
}
Expand Down
Loading

0 comments on commit f3d5c7e

Please sign in to comment.