ruby-puppetdb 1.6.1 → 2.0.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.
Files changed (34) hide show
  1. checksums.yaml +8 -8
  2. data/bin/find-nodes +34 -21
  3. data/lib/hiera/backend/puppetdb_backend.rb +15 -12
  4. data/lib/puppet/application/query.rb +10 -4
  5. data/lib/puppet/face/query.rb +37 -31
  6. data/lib/puppet/parser/functions/query_facts.rb +9 -4
  7. data/lib/puppet/parser/functions/query_nodes.rb +13 -8
  8. data/lib/puppet/parser/functions/query_resources.rb +35 -6
  9. data/lib/puppetdb.rb +1 -1
  10. data/lib/puppetdb/astnode.rb +92 -44
  11. data/lib/puppetdb/connection.rb +14 -69
  12. data/lib/puppetdb/grammar.racc +75 -40
  13. data/lib/puppetdb/lexer.rb +23 -4
  14. data/lib/puppetdb/lexer.rex +30 -24
  15. data/lib/puppetdb/parser.rb +245 -223
  16. data/lib/puppetdb/parser_helper.rb +48 -0
  17. data/spec/unit/puppetdb/parser_spec.rb +129 -0
  18. metadata +7 -28
  19. data/lib/puppet/parser/functions/pdbfactquery.rb +0 -31
  20. data/lib/puppet/parser/functions/pdbnodequery.rb +0 -38
  21. data/lib/puppet/parser/functions/pdbnodequery_all.rb +0 -40
  22. data/lib/puppet/parser/functions/pdbquery.rb +0 -41
  23. data/lib/puppet/parser/functions/pdbresourcequery.rb +0 -39
  24. data/lib/puppet/parser/functions/pdbresourcequery_all.rb +0 -37
  25. data/lib/puppet/parser/functions/pdbstatusquery.rb +0 -31
  26. data/lib/puppetdb/util.rb +0 -18
  27. data/spec/unit/puppet/parser/functions/pdbfactquery_spec.rb +0 -19
  28. data/spec/unit/puppet/parser/functions/pdbnodequery_all_spec.rb +0 -19
  29. data/spec/unit/puppet/parser/functions/pdbnodequery_spec.rb +0 -19
  30. data/spec/unit/puppet/parser/functions/pdbquery_spec.rb +0 -19
  31. data/spec/unit/puppet/parser/functions/pdbresourcequery_all_spec.rb +0 -19
  32. data/spec/unit/puppet/parser/functions/pdbresourcequery_spec.rb +0 -19
  33. data/spec/unit/puppet/parser/functions/pdbstatusquery_spec.rb +0 -19
  34. data/spec/unit/puppetdb/connection_spec.rb +0 -67
@@ -0,0 +1,48 @@
1
+ # Helper methods for the parser, included in the parser class
2
+ require 'puppetdb'
3
+ module PuppetDB::ParserHelper
4
+ # Parse a query string into a PuppetDB query
5
+ #
6
+ # @param query [String] the query string to parse
7
+ # @param endpoint [Symbol] the endpoint for which the query should be evaluated
8
+ # @return [Array] the PuppetDB query
9
+ def parse(query, endpoint = :nodes)
10
+ if query = scan_str(query)
11
+ query.optimize.evaluate [endpoint]
12
+ end
13
+ end
14
+
15
+ # Create a query for facts on nodes matching a query string
16
+ #
17
+ # @param query [String] the query string to parse
18
+ # @param facts [Array] an array of facts to get
19
+ # @return [Array] the PuppetDB query
20
+ def facts_query(query, facts = nil)
21
+ nodequery = parse(query, :facts)
22
+ if facts.nil?
23
+ nodequery
24
+ else
25
+ factquery = ['or', *facts.collect { |f| ['=', 'name', f] }]
26
+ if nodequery
27
+ ['and', nodequery, factquery]
28
+ else
29
+ factquery
30
+ end
31
+ end
32
+ end
33
+
34
+ # Turn an array of facts into a hash of nodes containing facts
35
+ #
36
+ # @param facts [Array] fact values
37
+ # @return [Hash] nodes as keys containing a hash of facts as value
38
+ def facts_hash(facts)
39
+ facts.reduce({}) do |ret, fact|
40
+ if ret.include? fact['certname']
41
+ ret[fact['certname']][fact['name']] = fact['value']
42
+ else
43
+ ret[fact['certname']] = { fact['name'] => fact['value'] }
44
+ end
45
+ ret
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,129 @@
1
+ #! /usr/bin/env ruby -S rspec
2
+
3
+ require 'spec_helper'
4
+ require 'puppetdb/parser'
5
+
6
+ describe PuppetDB::Parser do
7
+ context "Query parsing" do
8
+ let(:parser) { PuppetDB::Parser.new }
9
+ it "should handle empty queries" do
10
+ parser.parse('').should be_nil
11
+ end
12
+
13
+ it "should handle double quoted strings" do
14
+ parser.parse('foo="bar"').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo"]], ["=", "value", "bar"]]]]]
15
+ end
16
+
17
+ it "should handle single quoted strings" do
18
+ parser.parse('foo=\'bar\'').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo"]], ["=", "value", "bar"]]]]]
19
+ end
20
+
21
+ it "should handle precedence" do
22
+ parser.parse('foo=1 or bar=2 and baz=3').should eq ["or", ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo"]], ["=", "value", 1]]]]], ["and", ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["bar"]], ["=", "value", 2]]]]], ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["baz"]], ["=", "value", 3]]]]]]]
23
+ parser.parse('(foo=1 or bar=2) and baz=3').should eq ["and", ["or", ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo"]], ["=", "value", 1]]]]], ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["bar"]], ["=", "value", 2]]]]]], ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["baz"]], ["=", "value", 3]]]]]]
24
+ end
25
+
26
+ it "should parse resource queries for exported resources" do
27
+ parser.parse('@@file[foo]').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "File"], ["=", "title", "foo"], ["=", "exported", true]]]]]
28
+ end
29
+
30
+ it "should parse resource queries with type, title and parameters" do
31
+ parser.parse('file[foo]{bar=baz}').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "File"], ["=", "title", "foo"], ["=", "exported", false], ["=", ["parameter", "bar"], "baz"]]]]]
32
+ end
33
+
34
+ it "should parse resource queries with tags" do
35
+ parser.parse('file[foo]{tag=baz}').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "File"], ["=", "title", "foo"], ["=", "exported", false], ["=", "tag", "baz"]]]]]
36
+ end
37
+
38
+ it "should handle precedence within resource parameter queries" do
39
+ parser.parse('file[foo]{foo=1 or bar=2 and baz=3}').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "File"], ["=", "title", "foo"], ["=", "exported", false], ["or", ["=", ["parameter", "foo"], 1], ["and", ["=", ["parameter", "bar"], 2], ["=", ["parameter", "baz"], 3]]]]]]]
40
+ parser.parse('file[foo]{(foo=1 or bar=2) and baz=3}').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "File"], ["=", "title", "foo"], ["=", "exported", false], ["and", ["or", ["=", ["parameter", "foo"], 1], ["=", ["parameter", "bar"], 2]], ["=", ["parameter", "baz"], 3]]]]]]
41
+ end
42
+
43
+ it "should capitalize class names" do
44
+ parser.parse('class[foo::bar]').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "Class"], ["=", "title", "Foo::Bar"], ["=", "exported", false]]]]]
45
+ end
46
+
47
+ it "should parse resource queries with regeexp title matching" do
48
+ parser.parse('class[~foo]').should eq ["in", "certname", ["extract", "certname", ["select_resources", ["and", ["=", "type", "Class"], ["~", "title", "foo"], ["=", "exported", false]]]]]
49
+ end
50
+
51
+ it "should be able to negate expressions" do
52
+ parser.parse('not foo=bar').should eq ["not", ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo"]], ["=", "value", "bar"]]]]]]
53
+ end
54
+
55
+ it "should handle single string expressions" do
56
+ parser.parse('foo.bar.com').should eq ["~", "certname", "foo\\.bar\\.com"]
57
+ end
58
+
59
+ it "should handle structured facts" do
60
+ parser.parse('foo.bar=baz').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo", "bar"]], ["=", "value", "baz"]]]]]
61
+ end
62
+
63
+ it "should handle structured facts with array component" do
64
+ parser.parse('foo.bar.0=baz').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo", "bar", 0]], ["=", "value", "baz"]]]]]
65
+ end
66
+
67
+ it "should handle structured facts with match operator" do
68
+ parser.parse('foo.bar.~".*"=baz').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["~>", "path", ["foo", "bar", ".*"]], ["=", "value", "baz"]]]]]
69
+ end
70
+
71
+ it "should handle structured facts with wildcard operator" do
72
+ parser.parse('foo.bar.*=baz').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["~>", "path", ["foo", "bar", ".*"]], ["=", "value", "baz"]]]]]
73
+ end
74
+
75
+ it "should handle #node subqueries" do
76
+ parser.parse('#node.catalog_environment=production').should eq ["in", "certname", ["extract", "certname", ["select_nodes", ["=", "catalog_environment", "production"]]]]
77
+ end
78
+
79
+ it "should handle #node subqueries with block of conditions" do
80
+ parser.parse('#node { catalog_environment=production }').should eq ["in", "certname", ["extract", "certname", ["select_nodes", ["=", "catalog_environment", "production"]]]]
81
+ end
82
+
83
+ it "should handle #node subquery combined with fact query" do
84
+ parser.parse('#node.catalog_environment=production and foo=bar').should eq ["and", ["in", "certname", ["extract", "certname", ["select_nodes", ["=", "catalog_environment", "production"]]]], ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["foo"]], ["=", "value", "bar"]]]]]]
85
+ end
86
+
87
+ it "should escape non match parts on structured facts with match operator" do
88
+ parser.parse('"foo.bar".~".*"=baz').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["~>", "path", ["foo\\.bar", ".*"]], ["=", "value", "baz"]]]]]
89
+ end
90
+
91
+ it "should parse dates in queries" do
92
+ date = Time.new(2014,9,9).iso8601
93
+ parser.parse('#node.report_timestamp<@"Sep 9, 2014"').should eq ["in", "certname", ["extract", "certname", ["select_nodes", ["<", "report_timestamp", date]]]]
94
+ end
95
+ end
96
+
97
+ context "facts_query" do
98
+ let(:parser) { PuppetDB::Parser.new }
99
+ it 'should return a query for all if no facts are specified' do
100
+ parser.facts_query('kernel=Linux').should eq ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["kernel"]], ["=", "value", "Linux"]]]]]
101
+ end
102
+
103
+ it 'should return a query for specific facts if they are specified' do
104
+ parser.facts_query('kernel=Linux', ['ipaddress']). should eq ["and", ["in", "certname", ["extract", "certname", ["select_fact_contents", ["and", ["=", "path", ["kernel"]], ["=", "value", "Linux"]]]]], ["or", ["=", "name", "ipaddress"]]]
105
+ end
106
+
107
+ it 'should return a query for matching facts on all nodes if query is missing' do
108
+ parser.facts_query('', ['ipaddress']).should eq ["or", ["=", "name", "ipaddress"]]
109
+ end
110
+ end
111
+
112
+ context 'facts_hash' do
113
+ let(:parser) { PuppetDB::Parser.new }
114
+ it 'should merge facts into a nested hash' do
115
+ parser.facts_hash([
116
+ {"certname"=>"ip-172-31-45-32.eu-west-1.compute.internal", "environment"=>"production", "name"=>"kernel", "value"=>"Linux"},
117
+ {"certname"=>"ip-172-31-33-234.eu-west-1.compute.internal", "environment"=>"production", "name"=>"kernel", "value"=>"Linux"},
118
+ {"certname"=>"ip-172-31-5-147.eu-west-1.compute.internal", "environment"=>"production", "name"=>"kernel", "value"=>"Linux"},
119
+ {"certname"=>"ip-172-31-45-32.eu-west-1.compute.internal", "environment"=>"production", "name"=>"fqdn", "value"=>"ip-172-31-45-32.eu-west-1.compute.internal"},
120
+ {"certname"=>"ip-172-31-33-234.eu-west-1.compute.internal", "environment"=>"production", "name"=>"fqdn", "value"=>"ip-172-31-33-234.eu-west-1.compute.internal"},
121
+ {"certname"=>"ip-172-31-5-147.eu-west-1.compute.internal", "environment"=>"production", "name"=>"fqdn", "value"=>"ip-172-31-5-147.eu-west-1.compute.internal"}
122
+ ]).should eq(
123
+ "ip-172-31-45-32.eu-west-1.compute.internal"=>{"kernel"=>"Linux", "fqdn"=>"ip-172-31-45-32.eu-west-1.compute.internal"},
124
+ "ip-172-31-33-234.eu-west-1.compute.internal"=>{"kernel"=>"Linux", "fqdn"=>"ip-172-31-33-234.eu-west-1.compute.internal"},
125
+ "ip-172-31-5-147.eu-west-1.compute.internal"=>{"kernel"=>"Linux", "fqdn"=>"ip-172-31-5-147.eu-west-1.compute.internal"}
126
+ )
127
+ end
128
+ end
129
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-puppetdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Dalen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-09 00:00:00.000000000 Z
11
+ date: 2015-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -171,13 +171,6 @@ files:
171
171
  - lib/hiera/backend/puppetdb_backend.rb
172
172
  - lib/puppet/application/query.rb
173
173
  - lib/puppet/face/query.rb
174
- - lib/puppet/parser/functions/pdbfactquery.rb
175
- - lib/puppet/parser/functions/pdbnodequery.rb
176
- - lib/puppet/parser/functions/pdbnodequery_all.rb
177
- - lib/puppet/parser/functions/pdbquery.rb
178
- - lib/puppet/parser/functions/pdbresourcequery.rb
179
- - lib/puppet/parser/functions/pdbresourcequery_all.rb
180
- - lib/puppet/parser/functions/pdbstatusquery.rb
181
174
  - lib/puppet/parser/functions/query_facts.rb
182
175
  - lib/puppet/parser/functions/query_nodes.rb
183
176
  - lib/puppet/parser/functions/query_resources.rb
@@ -188,18 +181,11 @@ files:
188
181
  - lib/puppetdb/lexer.rb
189
182
  - lib/puppetdb/lexer.rex
190
183
  - lib/puppetdb/parser.rb
191
- - lib/puppetdb/util.rb
184
+ - lib/puppetdb/parser_helper.rb
192
185
  - spec/spec_helper.rb
193
- - spec/unit/puppet/parser/functions/pdbfactquery_spec.rb
194
- - spec/unit/puppet/parser/functions/pdbnodequery_all_spec.rb
195
- - spec/unit/puppet/parser/functions/pdbnodequery_spec.rb
196
- - spec/unit/puppet/parser/functions/pdbquery_spec.rb
197
- - spec/unit/puppet/parser/functions/pdbresourcequery_all_spec.rb
198
- - spec/unit/puppet/parser/functions/pdbresourcequery_spec.rb
199
- - spec/unit/puppet/parser/functions/pdbstatusquery_spec.rb
200
186
  - spec/unit/puppet/parser/functions/query_facts_spec.rb
