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') #TRANSLATOR PuppetDB is a product name and should not be translated 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 #TRANSLATOR PuppetDB is a product name and should not be translated = _("Unexpected response from PuppetDB when looking up %{capability}:") % { capability: cap } += "\n" + _("expected exactly one resource but got %{count};") % { count: resources.size } += "\n" + _("returned data is:\n%{resources}") % { resources: resources.inspect } raise Puppet::DevError, 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.
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 111 |
# File 'lib/puppet/resource/capability_finder.rb', line 83 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 Puppet::Util::Json.load(response.body) end # The format of the response body is documented at # https://puppet.com/docs/puppetdb/3.0/api/query/v4/resources.html#response-format unless result.is_a?(Array) #TRANSLATOR PuppetDB is a product name and should not be translated raise Puppet::DevError, _("Unexpected response from PuppetDB when looking up %{capability}: expected an Array but got %{result}") % { capability: cap, result: result.inspect } end result rescue Puppet::Util::Json::ParseError => e #TRANSLATOR PuppetDB is a product name and should not be translated raise Puppet::DevError, _("Invalid JSON from PuppetDB when looking up %{capability}\n%{detail}") % { capability: cap, detail: 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 81 |
# 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 #TRANSLATOR PuppetDB is a product name and should not be translated Puppet.notice _("Looking up capability %{capability} in PuppetDB: %{query_terms}") % { capability: cap, query_terms: query_terms } query_puppetdb(query_terms) end |