From 886d6c3b010a29e5748ecb0c7227faf7e34658d7 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Fri, 15 Dec 2023 15:06:34 +0100 Subject: [PATCH 1/3] postgresql_ext: add comment argument --- changelogs/fragments/4-postgresql_ext.yml | 2 ++ plugins/module_utils/postgres.py | 6 +++++- plugins/modules/postgresql_ext.py | 23 +++++++++++++++++++++-- 3 files changed, 28 insertions(+), 3 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 00000000..ddc6a01e --- /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 42af6323..714847ce 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 7409809f..2d527e14 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, ) From eede6bd24a0ceb80ca49bf258819b559646c2021 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Fri, 15 Dec 2023 15:13:24 +0100 Subject: [PATCH 2/3] Add integration tests --- .../tasks/postgresql_ext_initial.yml | 103 +++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) 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 ff79f653..f035d29f 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,106 @@ 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" + +- 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" + +- 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" + +- 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 == [] + +- 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" + +- 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 From f96566d7406430969505e1d210a0b5a3026d5c94 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Fri, 15 Dec 2023 15:46:52 +0100 Subject: [PATCH 3/3] Fix tests --- .../targets/postgresql_ext/tasks/postgresql_ext_initial.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 f035d29f..8f730a36 100644 --- a/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_initial.yml +++ b/tests/integration/targets/postgresql_ext/tasks/postgresql_ext_initial.yml @@ -83,6 +83,7 @@ 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: @@ -111,6 +112,7 @@ 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: @@ -141,6 +143,7 @@ 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: @@ -162,7 +165,7 @@ - assert: that: - result is changed - - result.queries == [] + - result.queries == ["COMMENT ON EXTENSION \"postgis\" IS ''"] - name: Check the comment changed become_user: '{{ pg_user }}' @@ -170,6 +173,7 @@ 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: