ruby-puppetdb 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YmEzMGI0YWE2YjMyNmE5YmJlZTM0ZTEzNWU5NzVkYTk0MWQ5NjAyZg==
4
+ YjJmYWQ1NDc2ZDc4MGM4OGU1ZDhiZWFlNjI4NjdhZjJlNDMxNzFlYQ==
5
5
  data.tar.gz: !binary |-
6
- NWVjOGNiOGM2Y2EzOWFiODRhNDQ2MTliZjBjZDUxNmQ4NTMyNDg5MQ==
6
+ MGJlNTZhNjVhNDI5OTY4YmMxNWUwNzYwN2VlM2MxYTNlOGFhZDIxZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmE1MGQ2N2ZiNDUwODg5ZTJkOTEzMzczYWUzNjg0OTU3NDc2ZjE4MGU3MGMz
10
- OGQ0YTY0OTIxYWI4ZWMzMTcwMmIxYTJlZmNjOWIzMzZkMTNmNTFiZWRiYzQ5
11
- MTBhNmIxNDg1M2VkMGY2MzQ1YTc5NDQyM2EwNDA2MDExOWNiNGE=
9
+ NjlmZDlmOWQyNzUyODNkZTdlN2U2MWEzMWQyMzFlYmIxMjY5Y2IwY2MyMzcz
10
+ ZWZiN2RlNzJjNmUwMzgyZmRiZDI5M2IxNGUzNDZlYzBjZTg5MjRhMTkzMjY5
11
+ OTdiZmQwNGRhNzRkMzI2MzlmYzZiZTRjZmFlZmZkMjU5MjEyNjQ=
12
12
  data.tar.gz: !binary |-
13
- ZjE2YmViNDU3NDYyM2FlYzMwNzE3MDlmZDcyYjA3ZTc4NmVhNWEwNDU3YTRm
14
- Zjk2NjY1ZjFmYzg3YjYyYzljZGMwYmVmMWQ2Njk0NjgzMzFmY2QxNDg3ZGY1
15
- ZjkwZTJkY2U0ZGVlZjg3NWI0OTAwNmQzZjQ1ZWM1MzYzNmQyODU=
13
+ N2EzMjFkMTU3ZmRlNGQ3YjM0MDJiN2E4YmQ0NmNmZjU3NjE2ZDU4ZTAyNzhh
14
+ YjNkNDI3ZWM4YzY3M2Y5MmI2ZWFmMzU2NjE3ZmIzYTJiZTM5YTUxMGExMjU4
15
+ OTJlZTZkMDBkYWJiMTNhNzBlYTgyZTc0YWM4Njc4NjYxMmFlODk=
data/bin/find-nodes CHANGED
@@ -61,12 +61,12 @@ if options[:print]
61
61
  puts query.to_json
62
62
  elsif options[:facts]
63
63
  query = parser.facts_query(options[:query], options[:facts])
64
- parser.facts_hash(puppetdb.query(:facts, query, http)).each_value do |host|
64
+ parser.facts_hash(puppetdb.query(:facts, query, :extract => [:name, :value, :certname], :http => http)).each_value do |host|
65
65
  print options[:facts].collect { |f| host[f] if host.include? f }.join(',') + "\n"
66
66
  end
67
67
  else
68
68
  query = parser.parse(options[:query])
69
- results = puppetdb.query(:nodes, query, http)
69
+ results = puppetdb.query(:nodes, query, :extract => :certname, :http => http)
70
70
  hosts = results.collect { |host| host['certname'] }
71
71
  hosts.each { |host| print host + "\n" }
72
72
  end
@@ -20,9 +20,9 @@ class Puppet::Application::Query < Puppet::Application::FaceBase
20
20
  Puppet.debug(port)
21
21
  Puppet.debug("use_ssl=#{use_ssl}")
22
22
 
23
- { host: host,
24
- port: port,
25
- use_ssl: use_ssl
23
+ { :host => host,
24
+ :port => port,
25
+ :use_ssl => use_ssl
26
26
  }
27
27
  end
28
28
  end
@@ -52,7 +52,7 @@ Puppet::Face.define(:query, '1.0.0') do
52
52
  else
53
53
  factquery = parser.parse(query, :facts)
54
54
  end
55
- parser.facts_hash(puppetdb.query(:facts, factquery))
55
+ parser.facts_hash(puppetdb.query(:facts, factquery, :extract => [:certname, :name, :value]))
56
56
  end
57
57
  end
58
58
 
@@ -75,11 +75,13 @@ Puppet::Face.define(:query, '1.0.0') do
75
75
  when_invoked do |query, options|
76
76
  puppetdb = PuppetDB::Connection.new options[:host], options[:port], !options[:no_ssl]
77
77
  parser = PuppetDB::Parser.new
78
- nodes = puppetdb.query(:nodes, parser.parse(query, :nodes))
78
+ query = parser.parse(query, :nodes)
79
79
 
80
80
  if options[:node_info]
81
+ nodes = puppetdb.query(:nodes, query)
81
82
  Hash[nodes.collect { |node| [node['certname'], node.reject { |k, _v| k == 'certname' }] }]
82
83
  else
84
+ nodes = puppetdb.query(:nodes, query, :extract => :certname)
83
85
  nodes.collect { |node| node['certname'] }
84
86
  end
85
87
  end
@@ -129,8 +131,8 @@ Puppet::Face.define(:query, '1.0.0') do
129
131
  puppetdb = PuppetDB::Connection.new options[:host], options[:port], !options[:no_ssl]
130
132
  parser = PuppetDB::Parser.new
131
133
  nodes = puppetdb.query(:nodes, parser.parse(query, :nodes)).collect { |n| n['certname'] }
132
- starttime = Chronic.parse(options[:since], context: :past, guess: false).first.getutc.strftime('%FT%T.000Z')
133
- endtime = Chronic.parse(options[:until], context: :past, guess: false).last.getutc.strftime('%FT%T.000Z')
134
+ starttime = Chronic.parse(options[:since], :context => :past, :guess => false).first.getutc.strftime('%FT%T.000Z')
135
+ endtime = Chronic.parse(options[:until], :context => :past, :guess => false).last.getutc.strftime('%FT%T.000Z')
134
136
 
135
137
  events = []
136
138
  # Event API doesn't support subqueries at the moment and
@@ -1,4 +1,4 @@
1
- Puppet::Parser::Functions.newfunction(:query_facts, type: :rvalue, arity: 2, doc: <<-EOT
1
+ Puppet::Parser::Functions.newfunction(:query_facts, :type => :rvalue, :arity => 2, :doc => <<-EOT
2
2
 
3
3
  accepts two arguments, a query used to discover nodes, and a list of facts
4
4
  that should be returned from those hosts.
@@ -32,5 +32,5 @@ EOT
32
32
  puppetdb = PuppetDB::Connection.new(uri.host, uri.port, uri.scheme == 'https')
33
33
  parser = PuppetDB::Parser.new
34
34
  query = parser.facts_query query, facts if query.is_a? String
35
- parser.facts_hash(puppetdb.query(:facts, query))
35
+ parser.facts_hash(puppetdb.query(:facts, query, :extract => [:certname, :name, :value]))
36
36
  end
@@ -1,4 +1,4 @@
1
- Puppet::Parser::Functions.newfunction(:query_nodes, type: :rvalue, arity: -2, doc: <<-EOT
1
+ Puppet::Parser::Functions.newfunction(:query_nodes, :type => :rvalue, :arity => -2, :doc => <<-EOT
2
2
 
3
3
  accepts two arguments, a query used to discover nodes, and a optional
4
4
  fact that should be returned.
@@ -29,10 +29,10 @@ EOT
29
29
  puppetdb = PuppetDB::Connection.new(uri.host, uri.port, uri.scheme == 'https')
30
30
  parser = PuppetDB::Parser.new
31
31
  if fact
32
- query = parser.facts_query query, [fact]
33
- puppetdb.query(:facts, query).collect { |f| f['value'] }
32
+ query = parser.facts_query(query, [fact])
33
+ puppetdb.query(:facts, query, :extract => :value).collect { |f| f['value'] }
34
34
  else
35
- query = parser.parse query, :nodes if query.is_a? String
36
- puppetdb.query(:nodes, query).collect { |n| n['certname'] }
35
+ query = parser.parse(query, :nodes) if query.is_a? String
36
+ puppetdb.query(:nodes, query, :extract => :certname).collect { |n| n['certname'] }
37
37
  end
38
38
  end
@@ -1,4 +1,4 @@
1
- Puppet::Parser::Functions.newfunction(:query_resources, type: :rvalue, arity: -3, doc: <<-EOT
1
+ Puppet::Parser::Functions.newfunction(:query_resources, :type => :rvalue, :arity => -3, :doc => <<-EOT
2
2
 
3
3
  Accepts two or three arguments: a query used to discover nodes, a
4
4
  resource query for the resources that should be returned from
@@ -66,9 +66,7 @@ EOT
66
66
  # If grouphosts is true create a nested hash with nodes and resources
67
67
  if grouphosts
68
68
  results.reduce({}) do |ret, resource|
69
- unless ret.key? resource['certname']
70
- ret[resource['certname']] = []
71
- end
69
+ ret[resource['certname']] = [] unless ret.key? resource['certname']
72
70
  ret[resource['certname']] << resource
73
71
  end
74
72
  else
data/lib/puppetdb.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module PuppetDB
2
2
  # Current version of this module
3
- VERSION = [2, 0, 3]
3
+ VERSION = [2, 1, 0]
4
4
  end
@@ -16,33 +16,49 @@ class PuppetDB::Connection
16
16
  end
17
17
 
18
18
  def self.check_version
19
- begin
20
- require 'puppet/util/puppetdb'
21
- unless Puppet::Util::Puppetdb.config.respond_to?('server_urls')
22
- Puppet.warning <<-EOT
19
+ require 'puppet/util/puppetdb'
20
+ unless Puppet::Util::Puppetdb.config.respond_to?('server_urls')
21
+ Puppet.warning <<-EOT
23
22
  It looks like you are using a PuppetDB version < 3.0.
24
23
  This version of puppetdbquery requires at least PuppetDB 3.0 to work.
25
24
  Downgrade to puppetdbquery 1.x to use it with PuppetDB 2.x.
26
25
  EOT
27
- end
28
- rescue LoadError
29
26
  end
27
+ rescue LoadError
30
28
  end
31
29
 
32
30
  # Execute a PuppetDB query
33
31
  #
34
32
  # @param endpoint [Symbol] :resources, :facts or :nodes
35
33
  # @param query [Array] query to execute
34
+ # @param options [Hash] specify extract values or http connection
36
35
  # @return [Array] the results of the query
37
- def query(endpoint, query = nil, http = nil, version = :v4)
36
+ def query(endpoint, query = nil, options = {}, version = :v4)
38
37
  require 'json'
39
38
 
39
+ default_options = {
40
+ :http => nil, # A HTTP object to be used for the connection
41
+ :extract => nil # An array of fields to extract
42
+ }
43
+
44
+ if options.is_a? Hash
45
+ options = default_options.merge options
46
+ else
47
+ Puppet.deprecation_warning 'Specify http object with :http key instead'
48
+ options = default_options.merge(:http => options)
49
+ end
50
+ http = options[:http]
51
+
40
52
  unless http
41
53
  require 'puppet/network/http_pool'
42
54
  http = Puppet::Network::HttpPool.http_instance(@host, @port, @use_ssl)
43
55
  end
44
56
  headers = { 'Accept' => 'application/json' }
45
57
 
58
+ if options[:extract]
59
+ query = PuppetDB::ParserHelper.extract(*Array(options[:extract]), query)
60
+ end
61
+
46
62
  uri = "/pdb/query/#{version}/#{endpoint}"
47
63
  uri += URI.escape "?query=#{query.to_json}" unless query.nil? || query.empty?
48
64
 
@@ -45,4 +45,9 @@ module PuppetDB::ParserHelper
45
45
  ret
46
46
  end
47
47
  end
48
+
49
+ # Turn a query into one for only certain fields
50
+ def self.extract(*field, query)
51
+ ['extract', field.collect(&:to_s), query]
52
+ end
48
53
  end
@@ -5,9 +5,11 @@ require 'puppetdb/connection'
5
5
 
6
6
  describe 'query_facts' do
7
7
  it do
8
- PuppetDB::Connection.any_instance.stubs(:query).returns [
9
- { 'certname' => 'apache4.puppetexplorer.io', 'environment' => 'production', 'name' => 'ipaddress', 'value' => '172.31.6.80' }
10
- ]
8
+ PuppetDB::Connection.any_instance.expects(:query)
9
+ .with(:facts, ['or', ['=', 'name', 'ipaddress']], {:extract => [:certname, :name, :value]})
10
+ .returns [
11
+ { 'certname' => 'apache4.puppetexplorer.io', 'environment' => 'production', 'name' => 'ipaddress', 'value' => '172.31.6.80' }
12
+ ]
11
13
  should run.with_params('', ['ipaddress']).and_return('apache4.puppetexplorer.io' => { 'ipaddress' => '172.31.6.80' })
12
14
  end
13
15
  end
@@ -6,22 +6,8 @@ describe 'query_nodes' do
6
6
  context 'without fact parameter' do
7
7
  it do
8
8
  PuppetDB::Connection.any_instance.expects(:query)
9
- .with(:nodes, ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['hostname']], ['=', 'value', 'apache4']]]]])
10
- .returns [
11
- {
12
- 'certname' => 'apache4.puppetexplorer.io',
13
- 'deactivated' => nil,
14
- 'latest_report_hash' => '4ff9fc5c1344c1c6ce47d923eb139a3409ef530d',
15
- 'facts_environment' => 'production',
16
- 'report_environment' => 'production',
17
- 'catalog_environment' => 'production',
18
- 'facts_timestamp' => '2015-10-13T05:35:32.511Z',
19
- 'expired' => nil,
20
- 'report_timestamp' => '2015-10-13T05:33:17.011Z',
21
- 'catalog_timestamp' => '2015-10-13T05:35:38.661Z',
22
- 'latest_report_status' => 'changed'
23
- }
24
- ]
9
+ .with(:nodes, ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['hostname']], ['=', 'value', 'apache4']]]]], :extract => :certname)
10
+ .returns [ { 'certname' => 'apache4.puppetexplorer.io' } ]
25
11
  should run.with_params('hostname="apache4"').and_return(['apache4.puppetexplorer.io'])
