Skip to content

Commit

Permalink
Fixes #30441 - Global Registration Template API endpoint (theforeman#…
Browse files Browse the repository at this point in the history
  • Loading branch information
stejskalleos authored Jul 28, 2020
1 parent 2694144 commit bbbfdb3
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 2 deletions.
12 changes: 11 additions & 1 deletion app/controllers/api/v2/provisioning_templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ class ProvisioningTemplatesController < V2::BaseController
include Foreman::Controller::Parameters::ProvisioningTemplate
include Foreman::Controller::TemplateImport

before_action :find_optional_nested_object
before_action :find_optional_nested_object, except: [:global_registration]
before_action :find_resource, :only => %w{show update destroy clone export}

before_action :handle_template_upload, :only => [:create, :update]
before_action :process_template_kind, :only => [:create, :update]
before_action :process_operatingsystems, :only => [:create, :update]
before_action :find_global_registration, :only => [:global_registration]

api :GET, "/provisioning_templates/", N_("List provisioning templates")
api :GET, "/operatingsystems/:operatingsystem_id/provisioning_templates", N_("List provisioning templates per operating system")
Expand Down Expand Up @@ -130,6 +131,15 @@ def export
send_data @provisioning_template.to_erb, :type => 'text/plain', :disposition => 'attachment', :filename => @provisioning_template.filename
end

api :GET, '/register', N_('Render Global Registration template')
def global_registration
if @provisioning_template
render plain: @provisioning_template.render.html_safe
else
render plain: _('Global Registration Template with name %s defined via default_global_registration_item Setting not found, please configure the existing template name first') % Setting[:default_global_registration_item], status: :not_found
end
end

private

def resource_class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def load_vars_from_template
@operatingsystems = @template.operatingsystems if @template.respond_to?(:operatingsystems)
end

def find_global_registration
template_name = Setting[:default_global_registration_item]
@provisioning_template = ProvisioningTemplate.find_by(name: template_name)
end

private

def default_template_url(template, hostgroup)
Expand Down
1 change: 1 addition & 0 deletions app/models/setting/provisioning.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def self.default_settings
N_('Exclude pattern for facts stored in foreman')
),
set('maximum_structured_facts', N_("Maximum amount of keys in structured subtree, statistics stored in foreman::dropped_subtree_facts"), 100, N_('Maximum structured facts')),
set('default_global_registration_item', N_("Global Registration template"), 'Global Registration', N_("Default Global registration template")),
] + default_global_templates + default_local_boot_templates
end

Expand Down
1 change: 1 addition & 0 deletions app/registries/foreman/access_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
:"api/v2/hosts" => [:create],
:"api/v2/interfaces" => [:create],
:"api/v2/tasks" => [:index],
:"api/v2/provisioning_templates" => [:global_registration],
}
map.permission :edit_hosts, {:hosts => [:edit, :update, :multiple_actions, :reset_multiple, :submit_multiple_enable,
:select_multiple_hostgroup, :select_multiple_environment, :submit_multiple_disable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%#
kind: registration
name: Global Registration
model: ProvisioningTemplate
-%>

if ! [ $(id -u) = 0 ]; then
echo "Please run as root"
exit 1
fi
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@
end
end
end
get :register, to: 'api/v2/provisioning_templates#global_registration'

if Rails.env.development? && defined?(::GraphiQL::Rails::Engine)
mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/api/graphql'
Expand Down
20 changes: 20 additions & 0 deletions test/controllers/api/v2/provisioning_templates_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,24 @@ class Api::V2::ProvisioningTemplatesControllerTest < ActionController::TestCase
template_combination = TemplateCombination.find(template_combinations[0]['id'])
assert_equal response['id'], template_combination.provisioning_template_id
end

describe 'global registration template' do
test "should get template" do
get :global_registration
assert_response :success
assert_equal @response.body, templates(:global_registration).template
end

test "should render not_found" do
Setting::Provisioning.any_instance.stubs(:value).returns('not-existing-template')
get :global_registration
assert_response :not_found
end

test "should render error when template is invalid" do
Foreman::Renderer::Source::Database.any_instance.stubs(:content).returns("<% asda =!?== '2 % %>")
get :global_registration
assert_response :internal_server_error
end
end
end
2 changes: 1 addition & 1 deletion test/controllers/foreman_register/hosts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class HostsControllerTest < ActionController::TestCase
let(:organization) { FactoryBot.create(:organization) }
let(:tax_location) { FactoryBot.create(:location) }
let(:template_content) { 'template content <%= @host.name %>' }
let(:template_kind) { FactoryBot.create(:template_kind, name: 'registration') }
let(:template_kind) { template_kinds(:registration) }
let(:registration_template) do
FactoryBot.create(
:provisioning_template,
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,8 @@ attribute93:
category: Setting::Puppet
default: true
description: "Foreman will update a host's hostgroup from its facts"
attribute94:
name: default_global_registration_item
category: Setting::Provisioning
default: 'Global Registration'
description: "Default Global registration template"
4 changes: 4 additions & 0 deletions test/fixtures/template_kinds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ pxegrub:
pxegrub2:
name: PXEGrub2
description: description for pxegrub2 template

registration:
name: registration
description: description for registration template
8 changes: 8 additions & 0 deletions test/fixtures/templates.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,11 @@ autopart:
operatingsystems: centos5_3
locked: true
type: Ptable

global_registration:
name: Global Registration
template: 'echo "Default Global registration template"'
template_kind: registration
operatingsystems: redhat
locked: true
type: ProvisioningTemplate

0 comments on commit bbbfdb3

Please sign in to comment.