Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

postgresql_ext: add comment argument #652

Merged
merged 3 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/4-postgresql_ext.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- postgresql_ext - add the ``comment`` argument (https://github.com/ansible-collections/community.postgresql/issues/354).
6 changes: 5 additions & 1 deletion plugins/module_utils/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
23 changes: 21 additions & 2 deletions plugins/modules/postgresql_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -524,7 +543,7 @@ def main():
ext=ext,
prev_version=out_prev_version,
version=out_version,
queries=executed_queries
queries=executed_queries,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}'
Expand All @@ -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
Expand Down