Skip to content

Commit

Permalink
simplify app_info to get info for one app only
Browse files Browse the repository at this point in the history
  • Loading branch information
aalaesar committed Jul 30, 2023
1 parent c67def9 commit 364052b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 112 deletions.
59 changes: 0 additions & 59 deletions plugins/module_utils/app_infos.py

This file was deleted.

122 changes: 69 additions & 53 deletions plugins/modules/app_infos.py → plugins/modules/app_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -43,32 +43,29 @@
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"
"""

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:
Expand All @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 364052b

Please sign in to comment.