Skip to content

Commit

Permalink
postgresql_ext: add version parameter (ansible#58381)
Browse files Browse the repository at this point in the history
* postgresql_ext: add version new option

* postgresql_ext: add version new option, fix ssl tests

* postgresql_ext: add version new option, fix tests

* postgresql_ext: add version new option, fix examples

* postgresql_ext: add version new option, fix the doc

* postgresql_ext: add version new option, fix examples

* postgresql_ext: add version new option, fix typo in tests
  • Loading branch information
Andersson007 authored and mkrizek committed Jul 2, 2019
1 parent 051172f commit 4da6d8c
Show file tree
Hide file tree
Showing 9 changed files with 563 additions and 43 deletions.
191 changes: 169 additions & 22 deletions lib/ansible/modules/database/postgresql/postgresql_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
type: str
aliases: [ ssl_rootcert ]
version_added: '2.8'
version:
description:
- Extension version to add or update to. Has effect with I(state=present) only.
- If not specified, the latest extension version will be created.
- It can't downgrade an extension version.
When version downgrade is needed, remove the extension and create new one with appropriate version.
- Set I(version=latest) to update the extension to the latest available version.
type: str
version_added: '2.9'
notes:
- The default authentication assumes that you are either logging in as
or sudo'ing to the C(postgres) account on the host.
Expand All @@ -92,6 +101,8 @@
author:
- Daniel Schep (@dschep)
- Thomas O'Donnell (@andytom)
- Sandro Santilli (@strk)
- Andrew Klychkov (@Andersson007)
extends_documentation_fragment: postgres
'''

Expand Down Expand Up @@ -122,6 +133,18 @@
db: acme
cascade: yes
state: absent
- name: Create extension foo of version 1.2 or update it if it's already created
postgresql_ext:
db: acme
name: foo
version: 1.2
- name: Assuming extension foo is created, update it to the latest version
postgresql_ext:
db: acme
name: foo
version: latest
'''

RETURN = r'''
Expand All @@ -135,6 +158,8 @@

import traceback

from distutils.version import LooseVersion

try:
from psycopg2.extras import DictCursor
except ImportError:
Expand Down Expand Up @@ -180,18 +205,81 @@ def ext_delete(cursor, ext, cascade):
return False


def ext_create(cursor, ext, schema, cascade):
if not ext_exists(cursor, ext):
query = "CREATE EXTENSION \"%s\"" % ext
if schema:
query += " WITH SCHEMA \"%s\"" % schema
if cascade:
query += " CASCADE"
cursor.execute(query)
executed_queries.append(query)
return True
def ext_update_version(cursor, ext, version):
"""Update extension version.
Return True if success.
Args:
cursor (cursor) -- cursor object of psycopg2 library
ext (str) -- extension name
version (str) -- extension version
"""
if version != 'latest':
query = ("ALTER EXTENSION \"%s\" UPDATE TO '%s'" % (ext, version))
else:
return False
query = ("ALTER EXTENSION \"%s\" UPDATE" % ext)
cursor.execute(query)
executed_queries.append(query)
return True


def ext_create(cursor, ext, schema, cascade, version):
query = "CREATE EXTENSION \"%s\"" % ext
if schema:
query += " WITH SCHEMA \"%s\"" % schema
if version:
query += " VERSION '%s'" % version
if cascade:
query += " CASCADE"
cursor.execute(query)
executed_queries.append(query)
return True


def ext_get_versions(cursor, ext):
"""
Get the current created extension version and available versions.
Return tuple (current_version, [list of available versions]).
Note: the list of available versions contains only versions
that higher than the current created version.
If the extension is not created, this list will contain all
available versions.
Args:
cursor (cursor) -- cursor object of psycopg2 library
ext (str) -- extension name
"""

# 1. Get the current extension version:
query = ("SELECT extversion FROM pg_catalog.pg_extension "
"WHERE extname = '%s'" % ext)

current_version = '0'
cursor.execute(query)
res = cursor.fetchone()
if res:
current_version = res[0]

# 2. Get available versions:
query = ("SELECT version FROM pg_available_extension_versions "
"WHERE name = '%s'" % ext)
cursor.execute(query)
res = cursor.fetchall()

available_versions = []
if res:
# Make the list of available versions:
for line in res:
if LooseVersion(line[0]) > LooseVersion(current_version):
available_versions.append(line['version'])

if current_version == '0':
current_version = False

return (current_version, available_versions)

# ===========================================
# Module execution.
Expand All @@ -207,6 +295,7 @@ def main():
state=dict(type="str", default="present", choices=["absent", "present"]),
cascade=dict(type="bool", default=False),
session_role=dict(type="str"),
version=dict(type="str"),
)

module = AnsibleModule(
Expand All @@ -218,24 +307,82 @@ def main():
schema = module.params["schema"]
state = module.params["state"]
cascade = module.params["cascade"]
version = module.params["version"]
changed = False

if version and state == 'absent':
module.warn("Parameter version is ignored when state=absent")

conn_params = get_conn_params(module, module.params)
db_connection = connect_to_db(module, conn_params, autocommit=True)
cursor = db_connection.cursor(cursor_factory=DictCursor)

try:
if module.check_mode:
if state == "present":
changed = not ext_exists(cursor, ext)
elif state == "absent":
changed = ext_exists(cursor, ext)
else:
if state == "absent":
changed = ext_delete(cursor, ext, cascade)

elif state == "present":
changed = ext_create(cursor, ext, schema, cascade)
# Get extension info and available versions:
curr_version, available_versions = ext_get_versions(cursor, ext)

if state == "present":
if version == 'latest':
if available_versions:
version = available_versions[-1]
else:
version = ''

if version:
# If the specific version is passed and it is not available for update:
if version not in available_versions:
if not curr_version:
module.fail_json(msg="Passed version '%s' is not available" % version)

elif LooseVersion(curr_version) == LooseVersion(version):
changed = False

else:
module.fail_json(msg="Passed version '%s' is lower than "
"the current created version '%s' or "
"the passed version is not available" % (version, curr_version))

# If the specific version is passed and it is higher that the current version:
if curr_version and version:
if LooseVersion(curr_version) < LooseVersion(version):
if module.check_mode:
changed = True
else:
changed = ext_update_version(cursor, ext, version)

# If the specific version is passed and it is created now:
if curr_version == version:
changed = False

# If the ext doesn't exist and installed:
elif not curr_version and available_versions:
if module.check_mode:
changed = True
else:
changed = ext_create(cursor, ext, schema, cascade, version)

# If version is not passed:
else:
if not curr_version:
# If the ext doesn't exist and it's installed:
if available_versions:
if module.check_mode:
changed = True
else:
changed = ext_create(cursor, ext, schema, cascade, version)

# If the ext doesn't exist and not installed:
else:
module.fail_json(msg="Extension %s is not installed" % ext)

elif state == "absent":
if curr_version:
if module.check_mode:
changed = True
else:
changed = ext_delete(cursor, ext, cascade)
else:
changed = False

except Exception as e:
db_connection.close()
Expand Down
3 changes: 3 additions & 0 deletions test/integration/targets/postgresql/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,9 @@
- include: postgresql_ext.yml
when: postgres_version_resp.stdout is version('9.1', '>=') and ansible_distribution == 'Fedora'

- include: postgresql_ext_version_opt.yml
when: ansible_distribution == 'Ubuntu'

# Test postgresql_slot module.
# Physical replication slots are available from PostgreSQL 9.4
- include: postgresql_slot.yml
Expand Down
Loading

0 comments on commit 4da6d8c

Please sign in to comment.