dalen / puppet-puppetdbquery

Query functions for PuppetDB
Apache License 2.0
197 stars 69 forks source link

Support querying for nested facts #87

Closed mcasper closed 8 years ago

mcasper commented 8 years ago

Facter 3 introduced a lot of facts that are large hashes, which gets difficult to work with in Puppet when using query_nodes or query_facts. The types don't seem to coerce just right, so you'll get back a hash of values with the type Runtime[ruby string], which Puppet doesn't know what to do with at the manifest level.

This PR introduces the ability to pass a dotted fact to query for nested facts. You can do something like:

  query_nodes('<node_query>', 'networking.interfaces.eth0.network')

to get back eth0 network for all matching nodes.

It's implemented for both query_nodes and query_facts in this PR.

mcasper commented 8 years ago

/cc @dalen

dalen commented 8 years ago

Hi, sorry about the late reply.

This looks good to me. But could make it use dotted notation that hiera and the query language uses for nested facts instead?

So instead of ['networking', 'interfaces', 'eth0', 'network'] it would be 'networking.interfaces.eth0.network'. That way it would be more in line with the syntax for that in other places.

You can likely just split the string on dots and otherwise use the same code.

mcasper commented 8 years ago

@dalen Sure thing, just pushed up that change. I left it as a separate commit right now so that it's easier to review, can squash if this gets approved

mcasper commented 8 years ago

@dalen bump

dpash commented 8 years ago

I've just been testing this, because it solves an issue that I have. Unfortunately, it seems to have an issue:

jerome root% puppet query facts 'Class["Exim"]'
Error: wrong number of arguments (1 for 2) 
Error: Try 'puppet help query facts' for usage
jerome root% puppet query facts 'Class["Exim"]' 'networking'
Error: puppet query facts takes 1 argument, but you gave 2
Error: Try 'puppet help query facts' for usage

I'd expect the first option to give me all facts for the node and the second to give me just the networking section of the facts.

mcasper commented 8 years ago

@dpash Thanks for reporting this, I've pushed up a fix for the face CLI, with the exception that your second command needs to be tweaked:

puppet query facts 'Class["Exim"]' --facts 'networking'

Other than that, they both should be working now

mcasper commented 8 years ago

@dalen bump

ajvb commented 8 years ago

+1

rnkaufman commented 8 years ago

+1

ajvb commented 8 years ago

bump @dalen