1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-11 14:35:06 +00:00

Multiple modules using ModuleHelper (#4674)

* Multiple modules using ModuleHelper

Replaced raising exception with calling method do_raise() in MH.
Removed the importing of the exception class.

* added changelog fragment
This commit is contained in:
Alexei Znamensky 2022-05-23 17:19:24 +12:00 committed by GitHub
parent 319c29c2a2
commit 6052776de1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 35 deletions

View file

@ -134,7 +134,7 @@ EXAMPLES = '''
import os
from ansible_collections.community.general.plugins.module_utils.module_helper import (
ModuleHelper, CmdMixin, ArgFormat, ModuleHelperException
ModuleHelper, CmdMixin, ArgFormat
)
@ -171,10 +171,10 @@ class CPANMinus(CmdMixin, ModuleHelper):
v = self.vars
if v.mode == "compatibility":
if v.name_check:
raise ModuleHelperException("Parameter name_check can only be used with mode=new")
self.do_raise("Parameter name_check can only be used with mode=new")
else:
if v.name and v.from_path:
raise ModuleHelperException("Parameters 'name' and 'from_path' are mutually exclusive when 'mode=new'")
self.do_raise("Parameters 'name' and 'from_path' are mutually exclusive when 'mode=new'")
self.command = self.module.get_bin_path(v.executable if v.executable else self.command)
self.vars.set("binary", self.command)
@ -190,17 +190,16 @@ class CPANMinus(CmdMixin, ModuleHelper):
return rc == 0
@staticmethod
def sanitize_pkg_spec_version(pkg_spec, version):
def sanitize_pkg_spec_version(self, pkg_spec, version):
if version is None:
return pkg_spec
if pkg_spec.endswith('.tar.gz'):
raise ModuleHelperException(msg="parameter 'version' must not be used when installing from a file")
self.do_raise(msg="parameter 'version' must not be used when installing from a file")
if os.path.isdir(pkg_spec):
raise ModuleHelperException(msg="parameter 'version' must not be used when installing from a directory")
self.do_raise(msg="parameter 'version' must not be used when installing from a directory")
if pkg_spec.endswith('.git'):
if version.startswith('~'):
raise ModuleHelperException(msg="operator '~' not allowed in version parameter when installing from git repository")
self.do_raise(msg="operator '~' not allowed in version parameter when installing from git repository")
version = version if version.startswith('@') else '@' + version
elif version[0] not in ('@', '~'):
version = '~' + version
@ -228,7 +227,7 @@ class CPANMinus(CmdMixin, ModuleHelper):
def process_command_output(self, rc, out, err):
if self.vars.mode == "compatibility" and rc != 0:
raise ModuleHelperException(msg=err, cmd=self.vars.cmd_args)
self.do_raise(msg=err, cmd=self.vars.cmd_args)
return 'is up to date' not in err and 'is up to date' not in out

View file

@ -134,7 +134,7 @@ EXAMPLES = '''
import json
from ansible_collections.community.general.plugins.module_utils.module_helper import (
CmdStateModuleHelper, ArgFormat, ModuleHelperException
CmdStateModuleHelper, ArgFormat
)
from ansible.module_utils.facts.compat import ansible_facts
@ -153,9 +153,8 @@ class PipX(CmdStateModuleHelper):
module = dict(
argument_spec=dict(
state=dict(type='str', default='install',
choices=[
'present', 'absent', 'install', 'uninstall', 'uninstall_all',
'inject', 'upgrade', 'upgrade_all', 'reinstall', 'reinstall_all']),
choices=['present', 'absent', 'install', 'uninstall', 'uninstall_all',
'inject', 'upgrade', 'upgrade_all', 'reinstall', 'reinstall_all']),
name=dict(type='str'),
source=dict(type='str'),
install_deps=dict(type='bool', default=False),
@ -247,8 +246,7 @@ class PipX(CmdStateModuleHelper):
def state_upgrade(self):
if not self.vars.application:
raise ModuleHelperException(
"Trying to upgrade a non-existent application: {0}".format(self.vars.name))
self.do_raise("Trying to upgrade a non-existent application: {0}".format(self.vars.name))
if self.vars.force:
self.changed = True
if not self.module.check_mode:
@ -262,16 +260,14 @@ class PipX(CmdStateModuleHelper):
def state_reinstall(self):
if not self.vars.application:
raise ModuleHelperException(
"Trying to reinstall a non-existent application: {0}".format(self.vars.name))
self.do_raise("Trying to reinstall a non-existent application: {0}".format(self.vars.name))
self.changed = True
if not self.module.check_mode:
self.run_command(params=['state', 'name', 'python'])
def state_inject(self):
if not self.vars.application:
raise ModuleHelperException(
"Trying to inject packages into a non-existent application: {0}".format(self.vars.name))
self.do_raise("Trying to inject packages into a non-existent application: {0}".format(self.vars.name))
if self.vars.force:
self.changed = True
if not self.module.check_mode: