1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

django_createcachetable: new module (#8412)

* django_createcachetabe: new module

* add --noinput arg to testcase

* add module to BOTMETA

* rename module class name

* fix examples documentation

* remove unused config

* adjust version_added
This commit is contained in:
Alexei Znamensky 2024-06-02 08:45:17 +12:00 committed by GitHub
parent 7d72300c36
commit 54df0c9b3a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 141 additions and 4 deletions

2
.github/BOTMETA.yml vendored
View file

@ -506,6 +506,8 @@ files:
ignore: scottanderson42 tastychutney ignore: scottanderson42 tastychutney
labels: django_manage labels: django_manage
maintainers: russoz maintainers: russoz
$modules/django_createcachetable.py:
maintainers: russoz
$modules/django_command.py: $modules/django_command.py:
maintainers: russoz maintainers: russoz
$modules/dnf_versionlock.py: $modules/dnf_versionlock.py:

View file

@ -51,3 +51,12 @@ seealso:
Please make sure that you select the right version of Django in the version selector on that page. Please make sure that you select the right version of Django in the version selector on that page.
link: https://docs.djangoproject.com/en/5.0/ref/django-admin/ link: https://docs.djangoproject.com/en/5.0/ref/django-admin/
''' '''
DATABASE = r'''
options:
database:
description:
- Specify the database to be used.
type: str
default: default
'''

View file

@ -7,6 +7,7 @@ from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
from ansible.module_utils.common.dict_transformations import dict_merge
from ansible_collections.community.general.plugins.module_utils.cmd_runner import cmd_runner_fmt from ansible_collections.community.general.plugins.module_utils.cmd_runner import cmd_runner_fmt
from ansible_collections.community.general.plugins.module_utils.python_runner import PythonRunner from ansible_collections.community.general.plugins.module_utils.python_runner import PythonRunner
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
@ -33,6 +34,18 @@ _django_std_arg_fmts = dict(
skip_checks=cmd_runner_fmt.as_bool("--skip-checks"), skip_checks=cmd_runner_fmt.as_bool("--skip-checks"),
) )
_django_database_args = dict(
database=dict(type="str", default="default"),
)
_args_menu = dict(
std=(django_std_args, _django_std_arg_fmts),
database=(_django_database_args, {"database": cmd_runner_fmt.as_opt_eq_val("--database")}),
noinput=({}, {"noinput": cmd_runner_fmt.as_fixed("--noinput")}),
dry_run=({}, {"dry_run": cmd_runner_fmt.as_bool("--dry-run")}),
check=({}, {"check": cmd_runner_fmt.as_bool("--check")}),
)
class _DjangoRunner(PythonRunner): class _DjangoRunner(PythonRunner):
def __init__(self, module, arg_formats=None, **kwargs): def __init__(self, module, arg_formats=None, **kwargs):
@ -55,15 +68,30 @@ class DjangoModuleHelper(ModuleHelper):
arg_formats = {} arg_formats = {}
django_admin_arg_order = () django_admin_arg_order = ()
use_old_vardict = False use_old_vardict = False
_django_args = []
_check_mode_arg = ""
def __init__(self): def __init__(self):
argument_spec = dict(django_std_args) self.module["argument_spec"], self.arg_formats = self._build_args(self.module.get("argument_spec", {}),
argument_spec.update(self.module.get("argument_spec", {})) self.arg_formats,
self.module["argument_spec"] = argument_spec *(["std"] + self._django_args))
super(DjangoModuleHelper, self).__init__(self.module) super(DjangoModuleHelper, self).__init__(self.module)
if self.django_admin_cmd is not None: if self.django_admin_cmd is not None:
self.vars.command = self.django_admin_cmd self.vars.command = self.django_admin_cmd
@staticmethod
def _build_args(arg_spec, arg_format, *names):
res_arg_spec = {}
res_arg_fmts = {}
for name in names:
args, fmts = _args_menu[name]
res_arg_spec = dict_merge(res_arg_spec, args)
res_arg_fmts = dict_merge(res_arg_fmts, fmts)
res_arg_spec = dict_merge(res_arg_spec, arg_spec)
res_arg_fmts = dict_merge(res_arg_fmts, arg_format)
return res_arg_spec, res_arg_fmts
def __run__(self): def __run__(self):
runner = _DjangoRunner(self.module, runner = _DjangoRunner(self.module,
default_args_order=self.django_admin_arg_order, default_args_order=self.django_admin_arg_order,
@ -71,7 +99,10 @@ class DjangoModuleHelper(ModuleHelper):
venv=self.vars.venv, venv=self.vars.venv,
check_rc=True) check_rc=True)
with runner() as ctx: with runner() as ctx:
results = ctx.run() run_params = self.vars.as_dict()
if self._check_mode_arg:
run_params.update({self._check_mode_arg: self.check_mode})
results = ctx.run(**run_params)
self.vars.stdout = ctx.results_out self.vars.stdout = ctx.results_out
self.vars.stderr = ctx.results_err self.vars.stderr = ctx.results_err
self.vars.cmd = ctx.cmd self.vars.cmd = ctx.cmd

View file

@ -0,0 +1,67 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2024, 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
DOCUMENTATION = """
module: django_createcachetable
author:
- Alexei Znamensky (@russoz)
short_description: Wrapper for C(django-admin createcachetable)
version_added: 9.1.0
description:
- This module is a wrapper for the execution of C(django-admin createcachetable).
extends_documentation_fragment:
- community.general.attributes
- community.general.django
- community.general.django.database
attributes:
check_mode:
support: full
diff_mode:
support: none
"""
EXAMPLES = """
- name: Create cache table in the default database
community.general.django_createcachetable:
settings: myproject.settings
- name: Create cache table in the other database
community.general.django_createcachetable:
database: myotherdb
settings: fancysite.settings
pythonpath: /home/joedoe/project/fancysite
venv: /home/joedoe/project/fancysite/venv
"""
RETURN = """
run_info:
description: Command-line execution information.
type: dict
returned: success and O(verbosity) >= 3
"""
from ansible_collections.community.general.plugins.module_utils.django import DjangoModuleHelper
class DjangoCreateCacheTable(DjangoModuleHelper):
module = dict(
supports_check_mode=True,
)
django_admin_cmd = "createcachetable"
django_admin_arg_order = "noinput database dry_run"
_django_args = ["noinput", "database", "dry_run"]
_check_mode_arg = "dry_run"
def main():
DjangoCreateCacheTable.execute()
if __name__ == '__main__':
main()

View file

@ -0,0 +1,13 @@
# Copyright (c) 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.modules import django_createcachetable
from .helper import Helper
Helper.from_module(django_createcachetable, __name__)

View file

@ -0,0 +1,15 @@
# -*- coding: utf-8 -*-
# Copyright (c) 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
---
- id: command_success
input:
settings: whatever.settings
run_command_calls:
- command: [/testbin/python, -m, django, createcachetable, --no-color, --settings=whatever.settings, --noinput, --database=default]
environ: &env-def {environ_update: {LANGUAGE: C, LC_ALL: C}, check_rc: true}
rc: 0
out: "whatever\n"
err: ""