diff --git a/plugins/modules/clickhouse_db.py b/plugins/modules/clickhouse_db.py index 6c55453..ffdd803 100644 --- a/plugins/modules/clickhouse_db.py +++ b/plugins/modules/clickhouse_db.py @@ -39,16 +39,11 @@ type: str choices: ['present', 'absent'] default: 'present' - required: true name: description: - Database name to add or remove. type: str required: true - comment: - description: - - Comment on the database. - type: str engine: description: - Database engine. @@ -64,7 +59,6 @@ login_password: my_password name: test_db engine: Memory - comment: Temporary test database ''' RETURN = r''' @@ -151,10 +145,10 @@ def __init__(self, module, client, name): self.module = module self.client = client self.name = name - self.exists, self.engine, self.comment = self.__populate_info() + self.exists, self.engine = self.__populate_info() def __populate_info(self): - query = "SELECT engine, comment FROM system.databases WHERE name = %(name)s" + query = "SELECT engine FROM system.databases WHERE name = %(name)s" # Will move this function to the lib later and reuse exec_kwargs = {'params': {'name': self.name}} result = execute_query(self.module, self.client, query, exec_kwargs) @@ -162,21 +156,17 @@ def __populate_info(self): # Assume the DB does not exist by default exists = False engine = None - comment = None if result: # If exists exists = True engine = result[0][0] - comment = result[0][1] - return exists, engine, comment + return exists, engine - def create(self, engine, comment): + def create(self, engine): query = "CREATE DATABASE %s" % self.name if engine: query += " ENGINE = %s" % engine - if comment: - query += " COMMENT '%s'" % comment executed_statements.append(query) @@ -185,10 +175,10 @@ def create(self, engine, comment): return True - def update(self, engine, comment): - # There's no way to change neither a comment - # nor the engine now, so just inform the users - # they have to recreate the DB in order to change them + def update(self, engine): + # There's no way to change the engine + # so just inform the users they have to recreate + # the DB in order to change them if engine and engine != self.engine: msg = ("The provided engine '%s' is different from " "the current one '%s'. It is NOT possible to " @@ -196,13 +186,6 @@ def update(self, engine, comment): "in order to change it." % (engine, self.engine)) self.module.warn(msg) - if comment and comment != self.comment: - msg = ("The provided comment '%s' is different from " - "the current one '%s'. It is NOT possible to " - "change it. The recreation of the database is required " - "in order to change it." % (comment, self.comment)) - self.module.warn(msg) - return False def drop(self): @@ -222,10 +205,9 @@ def main(): # and invoke here to return a dict with those arguments argument_spec = client_common_argument_spec() argument_spec.update( - state=dict(type='str', choices=['present', 'absent'], required=True), + state=dict(type='str', choices=['present', 'absent'], default='present'), name=dict(type='str', required=True), engine=dict(type='str', default=None), - comment=dict(type='str', default=None), ) # Instantiate an object of module class @@ -244,7 +226,6 @@ def main(): state = module.params['state'] name = module.params['name'] engine = module.params['engine'] - comment = module.params['comment'] # Will fail if no driver informing the user check_driver(module, HAS_DB_DRIVER) @@ -258,14 +239,15 @@ def main(): if state == 'present': if not database.exists: - changed = database.create(engine, comment) + changed = database.create(engine) else: # If database exists - changed = database.update(engine, comment) + changed = database.update(engine) else: # If state is absent - changed = database.drop() + if database.exists: + changed = database.drop() # Close connection client.disconnect_connection() diff --git a/tests/integration/targets/clickhouse_db/meta/main.yml b/tests/integration/targets/clickhouse_db/meta/main.yml new file mode 100644 index 0000000..4a78727 --- /dev/null +++ b/tests/integration/targets/clickhouse_db/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_clickhouse diff --git a/tests/integration/targets/clickhouse_db/tasks/initial.yml b/tests/integration/targets/clickhouse_db/tasks/initial.yml new file mode 100644 index 0000000..f2f6943 --- /dev/null +++ b/tests/integration/targets/clickhouse_db/tasks/initial.yml @@ -0,0 +1,224 @@ +#################################################################### +# WARNING: These are designed specifically for Ansible tests # +# and should not be used as examples of how to write Ansible roles # +#################################################################### + +# Test +- name: Create database in check mode + register: result + check_mode: true + community.clickhouse.clickhouse_db: + state: present + name: test_db + +- name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ['CREATE DATABASE test_db'] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT name FROM system.databases WHERE name = 'test_db'" + +- name: Check the DB is not there + ansible.builtin.assert: + that: + - result.result == [] + +# Test +- name: Create database in check mode with engine + register: result + check_mode: true + community.clickhouse.clickhouse_db: + state: present + name: test_db + engine: Memory + +- name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ["CREATE DATABASE test_db ENGINE = Memory"] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT name FROM system.databases WHERE name = 'test_db'" + +- name: Check the DB is not there + ansible.builtin.assert: + that: + - result.result == [] + +# Test +- name: Create database with engine in real mode + register: result + community.clickhouse.clickhouse_db: + name: test_db + engine: Memory + +- name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ["CREATE DATABASE test_db ENGINE = Memory"] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT engine FROM system.databases WHERE name = 'test_db'" + +- name: Check the DB is there + ansible.builtin.assert: + that: + - result.result == [["Memory"]] + +# Test +- name: Create database that already exists + register: result + community.clickhouse.clickhouse_db: + name: test_db + engine: Memory + +- name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] + +# Test +- name: Create database that already exists in check mode + register: result + check_mode: true + community.clickhouse.clickhouse_db: + name: test_db + engine: Memory + +- name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] + +# Test +- name: Create database that already exists with a different engine in check mode + register: result + check_mode: true + community.clickhouse.clickhouse_db: + name: test_db + engine: Atomic + +- name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT engine FROM system.databases WHERE name = 'test_db'" + +- name: Check the attributes are the same + ansible.builtin.assert: + that: + - result.result == [["Memory"]] + +# Test +- name: Create database that already exists with a different engine in real mode + register: result + community.clickhouse.clickhouse_db: + name: test_db + engine: Atomic + +- name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT engine FROM system.databases WHERE name = 'test_db'" + +- name: Check the attributes are the same + ansible.builtin.assert: + that: + - result.result == [["Memory"]] + +# Test +- name: Drop database in check mode + register: result + check_mode: true + community.clickhouse.clickhouse_db: + name: test_db + state: absent + +- name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ["DROP DATABASE test_db"] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT engine FROM system.databases WHERE name = 'test_db'" + +- name: Check the DB is there + ansible.builtin.assert: + that: + - result.result == [["Memory"]] + +# Test +- name: Drop database + register: result + community.clickhouse.clickhouse_db: + name: test_db + state: absent + +- name: Check ret values + ansible.builtin.assert: + that: + - result is changed + - result.executed_statements == ["DROP DATABASE test_db"] + +- name: Check the actual state + register: result + community.clickhouse.clickhouse_client: + execute: "SELECT engine FROM system.databases WHERE name = 'test_db'" + +- name: Check the DB is there + ansible.builtin.assert: + that: + - result.result == [] + +# Test +- name: Drop non-existing database + register: result + community.clickhouse.clickhouse_db: + name: test_db + state: absent + +- name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] + +# Test +- name: Drop non-existing database in check mode + register: result + check_mode: true + community.clickhouse.clickhouse_db: + name: test_db + state: absent + +- name: Check ret values + ansible.builtin.assert: + that: + - result is not changed + - result.executed_statements == [] diff --git a/tests/integration/targets/clickhouse_db/tasks/main.yml b/tests/integration/targets/clickhouse_db/tasks/main.yml new file mode 100644 index 0000000..489ed8f --- /dev/null +++ b/tests/integration/targets/clickhouse_db/tasks/main.yml @@ -0,0 +1,7 @@ +#################################################################### +# WARNING: These are designed specifically for Ansible tests # +# and should not be used as examples of how to write Ansible roles # +#################################################################### + +# Initial CI tests of clickhouse_db module +- import_tasks: initial.yml