201
187
  - spec/unit/puppet/parser/functions/query_nodes_spec.rb
202
- - spec/unit/puppetdb/connection_spec.rb
188
+ - spec/unit/puppetdb/parser_spec.rb
203
189
  homepage: https://github.com/dalen/puppet-puppetdbquery
204
190
  licenses:
205
191
  - Apache v2
@@ -225,17 +211,10 @@ signing_key:
225
211
  specification_version: 4
226
212
  summary: Query functions for PuppetDB
227
213
  test_files:
228
- - spec/spec_helper.rb
229
- - spec/unit/puppetdb/connection_spec.rb
230
- - spec/unit/puppet/parser/functions/query_facts_spec.rb
231
- - spec/unit/puppet/parser/functions/pdbfactquery_spec.rb
232
- - spec/unit/puppet/parser/functions/pdbstatusquery_spec.rb
233
- - spec/unit/puppet/parser/functions/pdbnodequery_all_spec.rb
234
- - spec/unit/puppet/parser/functions/pdbquery_spec.rb
214
+ - spec/unit/puppetdb/parser_spec.rb
235
215
  - spec/unit/puppet/parser/functions/query_nodes_spec.rb
236
- - spec/unit/puppet/parser/functions/pdbresourcequery_spec.rb
237
- - spec/unit/puppet/parser/functions/pdbresourcequery_all_spec.rb
238
- - spec/unit/puppet/parser/functions/pdbnodequery_spec.rb
216
+ - spec/unit/puppet/parser/functions/query_facts_spec.rb
217
+ - spec/spec_helper.rb
239
218
  - examples/query_resource_examples.pp
240
219
  - examples/query_node_examples.pp
241
220
  - examples/nova_functions.pp
@@ -1,31 +0,0 @@
1
- module Puppet::Parser::Functions
2
- newfunction(:pdbfactquery, :type => :rvalue, :doc => "\
3
- Perform a PuppetDB fact query
4
-
5
- The first argument is the node to get facts for.
6
- Second argument is optional, if specified only return that specific fact.
7
-
8
- Examples:
9
- # Get hash of facts for foo.example.com
10
- pdbfactquery('foo.example.com')
11
- # Get the uptime fact for foo.example.com
12
- pdbfactquery('foo.example.com', 'uptime')") do |args|
13
-
14
- raise(Puppet::ParseError, "pdbquery(): Wrong number of arguments " +
15
- "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
16
-
17
- Puppet::Parser::Functions.autoloader.load(:pdbquery) unless Puppet::Parser::Functions.autoloader.loaded?(:pdbquery)
18
-
19
- node, fact = args
20
- if node.is_a?(Array) then
21
- node.collect { |n| function_pdbfactquery([n,fact]) }
22
- else
23
- facts = function_pdbquery(["facts/#{args[0]}"])['facts']
24
- if fact then
25
- facts[fact]
26
- else
27
- facts
28
- end
29
- end
30
- end
31
- end
@@ -1,38 +0,0 @@
1
- module Puppet::Parser::Functions
2
- newfunction(:pdbnodequery, :type => :rvalue, :doc => "\
3
- Perform a PuppetDB node query
4
-
5
- The first argument is the node query, it has to be an array.
6
- Second argument is optional but allows you to specify a resource query
7
- that the nodes returned also have to match.
8
-
9
- This function excludes any deactivated hosts.
10
-
11
- Returns a array of strings with the certname of the nodes (fqdn by default).
12
-
13
- # Return an array of active nodes with an uptime more than 30 days
14
- $ret = pdbnodequery(['>',['fact','uptime_days'],30])
15
-
16
- # Return an array of active nodes with an uptime more than 30 days and
17
- # having the class 'apache'
18
- $ret = pdbnodequery(['>',['fact','uptime_days'],30], ['and',['=','type','Class'],['=','title','Apache']])") do |args|
19
-
20
- raise(Puppet::ParseError, "pdbquery(): Wrong number of arguments " +
21
- "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
22
-
23
- Puppet::Parser::Functions.autoloader.load(:pdbquery) unless Puppet::Parser::Functions.autoloader.loaded?(:pdbquery)
24
- Puppet::Parser::Functions.autoloader.load(:pdbresourcequery) unless Puppet::Parser::Functions.autoloader.loaded?(:pdbresourcequery)
25
-
26
- nodeq, resq = args
27
-
28
- nodeqnodes = function_pdbquery(['nodes', ['and',['=',['node','active'],true],nodeq] ])
29
-
30
- if resq then
31
- resqnodes = function_pdbresourcequery([resq, 'certname'])
32
- nodeqnodes & resqnodes
33
- else
34
- # No resource query to worry about, just return the nodequery
35
- nodeqnodes
36
- end
37
- end
38
- end
@@ -1,40 +0,0 @@
1
- module Puppet::Parser::Functions
2
- newfunction(:pdbnodequery_all, :type => :rvalue, :doc => "\
3
- Perform a PuppetDB node query
4
-
5
- The first argument is the node query.
6
- Second argument is optional but allows you to specify a resource query
7
- that the nodes returned also have to match.
8
-
9
- Returns a array of strings with the certname of the nodes (fqdn by default).
10
-
11
- # Return an array of both active and deactivaded nodes with an uptime more than 30 days
12
- $ret = pdbnodequery_all(['and',['>',['fact','uptime_days'],30]])
13
-
14
- # Return an array of both active and deactivated nodes with an uptime more
15
- # than 30 days and having the class 'apache'
16
- $ret = pdbnodequery_all(
17
- ['>',['fact','uptime_days'],30],
18
- ['and',
19
- ['=','type','Class'],
20
- ['=','title','Apache']])") do |args|
21
-
22
- raise(Puppet::ParseError, "pdbquery_all(): Wrong number of arguments " +
23
- "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
24
-
25
- Puppet::Parser::Functions.autoloader.load(:pdbquery) unless Puppet::Parser::Functions.autoloader.loaded?(:pdbquery)
26
- Puppet::Parser::Functions.autoloader.load(:pdbresourcequery_all) unless Puppet::Parser::Functions.autoloader.loaded?(:pdbresourcequery_all)
27
-
28
- nodeq, resq = args
29
-
30
- nodeqnodes = function_pdbquery(['nodes', nodeq])
31
-
32
- if resq then
33
- resqnodes = function_pdbresourcequery_all([resq, 'certname'])
34
- nodeqnodes & resqnodes
35
- else
36
- # No resource query to worry about, just return the nodequery
37
- nodeqnodes
38
- end
39
- end
40
- end
@@ -1,41 +0,0 @@
1
- module Puppet::Parser::Functions
2
- newfunction(:pdbquery, :type => :rvalue, :doc => "\
3
- Perform a PuppetDB query
4
-
5
- The first argument is the URL path that should be queried, for
6
- example 'nodes' or 'status/nodes/<nodename>'.
7
- The second argument if supplied if the query parameter, if it is
8
- a string it is assumed to be JSON formatted and sent as is,
9
- anything else is converted to JSON and then sent.
10
-
11
- Example: pdbquery('nodes', ['=', ['node', 'active'], true ])") do |args|
12
-
13
- raise(Puppet::ParseError, "pdbquery(): Wrong number of arguments " +
14
- "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
15
-
16
- require 'puppet/network/http_pool'
17
- require 'uri'
18
- require 'puppet/util/puppetdb'
19
- require 'puppet/indirector/rest'
20
-
21
- # Query type (URL path)
22
- t, q = args
23
-
24
- # Query contents
25
- if q then
26
- # Convert to JSON if it isn't already
27
- q=q.to_pson unless q.is_a? String
28
- params = URI.escape("?query=#{q}")
29
- else
30
- params = ''
31
- end
32
-
33
- conn = Puppet::Network::HttpPool.http_instance(Puppet::Util::Puppetdb.server, Puppet::Util::Puppetdb.port, use_ssl = true)
34
- response = conn.get("/v1/#{t}#{params}", { "Accept" => "application/json",})
35
-
36
- unless response.kind_of?(Net::HTTPSuccess)
37
- raise Puppet::ParseError, "PuppetDB query error: [#{response.code}] #{response.msg}"
38
- end
39
- PSON.load(response.body)
40
- end
41
- end
@@ -1,39 +0,0 @@
1
- module Puppet::Parser::Functions
2
- newfunction(:pdbresourcequery, :type => :rvalue, :doc => "\
3
- Perform a PuppetDB resource query
4
-
5
- The first argument is the resource query, it has to be an array.
6
- Second argument is optional but allows you to specify the item you want
7
- from the returned hash.
8
-
9
- This function excludes any deactivated hosts.
10
-
11
- Returns an array of hashes or array of strings if second argument is provided.
12
-
13
- Examples:
14
- # Return an array of hashes describing all files that are owned by root on active hosts.
15
- $ret = pdbresourcequery(
16
- ['and',
17
- ['=','type','File'],
18
- ['=',['parameter','owner'],'root']])
19
-
20
- # Return an array of host names having those resources
21
- $ret = pdbresourcequery(
22
- ['and',
23
- ['=','type','File'],
24
- ['=',['parameter','owner'],'root']], 'certname')") do |args|
25
-
26
- raise(Puppet::ParseError, "pdbresourcequery(): Wrong number of arguments " +
27
- "given (#{args.size} for 1 or 2)") if args.size < 1 or args.size > 2
28
-
29
- Puppet::Parser::Functions.autoloader.load(:pdbquery) unless Puppet::Parser::Functions.autoloader.loaded?(:pdbquery)
30
-
31
- resq, info = args
32
- ret = function_pdbquery(['resources', ['and',['=',['node','active'],true],resq] ])
33
- if info then
34
- ret.collect {|x| x[info]}
35
- else
36
- ret
37
- end
38
- end
39
- end