Revision 6f943886
Added by Joseph Magen over 10 years ago
Gemfile | ||
---|---|---|
gem 'rabl', '>= 0.7.5', '<= 0.9.0'
|
||
gem 'oauth'
|
||
gem 'foreigner', '~> 1.4.2'
|
||
gem 'rails3_before_render'
|
||
|
||
if RUBY_VERSION =~ /^1\.8/
|
||
# Older version of safemode for Ruby 1.8, as the latest causes regexp overflows (#2100)
|
app/controllers/api/v2/autosign_controller.rb | ||
---|---|---|
|
||
def index
|
||
autosign = @api.autosign
|
||
render :json => { @root_node_name => autosign }
|
||
render :json => { root_node_name => autosign }
|
||
end
|
||
|
||
private
|
app/controllers/api/v2/base_controller.rb | ||
---|---|---|
app_info "Foreman v2 is currently in development and is not the default version. You may use v2 by either passing 'version=2' in the Accept Header or entering api/v2/ in the URL."
|
||
end
|
||
|
||
before_filter :root_node_name, :only => :index
|
||
before_render :get_metadata, :only => :index
|
||
before_filter :setup_has_many_params, :only => [:create, :update]
|
||
layout 'api/v2/layouts/index_layout', :only => :index
|
||
|
||
helper_method :root_node_name, :metadata_total, :metadata_subtotal, :metadata_search,
|
||
:metadata_order, :metadata_by, :metadata_page, :metadata_per_page
|
||
|
||
def root_node_name
|
||
@root_node_name = if Rabl.configuration.use_controller_name_as_json_root
|
||
controller_name.split('/').last
|
||
elsif params['root_name'].present?
|
||
params['root_name']
|
||
else
|
||
Rabl.configuration.json_root_default_name
|
||
end
|
||
@root_node_name ||= if Rabl.configuration.use_controller_name_as_json_root
|
||
controller_name.split('/').last
|
||
elsif params['root_name'].present?
|
||
params['root_name']
|
||
else
|
||
Rabl.configuration.json_root_default_name
|
||
end
|
||
end
|
||
|
||
def metadata_total
|
||
@total ||= resource_scope.try(:count).to_i
|
||
end
|
||
|
||
def get_metadata
|
||
@results ||= instance_variable_get("@#{controller_name}")
|
||
#@total should be defined in individual controllers, but in case it's not.
|
||
@total ||= @results.try(:count).to_i
|
||
if (@search = params[:search]).present?
|
||
@subtotal ||= @results.try(:count).to_i
|
||
def metadata_subtotal
|
||
if params[:search].present?
|
||
@subtotal ||= instance_variable_get("@#{controller_name}").try(:count).to_i
|
||
else
|
||
@subtotal ||= @total
|
||
@subtotal ||= metadata_total
|
||
end
|
||
end
|
||
|
||
if params[:order].present? && (order_array = params[:order].split(' ')).any?
|
||
@by = order_array[0]
|
||
@order = order_array[1]
|
||
@order ||= 'ASC'
|
||
end
|
||
def metadata_search
|
||
@search ||= params[:search]
|
||
end
|
||
|
||
@per_page = params[:per_page].present? ? params[:per_page].to_i : Setting::General.entries_per_page
|
||
def metadata_order
|
||
@order ||= params[:order].present? && (order_array = params[:order].split(' ')).any? ? (order_array[1] || 'ASC') : nil
|
||
end
|
||
|
||
if params[:page].present?
|
||
@page = params[:page].to_i
|
||
else
|
||
@page = 1
|
||
end
|
||
def metadata_by
|
||
@by ||= params[:order].present? && (order_array = params[:order].split(' ')).any? ? order_array[0] : nil
|
||
end
|
||
|
||
def metadata_page
|
||
@page ||= params[:page].present? ? params[:page].to_i : 1
|
||
end
|
||
|
||
def metadata_per_page
|
||
@per_page ||= params[:per_page].present? ? params[:per_page].to_i : Setting::General.entries_per_page
|
||
end
|
||
|
||
def setup_has_many_params
|
app/controllers/api/v2/compute_resources_controller.rb | ||
---|---|---|
param :per_page, String, :desc => "number of entries per request"
|
||
|
||
def index
|
||
@compute_resources = ComputeResource.my_compute_resources.search_for(*search_options).paginate(paginate_options)
|
||
@compute_resources = resource_scope.search_for(*search_options).paginate(paginate_options)
|
||
end
|
||
|
||
api :GET, "/compute_resources/:id/", "Show an compute resource."
|
||
... | ... | |
end
|
||
|
||
def resource_scope
|
||
resource_class.my_compute_resources
|
||
ComputeResource.my_compute_resources
|
||
end
|
||
|
||
end
|
app/controllers/api/v2/fact_values_controller.rb | ||
---|---|---|
param :per_page, String, :desc => "number of entries per request"
|
||
|
||
def index
|
||
values = FactValue.my_facts.no_timestamp_facts.
|
||
search_for(*search_options).paginate(paginate_options).
|
||
includes(:fact_name, :host)
|
||
values = resource_scope.includes(:fact_name, :host).search_for(*search_options).paginate(paginate_options)
|
||
@fact_values = FactValue.build_facts_hash(values.all)
|
||
end
|
||
|
||
def resource_scope
|
||
FactValue.my_facts.no_timestamp_facts
|
||
end
|
||
|
||
end
|
||
end
|
||
end
|
app/controllers/api/v2/home_controller.rb | ||
---|---|---|
module V2
|
||
class HomeController < V2::BaseController
|
||
before_filter :require_admin
|
||
skip_before_render :get_metadata, :only => :index
|
||
layout false
|
||
|
||
api :GET, "/", "Show available links."
|
app/controllers/api/v2/host_classes_controller.rb | ||
---|---|---|
include Api::TaxonomyScope
|
||
|
||
before_filter :find_host_id, :only => [:index, :create, :destroy]
|
||
skip_before_render :get_metadata, :only => :index
|
||
|
||
api :GET, "/hosts/:host_id/puppetclass_ids/", "List all puppetclass id's for host"
|
||
|
||
def index
|
||
render :json => { @root_node_name => HostClass.where(:host_id => host_id).pluck('puppetclass_id') }
|
||
render :json => { root_node_name => HostClass.where(:host_id => host_id).pluck('puppetclass_id') }
|
||
end
|
||
|
||
api :POST, "/hosts/:host_id/puppetclass_ids", "Add a puppetclass to host"
|
app/controllers/api/v2/hostgroup_classes_controller.rb | ||
---|---|---|
include Api::TaxonomyScope
|
||
|
||
before_filter :find_hostgroup_id, :only => [:index, :create, :destroy]
|
||
skip_before_render :get_metadata, :only => :index
|
||
|
||
api :GET, "/hostgroups/:hostgroup_id/puppetclass_ids/", "List all puppetclass id's for hostgroup"
|
||
|
||
def index
|
||
render :json => { @root_node_name => HostgroupClass.where(:hostgroup_id => hostgroup_id).pluck('puppetclass_id') }
|
||
render :json => { root_node_name => HostgroupClass.where(:hostgroup_id => hostgroup_id).pluck('puppetclass_id') }
|
||
end
|
||
|
||
api :POST, "/hostgroups/:hostgroup_id/puppetclass_ids", "Add a puppetclass to hostgroup"
|
app/controllers/api/v2/hosts_controller.rb | ||
---|---|---|
param :per_page, String, :desc => "number of entries per request"
|
||
|
||
def index
|
||
@hosts = Host.my_hosts.search_for(*search_options).paginate(paginate_options)
|
||
@hosts = resource_scope.search_for(*search_options).paginate(paginate_options)
|
||
end
|
||
|
||
api :GET, "/hosts/:id/", "Show a host."
|
||
... | ... | |
|
||
# we need to limit resources for a current user
|
||
def resource_scope
|
||
resource_class.my_hosts
|
||
Host.my_hosts
|
||
end
|
||
|
||
api :PUT, "/hosts/:id/puppetrun", "Force a puppet run on the agent."
|
app/controllers/api/v2/images_controller.rb | ||
---|---|---|
|
||
def index
|
||
@images = @compute_resource.images.search_for(*search_options).paginate(paginate_options)
|
||
@total = @compute_resource.images.count
|
||
end
|
||
|
||
api :GET, "/compute_resources/:compute_resource_id/images/:id/", "Show an image"
|
app/controllers/api/v2/interfaces_controller.rb | ||
---|---|---|
|
||
def index
|
||
@interfaces = @nested_obj.interfaces.paginate(paginate_options)
|
||
@total = @nested_obj.interfaces.count
|
||
end
|
||
|
||
api :GET, '/hosts/:host_id/interfaces/:id', 'Show an interface for host'
|
app/controllers/api/v2/os_default_templates_controller.rb | ||
---|---|---|
|
||
def index
|
||
@os_default_templates = nested_obj.os_default_templates.paginate(paginate_options)
|
||
@total = nested_obj.os_default_templates.count
|
||
end
|
||
|
||
api :GET, '/operatingsystems/:operatingsystem_id/os_default_templates/:id', 'Show a os default template kind for operating system'
|
app/controllers/api/v2/override_values_controller.rb | ||
---|---|---|
end
|
||
|
||
def find_override_values
|
||
@override_values = @smart.lookup_values.paginate(paginate_options) if @smart
|
||
if @smart
|
||
@override_values = @smart.lookup_values.paginate(paginate_options)
|
||
@total = @override_values.count
|
||
end
|
||
end
|
||
|
||
def find_override_value
|
app/controllers/api/v2/parameters_controller.rb | ||
---|---|---|
|
||
def index
|
||
@parameters = nested_obj.send(parameters_method).paginate(paginate_options)
|
||
@total = nested_obj.send(parameters_method).count
|
||
end
|
||
|
||
api :GET, "/hosts/:host_id/parameters/:id", "Show a nested parameter for host"
|
app/controllers/api/v2/plugins_controller.rb | ||
---|---|---|
api :GET, '/plugins', 'List of installed plugins'
|
||
def index
|
||
@plugins = Foreman::Plugin.all
|
||
@total = @plugins.try(:count).to_i
|
||
end
|
||
|
||
end
|
app/controllers/api/v2/reports_controller.rb | ||
---|---|---|
def index
|
||
@reports = Report.my_reports.includes(:logs => [:source, :message]).
|
||
search_for(*search_options).paginate(paginate_options)
|
||
@total = Report.my_reports.count
|
||
end
|
||
|
||
api :GET, "/reports/:id/", "Show a report."
|
app/controllers/api/v2/smart_class_parameters_controller.rb | ||
---|---|---|
class SmartClassParametersController < V2::BaseController
|
||
include Api::Version2
|
||
include Api::V2::LookupKeysCommonController
|
||
alias_method :resource_scope, :smart_class_parameters_resource_scope
|
||
|
||
api :GET, '/smart_class_parameters', 'List all smart class parameters'
|
||
api :GET, '/hosts/:host_id/smart_class_parameters', 'List of smart class parameters for a specific host'
|
||
... | ... | |
|
||
end
|
||
end
|
||
end
|
||
end
|
app/controllers/api/v2/smart_proxies_controller.rb | ||
---|---|---|
|
||
def index
|
||
@smart_proxies = proxies_by_type(params[:type]).paginate(paginate_options)
|
||
@subtotal = @smart_proxies.count
|
||
end
|
||
|
||
api :GET, "/smart_proxies/:id/", "Show a smart proxy."
|
||
... | ... | |
end
|
||
|
||
private
|
||
def proxies_by_type(type)
|
||
return SmartProxy.includes(:features).try(type.downcase+"_proxies") if not type.nil?
|
||
return SmartProxy.includes(:features).all
|
||
def proxies_by_type(type = nil)
|
||
return SmartProxy.includes(:features).try(type.downcase+"_proxies") if type.present?
|
||
return SmartProxy.includes(:features).scoped
|
||
end
|
||
|
||
def check_feature_type
|
app/controllers/api/v2/smart_variables_controller.rb | ||
---|---|---|
class SmartVariablesController < V2::BaseController
|
||
include Api::Version2
|
||
include Api::V2::LookupKeysCommonController
|
||
alias_method :resource_scope, :smart_variables_resource_scope
|
||
|
||
api :GET, '/smart_variables', 'List all smart variables'
|
||
api :GET, '/hosts/:host_id/smart_variables', 'List of smart variables for a specific host'
|
app/controllers/api/v2/tasks_controller.rb | ||
---|---|---|
module V2
|
||
class TasksController < BaseController
|
||
|
||
skip_before_render :get_metadata, :only => :index
|
||
layout false
|
||
|
||
api :GET, '/orchestration/:id/tasks/', 'List all tasks for a given orchestration event'
|
||
|
||
... | ... | |
# cache.fetch returns in JSON format, so convert @tasks back to hash
|
||
@tasks = JSON.parse(Rails.cache.fetch(id))
|
||
not_found if @tasks.blank?
|
||
render :json => { @root_node_name => @tasks }, :layout => false
|
||
render :json => { root_node_name => @tasks }
|
||
end
|
||
|
||
end
|
app/controllers/api/v2/template_combinations_controller.rb | ||
---|---|---|
param :config_template_id, :identifier, :required => true
|
||
def index
|
||
@template_combinations = @config_template.template_combinations
|
||
@total = @template_combinations.count
|
||
end
|
||
|
||
api :POST, "/config_templates/:config_template_id/template_combinations", "Add a Template Combination"
|
app/controllers/concerns/api/v2/lookup_keys_common_controller.rb | ||
---|---|---|
end
|
||
|
||
def find_smart_variables
|
||
@smart_variables = LookupKey.smart_variables.search_for(*search_options).paginate(paginate_options) unless (@puppetclass || @host || @hostgroup)
|
||
@smart_variables ||= if @puppetclass
|
||
LookupKey.global_parameters_for_class(@puppetclass.id).search_for(*search_options).paginate(paginate_options)
|
||
elsif @host || @hostgroup
|
||
puppetclass_ids = (@host || @hostgroup).all_puppetclasses.map(&:id)
|
||
LookupKey.global_parameters_for_class(puppetclass_ids).search_for(*search_options).paginate(paginate_options)
|
||
end
|
||
@smart_variables
|
||
@smart_variables = smart_variables_resource_scope.search_for(*search_options).paginate(paginate_options)
|
||
end
|
||
|
||
def smart_variables_resource_scope
|
||
return LookupKey.smart_variables unless (@puppetclass || @host || @hostgroup)
|
||
puppetclass_ids = @puppetclass.id if @puppetclass
|
||
puppetclass_ids ||= @hostgroup.all_puppetclasses.map(&:id) if @hostgroup
|
||
puppetclass_ids ||= @host.all_puppetclasses.map(&:id) if @host
|
||
LookupKey.global_parameters_for_class(puppetclass_ids)
|
||
end
|
||
|
||
def find_smart_class_parameter
|
||
id = params.keys.include?('smart_class_parameter_id') ? params['smart_class_parameter_id'] : params['id']
|
||
... | ... | |
end
|
||
|
||
def find_smart_class_parameters
|
||
@smart_class_parameters = LookupKey.smart_class_parameters.search_for(*search_options).paginate(paginate_options) unless (@puppetclass || @environment || @host || @hostgroup)
|
||
|
||
@smart_class_parameters ||= if @puppetclass && @environment
|
||
LookupKey.smart_class_parameters_for_class(@puppetclass.id, @environment.id)
|
||
elsif @puppetclass && !@environment
|
||
environment_ids = @puppetclass.environment_classes.pluck(:environment_id).uniq
|
||
LookupKey.smart_class_parameters_for_class(@puppetclass.id, environment_ids).search_for(*search_options).paginate(paginate_options)
|
||
elsif !@puppetclass && @environment
|
||
puppetclass_ids = @environment.environment_classes.pluck(:puppetclass_id).uniq
|
||
LookupKey.smart_class_parameters_for_class(puppetclass_ids, @environment.id).search_for(*search_options).paginate(paginate_options)
|
||
elsif @host || @hostgroup
|
||
puppetclass_ids = (@host || @hostgroup).all_puppetclasses.map(&:id)
|
||
environment_id = (@host || @hostgroup).environment_id
|
||
LookupKey.parameters_for_class(puppetclass_ids, environment_id).search_for(*search_options).paginate(paginate_options)
|
||
end
|
||
@smart_class_parameters
|
||
@smart_class_parameters = smart_class_parameters_resource_scope.search_for(*search_options).paginate(paginate_options)
|
||
end
|
||
|
||
def smart_class_parameters_resource_scope
|
||
return LookupKey.smart_class_parameters unless (@puppetclass || @environment || @host || @hostgroup)
|
||
if @puppetclass && @environment
|
||
LookupKey.smart_class_parameters_for_class(@puppetclass.id, @environment.id)
|
||
elsif @puppetclass && !@environment
|
||
environment_ids = @puppetclass.environment_classes.pluck(:environment_id).uniq
|
||
LookupKey.smart_class_parameters_for_class(@puppetclass.id, environment_ids)
|
||
elsif !@puppetclass && @environment
|
||
puppetclass_ids = @environment.environment_classes.pluck(:puppetclass_id).uniq
|
||
LookupKey.smart_class_parameters_for_class(puppetclass_ids, @environment.id)
|
||
elsif @host || @hostgroup
|
||
puppetclass_ids = (@host || @hostgroup).all_puppetclasses.map(&:id)
|
||
environment_id = (@host || @hostgroup).environment_id
|
||
# scope :parameters_for_class uses .override
|
||
LookupKey.parameters_for_class(puppetclass_ids, environment_id)
|
||
end
|
||
end
|
||
|
||
def find_smarts
|
app/views/api/v2/layouts/index_layout.json.erb | ||
---|---|---|
{
|
||
"total": <%= @total.to_json %>,
|
||
"subtotal": <%= @subtotal.to_json %>,
|
||
"page": <%= @page.to_json %>,
|
||
"per_page": <%= @per_page.to_json %>,
|
||
"search": <%= @search.to_json.html_safe %>,
|
||
"total": <%= metadata_total.to_json %>,
|
||
"subtotal": <%= metadata_subtotal.to_json %>,
|
||
"page": <%= metadata_page.to_json %>,
|
||
"per_page": <%= metadata_per_page.to_json %>,
|
||
"search": <%= metadata_search.to_json.html_safe %>,
|
||
"sort": {
|
||
"by": <%= @by.to_json.html_safe %>,
|
||
"order": <%= @order.to_json.html_safe %>
|
||
"by": <%= metadata_by.to_json.html_safe %>,
|
||
"order": <%= metadata_order.to_json.html_safe %>
|
||
},
|
||
"<%= "#{@root_node_name}" %>": <%= yield %>
|
||
"<%= "#{root_node_name}" %>": <%= yield %>
|
||
}
|
foreman.spec | ||
---|---|---|
Requires: %{?scl_prefix}rubygem(bootstrap-sass) >= 3.0.3.0
|
||
Requires: %{?scl_prefix}rubygem(bootstrap-sass) < 3.0.4
|
||
Requires: %{?scl_prefix}rubygem(foreigner) >= 1.4.2
|
||
Requires: %{?scl_prefix}rubygem(rails3_before_render)
|
||
BuildRequires: %{?scl_prefix}rubygem(ancestry) >= 2.0.0
|
||
BuildRequires: %{?scl_prefix}rubygem(ancestry) < 3.0.0
|
||
BuildRequires: %{?scl_prefix}rubygem(apipie-rails) >= 0.0.23
|
||
... | ... | |
BuildRequires: %{?scl_prefix}rubygem(spice-html5-rails)
|
||
BuildRequires: %{?scl_prefix}rubygem(flot-rails) = 0.0.3
|
||
BuildRequires: %{?scl_prefix}rubygem(foreigner) >= 1.4.2
|
||
BuildRequires: %{?scl_prefix}rubygem(rails3_before_render)
|
||
BuildRequires: %{?scl_prefix}facter
|
||
BuildRequires: gettext
|
||
BuildRequires: asciidoc
|
||
... | ... | |
%changelog
|
||
* Thu Jan 16 2014 Dominic Cleal <dcleal@redhat.com> - 1.5.0-0.develop
|
||
- Bump version to 1.5-develop
|
||
- Remove rails3_before_render dependency
|
||
|
||
* Thu Nov 21 2013 Dominic Cleal <dcleal@redhat.com> - 1.4.0-0.develop
|
||
- Bump and change versioning scheme, don't overwrite VERSION (#3712)
|
test/functional/api/v2/hosts_controller_test.rb | ||
---|---|---|
assert @response.body =~ /Available devices are/
|
||
end
|
||
|
||
test "should return correct total and subtotal metadata if search param is passed" do
|
||
get :index, {:search => 'my5name.mydomain.net' }
|
||
assert_response :success
|
||
response = ActiveSupport::JSON.decode(@response.body)
|
||
assert_equal 15, response['total']
|
||
assert_equal 1, response['subtotal']
|
||
assert_equal 'my5name.mydomain.net', response['search']
|
||
end
|
||
|
||
end
|
||
|
||
end
|
Also available in: Unified diff
fixes #3760 - API v2 define metadata @total for each controller