Skip to content

Commit

Permalink
fix: returns the actual result for creating aws queues and topics (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
KauanCarvalho authored Sep 18, 2024
1 parent 9659d87 commit 04b915f
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 27 deletions.
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ COPY . /app

RUN mix do local.hex --force, local.rebar --force, deps.get

# DEV
FROM base AS dev

ENV EX_AWS_HOST="localstack"

RUN apk add --update --no-cache curl

# TEST
FROM base as test
ENV MIX_ENV=test
Expand Down
4 changes: 2 additions & 2 deletions config/dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ config :ex_aws,

config :ex_aws, :sns,
scheme: "http://",
host: "localhost",
host: System.get_env("EX_AWS_HOST", "localhost"),
port: 4566

config :ex_aws, :sqs,
scheme: "http://",
host: "localhost",
host: System.get_env("EX_AWS_HOST", "localhost"),
port: 4566

config :ex_aws_configurator,
Expand Down
4 changes: 2 additions & 2 deletions config/test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ config :ex_aws,

config :ex_aws, :sns,
scheme: "http://",
host: "localhost",
host: System.get_env("EX_AWS_HOST", "localhost"),
port: 4566

config :ex_aws, :sqs,
scheme: "http://",
host: "localhost",
host: System.get_env("EX_AWS_HOST", "localhost"),
port: 4566

config :ex_aws_configurator,
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,14 @@ services:
- "4571:4571"
environment:
- SERVICES=sns,sqs

lib:
container_name: ex_aws_configurator_lib
depends_on:
- localstack
volumes:
- .:/app
build:
context: .
target: dev
command: tail -f /dev/null
58 changes: 51 additions & 7 deletions lib/ex_aws_configurator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,59 @@ defmodule ExAwsConfigurator do
topics = get_env(:topics)
queues = get_env(:queues)

Enum.each(topics, fn {key, _} ->
SNS.create_topic(key)
end)
topics_not_created =
Enum.reduce(topics, [], fn {key, _}, acc ->
case SNS.create_topic(key) do
{:ok, _} ->
acc

{:error, _} ->
[key | acc]
end
end)

queues_not_created =
Enum.reduce(queues, [], fn {queue_name, queue_config}, acc ->
case SQS.create_queue(queue_name) do
{:ok, _} ->
Enum.each(queue_config[:topics], &SQS.subscribe(queue_name, &1))
acc

{:error, _} ->
[queue_name | acc]
end
end)

cond do
length(topics_not_created) > 0 ->
Logger.error("Some topics was not created: #{inspect(topics_not_created)}")
{:error, :topics}

length(queues_not_created) > 0 ->
Logger.error("Some queues was not created: #{inspect(queues_not_created)}")
{:error, :queues}

true ->
:ok
end
end

Enum.each(queues, fn {queue_name, queue_config} ->
SQS.create_queue(queue_name)
@doc """
Create all topics, create all queue and all subscrition present into configuration,
can raise an exception in case of error.
Enum.each(queue_config[:topics], &SQS.subscribe(queue_name, &1))
end)
We recommended that use this only if you change some configuration, however you can add this
method to trigger by CI ever deploy
"""
@spec setup!() :: :ok | no_return
def setup! do
case setup() do
{:error, type} ->
raise ExAwsConfigurator.SetupError, type: type

:ok ->
:ok
end
end

@doc """
Expand Down
12 changes: 12 additions & 0 deletions lib/ex_aws_configurator/exceptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,15 @@ defmodule ExAwsConfigurator.NoResultsError do
"not found any configuration with key #{name}"
end
end

defmodule ExAwsConfigurator.SetupError do
defexception [:type, :message]

def message(%{type: :topics}),
do:
"something went wrong when creating the topics, ensure that the credentials have the necessary permissions to perform this operation"

def message(%{type: :queues}),
do:
"something went wrong when creating the queues, ensure that the credentials have the necessary permissions to perform this operation in addition to being able to subscribe to topics"
end
22 changes: 18 additions & 4 deletions lib/ex_aws_configurator/sns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,30 @@ defmodule ExAwsConfigurator.SNS do
def create_topic(topic_name) when is_atom(topic_name) do
topic = ExAwsConfigurator.get_topic(topic_name)
full_name = Topic.full_name(topic)

attributes =
topic.attributes
|> Map.from_struct
|> Map.from_struct()
|> Enum.reject(fn {key, value} -> key in @fifo_attributes and is_nil(value) end)

Logger.info("Creating topic #{full_name} on #{topic.region}")

full_name
|> SNS.create_topic(attributes)
|> ExAws.request(region: topic.region)
topic_creation_result =
full_name
|> SNS.create_topic(attributes)
|> ExAws.request(region: topic.region)

case topic_creation_result do
{:ok, _} ->
Logger.info("Topic #{full_name} created successfully on #{topic.region}")

{:error, term} ->
Logger.error(
"Error creating topic #{full_name} on #{topic.region}, reason: #{inspect(term)}"
)
end

topic_creation_result
end

@doc """
Expand Down
20 changes: 19 additions & 1 deletion lib/ex_aws_configurator/sqs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,25 @@ defmodule ExAwsConfigurator.SQS do
}
""")

create_queue_on_sqs(full_name, queue, tags)
queue_creation_result = create_queue_on_sqs(full_name, queue, tags)

case queue_creation_result do
{:ok, _} ->
Logger.info(~s"""
\n\n #{IO.ANSI.green()}Queue #{full_name} created successfully on #{queue.region}#{
IO.ANSI.reset()
}
""")

{:error, term} ->
Logger.error(~s"""
\n\n #{IO.ANSI.red()}Error creating queue #{full_name} on #{queue.region}, reason: #{
inspect(term)
}#{IO.ANSI.reset()}
""")
end

queue_creation_result
end

@doc """
Expand Down
17 changes: 15 additions & 2 deletions test/ex_aws_configurator/sns_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule ExAwsConfigurator.SNSTest do
use ExAwsConfigurator.Case

