Skip to content

Fix querying for hosts using puppet class#394

Merged
nadjaheitmann merged 1 commit into
theforeman:masterfrom
bmagistro:host_class_query
Nov 6, 2024
Merged

Fix querying for hosts using puppet class#394
nadjaheitmann merged 1 commit into
theforeman:masterfrom
bmagistro:host_class_query

Conversation

@bmagistro
Copy link
Copy Markdown

When trying to query foreman for hosts that are using a particular puppet class, the query does not return the desired results. In the process of tracking this down we also encountered #386 / #387 .


Given the puppet class, it correctly lists that there are two hosts using that class.
Screenshot 2024-03-30 at 22 32 08

If we follow the link we are presented with the following hosts using the class
Screenshot 2024-03-30 at 22 32 38

This was tracked down to a missing sql join. We got there by raising errors and doing some explains. The queries below were ran directly to help us debug.

foreman=# SELECT "config_groups"."id" FROM "config_groups" INNER JOIN "config_group_classes" ON "config_group_classes"."config_group_id" = "config_groups"."id" INNER JOIN "puppetclasses" ON "puppetclasses"."id" = "config_group_classes"."puppetclass_id" WHERE (puppetclasses.name = 'profiles::lanes::protected') ORDER BY config_groups.name;
 id 
----
 10
(1 row)


foreman=# SELECT "host_config_groups"."host_id" FROM "host_config_groups" WHERE "host_config_groups"."host_type" = 'ForemanPuppet::HostPuppetFacet' AND "host_config_groups"."config_group_id" = 10;
 host_id 
---------
     270
     183
(2 rows)

The above host_ids are actually host facet ids so the it tries to query these as host ids resulting in the mismatch.
Adding the below sql before running the query for hosts we get to what we expect

foreman=# select * from host_puppet_facets where id = 270 or id = 183;
 id  | host_id | environment_id | puppet_proxy_id | created_at |         updated_at         
-----+---------+----------------+-----------------+------------+----------------------------
 270 |     430 |              1 |                 |            | 
 183 |     460 |              1 |                 |            | 2022-07-03 18:29:34.534469
(2 rows)

foreman=# select id, name from hosts where id = 430 or id = 460;
 id  |                    name                    
-----+--------------------------------------------
 460 | trust-0002.dev.***
 430 | trust-0001.dev.***
(2 rows)

Using the changed code that is in the PR, and performing the same query we get the desired results
Screenshot 2024-03-31 at 21 40 05

@stejskalleos
Copy link
Copy Markdown
Contributor

@bmagistro Thanks for the contribution, can you please fix the rubocop findings?

@stejskalleos
Copy link
Copy Markdown
Contributor

Failing tests are not related to the changes in this PR.

Comment thread app/models/concerns/foreman_puppet/extensions/host.rb Outdated
Copy link
Copy Markdown
Collaborator

@nadjaheitmann nadjaheitmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bmagistro Can you please squash your commits?

Co-authored-by: Ewoud Kohl van Wijngaarden <ewoud@kohlvanwijngaarden.nl>
Signed-off-by: Ben Magistro <koncept1@gmail.com>
@neomilium
Copy link
Copy Markdown

@nadjaheitmann Sounds good, commits are now squashed. Would you merge it?

@nadjaheitmann nadjaheitmann merged commit 01ffd20 into theforeman:master Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants