Skip to content

Commit

Permalink
fixes theforeman#141 - add support to extlookup kind of queries throu…
Browse files Browse the repository at this point in the history
…gh foreman

usage:
wget -q  -O - "http://foreman/lookup?key=ntpserver&order[]=my.domain&order[]=common"
if you want it in YAML, just add:
wget -q  -O - "http://foreman/lookup?key=ntpserver&order[]=my.domain&order[]=common&format=yml"
  • Loading branch information
ohadlevy committed Dec 19, 2009
1 parent a52b100 commit b33878a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
16 changes: 14 additions & 2 deletions app/controllers/hosts_controller.rb
@@ -1,6 +1,6 @@
class HostsController < ApplicationController
before_filter :require_login, :except => [ :query, :externalNodes ]
before_filter :require_ssl, :except => [ :query, :externalNodes ]
before_filter :require_login, :except => [ :query, :externalNodes, :lookup ]
before_filter :require_ssl, :except => [ :query, :externalNodes, :lookup ]
before_filter :find_hosts, :only => :query

helper :hosts
Expand Down Expand Up @@ -108,6 +108,18 @@ def facts
@host = Host.find(params[:id])
end

# query action providing variable names - e.g. for extlookup
def lookup
key, order = params[:key], params[:order]
invalid_request if key.nil? or order.nil? or not order.is_a?(Array)
output = Lookup.search(key, order)
render :text => '404 Not Found', :status => 404 and return unless output
respond_to do |format|
format.html { render :text => output }
format.yml { render :text => output.to_yaml }
end
end

private
def find_hosts
fact, klass = params[:fact], params[:class]
Expand Down
14 changes: 14 additions & 0 deletions app/models/lookup.rb
@@ -0,0 +1,14 @@
class Lookup < ActiveRecord::Base
validates_uniqueness_of :key, :scope => :priority
validates_presence_of :key, :value, :priority

def self.search(key, order = [])
order.each do |prio|
v = find(:first, :conditions => {:key => key, :priority => prio})
return v.value if v
end
# nothing was found
return false
end

end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -6,6 +6,7 @@
map.connect "node/:name", :controller => 'hosts', :action => 'externalNodes',
:requirements => { :name => /[^\.][\w\.-]+/ }
map.connect "/hosts/query", :controller => 'hosts', :action => 'query'
map.connect "/lookup", :controller => 'hosts', :action => 'lookup'
map.resources :hosts,
:member => {:report => :get, :reports => :get, :facts => :get},
:collection => { :show_search => :get},
Expand Down
19 changes: 19 additions & 0 deletions db/migrate/20091219115148_create_lookups.rb
@@ -0,0 +1,19 @@
class CreateLookups < ActiveRecord::Migration
def self.up
create_table :lookups do |t|
t.string :key
t.string :value
t.string :priority

t.timestamps
end
add_index "lookups", "priority"
add_index "lookups", "key"
end

def self.down
remove_index "lookups", "priority"
remove_index "lookups", "key"
drop_table :lookups
end
end
11 changes: 11 additions & 0 deletions test/fixtures/lookups.yml
@@ -0,0 +1,11 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

one:
key: MyString
value: MyString
priority: MyString

two:
key: MyString
value: MyString
priority: MyString
8 changes: 8 additions & 0 deletions test/unit/lookup_test.rb
@@ -0,0 +1,8 @@
require 'test_helper'

class LookupTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end

0 comments on commit b33878a

Please sign in to comment.