[PATCH/foreman 1/1] FEATURE #982 API call to get fact keys

Signed-off-by: Corey Osman <corey@logicminds.biz>

··· --- app/controllers/facts_controller.rb | 49 ++++++++++++++++++++++++++++++ config/routes.rb | 1 + test/functional/facts_controller_test.rb | 20 ++++++++++++ 3 files changed, 70 insertions(+), 0 deletions(-) create mode 100644 app/controllers/facts_controller.rb create mode 100644 test/functional/facts_controller_test.rb

diff --git a/app/controllers/facts_controller.rb b/app/controllers/facts_controller.rb
new file mode 100644
index 0000000…df820d9
— /dev/null
+++ b/app/controllers/facts_controller.rb
@@ -0,0 +1,49 @@
+class FactsController < ApplicationController
+

  • include Foreman::Controller::AutoCompleteSearch
  • skip_before_filter :require_ssl, :only => :create
  • skip_before_filter :require_login, :only => :create
  • skip_before_filter :authorize, :only => :create
  • skip_before_filter :verify_authenticity_token, :only => :create
  • before_filter :set_admin_user, :only => :create
  • before_filter :setup_search_options, :only => :index
  • Shortcut

  • FactName = Puppet::Rails::FactName
  • FactValue = Puppet::Rails::FactValue
  • avoids storing the facts data in the log files

  • filter_parameter_logging :facts
  • #TODO: Create view templates for “just the facts” any attempt
  • to http://foreman/facts will not work unless ?format=json is used

  • def index
  • begin
  •  values = FactName.all(:select => "name", :conditions => ["fact_names.name <> ?","--- !ruby/sym _timestamp"])
    
  • rescue
  •  values = []
    
  • end
  • respond_to do |format|
  •  format.html { }
    
  • # format.html { @fact_names = values.paginate :page => params[:page]}
    
  •  format.json { render :json => values }
    
  • end
  • end
  • def show
  • return not_found if params[:id].blank?
  • begin
  •  values = FactName.find_by_name(params[:id]).fact_values
    
  • rescue
  •  values = []
    
  • end
  • respond_to do |format|
  •  format.html {  }
    
  •  format.json { render :json => values }
    
  • end
  • end

+end
diff --git a/config/routes.rb b/config/routes.rb
index bc686c0…7f21250 100644
— a/config/routes.rb
+++ b/config/routes.rb
@@ -42,6 +42,7 @@ ActionController::Routing::Routes.draw do |map|
map.resources :common_parameters
map.resources :environments, :collection => {:import_environments => :get, :obsolete_and_new => :post}
map.resources :fact_values, :only => [:create, :index], :collection => { :auto_complete_search => :get }

  • map.resources :facts, :only => [:index, :show], :collection => { :auto_complete_search => :get }
    map.resources :ptables
    map.resources :roles, :collection => {:report => [:get, :post]}
    map.resources :auth_source_ldaps
    diff --git a/test/functional/facts_controller_test.rb b/test/functional/facts_controller_test.rb
    new file mode 100644
    index 0000000…1190e6b
    — /dev/null
    +++ b/test/functional/facts_controller_test.rb
    @@ -0,0 +1,20 @@
    +require ‘test_helper’

+class FactsControllerTest < ActionController::TestCase
+

  • def test_index_json
  • get :index, {:format => “json”}, set_session_user
  • facts = ActiveSupport::JSON.decode(@response.body)
  • assert facts.is_a?(Array)
  • assert_response :success
  • end
  • def test_show_json
  • get :show, {:format => “json”, :id => “kernelversion”}, set_session_user
  • factvalues = ActiveSupport::JSON.decode(@response.body)
  • assert factvalues.is_a?(Array)
  • assert_response :success
  • end

+end
+

1.7.4.1

