Add ansible action plugin `collect` 57/33057/3
authorYujun Zhang <zhang.yujunz@zte.com.cn>
Thu, 6 Apr 2017 09:18:54 +0000 (17:18 +0800)
committerYujun Zhang <zhang.yujunz@zte.com.cn>
Thu, 6 Apr 2017 10:07:58 +0000 (18:07 +0800)
- this plugin collects information or metrics from string
- it is reworked from qtip.collector.parser.grep
- the captured subgroups are always list even only one match found

Change-Id: I1def3d7b40c7928b503fae1be531976a13e5d0be
Signed-off-by: Yujun Zhang <zhang.yujunz@zte.com.cn>
qtip/ansible_library/plugins/__init__.py [new file with mode: 0644]
qtip/ansible_library/plugins/action/__init__.py [new file with mode: 0644]
qtip/ansible_library/plugins/action/collect.py [new file with mode: 0644]
tests/unit/ansible_library/plugins/action/collect_test.py [new file with mode: 0644]

diff --git a/qtip/ansible_library/plugins/__init__.py b/qtip/ansible_library/plugins/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/qtip/ansible_library/plugins/action/__init__.py b/qtip/ansible_library/plugins/action/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/qtip/ansible_library/plugins/action/collect.py b/qtip/ansible_library/plugins/action/collect.py
new file mode 100644 (file)
index 0000000..88ad0e3
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/python
+
+###############################################################
+# Copyright (c) 2017 ZTE Corporation
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+from collections import defaultdict
+import re
+
+from ansible.plugins.action import ActionBase
+
+
+class ActionModule(ActionBase):
+
+    def run(self, tmp=None, task_vars=None):
+        result = super(ActionModule, self).run(tmp, task_vars)
+
+        if result.get('skipped', False):
+            return result
+
+        string = self._task.args.get('string')
+        patterns = self._task.args.get('patterns')
+
+        return collect(patterns, string)
+
+
+def collect(patterns, string):
+    """collect all named subgroups of the match into a list keyed by subgroup name
+    """
+    captured = defaultdict(list)
+
+    if not isinstance(patterns, list):
+        patterns = [patterns]
+
+    for p in patterns:
+        for match_obj in re.finditer(p, string, re.MULTILINE):
+            for (key, value) in match_obj.groupdict().items():
+                captured[key].append(value)
+
+    return captured
diff --git a/tests/unit/ansible_library/plugins/action/collect_test.py b/tests/unit/ansible_library/plugins/action/collect_test.py
new file mode 100644 (file)
index 0000000..5a9fc8f
--- /dev/null
@@ -0,0 +1,36 @@
+##############################################################################
+# Copyright (c) 2017 ZTE Corp and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+import pytest
+
+from qtip.ansible_library.plugins.action import collect
+
+
+@pytest.fixture
+def string():
+    return """Lorem ipsum dolor sit amet,
+consectetur adipiscing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
+
+Ut enim ad minim veniam,
+quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
+
+Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
+Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
+"""
+
+
+@pytest.mark.parametrize("patterns,expected", [
+    ('not exist', {}),
+    ('Lorem (\S+)', {}),
+    ('nisi ut (?P<name>\S+)', {'name': ['aliquip']}),
+    ('in\s(?P<in>\w+)', {'in': ['reprehenderit', 'voluptate', 'culpa']})
+])
+def test_collect(patterns, string, expected):
+    assert collect.collect(patterns, string) == expected