Giuseppe Lavagetto has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/311423

Change subject: puppetdbquery: add ability to order resources
......................................................................

puppetdbquery: add ability to order resources

Change-Id: I28bf175231ab025f367bb51bf83c41b29c83a2c6
---
M modules/puppetdbquery/lib/puppet/parser/functions/query_resources.rb
M modules/puppetdbquery/lib/puppetdb/connection.rb
2 files changed, 18 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/puppet 
refs/changes/23/311423/1

diff --git 
a/modules/puppetdbquery/lib/puppet/parser/functions/query_resources.rb 
b/modules/puppetdbquery/lib/puppet/parser/functions/query_resources.rb
index b16793e..c711f1f 100644
--- a/modules/puppetdbquery/lib/puppet/parser/functions/query_resources.rb
+++ b/modules/puppetdbquery/lib/puppet/parser/functions/query_resources.rb
@@ -10,6 +10,9 @@
 
   If the third parameters is false the result will be a an array of all 
resources found.
 
+  If the (optional) fourth parameter is added, it will be possible to order 
the result based
+  on one resource field
+
   Examples:
 
     Returns the parameters and such for the ntp class for all CentOS nodes:
@@ -28,9 +31,12 @@
 
       query_resources(false, 'Class["apache"]', false)
 
+    Same as above, but ordered by resource title (ascending)
+      query_resources(false, 'Class["apache"]', false, 'title')
+
 EOT
 ) do |args|
-  nodequery, resquery, grouphosts = args
+  nodequery, resquery, grouphosts, order_by = args
 
   require 'puppet/util/puppetdb'
   # This is needed if the puppetdb library isn't pluginsynced to the master
@@ -44,5 +50,5 @@
   puppetdb = PuppetDB::Connection.new(Puppet::Util::Puppetdb.server, 
Puppet::Util::Puppetdb.port)
   nodequery = puppetdb.parse_query nodequery, :facts if nodequery and 
nodequery.is_a? String and ! nodequery.empty?
   resquery = puppetdb.parse_query resquery, :none if resquery and 
resquery.is_a? String and ! resquery.empty?
-  return puppetdb.resources(nodequery, resquery, nil, grouphosts)
+  return puppetdb.resources(nodequery, resquery, nil, grouphosts, order_by)
 end
diff --git a/modules/puppetdbquery/lib/puppetdb/connection.rb 
b/modules/puppetdbquery/lib/puppetdb/connection.rb
index 1622d76..c080ece 100644
--- a/modules/puppetdbquery/lib/puppetdb/connection.rb
+++ b/modules/puppetdbquery/lib/puppetdb/connection.rb
@@ -56,8 +56,9 @@
   # @param resquery [Array] a resources query for what resources to fetch
   # @param nodequery [Array] the query to find the nodes to fetch resources 
for, optionally empty
   # @param grouphosts [Boolean] whether or not to group the results by the 
host they belong to
+  # @param order_by [String] a field to order the result by
   # @return [Hash|Array] a hash of hashes with resources for each node mathing 
query or array if grouphosts was false
-  def resources(nodequery, resquery, http=nil, grouphosts=true)
+  def resources(nodequery, resquery, http=nil, grouphosts=true, order_by=nil)
     if resquery and ! resquery.empty?
       if nodequery and ! nodequery.empty?
         q = ['and', resquery, nodequery]
@@ -68,7 +69,7 @@
       raise RuntimeError, "PuppetDB resources query error: at least one 
argument must be non empty; arguments were: nodequery: #{nodequery.inspect} and 
requery: #{resquery.inspect}"
     end
     resources = {}
-    results = query(:resources, q, http)
+    results = query(:resources, q, http, order_by)
 
     if grouphosts
       results.each do |resource|
@@ -89,7 +90,7 @@
   # @param endpoint [Symbol] :resources, :facts or :nodes
   # @param query [Array] query to execute
   # @return [Array] the results of the query
-  def query(endpoint, query = nil, http = nil, version = :v3)
+  def query(endpoint, query = nil, http = nil, version = :v3, order_by = nil)
     require 'json'
 
     unless http
@@ -100,7 +101,12 @@
 
     uri = "/#{version}/#{endpoint}"
     uri += URI.escape "?query=#{query.to_json}" unless query.nil? || 
query.empty?
-
+    unless order_by.nil?
+      field, order = order_by.downcase.split(" ")
+      order ||= 'asc'
+      order_query = [{'field' => field, 'order' => order}]
+      uri += URI.escape "&order-by=#{order_query.to_json}"
+    end
     debug("PuppetDB query: #{query.to_json}")
 
     resp = http.get(uri, headers)

-- 
To view, visit https://gerrit.wikimedia.org/r/311423
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I28bf175231ab025f367bb51bf83c41b29c83a2c6
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: Giuseppe Lavagetto <glavage...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to