Debian Codegraph @

test/integration/targets/config/lookup_plugins/casting.py

raw
1 # (c) 2021 Ansible Project
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 from __future__ import annotations
5
6
7 DOCUMENTATION = """
8 name: casting
9 author: Ansible Core Team
10 version_added: hysterical
11 short_description: returns what you gave it
12 description:
13 - this is mostly a noop
14 options:
15 _terms:
16 description: stuff to pass through
17 test_list:
18 description: does nothing, just for testing values
19 type: list
20 test_int:
21 description: does nothing, just to test casting
22 type: int
23 test_bool:
24 description: does nothing, just to test casting
25 type: bool
26 test_str:
27 description: does nothing, just to test casting
28 type: str
29 """
30
31 EXAMPLES = """
32 - name: like some other plugins, this is mostly useless
33 debug: msg={{ q('casting', [1,2,3])}}
34 """
35
36 RETURN = """
37 _list:
38 description: basically the same as you fed in
39 type: list
40 elements: raw
41 """
42
43 from ansible.plugins.lookup import LookupBase
44
45
46 class LookupModule(LookupBase):
47
48 def run(self, terms, variables=None, **kwargs):
49
50 self.set_options(var_options=variables, direct=kwargs)
51
52 for cast in (list, int, bool, str):
53 option = 'test_%s' % str(cast).replace("<class '", '').replace("'>", '')
54 value = self.get_option(option)
55 if value is None or isinstance(value, cast):
56 continue
57 raise Exception('%s is not a %s: got %s/%s' % (option, cast, type(value), value))
58
59 return terms