Skip to content

Commit

Permalink
Fixes #37683 - Add product counting on a per host basis (Katello#11091)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadjaheitmann authored Aug 20, 2024
1 parent 28e0a4e commit 42f890d
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 13 deletions.
24 changes: 15 additions & 9 deletions app/controllers/katello/api/v2/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ def index
format.csv do
options[:csv] = true
collection = scoped_search(*base_args, options)
csv_response(collection,
[:id, :subscription_id, :name, :cp_id, :organization_id, :sockets, :cores,
:start_date, :end_date, :consumed, :quantity, :account_number, :contract_number,
:support_level, :ram, :stacking_id, :multi_entitlement, :type, :product_id,
:unmapped_guest, :virt_only, :virt_who, :upstream?],
['Pool Id Number', 'Subscription Id', 'Name', 'Pool Id', 'Organization Id',
'Sockets', 'Cores', 'Start Date', 'End Date', 'Consumed', 'Quantity', 'Account Number',
'Contract Number', 'Support Level', 'RAM', 'Stacking Id', 'Multi Entitlement', 'Type',
'Product Id', 'Unmapped Guest', 'Virt Only', 'Requires Virt Who', 'Upstream'])
fields = [:id, :subscription_id, :name, :cp_id, :organization_id, :sockets, :cores,
:start_date, :end_date, :consumed, :quantity, :account_number, :contract_number,
:support_level, :ram, :stacking_id, :multi_entitlement, :type, :product_id,
:unmapped_guest, :virt_only, :virt_who, :upstream?, :product_host_count]
headers = ['Pool Id Number', 'Subscription Id', 'Name', 'Pool Id', 'Organization Id',
'Sockets', 'Cores', 'Start Date', 'End Date', 'Consumed', 'Quantity', 'Account Number',
'Contract Number', 'Support Level', 'RAM', 'Stacking Id', 'Multi Entitlement', 'Type',
'Product Id', 'Unmapped Guest', 'Virt Only', 'Requires Virt Who', 'Upstream', 'Product Host Count']
csv_response(collection, fields, headers)
end

format.any do
collection = scoped_search(*base_args, options)
if params[:activation_key_id]
Expand All @@ -59,6 +60,9 @@ def index
ActivationKeySubscriptionsPresenter.new(pool, key_pools)
end
end
collection[:results] = collection[:results].map do |pool|
ProductHostCountPresenter.new(pool)
end
respond(:collection => collection)
end
end
Expand Down Expand Up @@ -88,6 +92,8 @@ def show
fail HttpErrors::BadRequest, N_('This subscription is not relevant to the current organization.')
end

@resource = ProductHostCountPresenter.new(@resource)

respond(:resource => @resource)
end

Expand Down
6 changes: 5 additions & 1 deletion app/models/katello/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Pool < Katello::Model

validate :subscription_matches_organization

def product_host_count
Katello::Host::ContentFacet.joins(bound_repositories: { product: :pools }).where(pools: { id: id }).distinct.count
end

def subscription_matches_organization
return if errors[:subscription].any? || errors[:organization].any? # let other validations catch this
unless subscription&.organization_id == self.organization_id
Expand Down Expand Up @@ -147,7 +151,7 @@ def default_sort
property :days_until_expiration, Integer, desc: 'Returns number of days until expiration'
end
class Jail < ::Safemode::Jail
allow :id, :name, :available, :quantity, :product_id, :contract_number, :type, :account_number, :start_date, :end_date, :organization, :consumed, :days_until_expiration
allow :id, :name, :available, :quantity, :product_id, :contract_number, :type, :account_number, :start_date, :end_date, :organization, :consumed, :days_until_expiration, :product_host_count
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions app/presenters/katello/product_host_count_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Katello
class ProductHostCountPresenter < SimpleDelegator
attr_reader :product_host_count

def initialize(pool)
@product_host_count = pool.product_host_count
super(pool)
end
end
end
1 change: 1 addition & 0 deletions app/views/katello/api/v2/subscriptions/index.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ extends 'katello/api/v2/subscriptions/permissions'

child @collection[:results] => :results do
attributes :quantity_attached
attributes :product_host_count
extends "katello/api/v2/subscriptions/base"
end
1 change: 1 addition & 0 deletions app/views/katello/api/v2/subscriptions/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ attributes :arch
attributes :description
attributes :support_type
attributes :roles, :usage, :addons
attributes :product_host_count

