Debian Codegraph @

ansible_collections/community/clickhouse/SPEC.md

raw
1 # community.clickhouse — Technical Specification
2
3 ## Collection Identity
4
5 | Field | Value |
6 |---|---|
7 | Namespace | `community` |
8 | Name | `clickhouse` |
9 | Full name | `community.clickhouse` |
10 | License | GPL-3.0-or-later |
11 | Required Python package | `clickhouse-driver` |
12 | Optional Python packages | `pyyaml` (YAML config parsing), `xmltodict` (XML config parsing) |
13
14 Current version, min ansible-core, and Python requirements are in `galaxy.yml` and `meta/runtime.yml`.
15
16 ---
17
18 ## Repository Layout
19
20 - `plugins/modules/` — modules
21 - `plugins/module_utils/clickhouse.py` — shared utilities
22 - `plugins/doc_fragments/client_inst_opts.py` — shared connection parameter docs
23 - `tests/integration/targets/` — integration test targets mirroring module names; `setup_clickhouse/` installs the ClickHouse server and is declared as a dependency by every other target
24 - `tests/unit/plugins/` — unit tests
25
26 ---
27
28 ## Modules
29
30 - All modules share connection parameters via the `client_inst_opts` doc fragment.
31 - All modules except `clickhouse_client` support `check_mode`.
32 - For full parameter and return value reference, read each module's `DOCUMENTATION` and `RETURN` blocks directly.
33
34 | Module | Purpose | Notable caveats |
35 |---|---|---|
36 | `clickhouse_client` | Execute arbitrary SQL via `Client.execute()` | No `check_mode`; always `changed=True`; converts `UUID`, `Decimal`, `IPv4/IPv6Address` to `str` |
37 | `clickhouse_cfg_info` | Read a ClickHouse config file (YAML/XML) as a dict | Requires `pyyaml` or `xmltodict`; reads from disk, no server connection |
38 | `clickhouse_db` | Create, drop, or rename a database | Engine is immutable after creation |
39 | `clickhouse_grants` | Grant or revoke privileges for a user or role | Set-difference logic — only issues statements for actual changes; supports column-level grants |
40 | `clickhouse_info` | Gather server info from system tables | Returns privilege errors (code 497) gracefully rather than failing |
41 | `clickhouse_quota` | Create or remove quotas with interval-based limits | Uses regex-based parsing to normalize complex quota definitions for idempotency |
42 | `clickhouse_role` | Create or drop roles with optional settings constraints | Parses `SHOW CREATE ROLE`; uses `ALTER ROLE` (not drop/recreate) to update settings |
43 | `clickhouse_user` | Create, drop, or modify users (passwords, hosts, roles, settings) | `update_password: on_create` skips password update if user already exists |
44
45 ---
46
47 ## Module Utilities
48
49 **File:** `plugins/module_utils/clickhouse.py`
50
51 ```python
52 PRIV_ERR_CODE = 497 # ClickHouse error code for insufficient privileges
53
54 def client_common_argument_spec() -> dict
55 # Returns argument_spec with connection params:
56 # login_host (default: 'localhost'), login_port, login_db,
57 # login_user, login_password (no_log), client_kwargs (dict, default: {})
58
59 def get_main_conn_kwargs(module) -> dict
60 # Translates module.params connection keys to clickhouse-driver Client() kwargs
61
62 def check_clickhouse_driver(module) -> None
63 # Calls module.fail_json() if clickhouse-driver is not installed
64
65 def version_clickhouse_driver() -> str
66 # Returns installed clickhouse-driver version string
67
68 def connect_to_db_via_client(module, main_conn_kwargs, client_kwargs) -> Client
69 # Creates and returns a clickhouse-driver Client connection object
70
71 def execute_query(module, client, query, execute_kwargs=None, set_settings=None)
72 # Executes a query; returns result list or PRIV_ERR_CODE on privilege error
73 # All other errors call module.fail_json()
74
75 def get_server_version(module, client) -> dict
76 # Returns {raw, year, feature, maintenance, build, type}
77 ```
78
79 ---
80
81 ## Documentation Fragment
82
83 **File:** `plugins/doc_fragments/client_inst_opts.py`
84
85 All modules extend this via `extends_documentation_fragment: community.clickhouse.client_inst_opts`. Shared connection parameters:
86
87 | Parameter | Default | Notes |
88 |---|---|---|
89 | `login_host` | `localhost` | ClickHouse server hostname |
90 | `login_port` | omit | Server port (driver default: 9000) |
91 | `login_db` | omit | Database to connect to |
92 | `login_user` | omit | Login username |
93 | `login_password` | omit | `no_log: true` |
94 | `client_kwargs` | `{}` | Extra kwargs passed directly to `clickhouse_driver.Client()` |
95
96 ---
97
98 ## Architectural Patterns
99
100 ### Module Bootstrap Pattern
101
102 Every module follows the same structure:
103
104 ```python
105 argument_spec = client_common_argument_spec()
106 argument_spec.update({...}) # module-specific params
107 module = AnsibleModule(argument_spec=argument_spec, ...)
108 check_clickhouse_driver(module)
109 client = connect_to_db_via_client(module, ...)
110 ```
111
112 ### Idempotency Strategy
113
114 State-management modules (db, user, role, quota, grants) all follow:
115 1. Query the relevant `system.*` table to fetch current state.
116 2. Compare desired state with current state.
117 3. Execute `CREATE`, `ALTER`, or `DROP` only if a difference is detected.
118 4. Return executed statements in `executed_statements` (populated in check_mode too).
119
120 ### Class-Based Module Pattern
121
122 Each state-management module encapsulates logic in a class (e.g., `ClickHouseDB`, `ClickHouseUser`). Classes hold the client connection and implement methods like `get()`, `create()`, `drop()`, `update()`.
123
124 ### Error Handling
125
126 - Privilege errors (code 497) are caught and returned as `PRIV_ERR_CODE` rather than failing.
127 - All other query errors call `module.fail_json()` with `to_native(e)`.
128 - Driver availability checked at module start via `check_clickhouse_driver()`.
129
130 ### Cluster Parameter
131
132 When `cluster` is specified, DDL statements are appended with `ON CLUSTER <cluster_name>`. Consistent across all modules that accept the parameter.
133
134 ---
135
136 ## Testing
137
138 ### Integration Test Pattern
139
140 Each target in `tests/integration/targets/` declares `setup_clickhouse` as a dependency in `meta/main.yml`. Standard task pattern:
141
142 ```yaml
143 - name: <description>
144 register: result
145 community.clickhouse.<module>:
146 login_host: localhost
147 <params>
148
149 - name: Assert expected outcome
150 ansible.builtin.assert:
151 that:
152 - result is changed # or `result is not changed`
153 - result.<key> == <value>
154 ```
155
156 Verify database state by following up with `community.clickhouse.clickhouse_client`, registering the result, and asserting on it.
157
158 ### Test Commands
159
160 ```bash
161 # Sanity
162 ansible-test sanity plugins/modules/clickhouse_db.py --docker -v
163
164 # Units
165 ansible-test units tests/unit/plugins/modules/test_clickhouse_client.py --docker -v
166
167 # Integration
168 ansible-test integration clickhouse_db --docker default -v
169 ```
170
171 ---
172
173 ## Development Conventions
174
175 ### Changelog Fragments
176
177 Every PR that changes module behavior needs a fragment in `changelogs/fragments/<description>.yaml`. Docs, tests, and refactoring PRs are exempt. Fragments are consumed at release time (`keep_fragments: false`).
178
179 Valid sections:
180 - `major_changes`, `minor_changes`, `bugfixes`
181 - `breaking_changes`, `deprecated_features`, `removed_features`
182 - `security_fixes`, `known_issues`
183
184 ### Versioning
185
186 - **Major** — breaking changes (e.g., dropping Ansible version support)
187 - **Minor** — new modules, new non-breaking parameters
188 - **Patch** — bug fixes
189
190 ### PR Requirements
191
192 All three test types (sanity, units, integration) must pass. New modules and new parameters must have `version_added: 'x.y.z'` set to the next planned release version.