-
Notifications
You must be signed in to change notification settings - Fork 71
Adds support for enabling and Disabling relays #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
relay_id = Map.fetch!(announcement, "relay") | ||
|
||
online_status = case Map.fetch!(announcement, "online") do | ||
true -> :online | ||
false -> :offline | ||
end | ||
|
||
enabled_status = cond do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think an if
would be a bit more straightforward than a cond
for this.
|> MapSet.to_list | ||
end | ||
|
||
defp in_tracker?(tracker, relay_id) do | ||
relays = Enum.reduce(tracker.map, MapSet.new(), fn({_, relays}, acc) -> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be able to simplify this a bit and make the whole thing a pipeline by calling Map.values(tracker.map)
first.
bundle = bundle("relay-enable-bundle-1") | ||
relay_group = relay_group("relay-enable-group-1") | ||
add_relay_to_group(relay_group.id, relay.id) | ||
assign_bundle_to_group(relay_group.id, bundle.id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about making some helper methods that can do all this in one shot, like we now do with cogctl
.
I definitely think we should still have the fine-grained helpers that you've got here, but I think it'd help the tests to put a lot of that tedious setup logic behind some well-named helpers that can do it all in one shot.
@@ -46,6 +46,10 @@ defmodule Cog.V1.RelayController do | |||
changeset = Relay.changeset(relay, relay_params) | |||
case Repo.update(changeset) do | |||
{:ok, relay} -> | |||
# If the enabled flag has changed we need to enable/disable the relay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this functionality needs to move out of the controller and closer to the core. That is, whenever we update a relay from anywhere, we should be sure that the state of the system is consistent.
One way to do it is to put the database operations (like Repo.update
, here) behind another Cog-specific interface, which acts as a DDD-style "repository". The only way to update a relay would be to go through that interface, and behind it, we're free to do whatever we want. In this case, we can just make the call to update the status, but in the future it opens up more possibilities (e.g., emitting events that arbitrary system components can listen for and then take action on, etc.).
I started this with triggers - https://github.com/operable/cog/blob/master/lib/cog/repository/triggers.ex - thinking that each "thing" could perhaps have it's own "repository". That could ultimately change, but it seemed like a decent way to decompose things to start with.
9c8d1e0
to
a0f6d12
Compare
""" | ||
def create_relay_bundle_and_group(name, opts \\ []) do | ||
relay = relay("relay-#{name}", | ||
Keyword.get(opts, :token, "sekrit"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Documenting the options each test function takes would be nice
👍 |
8ea6c01
to
5acacea
Compare
This adds a couple functions to the
Cog.Relay.Relays
module to enable and disable relays.