From 3b4bba5aca3724bed438ee9eaf0315d5a46cfe14 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Fri, 15 Dec 2023 19:52:30 +0100 Subject: [PATCH] postgresql_ext: add comment argument (#652) --- changelogs/fragments/4-postgresql_ext.yml | 2 + plugins/module_utils/postgres.py | 6 +- plugins/modules/postgresql_ext.py | 23 +++- .../tasks/postgresql_ext_initial.yml | 107 +++++++++++++++++- 4 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/4-postgresql_ext.yml diff --git a/changelogs/fragments/4-postgresql_ext.yml b/changelogs/fragments/4-postgresql_ext.yml new file mode 100644 index 000000000..ddc6a01ea --- /dev/null +++ b/changelogs/fragments/4-postgresql_ext.yml @@ -0,0 +1,2 @@ +minor_changes: +- postgresql_ext - add the ``comment`` argument (https://github.com/ansible-collections/community.postgresql/issues/354). diff --git a/plugins/module_utils/postgres.py b/plugins/module_utils/postgres.py index 42af6323a..714847cee 100644 --- a/plugins/module_utils/postgres.py +++ b/plugins/module_utils/postgres.py @@ -541,8 +541,12 @@ def get_comment(cursor, obj_type, obj_name): query = '' if obj_type == 'role': query = ("SELECT pg_catalog.shobj_description(r.oid, 'pg_authid') AS comment " - "FROM pg_catalog.pg_roles r " + "FROM pg_catalog.pg_roles AS r " "WHERE r.rolname = %(obj_name)s") + elif obj_type == 'extension': + query = ("SELECT pg_catalog.obj_description(e.oid, 'pg_extension') AS comment " + "FROM pg_catalog.pg_extension AS e " + "WHERE e.extname = %(obj_name)s") cursor.execute(query, {'obj_name': obj_name}) return cursor.fetchone()['comment'] diff --git a/plugins/modules/postgresql_ext.py b/plugins/modules/postgresql_ext.py index 7409809f2..2d527e14a 100644 --- a/plugins/modules/postgresql_ext.py +++ b/plugins/modules/postgresql_ext.py @@ -88,6 +88,13 @@ type: bool default: true version_added: '0.2.0' + comment: + description: + - Sets a comment on the extension. + - To reset the comment, pass an empty string. + type: str + version_added: '3.3.0' + seealso: - name: PostgreSQL extensions description: General information about PostgreSQL extensions. @@ -127,6 +134,7 @@ name: postgis db: acme schema: foo + comment: Test extension - name: Removes postgis extension to the database acme community.postgresql.postgresql_ext: @@ -191,9 +199,11 @@ from ansible_collections.community.postgresql.plugins.module_utils.postgres import ( connect_to_db, ensure_required_libs, + get_comment, get_conn_params, pg_cursor_args, postgres_common_argument_spec, + set_comment, ) executed_queries = [] @@ -391,6 +401,7 @@ def main(): session_role=dict(type="str"), version=dict(type="str"), trust_input=dict(type="bool", default=True), + comment=dict(type="str", default=None), ) module = AnsibleModule( @@ -405,10 +416,12 @@ def main(): version = module.params["version"] session_role = module.params["session_role"] trust_input = module.params["trust_input"] + comment = module.params["comment"] + changed = False if not trust_input: - check_input(module, ext, schema, version, session_role) + check_input(module, ext, schema, version, session_role, comment) if version and state == 'absent': module.warn("Parameter version is ignored when state=absent") @@ -494,6 +507,12 @@ def main(): else: module.fail_json(msg="Extension %s is not available" % ext) + if comment is not None and comment != get_comment(cursor, 'extension', ext): + if module.check_mode: + changed = True + else: + changed = set_comment(cursor, comment, 'extension', ext, executed_queries) + elif state == "absent": if curr_version: changed = ext_delete(module.check_mode, cursor, ext, cascade) @@ -524,7 +543,7 @@ def main(): ext=ext, prev_version=out_prev_version, version=out_version, - queries=executed_queries + queries=executed_queries, ) diff --git a/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_initial.yml b/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_initial.yml index ff79f653b..8f730a367 100644 --- a/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_initial.yml +++ b/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_initial.yml @@ -55,13 +55,14 @@ login_db: postgres login_port: 5432 name: postgis + comment: Test comment 1 ignore_errors: true register: result - assert: that: - result is changed - - result.queries == ['CREATE EXTENSION "postgis"'] + - result.queries == ['CREATE EXTENSION "postgis"', "COMMENT ON EXTENSION \"postgis\" IS 'Test comment 1'"] - name: postgresql_ext - check that extension exists after the previous step become_user: '{{ pg_user }}' @@ -76,6 +77,110 @@ that: - result.rowcount == 1 +- name: Check the comment + become_user: '{{ pg_user }}' + become: true + postgresql_query: + db: postgres + query: "SELECT obj_description((SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'postgis'), 'pg_extension') AS comment" + register: result + +- name: Check the comments match + assert: + that: + - result.query_result[0]['comment'] == "Test comment 1" + + +- name: Now after the comment was set, invoke again not pass the comment explicitly + become_user: '{{ pg_user }}' + become: true + postgresql_ext: + login_db: postgres + login_port: 5432 + name: postgis + ignore_errors: true + register: result + +- assert: + that: + - result is not changed + - result.queries == [] + +- name: Check the comment didn't change + become_user: '{{ pg_user }}' + become: true + postgresql_query: + db: postgres + query: "SELECT obj_description((SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'postgis'), 'pg_extension') AS comment" + register: result + +- name: Check the comments match + assert: + that: + - result.query_result[0]['comment'] == "Test comment 1" + + +- name: Reset the comment in check mode + become_user: '{{ pg_user }}' + become: true + postgresql_ext: + login_db: postgres + login_port: 5432 + name: postgis + comment: '' + ignore_errors: true + register: result + check_mode: true + +- assert: + that: + - result is changed + - result.queries == [] + +- name: Check the comment didn't change + become_user: '{{ pg_user }}' + become: true + postgresql_query: + db: postgres + query: "SELECT obj_description((SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'postgis'), 'pg_extension') AS comment" + register: result + +- name: Check the comments match + assert: + that: + - result.query_result[0]['comment'] == "Test comment 1" + + +- name: Reset the comment in real mode + become_user: '{{ pg_user }}' + become: true + postgresql_ext: + login_db: postgres + login_port: 5432 + name: postgis + comment: '' + ignore_errors: true + register: result + +- assert: + that: + - result is changed + - result.queries == ["COMMENT ON EXTENSION \"postgis\" IS ''"] + +- name: Check the comment changed + become_user: '{{ pg_user }}' + become: true + postgresql_query: + db: postgres + query: "SELECT obj_description((SELECT oid FROM pg_catalog.pg_extension WHERE extname = 'postgis'), 'pg_extension') AS comment" + register: result + +- name: Check the comments match + assert: + that: + - result.query_result[0]['comment'] == None + + - name: postgresql_ext - drop extension postgis become_user: '{{ pg_user }}' become: true