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 |
# 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] results = 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? next unless data.include?(key) Hiera.debug("Found #{key} in #{source}") new_answer = Backend.parse_answer(data[key], scope) results = query(new_answer) end return results end |
#query(query) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/hiera/backend/postgres_backend.rb', line 51 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 = PG::Connection.new(:host => pg_host, :user => pg_user, :password => pg_pass, :dbname => pg_database) begin data = client.exec(query).to_a Hiera.debug("PostgreSQL Query returned #{data.size} rows") rescue => e Hiera.debug e. data = nil ensure client.close end return data end |