module_utils/basic: add generic method for checking for missing params when argspec can not be used.
This commit is contained in:
parent
c3dd0213ef
commit
11522b22c3
1 changed files with 35 additions and 22 deletions
|
@ -3,27 +3,27 @@
|
||||||
# Modules you write using this snippet, which is embedded dynamically by Ansible
|
# Modules you write using this snippet, which is embedded dynamically by Ansible
|
||||||
# still belong to the author of the module, and may assign their own license
|
# still belong to the author of the module, and may assign their own license
|
||||||
# to the complete work.
|
# to the complete work.
|
||||||
#
|
#
|
||||||
# Copyright (c), Michael DeHaan <michael.dehaan@gmail.com>, 2012-2013
|
# Copyright (c), Michael DeHaan <michael.dehaan@gmail.com>, 2012-2013
|
||||||
# All rights reserved.
|
# All rights reserved.
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without modification,
|
# Redistribution and use in source and binary forms, with or without modification,
|
||||||
# are permitted provided that the following conditions are met:
|
# are permitted provided that the following conditions are met:
|
||||||
#
|
#
|
||||||
# * Redistributions of source code must retain the above copyright
|
# * Redistributions of source code must retain the above copyright
|
||||||
# notice, this list of conditions and the following disclaimer.
|
# notice, this list of conditions and the following disclaimer.
|
||||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
# this list of conditions and the following disclaimer in the documentation
|
# this list of conditions and the following disclaimer in the documentation
|
||||||
# and/or other materials provided with the distribution.
|
# and/or other materials provided with the distribution.
|
||||||
#
|
#
|
||||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||||
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||||
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||||
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
@ -186,7 +186,7 @@ except ImportError:
|
||||||
try:
|
try:
|
||||||
from ast import literal_eval as _literal_eval
|
from ast import literal_eval as _literal_eval
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# a replacement for literal_eval that works with python 2.4. from:
|
# a replacement for literal_eval that works with python 2.4. from:
|
||||||
# https://mail.python.org/pipermail/python-list/2009-September/551880.html
|
# https://mail.python.org/pipermail/python-list/2009-September/551880.html
|
||||||
# which is essentially a cut/paste from an earlier (2.6) version of python's
|
# which is essentially a cut/paste from an earlier (2.6) version of python's
|
||||||
# ast.py
|
# ast.py
|
||||||
|
@ -944,14 +944,14 @@ class AnsibleModule(object):
|
||||||
else:
|
else:
|
||||||
raise ValueError("bad symbolic permission for mode: %s" % mode)
|
raise ValueError("bad symbolic permission for mode: %s" % mode)
|
||||||
return new_mode
|
return new_mode
|
||||||
|
|
||||||
def _apply_operation_to_mode(self, user, operator, mode_to_apply, current_mode):
|
def _apply_operation_to_mode(self, user, operator, mode_to_apply, current_mode):
|
||||||
if operator == '=':
|
if operator == '=':
|
||||||
if user == 'u': mask = stat.S_IRWXU | stat.S_ISUID
|
if user == 'u': mask = stat.S_IRWXU | stat.S_ISUID
|
||||||
elif user == 'g': mask = stat.S_IRWXG | stat.S_ISGID
|
elif user == 'g': mask = stat.S_IRWXG | stat.S_ISGID
|
||||||
elif user == 'o': mask = stat.S_IRWXO | stat.S_ISVTX
|
elif user == 'o': mask = stat.S_IRWXO | stat.S_ISVTX
|
||||||
|
|
||||||
# mask out u, g, or o permissions from current_mode and apply new permissions
|
# mask out u, g, or o permissions from current_mode and apply new permissions
|
||||||
inverse_mask = mask ^ PERM_BITS
|
inverse_mask = mask ^ PERM_BITS
|
||||||
new_mode = (current_mode & inverse_mask) | mode_to_apply
|
new_mode = (current_mode & inverse_mask) | mode_to_apply
|
||||||
elif operator == '+':
|
elif operator == '+':
|
||||||
|
@ -959,10 +959,10 @@ class AnsibleModule(object):
|
||||||
elif operator == '-':
|
elif operator == '-':
|
||||||
new_mode = current_mode - (current_mode & mode_to_apply)
|
new_mode = current_mode - (current_mode & mode_to_apply)
|
||||||
return new_mode
|
return new_mode
|
||||||
|
|
||||||
def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms):
|
def _get_octal_mode_from_symbolic_perms(self, path_stat, user, perms):
|
||||||
prev_mode = stat.S_IMODE(path_stat.st_mode)
|
prev_mode = stat.S_IMODE(path_stat.st_mode)
|
||||||
|
|
||||||
is_directory = stat.S_ISDIR(path_stat.st_mode)
|
is_directory = stat.S_ISDIR(path_stat.st_mode)
|
||||||
has_x_permissions = (prev_mode & EXEC_PERM_BITS) > 0
|
has_x_permissions = (prev_mode & EXEC_PERM_BITS) > 0
|
||||||
apply_X_permission = is_directory or has_x_permissions
|
apply_X_permission = is_directory or has_x_permissions
|
||||||
|
@ -1480,7 +1480,7 @@ class AnsibleModule(object):
|
||||||
raise
|
raise
|
||||||
return cwd
|
return cwd
|
||||||
except:
|
except:
|
||||||
# we don't have access to the cwd, probably because of sudo.
|
# we don't have access to the cwd, probably because of sudo.
|
||||||
# Try and move to a neutral location to prevent errors
|
# Try and move to a neutral location to prevent errors
|
||||||
for cwd in [os.path.expandvars('$HOME'), tempfile.gettempdir()]:
|
for cwd in [os.path.expandvars('$HOME'), tempfile.gettempdir()]:
|
||||||
try:
|
try:
|
||||||
|
@ -1489,9 +1489,9 @@ class AnsibleModule(object):
|
||||||
return cwd
|
return cwd
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
# we won't error here, as it may *not* be a problem,
|
# we won't error here, as it may *not* be a problem,
|
||||||
# and we don't want to break modules unnecessarily
|
# and we don't want to break modules unnecessarily
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_bin_path(self, arg, required=False, opt_dirs=[]):
|
def get_bin_path(self, arg, required=False, opt_dirs=[]):
|
||||||
'''
|
'''
|
||||||
|
@ -1584,6 +1584,19 @@ class AnsibleModule(object):
|
||||||
print(self.jsonify(kwargs))
|
print(self.jsonify(kwargs))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
def fail_on_missing_params(self, required_params=None):
|
||||||
|
''' This is for checking for required params when we can not check via argspec because we
|
||||||
|
need more information than is simply given in the argspec.
|
||||||
|
'''
|
||||||
|
if not required_params:
|
||||||
|
return
|
||||||
|
missing_params = []
|
||||||
|
for required_param in required_params:
|
||||||
|
if not self.params.get(required_param):
|
||||||
|
missing_params.append(required_param)
|
||||||
|
if missing_params:
|
||||||
|
self.fail_json(msg="missing required arguments: %s" % ','.join(missing_params))
|
||||||
|
|
||||||
def digest_from_file(self, filename, algorithm):
|
def digest_from_file(self, filename, algorithm):
|
||||||
''' Return hex digest of local file for a digest_method specified by name, or None if file is not present. '''
|
''' Return hex digest of local file for a digest_method specified by name, or None if file is not present. '''
|
||||||
if not os.path.exists(filename):
|
if not os.path.exists(filename):
|
||||||
|
|
Loading…
Reference in a new issue