Created
January 29, 2020 19:11
-
-
Save ody/a1ac265252b360116652227c047ec664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /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