import ExUnit.CaptureLog

alias ExAwsConfigurator.SNS

doctest SNS
Expand All @@ -10,6 +12,7 @@ defmodule ExAwsConfigurator.SNSTest do
setup do
add_topic_to_config(build(:topic_config, name: :topic_name))
add_topic_to_config(%{topic_min_config: %{}})
add_topic_to_config(%{:"!nv@l!d-N@me" => %{}})

SNS.create_topic(:topic_min_config)
SNS.create_topic(:topic_name)
Expand All @@ -19,11 +22,21 @@ defmodule ExAwsConfigurator.SNSTest do

describe "create_topic/1" do
test "create topic when receive a atom with correct configuration" do
assert {:ok, %{status_code: 200}} = SNS.create_topic(:topic_name)
assert capture_log(fn ->
assert {:ok, %{status_code: 200}} = SNS.create_topic(:topic_name)
end) =~ "created successfully"
end

test "create topic with min attributes" do
assert {:ok, %{status_code: 200}} = SNS.create_topic(:topic_min_config)
assert capture_log(fn ->
assert {:ok, %{status_code: 200}} = SNS.create_topic(:topic_min_config)
end) =~ "created successfully"
end

test "do not create an invalid topic" do
assert capture_log(fn ->
assert {:error, _} = SNS.create_topic(:"!nv@l!d-N@me")
end) =~ "Error creating topic"
end

test "raise when tries to create a topic without configuration" do
Expand Down
35 changes: 27 additions & 8 deletions test/ex_aws_configurator/sqs_test.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
defmodule ExAwsConfigurator.SQSTest do
use ExAwsConfigurator.Case

import ExUnit.CaptureLog

alias ExAwsConfigurator.{Queue, SNS, SQS}

doctest SQS
Expand All @@ -12,6 +14,7 @@ defmodule ExAwsConfigurator.SQSTest do
add_queue_to_config(%{queue_min_config: %{}})
add_topic_to_config(build(:topic_config, name: :topic_name))
add_queue_to_config(build(:queue_config, name: :raw_queue, raw_message_delivery: true))
add_queue_to_config(build(:queue_config, name: :"!nv@l!d-N@me"))

add_queue_to_config(
build(:queue_config,
Expand All @@ -34,24 +37,40 @@ defmodule ExAwsConfigurator.SQSTest do

describe "create_queue/1" do
test "create queue when receive a atom with correct configuration" do
assert {:ok, %{status_code: 200}} = SQS.create_queue(:queue_name)
assert capture_log(fn ->
assert {:ok, %{status_code: 200}} = SQS.create_queue(:queue_name)
end) =~ "created successfully"
end

test "create queue without dead letter queue" do
assert {:ok, %{status_code: 200}} = SQS.create_queue(:without_failures_queue)
assert capture_log(fn ->
assert {:ok, %{status_code: 200}} = SQS.create_queue(:without_failures_queue)
end) =~ "created successfully"
end

test "create queue with min attributes" do
assert {:ok, %{status_code: 200}} = SQS.create_queue(:queue_min_config)
assert capture_log(fn ->
assert {:ok, %{status_code: 200}} = SQS.create_queue(:queue_min_config)
end) =~ "created successfully"
end

test "create a fifo queue" do
assert {:ok, %{status_code: 200}} = SQS.create_queue(:"queue.fifo")
assert capture_log(fn ->
assert {:ok, %{status_code: 200}} = SQS.create_queue(:"queue.fifo")

queue = ExAwsConfigurator.get_queue(:"queue.fifo")

queue = ExAwsConfigurator.get_queue(:"queue.fifo")
assert %{attributes: %{content_based_deduplication: true, fifo_queue: true}} =
queue

assert Queue.full_name(queue) == "prefix_test_queue.fifo"
end) =~ "created successfully"
end

assert %{attributes: %{content_based_deduplication: true, fifo_queue: true}} = queue
assert Queue.full_name(queue) == "prefix_test_queue.fifo"
test "do not create an invalid queue" do
assert capture_log(fn ->
assert {:error, _} = SQS.create_queue(:"!nv@l!d-N@me")
end) =~ "Error creating queue"
end

test "raise when tries to create a queue without configuration" do
Expand Down Expand Up @@ -89,7 +108,7 @@ defmodule ExAwsConfigurator.SQSTest do
end

test "public an message to an non existent queue" do
assert {:error, {:http_error, _, %{code: "QueueDoesNotExist"}}} =
assert {:error, {:http_error, _, %{code: "AWS.SimpleQueueService.NonExistentQueue"}}} =
SQS.send_message(:non_created_queue, "message")
end

Expand Down
Loading

0 comments on commit 04b915f

Please sign in to comment.