Debian Codegraph @

ansible_collections/community/general/plugins/modules/snap_alias.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2021, Alexei Znamensky (russoz) <[email protected]>
4 #
5 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
6 # SPDX-License-Identifier: GPL-3.0-or-later
7
8 from __future__ import annotations
9
10 DOCUMENTATION = r"""
11 module: snap_alias
12 short_description: Manages snap aliases
13 version_added: 4.0.0
14 description:
15 - Manages snaps aliases.
16 extends_documentation_fragment:
17 - community.general._attributes
18 attributes:
19 check_mode:
20 support: full
21 diff_mode:
22 support: full
23 options:
24 state:
25 description:
26 - Desired state of the alias.
27 type: str
28 choices: [absent, present]
29 default: present
30 name:
31 description:
32 - Name of the snap.
33 type: str
34 alias:
35 description:
36 - Aliases to be created or removed.
37 type: list
38 elements: str
39 aliases: [aliases]
40
41 author:
42 - Alexei Znamensky (@russoz) <[email protected]>
43
44 seealso:
45 - module: community.general.snap
46 """
47
48 EXAMPLES = r"""
49 # Install "foo" and "bar" snap
50 - name: Create snap alias
51 community.general.snap_alias:
52 name: hello-world
53 alias: hw
54
55 - name: Create multiple aliases
56 community.general.snap_alias:
57 name: hello-world
58 aliases:
59 - hw
60 - hw2
61 - hw3
62 state: present # optional
63
64 - name: Remove one specific aliases
65 community.general.snap_alias:
66 name: hw
67 state: absent
68
69 - name: Remove all aliases for snap
70 community.general.snap_alias:
71 name: hello-world
72 state: absent
73 """
74
75 RETURN = r"""
76 snap_aliases:
77 description: The snap aliases after execution. If called in check mode, then the list represents the state before execution.
78 type: list
79 elements: str
80 returned: always
81 version:
82 description: Versions of snap components as reported by C(snap version).
83 type: dict
84 returned: always
85 version_added: 10.3.0
86 """
87
88
89 import re
90
91 from ansible_collections.community.general.plugins.module_utils._module_helper import StateModuleHelper
92 from ansible_collections.community.general.plugins.module_utils._snap import get_version, snap_runner
93
94
95 class SnapAlias(StateModuleHelper):
96 _RE_ALIAS_LIST = re.compile(r"^(?P<snap>\S+)\s+(?P<alias>[\w-]+)\s+.*$")
97
98 module = dict(
99 argument_spec={
100 "state": dict(type="str", choices=["absent", "present"], default="present"),
101 "name": dict(type="str"),
102 "alias": dict(type="list", elements="str", aliases=["aliases"]),
103 },
104 required_if=[
105 ("state", "present", ["name", "alias"]),
106 ("state", "absent", ["name", "alias"], True),
107 ],
108 supports_check_mode=True,
109 )
110
111 def _aliases(self):
112 n = self.vars.name
113 return {n: self._get_aliases_for(n)} if n else self._get_aliases()
114
115 def __init_module__(self):
116 self.runner = snap_runner(self.module)
117 self.vars.version = get_version(self.runner)
118 self.vars.set("snap_aliases", self._aliases(), change=True, diff=True)
119
120 def __quit_module__(self):
121 self.vars.snap_aliases = self._aliases()
122
123 def _get_aliases(self):
124 def process(rc, out, err):
125 if err:
126 return {}
127 aliases = [self._RE_ALIAS_LIST.match(a.strip()) for a in out.splitlines()[1:]]
128 snap_alias_list = [(entry.group("snap"), entry.group("alias")) for entry in aliases]
129 results = {}
130 for snap, alias in snap_alias_list:
131 results[snap] = results.get(snap, []) + [alias]
132 return results
133
134 with self.runner("state_alias name", check_rc=True, output_process=process) as ctx:
135 aliases = ctx.run(state_alias="info")
136 if self.verbosity >= 4:
137 self.vars.get_aliases_run_info = ctx.run_info
138 return aliases
139
140 def _get_aliases_for(self, name):
141 return self._get_aliases().get(name, [])
142
143 def _has_alias(self, name=None, alias=None):
144 if name:
145 if name not in self.vars.snap_aliases:
146 return False
147 if alias is None:
148 return bool(self.vars.snap_aliases[name])
149 return alias in self.vars.snap_aliases[name]
150
151 return any(alias in aliases for aliases in self.vars.snap_aliases.values())
152
153 def state_present(self):
154 for _alias in self.vars.alias:
155 if not self._has_alias(self.vars.name, _alias):
156 self.changed = True
157 with self.runner("state_alias name alias", check_mode_skip=True) as ctx:
158 ctx.run(state_alias=self.vars.state, alias=_alias)
159 if self.verbosity >= 4:
160 self.vars.run_info = ctx.run_info
161
162 def state_absent(self):
163 if not self.vars.alias:
164 if self._has_alias(self.vars.name):
165 self.changed = True
166 with self.runner("state_alias name", check_mode_skip=True) as ctx:
167 ctx.run(state_alias=self.vars.state)
168 if self.verbosity >= 4:
169 self.vars.run_info = ctx.run_info
170 else:
171 for _alias in self.vars.alias:
172 if self._has_alias(self.vars.name, _alias):
173 self.changed = True
174 with self.runner("state_alias alias", check_mode_skip=True) as ctx:
175 ctx.run(state_alias=self.vars.state, alias=_alias)
176 if self.verbosity >= 4:
177 self.vars.run_info = ctx.run_info
178
179
180 def main():
181 SnapAlias.execute()
182
183
184 if __name__ == "__main__":
185 main()