Skip to content

Instantly share code, notes, and snippets.

@kostecky
Created December 10, 2015 00:58
Show Gist options
  • Save kostecky/b58c5829218cb42ae67f to your computer and use it in GitHub Desktop.
Save kostecky/b58c5829218cb42ae67f to your computer and use it in GitHub Desktop.
Puppet custom function to return information about a resource_type as a puppet data structure
run_drequire 'puppet/parser/functions'
Puppet::Parser::Functions.newfunction(:get_resource_info,
:type => :rvalue,
:doc => <<-'ENDOFDOC'
Takes a resource name and returns a puppet structure of information about that resource
as documented in https://docs.puppetlabs.com/references/3.6.2/man/resource_type.html and
http://docs.puppetlabs.com/puppet/latest/reference/http_api/http_resource_type.html
This function relies on a puppet face to parse the class definitions in the module paths as
they may not be available in the execution context of the catalog compile.
This funtion relies on the puppetlabs-stdlib module to be installed as it leverages parsejson()
*Examples:*
# Get info about a class called 'nginx'
get_resource_info('dnsmasq')
Would return: {
"line": 111,
"parent": "::dnsmasq::params",
"parameters": {
"config_dir": "$::dnsmasq::params::config_dir",
"config_file": "$::dnsmasq::params::config_file",
"config_template": "$::dnsmasq::params::config_template",
"ethers_file": "$::dnsmasq::params::ethers_file",
"exported": "$::dnsmasq::params::exported",
"hosts_file": "$::dnsmasq::params::hosts_file",
"package_name": "$::dnsmasq::params::package_name",
"purge": "$::dnsmasq::params::purge",
"resolv_file": "$::dnsmasq::params::resolv_file",
"service_name": "$::dnsmasq::params::service_name"
},
"name": "dnsmasq",
"kind": "class"
}
ENDOFDOC
) do |vals|
resource_name = vals[0]
raise(ArgumentError, 'Must specify a resource_name') unless resource_name
module_path = lookupvar("::settings::modulepath")
resource_info = `/opt/puppetlabs/bin/puppet resource_type find '#{resource_name}' --modulepath=#{module_path}`
resource_struct = function_parsejson([resource_info])
return resource_struct
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment