Class: Hiera::Backend::Postgres_backend
- Inherits:
-
Object
- Object
- Hiera::Backend::Postgres_backend
- Defined in:
- lib/hiera/backend/postgres_backend.rb
Instance Method Summary collapse
-
#initialize(cache = nil) ⇒ Postgres_backend
constructor
A new instance of Postgres_backend.
- #lookup(key, scope, order_override, resolution_type) ⇒ Object
- #query(query) ⇒ Object
Constructor Details
#initialize(cache = nil) ⇒ Postgres_backend
Returns a new instance of Postgres_backend.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/hiera/backend/postgres_backend.rb', line 5 def initialize(cache=nil) begin require 'pg' rescue LoadError require 'rubygems' require 'pg' end @cache = cache || Filecache.new Hiera.debug("Hiera PostgreSQL initialized") end |
Instance Method Details
#lookup(key, scope, order_override, resolution_type) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hiera/backend/postgres_backend.rb', line 18 def lookup(key, scope, order_override, resolution_type) # default answer is set to nil otherwise the lookup ends up returning # an Array of nils and causing a Puppet::Parser::AST::Resource failed with error ArgumentError # for any other lookup because their default value is overwriten by [nil,nil,nil,nil] # so hiera('myvalue', 'test1') returns [nil,nil,nil,nil] answer = nil Hiera.debug("looking up #{key} in PostgreSQL Backend") Hiera.debug("resolution type is #{resolution_type}") Backend.datasources(scope, order_override) do |source| Hiera.debug("Looking for data source #{source}") sqlfile = Backend.datafile(:postgres, scope, source, "sql") || next next unless File.exist?(sqlfile) data = @cache.read(sqlfile, Hash, {}) do |datafile| YAML.load(datafile) end Hiera.debug("data #{data.inspect}") next if data.empty? if data.include?(key) Hiera.debug("Found #{key} in #{source}") new_answer = Backend.parse_answer(data[key], scope, { 'key' => key } ) elsif data.include?('default_query') Hiera.debug("Found default_query in #{source}") new_answer = Backend.parse_answer(data['default_query'], scope, { 'key' => key } ) else next end case resolution_type when :array raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String answer ||= [] answer << query(new_answer) when :hash raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash answer ||= {} answer = Backend.merge_answer(query(new_answer),answer) else answer = query(new_answer) if answer.length == 0 answer = nil next end break end end unless answer.kind_of? String if answer==nil or answer.empty? answer = nil end end return answer end |
#query(query) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/hiera/backend/postgres_backend.rb', line 77 def query(query) Hiera.debug("Executing SQL Query: #{query}") data=nil pg_host = Config[:postgres][:host] pg_user = Config[:postgres][:user] pg_pass = Config[:postgres][:pass] pg_database = Config[:postgres][:database] client = PGconn.open(:host => pg_host, :user => pg_user, :password => pg_pass, :dbname => pg_database) begin rows=client.exec(query) if rows.fields().count==1 and rows.count()==1 data=rows[0][rows.fields()[0]] elsif rows.fields().count==1 and rows.count()>1 ary=Array.new rows.each { |row| ary.push(row[rows.fields()[0]]) } data=ary else data = rows.to_a end Hiera.debug("returned rows " + rows.count().to_s + " columns " + rows.fields.count().to_s) rescue => e Hiera.debug e. data = nil ensure client.close end return data end |