From 0243325d66df1b3b96cdd4dc5611a768fec491fd Mon Sep 17 00:00:00 2001
From: Adam Miller <maxamillion@fedoraproject.org>
Date: Feb 02 2017 23:12:44 +0000
Subject: [PATCH 1/2] add filters param to datagrepper_query, remove delta default


Signed-off-by: Adam Miller <maxamillion@fedoraproject.org>

---

diff --git a/ansible/datagrepperdemo2.yml b/ansible/datagrepperdemo2.yml
new file mode 100644
index 0000000..1fbb1e3
--- /dev/null
+++ b/ansible/datagrepperdemo2.yml
@@ -0,0 +1,13 @@
+---
+- name: Demo2 datagrepper ansible plugin
+  hosts: automator
+  tasks:
+  - name: datagrepper query
+    datagrepper_query:
+      url: "https://apps.fedoraproject.org/datagrepper/raw"
+      topic: "org.fedoraproject.prod.buildsys.tag"
+      filters: "user==containerbuild"
+    register: query_data
+
+  - debug: var="query_data['data'][0]"
+
diff --git a/ansible/library/datagrepper_query.py b/ansible/library/datagrepper_query.py
index 1ad518a..0de23d0 100755
--- a/ansible/library/datagrepper_query.py
+++ b/ansible/library/datagrepper_query.py
@@ -43,8 +43,14 @@ options:
         request all available. Default is 1
     default: 1
     required: false
+  filters:
+    description:
+      - comma separated string of filters to apply to the datagrepper query
+        request all available. Default is None. Filter reference:
+        https://apps.fedoraproject.org/datagrepper/reference
+    default: 1
+    required: false
 
-FIXME
 '''
 
 EXAMPLES = '''
@@ -55,7 +61,12 @@ EXAMPLES = '''
     delta: 1209600
     pages: 1
 
-FIXME
+- datagrepper_query:
+    topic: org.fedoraproject.prod.buildsys.tag
+    url: https://apps.fedoraproject.org/datagrepper/raw
+    delta: 1209600
+    pages: 1
+    filters: "user==containerbuild"
 '''
 
 import requests
@@ -66,19 +77,32 @@ def main():
         argument_spec=dict(
             url=dict(required=True, default=None),
             topic=dict(required=True, default=None),
-            delta=dict(required=False, type="int", default=1209600),
+            delta=dict(required=False, type="int", default=0),
             pages=dict(required=False, type="int", default=1),
+            filters=dict(required=False, default=None)
         ),
         supports_check_mode=False
     )
 
     # Maximum rows per page is 100, let's just do that
     request_params = {
-        "delta": module.params['delta'],
         "topic": module.params['topic'],
         "rows_per_page": 100,
     }
 
+    # Only set delta in query if needed
+    if module.params['delta'] > 0:
+        request_params['delta'] = module.params['delta']
+
+    if module.params['filters']:
+        try:
+            for filter_arg in module.params['filters'].split(','):
+                key, val = filter_arg.split('==')
+                request_params[key] = val
+        except ValueError:
+            module.fail_json(
+                "ERROR: Incorrectly formatted 'filters' param provided"
+            )
 
     query_request = requests.get(module.params['url'], params=request_params)
 

From 77b390ddf56e5f6c8af0b1ff51c8f531b213abaf Mon Sep 17 00:00:00 2001
From: Adam Miller <maxamillion@fedoraproject.org>
Date: Feb 03 2017 14:30:48 +0000
Subject: [PATCH 2/2] switch from comma delimited string to yaml list for datagrepper_query filters


Signed-off-by: Adam Miller <maxamillion@fedoraproject.org>

---

diff --git a/ansible/datagrepperdemo2.yml b/ansible/datagrepperdemo2.yml
index 1fbb1e3..b708f51 100644
--- a/ansible/datagrepperdemo2.yml
+++ b/ansible/datagrepperdemo2.yml
@@ -6,7 +6,7 @@
     datagrepper_query:
       url: "https://apps.fedoraproject.org/datagrepper/raw"
       topic: "org.fedoraproject.prod.buildsys.tag"
-      filters: "user==containerbuild"
+      filters: ["user==containerbuild"]
     register: query_data
 
   - debug: var="query_data['data'][0]"
diff --git a/ansible/library/datagrepper_query.py b/ansible/library/datagrepper_query.py
index 0de23d0..c1b0bfe 100755
--- a/ansible/library/datagrepper_query.py
+++ b/ansible/library/datagrepper_query.py
@@ -45,8 +45,8 @@ options:
     required: false
   filters:
     description:
-      - comma separated string of filters to apply to the datagrepper query
-        request all available. Default is None. Filter reference:
+      - list of filters to apply to the datagrepper query request all available.
+        Default is emtpy list. Filter reference:
         https://apps.fedoraproject.org/datagrepper/reference
     default: 1
     required: false
@@ -66,7 +66,7 @@ EXAMPLES = '''
     url: https://apps.fedoraproject.org/datagrepper/raw
     delta: 1209600
     pages: 1
-    filters: "user==containerbuild"
+    filters: ["user==containerbuild"]
 '''
 
 import requests
@@ -79,7 +79,7 @@ def main():
             topic=dict(required=True, default=None),
             delta=dict(required=False, type="int", default=0),
             pages=dict(required=False, type="int", default=1),
-            filters=dict(required=False, default=None)
+            filters=dict(required=False, type="list", default=[])
         ),
         supports_check_mode=False
     )
@@ -94,9 +94,10 @@ def main():
     if module.params['delta'] > 0:
         request_params['delta'] = module.params['delta']
 
-    if module.params['filters']:
+    filter_args = [ fa.strip() for fa in module.params['filters']]
+    if filter_args:
         try:
-            for filter_arg in module.params['filters'].split(','):
+            for filter_arg in filter_args:
                 key, val = filter_arg.split('==')
                 request_params[key] = val
         except ValueError:

