-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Realm Management: provide useful feedback when trigger action is malf…
…ormed Display detailed feedback upon failed installation of a trigger with malformed action field. Under the hood, the Trigger changeset now embeds a Action changeset that wraps a valid AMQP/HTTP action. Validation is still performed by the two related modules. The Action modules handles JSON encoding/decoding, too. Signed-off-by: Arnaldo Cesco <arnaldo.cesco@secomind.com>
- Loading branch information
Showing
8 changed files
with
245 additions
and
189 deletions.
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
94 changes: 94 additions & 0 deletions
94
apps/astarte_realm_management_api/lib/astarte_realm_management_api/triggers/action.ex
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,94 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2023 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Astarte.RealmManagement.API.Triggers.Action do | ||
@moduledoc """ | ||
This module provides a wrapper for data held | ||
either in an Astarte.RealmManagement.API.Triggers.AMQPAction | ||
or in an Astarte.RealmManagement.API.Triggers.HttpAction struct. | ||
Validation is performed in the related modules. | ||
""" | ||
use Ecto.Schema | ||
alias Astarte.RealmManagement.API.Triggers.Action | ||
|
||
@primary_key false | ||
embedded_schema do | ||
field :http_url, :string | ||
field :http_method, :string | ||
field :http_static_headers, {:map, :string} | ||
field :template, :string | ||
field :template_type, :string | ||
field :http_post_url, :string, virtual: true | ||
field :amqp_exchange, :string | ||
field :amqp_routing_key, :string | ||
field :amqp_static_headers, {:map, :string} | ||
field :amqp_message_expiration_ms, :integer | ||
field :amqp_message_priority, :integer | ||
field :amqp_message_persistent, :boolean | ||
end | ||
|
||
defimpl Jason.Encoder, for: Action do | ||
def encode( | ||
%Action{ | ||
http_url: http_url, | ||
http_method: http_method, | ||
http_static_headers: http_static_headers, | ||
template: template, | ||
template_type: template_type | ||
}, | ||
opts | ||
) when not is_nil(http_url) do | ||
%{ | ||
"http_url" => http_url, | ||
"http_method" => http_method | ||
} | ||
|> maybe_put("http_static_headers", http_static_headers) | ||
|> maybe_put("template", template) | ||
|> maybe_put("template_type", template_type) | ||
|> Jason.Encode.map(opts) | ||
end | ||
|
||
def encode( | ||
%Action{ | ||
amqp_exchange: amqp_exchange, | ||
amqp_routing_key: amqp_routing_key, | ||
amqp_static_headers: amqp_headers, | ||
amqp_message_expiration_ms: amqp_message_expiration_ms, | ||
amqp_message_persistent: amqp_message_persistent, | ||
amqp_message_priority: amqp_message_priority | ||
}, | ||
opts | ||
) when not is_nil(amqp_exchange) do | ||
%{ | ||
"amqp_exchange" => amqp_exchange, | ||
"amqp_routing_key" => amqp_routing_key, | ||
"amqp_message_expiration_ms" => amqp_message_expiration_ms, | ||
"amqp_message_persistent" => amqp_message_persistent | ||
} | ||
|> maybe_put("amqp_static_headers", amqp_headers) | ||
|> maybe_put("amqp_message_priority", amqp_message_priority) | ||
|> Jason.Encode.map(opts) | ||
end | ||
|
||
defp maybe_put(map, _key, nil), | ||
do: map | ||
|
||
defp maybe_put(map, key, value), | ||
do: Map.put(map, key, value) | ||
end | ||
end |
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
121 changes: 121 additions & 0 deletions
121
apps/astarte_realm_management_api/test/astarte_realm_management_api/triggers/action_test.exs
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,121 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2023 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
defmodule Astarte.RealmManagement.API.Triggers.ActionTest do | ||
use ExUnit.Case | ||
alias Astarte.RealmManagement.API.Triggers.Action | ||
|
||
describe "well-formed HTTP action is correctly encoded" do | ||
test "when headers are set" do | ||
action = %Action{ | ||
http_url: "https://example.com/", | ||
http_method: "put", | ||
http_static_headers: %{ | ||
"Foo" => "Bar", | ||
"Key" => "Value" | ||
} | ||
} | ||
|
||
jason_out_map = | ||
action | ||
|> Jason.encode!() | ||
|> Jason.decode!() | ||
|
||
assert %{ | ||
"http_url" => "https://example.com/", | ||
"http_method" => "put", | ||
"http_static_headers" => %{"Foo" => "Bar", "Key" => "Value"} | ||
} = jason_out_map | ||
end | ||
end | ||
|
||
describe "well-formed AMQPAction is correctly encoded" do | ||
test "when all is set" do | ||
action = %Action{ | ||
amqp_exchange: "astarte_events_test_custom_exchange", | ||
amqp_routing_key: "test_routing_key", | ||
amqp_message_persistent: true, | ||
amqp_message_expiration_ms: 5000, | ||
amqp_message_priority: 3 | ||
} | ||
|
||
jason_out_map = | ||
action | ||
|> Jason.encode!() | ||
|> Jason.decode!() | ||
|
||
assert jason_out_map == %{ | ||
"amqp_exchange" => "astarte_events_test_custom_exchange", | ||
"amqp_routing_key" => "test_routing_key", | ||
"amqp_message_persistent" => true, | ||
"amqp_message_expiration_ms" => 5000, | ||
"amqp_message_priority" => 3 | ||
} | ||
end | ||
|
||
test "when message priority is not set (it is omitted)" do | ||
action = %Action{ | ||
amqp_exchange: "astarte_events_test_custom_exchange", | ||
amqp_routing_key: "test_routing_key", | ||
amqp_message_persistent: false, | ||
amqp_message_expiration_ms: 5000 | ||
} | ||
|
||
jason_out_map = | ||
action | ||
|> Jason.encode!() | ||
|> Jason.decode!() | ||
|
||
assert jason_out_map == %{ | ||
"amqp_exchange" => "astarte_events_test_custom_exchange", | ||
"amqp_routing_key" => "test_routing_key", | ||
"amqp_message_persistent" => false, | ||
"amqp_message_expiration_ms" => 5000 | ||
} | ||
end | ||
|
||
test "when headers are set" do | ||
action = %Action{ | ||
amqp_exchange: "astarte_events_test_custom_exchange", | ||
amqp_routing_key: "test", | ||
amqp_message_persistent: true, | ||
amqp_message_expiration_ms: 100, | ||
amqp_static_headers: %{ | ||
"Foo" => "Bar", | ||
"X-Test" => "Test" | ||
} | ||
} | ||
|
||
jason_out_map = | ||
action | ||
|> Jason.encode!() | ||
|> Jason.decode!() | ||
|
||
assert jason_out_map == %{ | ||
"amqp_exchange" => "astarte_events_test_custom_exchange", | ||
"amqp_routing_key" => "test", | ||
"amqp_message_persistent" => true, | ||
"amqp_message_expiration_ms" => 100, | ||
"amqp_static_headers" => %{ | ||
"Foo" => "Bar", | ||
"X-Test" => "Test" | ||
} | ||
} | ||
end | ||
end | ||
end |
Oops, something went wrong.