Skip to content

Commit

Permalink
clickhouse_cfg_info: add XML files support
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 committed Nov 1, 2024
1 parent 9e297b5 commit afda693
Show file tree
Hide file tree
Showing 4 changed files with 1,815 additions and 15 deletions.
63 changes: 51 additions & 12 deletions plugins/modules/clickhouse_cfg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
description:
- Retrieves ClickHouse config file content and returns it as JSON.
- Only config files in YAML format are supported at the moment.
- Supports config files in the YAML and XML formats.
- When the config file is XML, options values are returned as strings.
- Does not change server state.
attributes:
Expand All @@ -36,11 +37,10 @@
required: true
requirements:
- pyyaml
- pyyaml (for YAML config files)
- xmltodict (for XML config files)
'''

# TODO: it should also handle xml configs

EXAMPLES = r'''
- name: Get server information
register: result
Expand All @@ -52,31 +52,58 @@
var: result
'''

RETURN = r''' # '''
RETURN = r'''
config:
description:
- The content of the config file.
- When the file is XML, options values
are returned as strings.
returned: success
type: dict
'''

try:
import yaml
HAS_PYYAML = True
except ImportError:
HAS_PYYAML = False

try:
import xmltodict
HAS_XMLTODICT = True
except ImportError:
HAS_XMLTODICT = False

from ansible.module_utils.basic import (
AnsibleModule,
missing_required_lib,
)


def load_from_yaml(module, path):
def load_config(module, load_func, path):
try:
f = open(path, 'r')
content = yaml.safe_load(f)
content = load_func(f)
except Exception as e:
module.fail_json(msg="Could not open/load YAML from the file %s: %s" % (path, e))
fail_msg = "Could not open/load from file %s: %s" % (path, e)
module.fail_json(msg=fail_msg)
else:
f.close()
return content


def load_from_yaml(f):
return yaml.safe_load(f)


def load_from_xml(f):
return xmltodict.parse(f.read())['clickhouse']


def is_xml(path):
return True if len(path) > 4 and path[-4:] == '.xml' else False


def main():
argument_spec = {}
argument_spec.update(
Expand All @@ -89,13 +116,25 @@ def main():
supports_check_mode=True,
)

if not HAS_PYYAML:
module.fail_json(msg=missing_required_lib('pyyaml'))
path = module.params['path']

# When XML
if is_xml(path):
if not HAS_XMLTODICT:
module.fail_json(msg=missing_required_lib('xmltodict'))
load_func = load_from_xml

# When YAML, the default
else:
if not HAS_PYYAML:
module.fail_json(msg=missing_required_lib('pyyaml'))
load_func = load_from_yaml

cfg_content = load_from_yaml(module, module.params['path'])
# Load content as dict
cfg_content = load_config(module, load_func, path)

# Users will get this in JSON output after execution
module.exit_json(changed=False, **cfg_content)
module.exit_json(changed=False, config=cfg_content)


if __name__ == '__main__':
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
clickhouse_driver
pyyaml
xmltodict
Loading

0 comments on commit afda693

Please sign in to comment.