> Signed-off-by: Corey Osman <corey@logicminds.biz>
> —
> app/controllers/facts_controller.rb | 49 ++++++++++++++++++++++++++++++
> config/routes.rb | 1 +
> test/functional/facts_controller_test.rb | 20 ++++++++++++
> 3 files changed, 70 insertions(+), 0 deletions(-)
> create mode 100644 app/controllers/facts_controller.rb
> create mode 100644 test/functional/facts_controller_test.rb
>
> diff --git a/app/controllers/facts_controller.rb b/app/controllers/facts_controller.rb
> new file mode 100644
> index 0000000…df820d9
> — /dev/null
> +++ b/app/controllers/facts_controller.rb
> @@ -0,0 +1,49 @@
> +class FactsController < ApplicationController
> +
> + include Foreman::Controller::AutoCompleteSearch
> +
> + skip_before_filter :require_ssl, :only => :create
> + skip_before_filter :require_login, :only => :create
> + skip_before_filter :authorize, :only => :create
> + skip_before_filter :verify_authenticity_token, :only => :create
> + before_filter :set_admin_user, :only => :create
all of the filter above are not required… the create method (for the
fact_values controller is how we push facts over http), this controller
does not take care for that action at the moment.
> + before_filter :setup_search_options, :only => :index
Does the search bar do anything? I dont think it works at the moment
(hence this line and the auto completer above are not required).
> +
> + # Shortcut
> + FactName = Puppet::Rails::FactName
> + FactValue = Puppet::Rails::FactValue
dont think you need the second one, as we already have a factvalue model
in foreman.
> +
> + # avoids storing the facts data in the log files
> + filter_parameter_logging :facts
not required too.
> +
> + #TODO: Create view templates for "just the facts" any attempt
> + # to http://foreman/facts will not work unless ?format=json is used
> + def index
> + begin
> + values = FactName.all(:select => "name", :conditions => ["fact_names.name <> ?","— !ruby/sym _timestamp"])
ideally, the no timestamps should be a scope inside the fact_name model.
> + rescue
> + values = []
> + end
> +
> + respond_to do |format|
> + format.html { }
if we dont support it, can we atleast return a useful http error (500 is
not really nice)… alternatively, we could easly make a simple html
view.
> + # format.html { @fact_names = values.paginate :page => params[:page]}
> + format.json { render :json => values }
> + end
> + end
> +
> + def show
> + return not_found if params[:id].blank?
this is not really required as the find method below usually will render
404 as well, but it doesnt hurt i guess.

··· On Sat, 2011-07-23 at 00:52 -0700, Corey Osman wrote:
  • begin
  •  values = FactName.find_by_name(params[:id]).fact_values
    
  • rescue
  •  values = []
    
  • end
  • respond_to do |format|
  •  format.html {  }
    
  •  format.json { render :json => values }
    
  • end
  • end

+end
diff --git a/config/routes.rb b/config/routes.rb
index bc686c0…7f21250 100644
— a/config/routes.rb
+++ b/config/routes.rb
@@ -42,6 +42,7 @@ ActionController::Routing::Routes.draw do |map|
map.resources :common_parameters
map.resources :environments, :collection => {:import_environments => :get, :obsolete_and_new => :post}
map.resources :fact_values, :only => [:create, :index], :collection => { :auto_complete_search => :get }

  • map.resources :facts, :only => [:index, :show], :collection => { :auto_complete_search => :get }
    map.resources :ptables
    map.resources :roles, :collection => {:report => [:get, :post]}
    map.resources :auth_source_ldaps
    diff --git a/test/functional/facts_controller_test.rb b/test/functional/facts_controller_test.rb
    new file mode 100644
    index 0000000…1190e6b
    — /dev/null
    +++ b/test/functional/facts_controller_test.rb
    @@ -0,0 +1,20 @@
    +require ‘test_helper’

+class FactsControllerTest < ActionController::TestCase
+

  • def test_index_json
  • get :index, {:format => “json”}, set_session_user
  • facts = ActiveSupport::JSON.decode(@response.body)
  • assert facts.is_a?(Array)
  • assert_response :success
  • end
  • def test_show_json
  • get :show, {:format => “json”, :id => “kernelversion”}, set_session_user
  • factvalues = ActiveSupport::JSON.decode(@response.body)
  • assert factvalues.is_a?(Array)
  • assert_response :success
  • end

+end
+