From 29a8e15550813b67bdb3bfdf9a4390f9eb3e5580 Mon Sep 17 00:00:00 2001 From: Ian Ballou Date: Tue, 17 Dec 2024 20:19:56 +0000 Subject: [PATCH] Refs #38072 - add host searching to bootc images api --- .../api/v2/host_bootc_images_controller.rb | 21 ++++++++++++++++--- config/routes/api/v2.rb | 4 ++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/controllers/katello/api/v2/host_bootc_images_controller.rb b/app/controllers/katello/api/v2/host_bootc_images_controller.rb index 545e511bc7b..ddf47e02dae 100644 --- a/app/controllers/katello/api/v2/host_bootc_images_controller.rb +++ b/app/controllers/katello/api/v2/host_bootc_images_controller.rb @@ -1,5 +1,7 @@ module Katello class Api::V2::HostBootcImagesController < Api::V2::ApiController + include Katello::Concerns::FilteredAutoCompleteSearch + resource_description do api_version 'v2' api_base_url "/api" @@ -9,7 +11,8 @@ class Api::V2::HostBootcImagesController < Api::V2::ApiController param :page, :number, :desc => N_("Page number, starting at 1") param :per_page, :number, :desc => N_("Number of results per page to return") def bootc_images - bootc_image_map = bootc_host_image_map + bootc_image_map = bootc_host_image_map(params[:search]) + page = params[:page].to_i || 1 per_page = params[:per_page].to_i || Setting[:entries_per_page] paged_images = bootc_image_map.to_a.paginate(page: page, per_page: per_page) @@ -19,8 +22,20 @@ def bootc_images private - def bootc_host_image_map - aggregate_bootc_data = ::Katello::Host::ContentFacet.where.not(bootc_booted_image: nil, bootc_booted_digest: nil). + def index_relation + query = resource_class.authorized(:view_hosts).distinct + query.joins(:content_facet).where.not(bootc_booted_image: nil, bootc_booted_digest: nil) + query + end + + def resource_class + ::Host::Managed + end + + def bootc_host_image_map(host_search) + # Todo: can this be optimized such that it doesn't require two queries? + content_facets = ::Katello::Host::ContentFacet.where(host_id: ::Host::Managed.joins(:content_facet).search_for(host_search).pluck(:id)) + aggregate_bootc_data = content_facets.where.not(bootc_booted_image: nil, bootc_booted_digest: nil). select(:bootc_booted_image, :bootc_booted_digest, 'COUNT(hosts.id) as host_count'). joins(:host).group(:bootc_booted_image, :bootc_booted_digest).order(:bootc_booted_image) bootc_image_map = Hash.new { |h, k| h[k] = [] } diff --git a/config/routes/api/v2.rb b/config/routes/api/v2.rb index c6c894a76fe..e945334f019 100644 --- a/config/routes/api/v2.rb +++ b/config/routes/api/v2.rb @@ -30,6 +30,10 @@ class ActionDispatch::Routing::Mapper end end + api_resources :host_bootc_images, :only => [:bootc_images] do + get :auto_complete_search, :on => :collection + end + api_resources :capsules, :only => [:index, :show] do member do resource :content, :only => [], :controller => 'capsule_content' do