Skip to content

Instantly share code, notes, and snippets.

@ody
Created January 29, 2020 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ody/a1ac265252b360116652227c047ec664 to your computer and use it in GitHub Desktop.
Save ody/a1ac265252b360116652227c047ec664 to your computer and use it in GitHub Desktop.
# /etc/puppetlabs/code/environments/production/modules/mymodule/lib/puppet/functions/mymodule/upcase.rb
require 'cassandra'
require 'json'
require 'yaml'
Puppet::Functions.create_function(:'puppet_metadata_service::data_hash') do
dispatch :hiera_data do
param 'Hash', :options
param 'Puppet::LookupContext', :context
end
def hiera_data(options, context)
uri = options['uri']
context.explain { "[puppet_metadata_service::data_hash] Creating connection to #{Socket.gethostname}" }
cluster = Cassandra.cluster(hosts: [Socket.gethostname])
session = cluster.connect('puppet')
context.explain { "[puppet_metadata_service::data_hash] Loading hierarchy: #{uri} from #{Socket.gethostname}" }
data = session.execute(
"SELECT key,value FROM hieradata where level=%s" % "$$#{uri}$$").rows.map { |row|
{ row['key'] => row['value'] }
}.reduce({}, :merge
)
if data.empty?
context.not_found
else
return data
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment