Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 committed Feb 1, 2024
1 parent f58ead3 commit 1913a68
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 125 deletions.
58 changes: 58 additions & 0 deletions plugins/doc_fragments/client_inst_opts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type


class ModuleDocFragment(object):
DOCUMENTATION = r'''
options:
login_host:
description:
- The same as the C(Client(host='...')) argument.
type: str
default: 'localhost'
login_port:
description:
- The same as the C(Client(port='...')) argument.
- If not passed, relies on the driver's default argument value.
type: int
login_db:
description:
- The same as the C(Client(database='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
login_user:
description:
- The same as the C(Client(user='...')) argument.
- If not passed, relies on the driver's default argument value.
- Be sure your the user has permissions to read the system tables
listed in the RETURN section.
type: str
login_password:
description:
- The same as the C(Client(password='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
client_kwargs:
description:
- Any additional keyword arguments you want to pass
to the Client interface when instantiating its object.
type: dict
default: {}
requirements: [ 'clickhouse-driver' ]
notes:
- See the clickhouse-driver
L(documentation,https://clickhouse-driver.readthedocs.io/en/latest)
for more information about the driver interface.
'''
38 changes: 38 additions & 0 deletions plugins/module_utils/connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This code is part of Ansible, but is an independent component.
# This particular file snippet, and this file snippet only, is BSD licensed.
# Modules you write using this snippet, which is embedded dynamically by Ansible
# still belong to the author of the module, and may assign their own license
# to the complete work.
#
# Simplified BSD License (see simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)

from __future__ import absolute_import, division, print_function

__metaclass__ = type

from ansible.module_utils.basic import missing_required_lib


def client_common_argument_spec():
"""
Return a dictionary with connection options.
The options are commonly used by many modules.
"""
return dict(
login_host=dict(type='str', default='localhost'),
login_port=dict(type='int', default=None),
login_db=dict(type='str', default=None),
login_user=dict(type='str', default=None),
login_password=dict(type='str', default=None, no_log=True),
client_kwargs=dict(type='dict', default={}),
)


def check_driver(module, has_db_driver):
"""Checks if the driver is present.
Informs user if no driver and fails.
"""
if not has_db_driver:
module.fail_json(msg=missing_required_lib('clickhouse_driver'))
73 changes: 13 additions & 60 deletions plugins/modules/clickhouse_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
author:
- Andrew Klychkov (@Andersson007)
extends_documentation_fragment:
- community.clickhouse.client_inst_opts
notes:
- Does not support C(check_mode).
- See the clickhouse-driver
L(documentation,https://clickhouse-driver.readthedocs.io/en/latest)
for more information about the driver interface.
options:
execute:
Expand All @@ -44,43 +44,6 @@
through the I(execute) argument.
type: dict
default: {}
login_host:
description:
- The same as the C(Client(host='...')) argument.
type: str
default: 'localhost'
login_port:
description:
- The same as the C(Client(port='...')) argument.
- If not passed, relies on the driver's default argument value.
type: int
login_db:
description:
- The same as the C(Client(database='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
login_user:
description:
- The same as the C(Client(user='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
login_password:
description:
- The same as the C(Client(password='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
client_kwargs:
description:
- Any additional keyword arguments you want to pass
to the Client interface when instantiating its object.
type: dict
default: {}
'''

EXAMPLES = r'''
Expand Down Expand Up @@ -152,16 +115,21 @@
type: dict
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native

from ansible_collections.community.clickhouse.plugins.module_utils.connect import (
check_driver,
client_common_argument_spec,
)

Client = None
try:
from clickhouse_driver import Client
HAS_DB_DRIVER = True
except ImportError:
HAS_DB_DRIVER = False

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native


def get_main_conn_kwargs(module):
"""Retrieves main connection arguments values and translates
Expand Down Expand Up @@ -262,28 +230,13 @@ def connect_to_db_via_client(module, main_conn_kwargs, client_kwargs):
return client


def check_driver(module):
"""Checks if the driver is present.
Informs user if no driver and fails.
"""
if not HAS_DB_DRIVER:
module.fail_json(msg=missing_required_lib('clickhouse_driver'))


def main():
# Set up arguments.
# If there are common arguments shared across several modules,
# create the common_argument_spec() function under plugins/module_utils/*
# and invoke here to return a dict with those arguments
argument_spec = {}
argument_spec = client_common_argument_spec()
argument_spec.update(
login_host=dict(type='str', default='localhost'),
login_port=dict(type='int', default=None),
login_db=dict(type='str', default=None),
login_user=dict(type='str', default=None),
login_password=dict(type='str', default=None, no_log=True),
client_kwargs=dict(type='dict', default={}),
execute=dict(type='str', required=True),
execute_kwargs=dict(type='dict', default={}),
)
Expand All @@ -305,7 +258,7 @@ def main():
main_conn_kwargs = get_main_conn_kwargs(module)

# Will fail if no driver informing the user
check_driver(module)
check_driver(module, HAS_DB_DRIVER)

# Connect to DB
client = connect_to_db_via_client(module, main_conn_kwargs, client_kwargs)
Expand Down
77 changes: 12 additions & 65 deletions plugins/modules/clickhouse_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,58 +23,15 @@
description: Supports check_mode.
support: full
requirements: ['clickhouse-driver']
version_added: '0.1.0'
author:
- Andrew Klychkov (@Andersson007)
notes:
- See the clickhouse-driver
L(documentation,https://clickhouse-driver.readthedocs.io/en/latest)
for more information about the driver interface.
extends_documentation_fragment:
- community.clickhouse.client_inst_opts
options:
login_host:
description:
- The same as the C(Client(host='...')) argument.
type: str
default: 'localhost'
login_port:
description:
- The same as the C(Client(port='...')) argument.
- If not passed, relies on the driver's default argument value.
type: int
login_db:
description:
- The same as the C(Client(database='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
login_user:
description:
- The same as the C(Client(user='...')) argument.
- If not passed, relies on the driver's default argument value.
- Be sure your the user has permissions to read the system tables
listed in the RETURN section.
type: str
login_password:
description:
- The same as the C(Client(password='...')) argument.
- If not passed, relies on the driver's default argument value.
type: str
client_kwargs:
description:
- Any additional keyword arguments you want to pass
to the Client interface when instantiating its object.
type: dict
default: {}
limit:
description:
- Limits a set of return values you want to get.
Expand Down Expand Up @@ -154,6 +111,14 @@
sample: { "test_cluster_two_shards": "..." }
'''

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native

from ansible_collections.community.clickhouse.plugins.module_utils.connect import (
check_driver,
client_common_argument_spec,
)

Client = None
try:
from clickhouse_driver import Client
Expand All @@ -162,9 +127,6 @@
except ImportError:
HAS_DB_DRIVER = False

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils._text import to_native

PRIV_ERR_CODE = 497


Expand Down Expand Up @@ -422,15 +384,6 @@ def get_driver(module, client):
return {"version": driver_version}


def check_driver(module):
"""Checks if the driver is present.
Informs user if no driver and fails.
"""
if not HAS_DB_DRIVER:
module.fail_json(msg=missing_required_lib('clickhouse_driver'))


def handle_limit_values(module, supported_ret_vals, limit):
"""Checks if passed limit values match module return values.
Expand All @@ -456,14 +409,8 @@ def main():
# If there are common arguments shared across several modules,
# create the common_argument_spec() function under plugins/module_utils/*
# and invoke here to return a dict with those arguments
argument_spec = {}
argument_spec = client_common_argument_spec()
argument_spec.update(
login_host=dict(type='str', default='localhost'),
login_port=dict(type='int', default=None),
login_db=dict(type='str', default=None),
login_user=dict(type='str', default=None),
login_password=dict(type='str', default=None, no_log=True),
client_kwargs=dict(type='dict', default={}),
limit=dict(type='list', elements='str'),
)

Expand Down Expand Up @@ -502,7 +449,7 @@ def main():
limit = ret_val_func_mapping.keys()

# Will fail if no driver informing the user
check_driver(module)
check_driver(module, HAS_DB_DRIVER)

# Connect to DB
client = connect_to_db_via_client(module, main_conn_kwargs, client_kwargs)
Expand Down

0 comments on commit 1913a68

Please sign in to comment.