From 3fae8757bed262e6b0922b675b7a51f456f88d88 Mon Sep 17 00:00:00 2001 From: Leos Stejskal Date: Wed, 16 Oct 2024 09:57:56 +0200 Subject: [PATCH] Fixes #37917 - Search by title for for registration parameters Parameters: * Organization * Location * Host group * Operating System --- .../api/v2/registration_controller.rb | 12 ++++-- .../foreman/controller/registration.rb | 25 +++++++---- .../api/v2/registration_controller_test.rb | 42 ++++++++++++++++++- 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/app/controllers/api/v2/registration_controller.rb b/app/controllers/api/v2/registration_controller.rb index 9a5c750759d..cff0ab7f9a8 100644 --- a/app/controllers/api/v2/registration_controller.rb +++ b/app/controllers/api/v2/registration_controller.rb @@ -15,10 +15,14 @@ class RegistrationController < V2::BaseController end api :GET, '/register', N_('Render Global registration template') - param :organization_id, :number, desc: N_("ID of the Organization to register the host in") - param :location_id, :number, desc: N_("ID of the Location to register the host in") - param :hostgroup_id, :number, desc: N_("ID of the Host group to register the host in") - param :operatingsystem_id, :number, desc: N_("ID of the Operating System to register the host in") + param :organization_id, :number, desc: N_("ID of the Organization to register the host in. Takes precedence over the `organization` parameter") + param :organization, String, desc: N_("Title of the Organization to register the host in") + param :location_id, :number, desc: N_("ID of the Location to register the host in. Takes precedence over the `location` parameter") + param :location, String, desc: N_("Title of the Location to register the host in") + param :hostgroup_id, :number, desc: N_("ID of the Host group to register the host in. Takes precedence over the `hostgroup` parameter") + param :hostgroup, String, desc: N_("Title of the Host group to register the host in") + param :operatingsystem_id, :number, desc: N_("ID of the Operating System to register the host in. Takes precedence over the `operatingsystem` parameter") + param :operatingsystem, String, desc: N_("Title of the Operating System to register the host in") param :setup_insights, :bool, desc: N_("Set 'host_registration_insights' parameter for the host. If it is set to true, insights client will be installed and registered on Red Hat family operating systems") param :setup_remote_execution, :bool, desc: N_("Set 'host_registration_remote_execution' parameter for the host. If it is set to true, SSH keys will be installed on the host") param :packages, String, desc: N_("Packages to install on the host when registered. Can be set by `host_packages` parameter, example: `pkg1 pkg2`") diff --git a/app/controllers/concerns/foreman/controller/registration.rb b/app/controllers/concerns/foreman/controller/registration.rb index 03d7b2dbff0..17736f2e141 100644 --- a/app/controllers/concerns/foreman/controller/registration.rb +++ b/app/controllers/concerns/foreman/controller/registration.rb @@ -14,11 +14,6 @@ def global_registration_vars .map(&:allowed_registration_vars) .flatten.compact.uniq - organization_from_param = Organization.authorized(:view_organizations).find(params['organization_id']) if params['organization_id'].present? - location_from_param = Location.authorized(:view_locations).find(params['location_id']) if params['location_id'].present? - host_group = Hostgroup.authorized(:view_hostgroups).find(params['hostgroup_id']) if params["hostgroup_id"].present? - operatingsystem = Operatingsystem.authorized(:view_operatingsystems).find(params['operatingsystem_id']) if params["operatingsystem_id"].present? - if params['repo'].present? repo_data = {} repo_data[params['repo']] = params['repo_gpg_key_url'] || '' @@ -29,12 +24,17 @@ def global_registration_vars params['repo_data'].each { |repo| repo_data[repo['repo']] = repo['repo_gpg_key_url'] } end + organization = find_object(Organization, params['organization_id'], params['organization']) || default_organization + location = find_object(Location, params['location_id'], params['location']) || default_location + hostgroup = find_object(Hostgroup, params['hostgroup_id'], params['hostgroup']) + operatingsystem = find_object(Operatingsystem, params['operatingsystem_id'], params['operatingsystem']) + context = { user: User.current, auth_token: api_authorization_token, - organization: organization_from_param || default_organization, - location: location_from_param || default_location, - hostgroup: host_group, + organization: organization, + location: location, + hostgroup: hostgroup, operatingsystem: operatingsystem, setup_insights: ActiveRecord::Type::Boolean.new.deserialize(params['setup_insights']), setup_remote_execution: ActiveRecord::Type::Boolean.new.deserialize(params['setup_remote_execution']), @@ -145,6 +145,15 @@ def api_authorization_token User.current.jwt_token!(expiration: 4.hours.to_i, scope: scope) end + def find_object(klass, id = nil, title = nil) + return unless id || title + + permission = "view_#{klass.name.underscore.pluralize}".to_sym + scope = klass.authorized(permission) + + scope.friendly.find(id || title) + end + def default_organization User.current.default_organization || User.current.my_organizations.first end diff --git a/test/controllers/api/v2/registration_controller_test.rb b/test/controllers/api/v2/registration_controller_test.rb index 856a15189c9..19d242bb641 100644 --- a/test/controllers/api/v2/registration_controller_test.rb +++ b/test/controllers/api/v2/registration_controller_test.rb @@ -133,7 +133,12 @@ class Api::V2::RegistrationControllerTest < ActionController::TestCase assert_equal taxonomies(:organization1), assigns(:global_registration_vars)[:organization] end - test 'without organization_id, with user default' do + test 'with title' do + get :global, params: { organization: taxonomies(:organization1).title }, session: set_session_user + assert_equal taxonomies(:organization1), assigns(:global_registration_vars)[:organization] + end + + test 'without id or title, with user default' do user = FactoryBot.create(:user, organizations: orgs, default_organization: orgs[1], admin: true) as_user(user) do @@ -158,7 +163,12 @@ class Api::V2::RegistrationControllerTest < ActionController::TestCase assert_equal taxonomies(:location1), assigns(:global_registration_vars)[:location] end - test 'without location_id, with user default' do + test 'with title' do + get :global, params: { location: taxonomies(:location1).title }, session: set_session_user + assert_equal taxonomies(:location1), assigns(:global_registration_vars)[:location] + end + + test 'without id or title, with user default' do user = FactoryBot.create(:user, locations: locs, default_location: locs[1], admin: true) as_user(user) do @@ -174,6 +184,34 @@ class Api::V2::RegistrationControllerTest < ActionController::TestCase assert_equal user.my_locations.first, assigns(:global_registration_vars)[:location] end end + + context 'find hostgroup' do + let(:hg) { hostgroups(:common) } + + test 'with hostgroup_id' do + get :global, params: { hostgroup_id: hg.id }, session: set_session_user + assert_equal hg, assigns(:global_registration_vars)[:hostgroup] + end + + test 'with title' do + get :global, params: { hostgroup: hg.title }, session: set_session_user + assert_equal hg, assigns(:global_registration_vars)[:hostgroup] + end + end + + context 'find operatingsystem' do + let(:os) { operatingsystems(:redhat) } + + test 'with operatingsystem_id' do + get :global, params: { operatingsystem_id: os.id }, session: set_session_user + assert_equal os, assigns(:global_registration_vars)[:operatingsystem] + end + + test 'with title' do + get :global, params: { operatingsystem: os.title }, session: set_session_user + assert_equal os, assigns(:global_registration_vars)[:operatingsystem] + end + end end describe 'host registration' do