Skip to content

Commit 93dbdb2

Browse files
jonathan lungfelixfontein
andcommitted
LastPass lookup: use config manager, improve documentation
Co-authored-by: Felix Fontein <felix@fontein.de>
1 parent 76b235c commit 93dbdb2

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
trivial:
2+
- lastpass - use config manager API in tests; improve documentation.

plugins/lookup/lastpass.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,24 @@
1111
- Andrew Zenk (!UNKNOWN) <azenk@umn.edu>
1212
requirements:
1313
- lpass (command line utility)
14-
- must have already logged into lastpass
15-
short_description: fetch data from lastpass
14+
- must have already logged into LastPass
15+
short_description: fetch data from LastPass
1616
description:
17-
- use the lpass command line utility to fetch specific fields from lastpass
17+
- Use the lpass command line utility to fetch specific fields from LastPass.
1818
options:
1919
_terms:
20-
description: key from which you want to retrieve the field
21-
required: True
20+
description: Key from which you want to retrieve the field.
21+
required: true
22+
type: list
23+
elements: str
2224
field:
23-
description: field to return from lastpass
25+
description: Field to return from LastPass.
2426
default: 'password'
27+
type: str
2528
'''
2629

2730
EXAMPLES = """
28-
- name: get 'custom_field' from lastpass entry 'entry-name'
31+
- name: get 'custom_field' from LastPass entry 'entry-name'
2932
ansible.builtin.debug:
3033
msg: "{{ lookup('community.general.lastpass', 'entry-name', field='custom_field') }}"
3134
"""
@@ -88,12 +91,14 @@ def get_field(self, key, field):
8891
class LookupModule(LookupBase):
8992

9093
def run(self, terms, variables=None, **kwargs):
94+
self.set_options(var_options=variables, direct=kwargs)
95+
field = self.get_option('field')
96+
9197
lp = LPass()
9298

9399
if not lp.logged_in:
94-
raise AnsibleError("Not logged into lastpass: please run 'lpass login' first")
100+
raise AnsibleError("Not logged into LastPass: please run 'lpass login' first")
95101

96-
field = kwargs.get('field', 'password')
97102
values = []
98103
for term in terms:
99104
values.append(lp.get_field(term, field))

tests/unit/plugins/lookup/test_lastpass.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from ansible.errors import AnsibleError
2828
from ansible.module_utils import six
29+
from ansible.plugins.loader import lookup_loader
2930
from ansible_collections.community.general.plugins.lookup.lastpass import LookupModule, LPass, LPassException
3031

3132

@@ -126,6 +127,9 @@ class LoggedOutMockLPass(MockLPass):
126127

127128
class TestLPass(unittest.TestCase):
128129

130+
def setUp(self):
131+
self.lookup = lookup_loader.get('community.general.lastpass')
132+
129133
def test_lastpass_cli_path(self):
130134
lp = MockLPass(path='/dev/null')
131135
self.assertEqual('/dev/null', lp.cli_path)
@@ -158,30 +162,27 @@ def test_lastpass_show(self):
158162

159163
class TestLastpassPlugin(unittest.TestCase):
160164

165+
def setUp(self):
166+
self.lookup = lookup_loader.get('community.general.lastpass')
167+
161168
@patch('ansible_collections.community.general.plugins.lookup.lastpass.LPass', new=MockLPass)
162169
def test_lastpass_plugin_normal(self):
163-
lookup_plugin = LookupModule()
164-
165170
for entry in MOCK_ENTRIES:
166171
entry_id = entry.get('id')
167172
for k, v in six.iteritems(entry):
168173
self.assertEqual(v.strip(),
169-
lookup_plugin.run([entry_id], field=k)[0])
174+
self.lookup.run([entry_id], field=k)[0])
170175

171176
@patch('ansible_collections.community.general.plugins.lookup.lastpass.LPass', LoggedOutMockLPass)
172177
def test_lastpass_plugin_logged_out(self):
173-
lookup_plugin = LookupModule()
174-
175178
entry = MOCK_ENTRIES[0]
176179
entry_id = entry.get('id')
177180
with self.assertRaises(AnsibleError):
178-
lookup_plugin.run([entry_id], field='password')
181+
self.lookup.run([entry_id], field='password')
179182

180183
@patch('ansible_collections.community.general.plugins.lookup.lastpass.LPass', DisconnectedMockLPass)
181184
def test_lastpass_plugin_disconnected(self):
182-
lookup_plugin = LookupModule()
183-
184185
entry = MOCK_ENTRIES[0]
185186
entry_id = entry.get('id')
186187
with self.assertRaises(AnsibleError):
187-
lookup_plugin.run([entry_id], field='password')
188+
self.lookup.run([entry_id], field='password')

0 commit comments

Comments
 (0)