diff --git a/app/controllers/api/v2/provisioning_templates_controller.rb b/app/controllers/api/v2/provisioning_templates_controller.rb index 199799b260a..976a7eb65a1 100644 --- a/app/controllers/api/v2/provisioning_templates_controller.rb +++ b/app/controllers/api/v2/provisioning_templates_controller.rb @@ -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") @@ -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 diff --git a/app/controllers/concerns/foreman/controller/provisioning_templates.rb b/app/controllers/concerns/foreman/controller/provisioning_templates.rb index e1e0864bceb..5f7410dcd6c 100644 --- a/app/controllers/concerns/foreman/controller/provisioning_templates.rb +++ b/app/controllers/concerns/foreman/controller/provisioning_templates.rb @@ -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) diff --git a/app/models/setting/provisioning.rb b/app/models/setting/provisioning.rb index 4d3fd523635..de0ee543dec 100644 --- a/app/models/setting/provisioning.rb +++ b/app/models/setting/provisioning.rb @@ -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 diff --git a/app/registries/foreman/access_permissions.rb b/app/registries/foreman/access_permissions.rb index f46b2517fb7..1bae36bcedb 100644 --- a/app/registries/foreman/access_permissions.rb +++ b/app/registries/foreman/access_permissions.rb @@ -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, diff --git a/app/views/unattended/provisioning_templates/registration/global_registration.erb b/app/views/unattended/provisioning_templates/registration/global_registration.erb new file mode 100644 index 00000000000..f6637ecc6d2 --- /dev/null +++ b/app/views/unattended/provisioning_templates/registration/global_registration.erb @@ -0,0 +1,10 @@ +<%# +kind: registration +name: Global Registration +model: ProvisioningTemplate +-%> + +if ! [ $(id -u) = 0 ]; then + echo "Please run as root" + exit 1 +fi diff --git a/config/routes.rb b/config/routes.rb index 26774623c5c..9651fc8307e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/test/controllers/api/v2/provisioning_templates_controller_test.rb b/test/controllers/api/v2/provisioning_templates_controller_test.rb index 075ecee2d70..43c0e8d1b22 100644 --- a/test/controllers/api/v2/provisioning_templates_controller_test.rb +++ b/test/controllers/api/v2/provisioning_templates_controller_test.rb @@ -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 diff --git a/test/controllers/foreman_register/hosts_controller_test.rb b/test/controllers/foreman_register/hosts_controller_test.rb index 0907c70aa65..f2b52ac41dd 100644 --- a/test/controllers/foreman_register/hosts_controller_test.rb +++ b/test/controllers/foreman_register/hosts_controller_test.rb @@ -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, diff --git a/test/fixtures/settings.yml b/test/fixtures/settings.yml index 17eee81344b..0b38f98eade 100644 --- a/test/fixtures/settings.yml +++ b/test/fixtures/settings.yml @@ -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" diff --git a/test/fixtures/template_kinds.yml b/test/fixtures/template_kinds.yml index ef64942ded8..5a16fd8bef0 100644 --- a/test/fixtures/template_kinds.yml +++ b/test/fixtures/template_kinds.yml @@ -26,3 +26,7 @@ pxegrub: pxegrub2: name: PXEGrub2 description: description for pxegrub2 template + +registration: + name: registration + description: description for registration template diff --git a/test/fixtures/templates.yml b/test/fixtures/templates.yml index 483732b0526..2529772e779 100644 --- a/test/fixtures/templates.yml +++ b/test/fixtures/templates.yml @@ -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