Skip to content

Commit

Permalink
Merge pull request huginn#612 from cantino/commander_agent_can_configure
Browse files Browse the repository at this point in the history
Commander agent can configure
  • Loading branch information
cantino committed Nov 7, 2014
2 parents bdbb969 + b18bf77 commit 2ea63f7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions app/concerns/agent_controller_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def validate_control_action
errors.add(:base, "#{target.name} cannot be scheduled")
end
}
when 'configure'
if options['configure_options'].nil? || options['configure_options'].keys.length == 0
errors.add(:base, "The 'configure_options' options hash must be supplied when using the 'configure' action.")
end
when 'enable', 'disable'
when nil
errors.add(:base, "action must be specified")
Expand Down Expand Up @@ -63,6 +67,9 @@ def control!
target.update!(disabled: true)
log "Agent '#{target.name}' is disabled"
end
when 'configure'
target.update!(options: target.options.merge(interpolated['configure_options']))
log "Agent '#{target.name}' is configured with #{interpolated['configure_options'].inspect}"
when ''
# Do nothing
else
Expand Down
8 changes: 6 additions & 2 deletions app/models/agents/commander_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CommanderAgent < Agent
cannot_create_events!

description <<-MD
This agent is triggered by schedule or an incoming event and commands other agents ("targets") to run, disable or enable themselves.
This agent is triggered by schedule or an incoming event and commands other agents ("targets") to run, disable, configure, or enable themselves.
# Action types
Expand All @@ -17,12 +17,16 @@ class CommanderAgent < Agent
* `enable`: Target Agents are enabled (if not) when this agent is triggered.
* `configure`: Target Agents have their options updated with the contents of `configure_options`.
Here's a tip: you can use Liquid templating to dynamically determine the action type. For example:
- To create a CommanderAgent that receives an event from WeatherAgent every morning to kick an agent flow that is only useful in a nice weather, try this: `{% if conditions contains 'Sunny' or conditions contains 'Cloudy' %}run{% endif %}`
- To create a CommanderAgent that receives an event from a WeatherAgent every morning to kick an agent flow that is only useful in a nice weather, try this: `{% if conditions contains 'Sunny' or conditions contains 'Cloudy' %}run{% endif %}`
- Likewise, if you have a scheduled agent flow specially crafted for rainy days, try this: `{% if conditions contains 'Rain' %}enable{% else %}disabled{% endif %}`
- If you want to update a WeatherAgent based on a UserLocationAgent, you could use `'action': 'configure'` and set 'configure_options' to `{ 'location': '{{_location_.latlng}}' }`.
# Targets
Select Agents that you want to control from this CommanderAgent.
Expand Down
6 changes: 5 additions & 1 deletion lib/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def empty?
!present?
end

def latlng
"#{lat},#{lng}"
end

private

def floatify(value)
Expand All @@ -100,7 +104,7 @@ def floatify(value)
end

class LocationDrop
KEYS = Location.members.map(&:to_s).concat(%w[latitude longitude])
KEYS = Location.members.map(&:to_s).concat(%w[latitude longitude latlng])

def before_method(key)
if KEYS.include?(key)
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
expect(location['lat']).to eq 2.0
end

it "has a convencience accessor for combined latitude and longitude" do
expect(location.latlng).to eq "2.0,3.0"
end

it "does not allow hash-style assignment" do
expect {
location[:lat] = 2.0
Expand Down Expand Up @@ -60,6 +64,7 @@
'{{location.latitude}}' => '2.0',
'{{location.lng}}' => '3.0',
'{{location.longitude}}' => '3.0',
'{{location.latlng}}' => '2.0,3.0',
}.each { |template, result|
expect(Liquid::Template.parse(template).render('location' => location.to_liquid)).to eq(result),
"expected #{template.inspect} to expand to #{result.inspect}"
Expand Down
21 changes: 21 additions & 0 deletions spec/support/shared_examples/agent_controller_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@
expect(agent).to be_valid
}
end

it "should ensure that 'configure_options' exists in options when the action is 'configure'" do
agent.options['action'] = 'configure'
expect(agent).not_to be_valid
agent.options['configure_options'] = {}
expect(agent).not_to be_valid
agent.options['configure_options'] = { 'key' => 'value' }
expect(agent).to be_valid
end
end
end

Expand Down Expand Up @@ -107,5 +116,17 @@
agent.control!
expect(agent.control_targets.reload).to all(satisfy { |a| !a.disabled? })
end

it "should configure targets" do
agent.options['action'] = 'configure'
agent.options['configure_options'] = { 'url' => 'http:https://some-new-url.com/{{"something" | upcase}}' }
agent.save!
old_options = agents(:bob_website_agent).options

agent.control!

expect(agent.control_targets.reload).to all(satisfy { |a| a.options['url'] == 'http:https://some-new-url.com/SOMETHING' })
expect(agents(:bob_website_agent).reload.options).to eq(old_options.merge('url' => 'http:https://some-new-url.com/SOMETHING'))
end
end
end

0 comments on commit 2ea63f7

Please sign in to comment.