1
0
Fork 0
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:
Alexei Znamensky 2023-04-28 20:02:07 +12:00 committed by GitHub
parent 0a26b6d48d
commit d4df585370
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 23 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- snap_alias - refactor code to module utils (https://github.com/ansible-collections/community.general/pull/6441).

View 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

View file

@ -86,15 +86,8 @@ snap_aliases:
import re 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 from ansible_collections.community.general.plugins.module_utils.module_helper import StateModuleHelper
from ansible_collections.community.general.plugins.module_utils.snap import snap_runner
_state_map = dict(
present='alias',
absent='unalias',
info='aliases',
)
class SnapAlias(StateModuleHelper): class SnapAlias(StateModuleHelper):
@ -113,18 +106,12 @@ class SnapAlias(StateModuleHelper):
supports_check_mode=True, 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): def _aliases(self):
n = self.vars.name n = self.vars.name
return {n: self._get_aliases_for(n)} if n else self._get_aliases() return {n: self._get_aliases_for(n)} if n else self._get_aliases()
def __init_module__(self): 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) self.vars.set("snap_aliases", self._aliases(), change=True, diff=True)
def __quit_module__(self): def __quit_module__(self):
@ -141,8 +128,8 @@ class SnapAlias(StateModuleHelper):
results[snap] = results.get(snap, []) + [alias] results[snap] = results.get(snap, []) + [alias]
return results return results
with self.runner("state name", check_rc=True, output_process=process) as ctx: with self.runner("state_alias name", check_rc=True, output_process=process) as ctx:
aliases = ctx.run(state="info") aliases = ctx.run(state_alias="info")
if self.verbosity >= 4: if self.verbosity >= 4:
self.vars.get_aliases_run_info = ctx.run_info self.vars.get_aliases_run_info = ctx.run_info
return aliases return aliases
@ -164,8 +151,8 @@ class SnapAlias(StateModuleHelper):
for _alias in self.vars.alias: for _alias in self.vars.alias:
if not self._has_alias(self.vars.name, _alias): if not self._has_alias(self.vars.name, _alias):
self.changed = True self.changed = True
with self.runner("state name alias", check_mode_skip=True) as ctx: with self.runner("state_alias name alias", check_mode_skip=True) as ctx:
ctx.run(alias=_alias) ctx.run(state_alias=self.vars.state, alias=_alias)
if self.verbosity >= 4: if self.verbosity >= 4:
self.vars.run_info = ctx.run_info self.vars.run_info = ctx.run_info
@ -173,16 +160,16 @@ class SnapAlias(StateModuleHelper):
if not self.vars.alias: if not self.vars.alias:
if self._has_alias(self.vars.name): if self._has_alias(self.vars.name):
self.changed = True self.changed = True
with self.runner("state name", check_mode_skip=True) as ctx: with self.runner("state_alias name", check_mode_skip=True) as ctx:
ctx.run() ctx.run(state_alias=self.vars.state)
if self.verbosity >= 4: if self.verbosity >= 4:
self.vars.run_info = ctx.run_info self.vars.run_info = ctx.run_info
else: else:
for _alias in self.vars.alias: for _alias in self.vars.alias:
if self._has_alias(self.vars.name, _alias): if self._has_alias(self.vars.name, _alias):
self.changed = True self.changed = True
with self.runner("state alias", check_mode_skip=True) as ctx: with self.runner("state_alias alias", check_mode_skip=True) as ctx:
ctx.run(alias=_alias) ctx.run(state_alias=self.vars.state, alias=_alias)
if self.verbosity >= 4: if self.verbosity >= 4:
self.vars.run_info = ctx.run_info self.vars.run_info = ctx.run_info