diff --git a/plugins/module_utils/app_infos.py b/plugins/module_utils/app_infos.py deleted file mode 100644 index 52f34ded..00000000 --- a/plugins/module_utils/app_infos.py +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# Copyright: (c) 2023, Marc Crébassa -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions are met: - -# * Redistributions of source code must retain the above copyright notice, this -# list of conditions and the following disclaimer. - -# * Redistributions in binary form must reproduce the above copyright notice, -# this list of conditions and the following disclaimer in the documentation -# and/or other materials provided with the distribution. - -# 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 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, 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 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 USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -from ansible_collections.nextcloud.admin.plugins.module_utils.occ import run_occ - - -def check_app_update(module, app_name): - _check_app_update = run_occ(module, ["app:update", "--showonly", app_name])[1] - if _check_app_update != "": - app_update = True - app_update_version_available = _check_app_update.split()[-1] - else: - app_update = False - app_update_version_available = None - - return app_update, app_update_version_available - - -def get_app_infos(module, app_name, all_shipped_apps, all_present_apps): - if app_name in all_present_apps["enabled"].keys(): - app_state = "present" - app_version = all_present_apps["enabled"][app_name] - elif app_name in all_present_apps["disabled"].keys(): - app_state = "disabled" - app_version = all_present_apps["disabled"][app_name] - else: - return dict(state="absent") - _app_updatable, _app_updatable_to = check_app_update(module, app_name) - return dict( - state=app_state, - is_shipped=app_name - in [s for a in all_shipped_apps.keys() for s in all_shipped_apps[a].keys()], - version=app_version, - update_available=_app_updatable, - version_available=_app_updatable_to, - path=run_occ(module, ["app:getpath", app_name])[1].strip(), - ) diff --git a/plugins/modules/app_infos.py b/plugins/modules/app_info.py similarity index 58% rename from plugins/modules/app_infos.py rename to plugins/modules/app_info.py index cf4cda09..34601aba 100644 --- a/plugins/modules/app_infos.py +++ b/plugins/modules/app_info.py @@ -25,15 +25,15 @@ DOCUMENTATION = r""" -module: app_infos +module: app_info -short_description: Display informations about applications present in a Nextcloud server +short_description: Display informations about an application installed in a Nextcloud server author: - "Marc Crébassa (@aalaesar)" description: - - Return a set of facts about applications present in a Nextcloud server. + - Return a set of facts about the requested application. - Always returns state as 'OK'. - This module requires to be run with advanced privileges unless it is run as the user that own the occ tool. @@ -43,12 +43,9 @@ options: name: - description: - - Only collect informations for this application. - - If not set, informations are collected for all present applications. - - Depending on the size of your cluster, the module may take a while to gather informations for all applications. + description: Collect informations for a specified nextcloud application. type: str - default: null + required: true requirements: - "python >=3.6" @@ -56,19 +53,19 @@ EXAMPLES = r""" - name: get the list of applications installed - nextcloud.admin.app_infos: + nextcloud.admin.app_info: nextcloud_path: /var/lib/www/nextcloud register: nc_apps_list - name: get configuration information about an application - nextcloud.admin.app_infos: + nextcloud.admin.app_info: nextcloud_path: /var/lib/www/nextcloud name: photos """ RETURN = r""" -nextcloud_applications: - description: The informations collected for each application requested or any present. +nextcloud_application: + description: The informations collected for the application requested. returned: always type: dict contains: @@ -91,40 +88,62 @@ type: bool returned: success version_available: - description: What is the version proposed for update + description: What is the version proposed for update. type: string returned: success - path: + app_path: description: The full path to the application folder. type: string returned: success -updates_available: - description: All applications name and version eligible for update - returned: always - type: dict """ import copy import json -from ansible.module_utils.basic import AnsibleModule, missing_required_lib +from ansible.module_utils.basic import AnsibleModule from ansible_collections.nextcloud.admin.plugins.module_utils.occ import run_occ -from ansible_collections.nextcloud.admin.plugins.module_utils.app_infos import ( - get_app_infos, -) from ansible_collections.nextcloud.admin.plugins.module_utils.occ_args_common import ( OCC_ARGS_SPEC, ) +def check_app_update(module, app_name): + _check_app_update = run_occ(module, ["app:update", "--showonly", app_name])[1] + if _check_app_update != "": + app_update = True + app_update_version_available = _check_app_update.split()[-1] + else: + app_update = False + app_update_version_available = None + + return app_update, app_update_version_available + + +def get_app_info(module, app_name, all_shipped_apps, all_present_apps): + if app_name in all_present_apps["enabled"].keys(): + app_state = "present" + app_version = all_present_apps["enabled"][app_name] + elif app_name in all_present_apps["disabled"].keys(): + app_state = "disabled" + app_version = all_present_apps["disabled"][app_name] + else: + return dict(state="absent") + _app_updatable, _app_updatable_to = check_app_update(module, app_name) + return dict( + state=app_state, + is_shipped=app_name + in [s for a in all_shipped_apps.keys() for s in all_shipped_apps[a].keys()], + version=app_version, + update_available=_app_updatable, + version_available=_app_updatable_to, + path=run_occ(module, ["app:getpath", app_name])[1].strip(), + ) + + def args_spec(): arg_spec = copy.deepcopy(OCC_ARGS_SPEC) arg_spec.update( dict( - name=dict( - type="str", - required=False, - default=None, - ), + name=dict(type="str", required=True), ), ) return arg_spec @@ -143,33 +162,30 @@ def main(): run_occ(module, ["app:list", "--output=json", "--shipped=true"])[1] ) all_present_apps = json.loads(run_occ(module, ["app:list", "--output=json"])[1]) - result = dict(nextcloud_applications={}, updates_available={}) - - # if name is not none, proceed to get app info for this one - if app_name: - result["nextcloud_applications"][app_name] = get_app_infos( - module, app_name, all_shipped_apps, all_present_apps - ) - if ( - result["nextcloud_applications"][app_name]["state"] == "present" - and result["nextcloud_applications"][app_name]["update_available"] - ): - result["updates_available"][app_name] = result["nextcloud_applications"][ - app_name - ]["version_available"] - + result = {} + + if app_name in all_present_apps["enabled"].keys(): + app_state = "present" + app_version = all_present_apps["enabled"][app_name] + elif app_name in all_present_apps["disabled"].keys(): + app_state = "disabled" + app_version = all_present_apps["disabled"][app_name] else: - # flatten the all_present_apps dict into a simple list of app names - for app_name in [ - s for a in all_present_apps.keys() for s in all_present_apps[a].keys() - ]: - result["nextcloud_applications"][app_name] = get_app_infos( - module, app_name, all_shipped_apps, all_present_apps - ) - if result["nextcloud_applications"][app_name]["update_available"]: - result["updates_available"][app_name] = result[ - "nextcloud_applications" - ][app_name]["version_available"] + app_state = None + result = dict(state="absent", name=app_name) + + if app_state in ["present", "disabled"]: + _app_updatable, _app_updatable_to = check_app_update(module, app_name) + result = dict( + name=app_name, + state=app_state, + is_shipped=app_name + in [s for a in all_shipped_apps.keys() for s in all_shipped_apps[a].keys()], + version=app_version, + update_available=_app_updatable, + version_available=_app_updatable_to, + app_path=run_occ(module, ["app:getpath", app_name])[1].strip(), + ) module.exit_json(changed=False, **result)