Skip to content

Commit

Permalink
postgresql_ext: add comment argument (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andersson007 authored Dec 15, 2023
1 parent 1e808fe commit 3b4bba5
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
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

0 comments on commit 3b4bba5

Please sign in to comment.