mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-01 05:49:09 -07:00
Add arg and doc validation for PowerShell modules (#53615)
* Add arg and doc validation for PowerShell modules * Verify if pwsh exists before running it
This commit is contained in:
parent
da9b19cef7
commit
f297229b52
13 changed files with 159 additions and 11 deletions
|
@ -17,12 +17,17 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import imp
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from contextlib import contextmanager
|
||||
|
||||
from ansible.module_utils.six import reraise
|
||||
|
||||
from utils import find_executable
|
||||
|
||||
|
||||
class AnsibleModuleCallError(RuntimeError):
|
||||
pass
|
||||
|
@ -75,7 +80,29 @@ def setup_env(filename):
|
|||
del sys.modules[k]
|
||||
|
||||
|
||||
def get_argument_spec(filename):
|
||||
def get_ps_argument_spec(filename):
|
||||
# This uses a very small skeleton of Ansible.Basic.AnsibleModule to return the argspec defined by the module. This
|
||||
# is pretty rudimentary and will probably require something better going forward.
|
||||
pwsh = find_executable('pwsh')
|
||||
if not pwsh:
|
||||
raise FileNotFoundError('Required program for PowerShell arg spec inspection "pwsh" not found.')
|
||||
|
||||
script_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ps_argspec.ps1')
|
||||
proc = subprocess.Popen([script_path, filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
|
||||
stdout, stderr = proc.communicate()
|
||||
|
||||
if proc.returncode != 0:
|
||||
raise AnsibleModuleImportError(stderr.decode('utf-8'))
|
||||
|
||||
kwargs = json.loads(stdout)
|
||||
|
||||
# the validate-modules code expects the options spec to be under the argument_spec key not options as set in PS
|
||||
kwargs['argument_spec'] = kwargs.pop('options', {})
|
||||
|
||||
return kwargs['argument_spec'], (), kwargs
|
||||
|
||||
|
||||
def get_py_argument_spec(filename):
|
||||
with setup_env(filename) as fake:
|
||||
try:
|
||||
# We use ``module`` here instead of ``__main__``
|
||||
|
@ -91,8 +118,16 @@ def get_argument_spec(filename):
|
|||
|
||||
try:
|
||||
try:
|
||||
# for ping kwargs == {'argument_spec':{'data':{'type':'str','default':'pong'}}, 'supports_check_mode':True}
|
||||
return fake.kwargs['argument_spec'], fake.args, fake.kwargs
|
||||
except KeyError:
|
||||
return fake.args[0], fake.args, fake.kwargs
|
||||
except TypeError:
|
||||
return {}, (), {}
|
||||
|
||||
|
||||
def get_argument_spec(filename):
|
||||
if filename.endswith('.py'):
|
||||
return get_py_argument_spec(filename)
|
||||
else:
|
||||
return get_ps_argument_spec(filename)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue