Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37917 - Search by title for registration parameters #10351

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions app/controllers/api/v2/registration_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
Expand Down
26 changes: 18 additions & 8 deletions app/controllers/concerns/foreman/controller/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || ''
Expand All @@ -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']),
Expand Down Expand Up @@ -144,6 +144,16 @@ def api_authorization_token
User.current.jwt_token!(expiration: 4.hours.to_i, scope: scope)
end

def find_object(klass, id = nil, title = nil)
permission = "view_#{klass.name.underscore.pluralize}".to_sym
scope = klass.authorized(permission)

obj ||= scope.find(id) if id.present?
obj ||= scope.find_by(title: title) if title.present?

obj
end

def default_organization
User.current.default_organization || User.current.my_organizations.first
end
Expand Down
42 changes: 40 additions & 2 deletions test/controllers/api/v2/registration_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,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
Expand All @@ -156,7 +161,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
Expand All @@ -172,6 +182,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
Expand Down
Loading