-
Notifications
You must be signed in to change notification settings - Fork 297
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 #38072 - add host bootc_images endpoint #11257
Open
ianballou
wants to merge
5
commits into
Katello:master
Choose a base branch
from
ianballou:38072-bootc-overview-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+118
−0
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7bbefa8
Fixes #38072 - add host bootc_images endpoint
ianballou fb3bd87
Refs #38072 - bootc api pagination data + UI-friendly data re-org
ianballou 723f2e8
Refs #38072 - add host searching to bootc images api
ianballou 8195888
Refs #38072 - select default pagination properly
ianballou 81a551d
Refs #38072 - add sorting support to bootc_image api
ianballou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
app/controllers/katello/api/v2/host_bootc_images_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Katello | ||
class Api::V2::HostBootcImagesController < Api::V2::ApiController | ||
include Katello::Concerns::FilteredAutoCompleteSearch | ||
|
||
resource_description do | ||
api_version 'v2' | ||
api_base_url "/api" | ||
end | ||
|
||
api :GET, "/hosts/bootc_images", N_("List booted bootc container images for hosts") | ||
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(params[:search]) | ||
|
||
page = params[:page].present? ? params[:page].to_i : 1 | ||
per_page = params[:per_page].present? ? params[:per_page].to_i : Setting[:entries_per_page] | ||
paged_images = bootc_image_map.to_a.paginate(page: page, per_page: per_page) | ||
results = paged_images.collect { |image| { image_name: image[0], digests: image[1] } } | ||
render json: { total: bootc_image_map.size, page: page, per_page: per_page, subtotal: bootc_image_map.size, results: results} | ||
end | ||
|
||
private | ||
|
||
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] = [] } | ||
aggregate_bootc_data.each do |host_image| | ||
bootc_image_map[host_image.bootc_booted_image] << { bootc_booted_digest: host_image.bootc_booted_digest, host_count: host_image.host_count.to_i } | ||
end | ||
bootc_image_map | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
test/controllers/api/v2/host_bootc_images_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# encoding: utf-8 | ||
|
||
require "katello_test_helper" | ||
|
||
module Katello | ||
class Api::V2::HostBootcImagesControllerTest < ActionController::TestCase | ||
tests ::Katello::Api::V2::HostBootcImagesController | ||
|
||
def setup | ||
setup_controller_defaults_api | ||
setup_foreman_routes | ||
@host1 = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => katello_content_views(:library_dev_view), | ||
:lifecycle_environment => katello_environments(:library)) | ||
@host2 = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => katello_content_views(:library_dev_view), | ||
:lifecycle_environment => katello_environments(:library)) | ||
@host3 = FactoryBot.create(:host, :with_content, :with_subscription, :content_view => katello_content_views(:library_dev_view), | ||
:lifecycle_environment => katello_environments(:library)) | ||
@host1.content_facet.update!(bootc_booted_image: 'image1') | ||
@host1.content_facet.update!(bootc_booted_digest: 'sha256:dcfb2965cda67bd3731408ace23dd07ff3116168c2b832e16bba8234525724a3') | ||
@host2.content_facet.update!(bootc_booted_image: 'image1') | ||
@host2.content_facet.update!(bootc_booted_digest: 'sha256:dcfb2965cda67bd3731408ace23dd07ff3116168c2b832e16bba8234525724a3') | ||
@host3.content_facet.update!(bootc_booted_image: 'image2') | ||
@host3.content_facet.update!(bootc_booted_digest: 'sha256:dcfb2965cda67bc3731408aae23dd07ff3116168c2b832e16bba8234525724a5') | ||
end | ||
|
||
def test_bootc_images_counts_properly_no_paging | ||
get :bootc_images | ||
assert_response :success | ||
results = JSON.parse(@response.body)['bootc_images'] | ||
assert_includes results, ["image1", [{"bootc_booted_digest" => "sha256:dcfb2965cda67bd3731408ace23dd07ff3116168c2b832e16bba8234525724a3", "host_count" => 2}]] | ||
assert_includes results, ["image2", [{"bootc_booted_digest" => "sha256:dcfb2965cda67bc3731408aae23dd07ff3116168c2b832e16bba8234525724a5", "host_count" => 1}]] | ||
end | ||
|
||
def test_bootc_images_pages | ||
@host2.content_facet.update!(bootc_booted_image: 'image3') | ||
@host2.content_facet.update!(bootc_booted_digest: 'sha256:dcfb2965cda67bd3731408ace93dd07ff3116168c2b832e16bba8234525724c9') | ||
get :bootc_images, params: { page: 1, per_page: 1 } | ||
page1 = @response.body | ||
get :bootc_images, params: { page: 2, per_page: 1 } | ||
page2 = @response.body | ||
get :bootc_images, params: { page: 3, per_page: 1 } | ||
page3 = @response.body | ||
get :bootc_images, params: { page: 4, per_page: 1 } | ||
page4 = @response.body | ||
|
||
assert_equal [["image1", [{"bootc_booted_digest" => "sha256:dcfb2965cda67bd3731408ace23dd07ff3116168c2b832e16bba8234525724a3", "host_count" => 1}]]], JSON.parse(page1)['bootc_images'] | ||
assert_equal [["image2", [{"bootc_booted_digest" => "sha256:dcfb2965cda67bc3731408aae23dd07ff3116168c2b832e16bba8234525724a5", "host_count" => 1}]]], JSON.parse(page2)['bootc_images'] | ||
assert_equal [["image3", [{"bootc_booted_digest" => "sha256:dcfb2965cda67bd3731408ace93dd07ff3116168c2b832e16bba8234525724c9", "host_count" => 1}]]], JSON.parse(page3)['bootc_images'] | ||
assert_empty JSON.parse(page4)['bootc_images'] | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This order would need to be parameterized based on API param.