Skip to content

Instantly share code, notes, and snippets.

@awaxa
Last active August 29, 2015 14:19
Show Gist options
  • Save awaxa/4890f40b32fd44d3052b to your computer and use it in GitHub Desktop.
Save awaxa/4890f40b32fd44d3052b to your computer and use it in GitHub Desktop.
require 'puppetclassify'
module Puppet::Parser::Functions
newfunction(:generate_hiera_bindings, :type => :rvalue, :arity => 1, :doc => <<-EOS
it does stuff
EOS
) do |args|
classifier_url = args[0]
raise ArgumentError, "invalid classifier URI" unless classifier_url =~ URI.regexp('https')
auth_info = {
'ca_certificate_path' => Puppet[:localcacert],
'certificate_path' => Puppet[:hostcert],
'private_key_path' => Puppet[:hostprivkey],
}
puppetclassify = PuppetClassify.new(classifier_url, auth_info)
classes = puppetclassify.classes.get_classes.reject do |klass|
klass['parameters'].empty?
end
response = {}
classes.each do |klass|
klass['parameters'].each do |param,val|
key = "#{klass['name']}::#{param}"
case val
when nil # skip params without defaults
next
when 'undef'
next
when /[a-z]+\(.*\)/ # skip function calls
next
when /^\$/ # defer interpolation
val[0] = ''
response[key] = "%{#{val}}"
else
response[key] = val
end
end
end
response
end
end
$foo = generate_hiera_bindings("https://master.inf.puppetlabs.demo:4433/classifier-api")
file { '/data.yaml':
content => inline_template("<%= @foo.to_yaml %>"),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment