mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
snap_alias: refactor code out to module utils (#6441)
* snap_alias: refactor code out to module utils * add changelog frag
This commit is contained in:
parent
0a26b6d48d
commit
d4df585370
3 changed files with 43 additions and 23 deletions
2
changelogs/fragments/6441-snap_alias-refactor.yml
Normal file
2
changelogs/fragments/6441-snap_alias-refactor.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- snap_alias - refactor code to module utils (https://github.com/ansible-collections/community.general/pull/6441).
|
31
plugins/module_utils/snap.py
Normal file
31
plugins/module_utils/snap.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2023, Alexei Znamensky <russoz@gmail.com>
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
||||
|
||||
|
||||
_alias_state_map = dict(
|
||||
present='alias',
|
||||
absent='unalias',
|
||||
info='aliases',
|
||||
)
|
||||
|
||||
|
||||
def snap_runner(module, **kwargs):
|
||||
runner = CmdRunner(
|
||||
module,
|
||||
module.get_bin_path("snap"),
|
||||
arg_formats=dict(
|
||||
state_alias=cmd_runner_fmt.as_map(_alias_state_map),
|
||||
name=cmd_runner_fmt.as_list(),
|
||||
alias=cmd_runner_fmt.as_list(),
|
||||
),
|
||||
check_rc=False,
|
||||
**kwargs
|
||||
)
|
||||
return runner
|
|
@ -86,15 +86,8 @@ snap_aliases:
|
|||
|
||||
import re
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
|
||||
from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
|
||||
|
||||
|
||||
_state_map = dict(
|
||||
present='alias',
|
||||
absent='unalias',
|
||||
info='aliases',
|
||||
)
|
||||
from ansible_collections.community.general.plugins.module_utils.snap import snap_runner
|
||||
|
||||
|
||||
class SnapAlias(StateModuleHelper):
|
||||
|
@ -113,18 +106,12 @@ class SnapAlias(StateModuleHelper):
|
|||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
command_args_formats = {
|
||||
"state": cmd_runner_fmt.as_map(_state_map),
|
||||
"name": cmd_runner_fmt.as_list(),
|
||||
"alias": cmd_runner_fmt.as_list(),
|
||||
}
|
||||
|
||||
def _aliases(self):
|
||||
n = self.vars.name
|
||||
return {n: self._get_aliases_for(n)} if n else self._get_aliases()
|
||||
|
||||
def __init_module__(self):
|
||||
self.runner = CmdRunner(self.module, "snap", self.command_args_formats, check_rc=False)
|
||||
self.runner = snap_runner(self.module)
|
||||
self.vars.set("snap_aliases", self._aliases(), change=True, diff=True)
|
||||
|
||||
def __quit_module__(self):
|
||||
|
@ -141,8 +128,8 @@ class SnapAlias(StateModuleHelper):
|
|||
results[snap] = results.get(snap, []) + [alias]
|
||||
return results
|
||||
|
||||
with self.runner("state name", check_rc=True, output_process=process) as ctx:
|
||||
aliases = ctx.run(state="info")
|
||||
with self.runner("state_alias name", check_rc=True, output_process=process) as ctx:
|
||||
aliases = ctx.run(state_alias="info")
|
||||
if self.verbosity >= 4:
|
||||
self.vars.get_aliases_run_info = ctx.run_info
|
||||
return aliases
|
||||
|
@ -164,8 +151,8 @@ class SnapAlias(StateModuleHelper):
|
|||
for _alias in self.vars.alias:
|
||||
if not self._has_alias(self.vars.name, _alias):
|
||||
self.changed = True
|
||||
with self.runner("state name alias", check_mode_skip=True) as ctx:
|
||||
ctx.run(alias=_alias)
|
||||
with self.runner("state_alias name alias", check_mode_skip=True) as ctx:
|
||||
ctx.run(state_alias=self.vars.state, alias=_alias)
|
||||
if self.verbosity >= 4:
|
||||
self.vars.run_info = ctx.run_info
|
||||
|
||||
|
@ -173,16 +160,16 @@ class SnapAlias(StateModuleHelper):
|
|||
if not self.vars.alias:
|
||||
if self._has_alias(self.vars.name):
|
||||
self.changed = True
|
||||
with self.runner("state name", check_mode_skip=True) as ctx:
|
||||
ctx.run()
|
||||
with self.runner("state_alias name", check_mode_skip=True) as ctx:
|
||||
ctx.run(state_alias=self.vars.state)
|
||||
if self.verbosity >= 4:
|
||||
self.vars.run_info = ctx.run_info
|
||||
else:
|
||||
for _alias in self.vars.alias:
|
||||
if self._has_alias(self.vars.name, _alias):
|
||||
self.changed = True
|
||||
with self.runner("state alias", check_mode_skip=True) as ctx:
|
||||
ctx.run(alias=_alias)
|
||||
with self.runner("state_alias alias", check_mode_skip=True) as ctx:
|
||||
ctx.run(state_alias=self.vars.state, alias=_alias)
|
||||
if self.verbosity >= 4:
|
||||
self.vars.run_info = ctx.run_info
|
||||
|
||||
|
|
Loading…
Reference in a new issue