Module: Puppet::Resource::CapabilityFinder Private
- Defined in:
- lib/puppet/resource/capability_finder.rb
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Class Method Summary collapse
-
.find(environment, code_id, cap) ⇒ Puppet::Resource?
private
Looks up a capability resource from PuppetDB.
- .query_puppetdb(query) ⇒ Object private
- .search(environment, code_id, cap) ⇒ Object private
Class Method Details
.find(environment, code_id, cap) ⇒ Puppet::Resource?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Looks up a capability resource from PuppetDB. Capability resources are required to be unique per environment and code id. If multiple copies of a capability resource are found, the one matching the current code id is used.
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 |
# File 'lib/puppet/resource/capability_finder.rb', line 24 def self.find(environment, code_id, cap) unless Puppet::Util.const_defined?('Puppetdb') raise Puppet::DevError, 'PuppetDB is not available' end resources = search(nil, nil, cap) if resources.size > 1 Puppet.debug "Found multiple resources when looking up capability #{cap}, filtering by environment #{environment}" resources = resources.select { |r| r['tags'].any? { |t| t == "producer:#{environment}" } } end if resources.size > 1 && code_id Puppet.debug "Found multiple resources when looking up capability #{cap}, filtering by code id #{code_id}" resources = search(environment, code_id, cap) end if resources.size > 1 raise Puppet::DevError, "Unexpected response from PuppetDB when looking up #{cap}:\n" \ "expected exactly one resource but got #{resources.size};\n" \ "returned data is:\n#{resources.inspect}" end if resource_hash = resources.first resource_hash['type'] = cap.resource_type instantiate_resource(resource_hash) else Puppet.debug "Could not find capability resource #{cap} in PuppetDB" nil end end |
.query_puppetdb(query) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
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/puppet/resource/capability_finder.rb', line 82 def self.query_puppetdb(query) begin # If using PuppetDB >= 4, use the API method query_puppetdb() result = if Puppet::Util::Puppetdb.respond_to?(:query_puppetdb) # PuppetDB 4 uses a unified query endpoint, so we have to specify what we're querying Puppet::Util::Puppetdb.query_puppetdb(["from", "resources", query]) # For PuppetDB < 4, use the old internal method action() else url = "/pdb/query/v4/resource?query=#{CGI.escape(query.to_json)}" response = Puppet::Util::Puppetdb::Http.action(url) do |conn, uri| conn.get(uri, { 'Accept' => 'application/json'}) end JSON.parse(response.body) end # The format of the response body is documented at # http://docs.puppetlabs.com/puppetdb/3.0/api/query/v4/resources.html#response-format unless result.is_a?(Array) raise Puppet::DevError, "Unexpected response from PuppetDB when looking up #{cap}: " \ "expected an Array but got #{result.inspect}" end result rescue JSON::JSONError => e raise Puppet::DevError, "Invalid JSON from PuppetDB when looking up #{cap}\n#{e}" end end |
.search(environment, code_id, cap) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/puppet/resource/capability_finder.rb', line 57 def self.search(environment, code_id, cap) query_terms = [ 'and', ['=', 'type', cap.type.capitalize], ['=', 'title', cap.title.to_s], ] if environment.nil? query_terms << ['~', 'tag', "^producer:"] else query_terms << ['=', 'tag', "producer:#{environment}"] end unless code_id.nil? query_terms << ['in', 'certname', ['extract', 'certname', ['select_catalogs', ['=', 'code_id', code_id]]]] end Puppet.notice "Looking up capability #{cap} in PuppetDB: #{query_terms}" query_puppetdb(query_terms) end |