node(:host_count) do |subscription|
subscription.hosts.count
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/api/v2/products_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_index_custom_products_only
get :index, params: { organization_id: @organization.id, custom: true }
body = JSON.parse(response.body)

assert_equal 7, body['total']
assert_equal Katello::Product.custom.count, body['total']
end

def test_index_no_custom_products
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/models/katello_products.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,10 @@ oracle:
cp_id: 123123123006
provider_id: <%= ActiveRecord::FixtureSet.identify(:anonymous) %>
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>

product_host_count:
name: product_host_count
description: A dummy product for pool tests
label: product_host_count_label
provider_id: <%= ActiveRecord::FixtureSet.identify(:anonymous) %>
organization_id: <%= ActiveRecord::FixtureSet.identify(:empty_organization) %>
16 changes: 15 additions & 1 deletion test/fixtures/models/katello_repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,18 @@ pulp3_ostree_1:
pulp_id: "Default_Organization-Cabinet-pulp3_OSTree_1"
relative_path: 'Default_Organization/library/pulp3_OSTree_1'
environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_default_version) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_default_version) %>

product_host_count_repo1:
root_id: <%= ActiveRecord::FixtureSet.identify(:product_host_count_repo1_root) %>
pulp_id: "default_organization-my-product-host-count-repo1"
relative_path: 'default_organization/library/my-product-host-count-repo1'
environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_default_version) %>

product_host_count_repo2:
root_id: <%= ActiveRecord::FixtureSet.identify(:product_host_count_repo2_root) %>
pulp_id: "default_organization-my-product-host-count-repo2"
relative_path: 'default_organization/library/my-product-host-count-repo2'
environment_id: <%= ActiveRecord::FixtureSet.identify(:library) %>
content_view_version_id: <%= ActiveRecord::FixtureSet.identify(:library_default_version) %>
21 changes: 21 additions & 0 deletions test/fixtures/models/katello_root_repositories.yml
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,24 @@ pulp3_ostree_root_1:
unprotected: <%= true %>
url: ""
mirroring_policy: "mirror_content_only"

product_host_count_repo1_root:
name: product host count repo 1
content_id: 34
content_type: yum
label: product_host_count_repo_1__label
product_id: <%= ActiveRecord::FixtureSet.identify(:product_host_count) %>
url: "https://product-host-count-repo1.com"
download_policy: on_demand
mirroring_policy: "mirror_content_only"

product_host_count_repo2_root:
name: product host count repo 2
content_id: 35
content_type: yum
label: product_host_count_repo_2__label
product_id: <%= ActiveRecord::FixtureSet.identify(:product_host_count) %>
url: "https://product-host-count-repo2.com"
download_policy: on_demand
mirroring_policy: "mirror_content_only"

2 changes: 1 addition & 1 deletion test/lib/tasks/check_candlepin_content_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_candlepin_check_with_bad_ping

def test_candlepin_content_check_with_missing_repos
Katello::Ping.expects(:ping).returns(:status => 'ok')
Katello::Util::CandlepinRepositoryChecker.expects(:repository_exist_in_backend?).at_most(12)
Katello::Util::CandlepinRepositoryChecker.expects(:repository_exist_in_backend?).at_most(14)
Rake.application.invoke_task('katello:check_candlepin_content')
end
end
Expand Down
21 changes: 21 additions & 0 deletions test/models/pool_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,26 @@ def test_audit_hook_to_find_records_should_return_hosts

assert_equal pool_host_ids.length, hosts_list.keys.length
end

def test_product_host_count
product1 = katello_products(:product_host_count)
product1.pools << @pool_one
product1.pools << @pool_two
repo1 = katello_repositories(:product_host_count_repo1)
repo2 = katello_repositories(:product_host_count_repo2)
repo1.product = product1
repo2.product = product1
content_facet1 = katello_content_facets(:content_facet_one)
content_facet2 = katello_content_facets(:content_facet_two)

# test if both hosts are counted correctly
content_facet1.bound_repositories = [repo1]
content_facet2.bound_repositories = [repo1]
assert_equal 2, @pool_one.product_host_count

