diff --git a/plugins/doc_fragments/client_inst_opts.py b/plugins/doc_fragments/client_inst_opts.py new file mode 100644 index 0000000..40a70b3 --- /dev/null +++ b/plugins/doc_fragments/client_inst_opts.py @@ -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. +''' diff --git a/plugins/module_utils/connect.py b/plugins/module_utils/connect.py new file mode 100644 index 0000000..b6e4491 --- /dev/null +++ b/plugins/module_utils/connect.py @@ -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')) diff --git a/plugins/modules/clickhouse_client.py b/plugins/modules/clickhouse_client.py index 77876bd..a6a24c8 100644 --- a/plugins/modules/clickhouse_client.py +++ b/plugins/modules/clickhouse_client.py @@ -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: @@ -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''' @@ -152,6 +115,14 @@ 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 @@ -159,9 +130,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 - def get_main_conn_kwargs(module): """Retrieves main connection arguments values and translates @@ -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={}), ) @@ -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) diff --git a/plugins/modules/clickhouse_info.py b/plugins/modules/clickhouse_info.py index 132588b..1acc0ed 100644 --- a/plugins/modules/clickhouse_info.py +++ b/plugins/modules/clickhouse_info.py @@ -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. @@ -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 @@ -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 @@ -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. @@ -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'), ) @@ -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)