From 2846242e9560c1923c4be11fd1ef00c24c366dfc Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 1 Aug 2022 09:59:36 +0200 Subject: [PATCH] lastpass lookup: use config manager, improve documentation (#5022) (#5047) * LastPass lookup: use config manager, improve documentation Co-authored-by: Felix Fontein * Update changelogs/fragments/5022-lastpass-lookup-cleanup.yml Co-authored-by: Felix Fontein Co-authored-by: jonathan lung Co-authored-by: Felix Fontein (cherry picked from commit e8e6b9bbd7b7f970638a453b7b0b81af13885f42) Co-authored-by: Jonathan Lung --- .../5022-lastpass-lookup-cleanup.yml | 2 ++ plugins/lookup/lastpass.py | 23 +++++++++++-------- tests/unit/plugins/lookup/test_lastpass.py | 19 +++++++-------- 3 files changed, 26 insertions(+), 18 deletions(-) create mode 100644 changelogs/fragments/5022-lastpass-lookup-cleanup.yml diff --git a/changelogs/fragments/5022-lastpass-lookup-cleanup.yml b/changelogs/fragments/5022-lastpass-lookup-cleanup.yml new file mode 100644 index 0000000000..b9f96d5b9e --- /dev/null +++ b/changelogs/fragments/5022-lastpass-lookup-cleanup.yml @@ -0,0 +1,2 @@ +minor_changes: + - lastpass - use config manager for handling plugin options (https://github.com/ansible-collections/community.general/pull/5022). \ No newline at end of file diff --git a/plugins/lookup/lastpass.py b/plugins/lookup/lastpass.py index 920d33176f..d3f04e6c4f 100644 --- a/plugins/lookup/lastpass.py +++ b/plugins/lookup/lastpass.py @@ -11,21 +11,24 @@ DOCUMENTATION = ''' - Andrew Zenk (!UNKNOWN) requirements: - lpass (command line utility) - - must have already logged into lastpass - short_description: fetch data from lastpass + - must have already logged into LastPass + short_description: fetch data from LastPass description: - - use the lpass command line utility to fetch specific fields from lastpass + - Use the lpass command line utility to fetch specific fields from LastPass. options: _terms: - description: key from which you want to retrieve the field - required: True + description: Key from which you want to retrieve the field. + required: true + type: list + elements: str field: - description: field to return from lastpass + description: Field to return from LastPass. default: 'password' + type: str ''' EXAMPLES = """ -- name: get 'custom_field' from lastpass entry 'entry-name' +- name: get 'custom_field' from LastPass entry 'entry-name' ansible.builtin.debug: msg: "{{ lookup('community.general.lastpass', 'entry-name', field='custom_field') }}" """ @@ -88,12 +91,14 @@ class LPass(object): class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): + self.set_options(var_options=variables, direct=kwargs) + field = self.get_option('field') + lp = LPass() if not lp.logged_in: - raise AnsibleError("Not logged into lastpass: please run 'lpass login' first") + raise AnsibleError("Not logged into LastPass: please run 'lpass login' first") - field = kwargs.get('field', 'password') values = [] for term in terms: values.append(lp.get_field(term, field)) diff --git a/tests/unit/plugins/lookup/test_lastpass.py b/tests/unit/plugins/lookup/test_lastpass.py index cce693d347..7002d48762 100644 --- a/tests/unit/plugins/lookup/test_lastpass.py +++ b/tests/unit/plugins/lookup/test_lastpass.py @@ -26,6 +26,7 @@ from ansible_collections.community.general.tests.unit.compat.mock import patch from ansible.errors import AnsibleError from ansible.module_utils import six +from ansible.plugins.loader import lookup_loader from ansible_collections.community.general.plugins.lookup.lastpass import LookupModule, LPass, LPassException @@ -126,6 +127,9 @@ class LoggedOutMockLPass(MockLPass): class TestLPass(unittest.TestCase): + def setUp(self): + self.lookup = lookup_loader.get('community.general.lastpass') + def test_lastpass_cli_path(self): lp = MockLPass(path='/dev/null') self.assertEqual('/dev/null', lp.cli_path) @@ -158,30 +162,27 @@ class TestLPass(unittest.TestCase): class TestLastpassPlugin(unittest.TestCase): + def setUp(self): + self.lookup = lookup_loader.get('community.general.lastpass') + @patch('ansible_collections.community.general.plugins.lookup.lastpass.LPass', new=MockLPass) def test_lastpass_plugin_normal(self): - lookup_plugin = LookupModule() - for entry in MOCK_ENTRIES: entry_id = entry.get('id') for k, v in six.iteritems(entry): self.assertEqual(v.strip(), - lookup_plugin.run([entry_id], field=k)[0]) + self.lookup.run([entry_id], field=k)[0]) @patch('ansible_collections.community.general.plugins.lookup.lastpass.LPass', LoggedOutMockLPass) def test_lastpass_plugin_logged_out(self): - lookup_plugin = LookupModule() - entry = MOCK_ENTRIES[0] entry_id = entry.get('id') with self.assertRaises(AnsibleError): - lookup_plugin.run([entry_id], field='password') + self.lookup.run([entry_id], field='password') @patch('ansible_collections.community.general.plugins.lookup.lastpass.LPass', DisconnectedMockLPass) def test_lastpass_plugin_disconnected(self): - lookup_plugin = LookupModule() - entry = MOCK_ENTRIES[0] entry_id = entry.get('id') with self.assertRaises(AnsibleError): - lookup_plugin.run([entry_id], field='password') + self.lookup.run([entry_id], field='password')