Volans has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/399821 )

Change subject: PuppetDB backend: add support for API v4
......................................................................

PuppetDB backend: add support for API v4

* Add support for PuppetDB API version 4.

Bug: T182575
Change-Id: I5fac12efcd89ea1b42d19aa68deff96df2074424
---
M cumin/backends/puppetdb.py
M cumin/tests/unit/backends/test_puppetdb.py
M doc/examples/config.yaml
3 files changed, 29 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/software/cumin 
refs/changes/21/399821/1

diff --git a/cumin/backends/puppetdb.py b/cumin/backends/puppetdb.py
index ff4a867..6b3783e 100644
--- a/cumin/backends/puppetdb.py
+++ b/cumin/backends/puppetdb.py
@@ -86,7 +86,8 @@
     """PuppetDB query builder.
 
     The `puppetdb` backend allow to use an existing PuppetDB instance for the 
hosts selection.
-    At the moment only PuppetDB v3 API are implemented.
+    The supported PuppetDB API versions are 3 and 4. It can be specified via 
the api_version configuration key, if
+    not configured, the v3 will be assumed.
 
     * Each query part can be composed with the others using boolean operators 
(``and``, ``or``, ``not``)
     * Multiple query parts can be grouped together with parentheses (``(``, 
``)``).
@@ -136,7 +137,7 @@
       ``host10[10-42].*.domain or (not F:key1 = value1 and host10*) or (F:key2 
> value2 and F:key3 ~ '^value[0-9]+')``
     """
 
-    base_url_template = 'https://{host}:{port}/v3/'
+    base_url_template = 'https://{host}:{port}'
     """:py:class:`str`: string template in the :py:meth:`str.format` style 
used to generate the base URL of the
     PuppetDB server."""
 
@@ -166,10 +167,18 @@
         self.current_group = self.grouped_tokens
         self._endpoint = None
         puppetdb_config = self.config.get('puppetdb', {})
-        self.url = self.base_url_template.format(
+        base_url = self.base_url_template.format(
             host=puppetdb_config.get('host', 'localhost'),
             port=puppetdb_config.get('port', 443))
 
+        self.api_version = puppetdb_config.get('api_version', 3)
+        if self.api_version == 3:
+            self.url = base_url + '/v3/'
+        elif self.api_version == 4:
+            self.url = base_url + '/pdb/query/v4/'
+        else:
+            raise InvalidQueryError('Unsupported PuppetDB API version 
{ver}'.format(ver=self.api_version))
+
         for exception in puppetdb_config.get('urllib3_disable_warnings', []):
             urllib3.disable_warnings(category=getattr(urllib3.exceptions, 
exception))
 
diff --git a/cumin/tests/unit/backends/test_puppetdb.py 
b/cumin/tests/unit/backends/test_puppetdb.py
index 7e76bd9..045aa59 100644
--- a/cumin/tests/unit/backends/test_puppetdb.py
+++ b/cumin/tests/unit/backends/test_puppetdb.py
@@ -37,11 +37,11 @@
     assert parsed[0].asDict() == hosts
 
 
-class TestPuppetDBQuery(object):
-    """PuppetDB backend query test class."""
+class TestPuppetDBQueryV3(object):
+    """PuppetDB backend query test class for API version 3."""
 
     def setup_method(self, _):
-        """Setup an instace of PuppetDBQuery for each test."""
+        """Set an instance of PuppetDBQuery for each test."""
         self.query = puppetdb.PuppetDBQuery({})  # pylint: 
disable=attribute-defined-outside-init
 
     def test_instantiation(self):
@@ -81,6 +81,19 @@
             self.query.endpoint = 'resources'
 
 
+def test_puppetdb_query_init_v4():
+    """Instantianting PuppetDBQuery with the API version 4 should properly 
initialize the base URL."""
+    query = puppetdb.PuppetDBQuery({'puppetdb': {'api_version': 4}})
+    assert isinstance(query, BaseQuery)
+    assert query.url == 'https://localhost:443/pdb/query/v4/'
+
+
+def test_puppetdb_query_init_invalid():
+    """Instantiating PuppetDBQuery with an unsupported API version should 
raise InvalidQueryError."""
+    with pytest.raises(InvalidQueryError, match='Unsupported PuppetDB API 
version'):
+        puppetdb.PuppetDBQuery({'puppetdb': {'api_version': 99}})
+
+
 @mock.patch.object(puppetdb.PuppetDBQuery, '_api_call')
 class TestPuppetDBQueryBuild(object):
     """PuppetDB backend query build test class."""
diff --git a/doc/examples/config.yaml b/doc/examples/config.yaml
index 0db6974..df437a2 100644
--- a/doc/examples/config.yaml
+++ b/doc/examples/config.yaml
@@ -16,6 +16,7 @@
 puppetdb:
     host: puppetdb.local
     port: 443
+    api_version: 4  # Support versions are v3 and v4. If not specified, v3 
will be used.
     urllib3_disable_warnings:  # List of Python urllib3 exceptions to ignore
         - SubjectAltNameWarning
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5fac12efcd89ea1b42d19aa68deff96df2074424
Gerrit-PatchSet: 1
Gerrit-Project: operations/software/cumin
Gerrit-Branch: master
Gerrit-Owner: Volans <rcocci...@wikimedia.org>

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

Reply via email to