Revision f6de8e1d
Added by Oleh Fedorenko about 5 years ago
app/controllers/api/v2/ping_controller.rb | ||
---|---|---|
module Api
|
||
module V2
|
||
class PingController < BaseController
|
||
skip_before_action :authorize, only: [:ping]
|
||
|
||
api :GET, '/ping', N_("Shows status of Foreman system and it's subcomponents")
|
||
description N_('This service is available for unauthenticated users')
|
||
def ping
|
||
@results = Ping.ping
|
||
end
|
||
|
||
api :GET, '/statuses', N_("Shows status and version information of Foreman system and it's subcomponents")
|
||
description N_('This service is only available for authenticated users')
|
||
def statuses
|
||
@results = Ping.statuses
|
||
end
|
||
end
|
||
end
|
||
end
|
app/registries/foreman/access_permissions.rb | ||
---|---|---|
}
|
||
end
|
||
|
||
permission_set.security_block :statuses do |map|
|
||
map.permission :view_statuses, {:"api/v2/ping" => [:statuses]}
|
||
end
|
||
|
||
permission_set.security_block :anonymous do |map|
|
||
map.permission :logout, {:users => [:logout]}, :public => true
|
||
end
|
app/registries/foreman/plugin.rb | ||
---|---|---|
def_field :name, :description, :url, :author, :author_url, :version, :path
|
||
attr_reader :id, :logging, :provision_methods, :compute_resources, :to_prepare_callbacks,
|
||
:facets, :rbac_registry, :dashboard_widgets, :info_providers, :smart_proxy_references,
|
||
:renderer_variable_loaders
|
||
:renderer_variable_loaders, :ping_extension, :status_extension
|
||
|
||
# Lists plugin's roles:
|
||
# Foreman::Plugin.find('my_plugin').registered_roles
|
||
... | ... | |
@rabl_template_extensions = {}
|
||
@smart_proxy_references = []
|
||
@renderer_variable_loaders = []
|
||
@ping_extension = nil
|
||
@status_extension = nil
|
||
end
|
||
|
||
def report_scanner_registry
|
||
... | ... | |
@renderer_variable_loaders << loader_name
|
||
end
|
||
|
||
def register_ping_extension(&block)
|
||
@ping_extension = block
|
||
end
|
||
|
||
def register_status_extension(&block)
|
||
@status_extension = block
|
||
end
|
||
|
||
private
|
||
|
||
def extend_template_helpers_by_module(mod)
|
app/services/ping.rb | ||
---|---|---|
class Ping
|
||
class << self
|
||
def ping
|
||
{
|
||
'foreman': {
|
||
database: ping_database
|
||
}
|
||
}.merge(plugins_ping)
|
||
end
|
||
|
||
def statuses
|
||
{
|
||
'foreman': {
|
||
version: SETTINGS[:version].full,
|
||
api: {
|
||
version: Apipie.configuration.default_version
|
||
},
|
||
plugins: Foreman::Plugin.all,
|
||
smart_proxies: statuses_smart_proxies,
|
||
compute_resources: statuses_compute_resources
|
||
}
|
||
}.merge(plugins_statuses).merge(ping) do |_key, old_val, new_val|
|
||
old_val.merge(new_val)
|
||
end
|
||
end
|
||
|
||
private
|
||
|
||
def ping_database
|
||
ActiveRecord::Base.connection.active?
|
||
end
|
||
|
||
def statuses_compute_resources
|
||
results = []
|
||
ComputeResource.all.index.map do |resource|
|
||
errors = resource.ping
|
||
results << {
|
||
name: resource.name,
|
||
status: errors.empty? ? 'ok' : 'FAIL',
|
||
errors: errors.full_messages
|
||
}
|
||
end
|
||
results
|
||
end
|
||
|
||
def statuses_smart_proxies
|
||
results = []
|
||
SmartProxy.all.includes(:features).map do |proxy|
|
||
begin
|
||
version = proxy.statuses[:version].version['version']
|
||
features = proxy.statuses[:version].version['modules']
|
||
failed_features = proxy.statuses[:logs].logs.failed_modules
|
||
status = 'ok'
|
||
rescue ::Foreman::WrappedException => error
|
||
version ||= 'N/A'
|
||
features ||= {}
|
||
failed_features ||= {}
|
||
status = error.to_s
|
||
end
|
||
results << {
|
||
name: proxy.name,
|
||
status: status,
|
||
version: version,
|
||
features: features,
|
||
failed_features: failed_features
|
||
}
|
||
end
|
||
results
|
||
end
|
||
|
||
def plugins_ping
|
||
Foreman::Plugin.all.inject({}) do |all, plugin|
|
||
next all if plugin.ping_extension.nil?
|
||
all.update({ "#{plugin.name}": plugin.ping_extension.call })
|
||
end
|
||
end
|
||
|
||
def plugins_statuses
|
||
Foreman::Plugin.all.inject({}) do |all, plugin|
|
||
next all if plugin.status_extension.nil?
|
||
all.update({ "#{plugin.name}": plugin.status_extension.call })
|
||
end
|
||
end
|
||
end
|
||
end
|
app/views/api/v2/ping/ping.json.rabl | ||
---|---|---|
node :results do
|
||
@results
|
||
end
|
app/views/api/v2/ping/statuses.json.rabl | ||
---|---|---|
node :results do
|
||
@results
|
||
end
|
config/routes/api/v2.rb | ||
---|---|---|
end
|
||
get 'orchestration/(:id)/tasks', :to => 'tasks#index'
|
||
resources :plugins, :only => [:index]
|
||
get 'ping', :to => 'ping#ping'
|
||
get 'statuses', :to => 'ping#statuses'
|
||
put 'auth_source_ldaps/(:id)/test', :to => 'auth_source_ldaps#test'
|
||
end
|
||
end
|
db/seeds.d/020-permissions_list.rb | ||
---|---|---|
['User', 'view_users'],
|
||
['User', 'create_users'],
|
||
['User', 'edit_users'],
|
||
['User', 'destroy_users']
|
||
['User', 'destroy_users'],
|
||
[nil, 'view_statuses']
|
||
]
|
||
end
|
||
end
|
test/controllers/api/v2/ping_controller_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Api::V2::PingControllerTest < ActionController::TestCase
|
||
test 'should get ping results' do
|
||
response = {
|
||
'foreman': {
|
||
database: true
|
||
}
|
||
}
|
||
Ping.stubs(:ping).returns(response)
|
||
get :ping
|
||
assert_response :success
|
||
assert_not_nil assigns(:results)
|
||
results = ActiveSupport::JSON.decode(@response.body)
|
||
assert_not results.empty?, 'Should response with ping results'
|
||
end
|
||
|
||
test 'should get statuses results' do
|
||
statuses = {
|
||
'foreman': {
|
||
version: '1.20.0',
|
||
api: {
|
||
version: 'v2'
|
||
}
|
||
}
|
||
}
|
||
Ping.stubs(:statuses).returns(statuses)
|
||
get :statuses
|
||
assert_response :success
|
||
assert_not_nil assigns(:results)
|
||
results = ActiveSupport::JSON.decode(@response.body)
|
||
assert_not results.empty?, 'Should response with statuses'
|
||
end
|
||
end
|
test/unit/foreman/access_permissions_test.rb | ||
---|---|---|
"active_storage/variants/show",
|
||
|
||
# graphql
|
||
"api/graphql/execute"
|
||
"api/graphql/execute",
|
||
|
||
# ping
|
||
"api/v2/ping/ping"
|
||
]
|
||
|
||
MAY_SKIP_AUTHORIZED = [ "about/index" ]
|
||
MAY_SKIP_AUTHORIZED = [ "about/index", "api/v2/ping/ping" ]
|
||
|
||
SPECIAL_PATH = ['api/v2/puppet_hosts/puppetrun']
|
||
|
Also available in: Unified diff
Fixes #19731 - V2 API ping call