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 |
# 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.empty? Puppet.debug "Could not find capability resource #{cap} in PuppetDB" elsif resources.size == 1 resource_hash = resources.first elsif code_id_resource = disambiguate_by_code_id(environment, code_id, cap) resource_hash = code_id_resource else 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 resource_hash['type'] = cap.resource_type instantiate_resource(resource_hash) 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.
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 |
# File 'lib/puppet/resource/capability_finder.rb', line 80 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=#{Puppet::Util.uri_query_encode(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 # https://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.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/puppet/resource/capability_finder.rb', line 55 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}") % { cap: cap, query_terms: query_terms } query_puppetdb(query_terms) end |