Skip to content

Commit

Permalink
clickhouse_client: Add ip types support (#34)
Browse files Browse the repository at this point in the history
* clickhouse_client module: add tests against more types

* clickhouse_client: add ip types support

* Fix sanity
  • Loading branch information
Andersson007 authored Feb 15, 2024
1 parent e88a4be commit 71d49e2
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion changelogs/fragments/1-clickhouse_client.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bugfixes:
- clickhouse_client - Add support for returning values of types ``UUID``, ``timedelta``, and ``decimal``.
- clickhouse_client - Add support for returned values of types ``UUID`` and ``decimal``.
2 changes: 2 additions & 0 deletions changelogs/fragments/2-clickhouse_client.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- clickhouse_client - Add support for returned values of types ``IPv4Address`` and ``IPv6Address``.
25 changes: 23 additions & 2 deletions plugins/modules/clickhouse_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@
type: dict
'''
from decimal import Decimal

HAS_IPADDRESS = False
try:
from ipaddress import IPv4Address, IPv6Address
HAS_IPADDRESS = True
except ImportError:
pass

from uuid import UUID

from ansible.module_utils.basic import AnsibleModule
Expand Down Expand Up @@ -156,10 +164,16 @@ def vals_to_supported(result):
if is_uuid(val):
# As tuple does not support change,
# we need some conversion here
result[idx_row] = replace_val_in_tuple(row, idx_val, str(val))
row = replace_val_in_tuple(row, idx_val, str(val))
result[idx_row] = row

elif isinstance(val, Decimal):
result[idx_row] = replace_val_in_tuple(row, idx_val, float(val))
row = replace_val_in_tuple(row, idx_val, float(val))
result[idx_row] = row

elif isinstance(val, IPv4Address) or isinstance(val, IPv6Address):
row = replace_val_in_tuple(row, idx_val, str(val))
result[idx_row] = row

return result

Expand Down Expand Up @@ -255,6 +269,13 @@ def main():
# Will fail if no driver informing the user
check_clickhouse_driver(module)

# There's no ipaddress package in Python 2
if not HAS_IPADDRESS:
msg = ("If you use Python 2 on your target host, "
"make sure you have the py2-ipaddress Python package installed there to avoid "
"crashes while querying tables containing columns of IPv4|6Address types.")
module.warn(msg)

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

Expand Down
40 changes: 39 additions & 1 deletion tests/integration/targets/clickhouse_client/tasks/initial.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
community.clickhouse.clickhouse_client:
execute: "INSERT INTO decimal_datetime VALUES ('4.01', '2019-01-01 00:00:00')"

- name: Insert Decimal and DateTime
- name: Select Decimal and DateTime
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT * FROM decimal_datetime"
Expand All @@ -101,3 +101,41 @@
ansible.builtin.assert:
that:
- result.result == [[4.01, '2019-01-01T00:00:00']]


- name: Create table with Map column
community.clickhouse.clickhouse_client:
execute: CREATE TABLE map (x Map(String, UInt64)) ENGINE = Memory

- name: Insert Map
community.clickhouse.clickhouse_client:
execute: "INSERT INTO map VALUES ({'a': 1, 'b': 2})"

- name: Select Map
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT * FROM map"

- name: Check the ret vals
ansible.builtin.assert:
that:
- result.result[0][0]['a'] == 1


- name: Create table with IPv4 and IPv6 columns
community.clickhouse.clickhouse_client:
execute: CREATE TABLE ip (v4 IPv4, v6 IPv6) ENGINE = Memory

- name: Insert IPv4 and IPv6
community.clickhouse.clickhouse_client:
execute: "INSERT INTO ip VALUES ('192.168.0.1', '001:44c8:129:2632:33:0:252:2')"

- name: Select IPv4 and IPv6
register: result
community.clickhouse.clickhouse_client:
execute: "SELECT * FROM ip"

- name: Check the ret vals
ansible.builtin.assert:
that:
- result.result == [["192.168.0.1", "1:44c8:129:2632:33:0:252:2"]]

0 comments on commit 71d49e2

Please sign in to comment.