# test if multiple repositories in one product are counted only as one
content_facet2.bound_repositories = [repo1, repo2]
assert_equal 2, @pool_one.product_host_count
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export default {
type: __('Type'),
multi_entitlement: __('Multi-entitlement'),
stacking_id: __('Stacking ID'),
product_host_count: __('Product Host Count'),
};
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ exports[`subscriptions detail associations page renders correctly 1`] = `
SER0421
</td>
</tr>
<tr>
<td>
<b>
Product Host Count
</b>
</td>
<td>
</td>
</tr>
<tr>
<td>
<b>
Expand Down
6 changes: 6 additions & 0 deletions webpack/scenes/Subscriptions/SubscriptionConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export const SUBSCRIPTION_TABLE_COLUMNS = [
label: __('Entitlements'),
value: false,
},
{
key: 'product_host_count',
label: __('Product Host Count'),
value: false,
},
];

export const SUBSCRIPTION_TABLE_DEFAULT_COLUMNS = [
Expand All @@ -114,4 +119,5 @@ export const SUBSCRIPTION_TABLE_DEFAULT_COLUMNS = [
'consumed',
'quantity',
'type',
'product_host_count',
];
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ exports[`subscriptions page should render 1`] = `
"instance_multiplier": 1,
"multi_entitlement": null,
"name": "zoo",
"product_host_count": 1,
"product_id": "853987721546",
"product_name": "zoo",
"quantity": -1,
Expand All @@ -143,6 +144,7 @@ exports[`subscriptions page should render 1`] = `
"instance_multiplier": 1,
"multi_entitlement": null,
"name": "hsdfhsdh",
"product_host_count": 0,
"product_id": "947637693017",
"product_name": "hsdfhsdh",
"quantity": -1,
Expand Down
14 changes: 14 additions & 0 deletions webpack/scenes/Subscriptions/__tests__/subscriptions.fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const requestSuccessResponse = Immutable({
unmapped_guest: false,
virt_only: false,
virt_who: false,
product_host_count: 1,
},
{
id: 4,
Expand All @@ -89,6 +90,7 @@ export const requestSuccessResponse = Immutable({
unmapped_guest: false,
virt_only: false,
virt_who: false,
product_host_count: 0,
},
],
});
Expand All @@ -110,6 +112,7 @@ export const quantitiesRequestSuccessResponse = Immutable({
4,
5,
],
product_host_count: 9469,
},
{
id: '6b123381519abf020151ab082c5e4678',
Expand All @@ -125,6 +128,7 @@ export const quantitiesRequestSuccessResponse = Immutable({
local_pool_ids: [
6,
],
product_host_count: 9469,
},
],
page: 1,
Expand Down Expand Up @@ -167,6 +171,7 @@ export const groupedSubscriptions = Immutable({
unmapped_guest: false,
virt_only: false,
virt_who: false,
product_host_count: 1,
},
{
id: 4,
Expand All @@ -193,6 +198,7 @@ export const groupedSubscriptions = Immutable({
unmapped_guest: false,
virt_only: false,
virt_who: false,
product_host_count: 0,
},
],
searchIsActive: false,
Expand Down Expand Up @@ -236,6 +242,7 @@ export const successState = Immutable({
unmapped_guest: false,
virt_only: false,
virt_who: false,
product_host_count: 1,
},
{
id: 4,
Expand All @@ -262,6 +269,7 @@ export const successState = Immutable({
unmapped_guest: false,
virt_only: false,
virt_who: false,
product_host_count: 0,
},
],
searchIsActive: false,
Expand Down Expand Up @@ -454,6 +462,11 @@ export const tableColumns = [
label: 'Entitlements',
value: true,
},
{
key: 'product_host_count',
label: 'Product Host Count',
value: true,
},
];

export const loadTableColumnsSuccessAction = [
Expand All @@ -470,6 +483,7 @@ export const loadTableColumnsSuccessAction = [
'consumed',
'quantity',
'type',
'product_host_count',
],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const buildTableCollapseRow = (subscriptionGroup) => {
name: first.name,
virt_only: first.virt_only,
hypervisor: first.hypervisor,
product_host_count: 'NA',
};
return heading;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,14 @@ export const createSubscriptionsTableSchema = (
formatters: [getEntitlementsFormatter(inlineEditController, hasPermission)],
},
},
{
property: 'product_host_count',
header: {
label: __('Product Host Count'),
formatters: [headerFormatter],
},
cell: {
formatters: [cellFormatter],
},
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const genericRow = Immutable({
product_id: 'RH00001',
start_date: 'NA',
virt_only: undefined,
product_host_count: 'NA',
});

export const subOneRowOne = Immutable({
Expand Down

0 comments on commit 42f890d

Please sign in to comment.