26
12
  end
27
13
  end
@@ -29,15 +15,8 @@ describe 'query_nodes' do
29
15
  context 'with a fact parameter' do
30
16
  it do
31
17
  PuppetDB::Connection.any_instance.expects(:query)
32
- .with(:facts, ['and', ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['hostname']], ['=', 'value', 'apache4']]]]], ['or', ['=', 'name', 'ipaddress']]])
33
- .returns [
34
- {
35
- 'certname' => 'apache4.puppetexplorer.io',
36
- 'environment' => 'production',
37
- 'name' => 'ipaddress',
38
- 'value' => '172.31.6.80'
39
- }
40
- ]
18
+ .with(:facts, ['and', ['in', 'certname', ['extract', 'certname', ['select_fact_contents', ['and', ['=', 'path', ['hostname']], ['=', 'value', 'apache4']]]]], ['or', ['=', 'name', 'ipaddress']]], :extract => :value)
19
+ .returns [ { 'value' => '172.31.6.80' } ]
41
20
  should run.with_params('hostname="apache4"', 'ipaddress').and_return(['172.31.6.80'])
42
21
  end
43
22
  end
@@ -222,4 +222,12 @@ describe PuppetDB::Parser do
222
222
  )
223
223
  end
224
224
  end
225
+
226
+ context 'extract' do
227
+ it 'should create an extract query' do
228
+ PuppetDB::ParserHelper.extract(:certname, :name, ['=', 'certname', 'foo.example.com']).should eq(
229
+ ['extract', ['certname', 'name'], ['=', 'certname', 'foo.example.com']]
230
+ )
231
+ end
232
+ end
225
233
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-puppetdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.3
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Dalen