From dd53c104d72a0961d5689fd588df300162b97dd4 Mon Sep 17 00:00:00 2001 From: Andrew Klychkov Date: Thu, 27 Feb 2020 00:25:16 +0300 Subject: [PATCH] postgresql_info: move subscription info to corresponding database dict (#67772) --- .../database/postgresql/postgresql_info.py | 51 ++++++++++--------- .../tasks/postgresql_info_initial.yml | 7 +-- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/ansible/modules/database/postgresql/postgresql_info.py b/lib/ansible/modules/database/postgresql/postgresql_info.py index fc1650ec8b5..654c2e160c1 100644 --- a/lib/ansible/modules/database/postgresql/postgresql_info.py +++ b/lib/ansible/modules/database/postgresql/postgresql_info.py @@ -26,7 +26,7 @@ options: - Limit the collected information by comma separated string or YAML list. - Allowable values are C(version), C(databases), C(settings), C(tablespaces), C(roles), - C(replications), C(repl_slots), C(subscriptions) (since 2.10). + C(replications), C(repl_slots). - By default, collects all subsets. - You can use shell-style (fnmatch) wildcard to pass groups of values (see Examples). - You can use '!' before value (for example, C(!settings)) to exclude it from the information. @@ -243,7 +243,17 @@ databases: returned: if configured type: dict version_added: "2.10" - sample: { "mydb": { "pub1": { "ownername": "postgres", "puballtables": true, "pubinsert": true, "pubupdate": true } } } + sample: { "pub1": { "ownername": "postgres", "puballtables": true, "pubinsert": true, "pubupdate": true } } + subscriptions: + description: + - Information about replication subscriptions (available for PostgreSQL 10 and higher) + U(https://www.postgresql.org/docs/current/logical-replication-subscription.html). + - Content depends on PostgreSQL server version. + returned: if configured + type: dict + version_added: "2.10" + sample: + - { "my_subscription": {"ownername": "postgres", "subenabled": true, "subpublications": ["first_publication"] } } repl_slots: description: - Replication slots (available in 9.4 and later) @@ -462,16 +472,6 @@ settings: returned: always type: bool sample: false -subscriptions: - description: - - Information about replication subscriptions (available for PostgreSQL 10 and higher) - U(https://www.postgresql.org/docs/current/logical-replication-subscription.html). - - Content depends on PostgreSQL server version. - returned: if configured - type: dict - version_added: "2.10" - sample: - - {"acme_db": {"my_subscription": {"ownername": "postgres", "subenabled": true, "subpublications": ["first_publication"]}}} ''' from fnmatch import fnmatch @@ -553,7 +553,6 @@ class PgClusterInfo(object): "repl_slots": {}, "settings": {}, "roles": {}, - "subscriptions": {}, "pending_restart_settings": [], } @@ -567,7 +566,6 @@ class PgClusterInfo(object): "repl_slots": self.get_rslot_info, "settings": self.get_settings, "roles": self.get_role_info, - "subscriptions": self.get_subscr_info, } incl_list = [] @@ -637,10 +635,7 @@ class PgClusterInfo(object): return publications def get_subscr_info(self): - """Get subscription statistics and fill out self.pg_info dictionary.""" - if self.cursor.connection.server_version < 100000: - return - + """Get subscription statistics.""" query = ("SELECT s.*, r.rolname AS ownername, d.datname AS dbname " "FROM pg_catalog.pg_subscription s " "JOIN pg_catalog.pg_database d " @@ -653,18 +648,22 @@ class PgClusterInfo(object): if result: result = [dict(row) for row in result] else: - return + return {} + + subscr_info = {} for elem in result: - if not self.pg_info['subscriptions'].get(elem['dbname']): - self.pg_info['subscriptions'][elem['dbname']] = {} + if not subscr_info.get(elem['dbname']): + subscr_info[elem['dbname']] = {} - if not self.pg_info['subscriptions'][elem['dbname']].get(elem['subname']): - self.pg_info['subscriptions'][elem['dbname']][elem['subname']] = {} + if not subscr_info[elem['dbname']].get(elem['subname']): + subscr_info[elem['dbname']][elem['subname']] = {} for key, val in iteritems(elem): if key not in ('subname', 'dbname'): - self.pg_info['subscriptions'][elem['dbname']][elem['subname']][key] = val + subscr_info[elem['dbname']][elem['subname']][key] = val + + return subscr_info def get_tablespaces(self): """Get information about tablespaces.""" @@ -951,6 +950,9 @@ class PgClusterInfo(object): size=i[6], ) + if self.cursor.connection.server_version >= 100000: + subscr_info = self.get_subscr_info() + for datname in db_dict: self.cursor = self.db_obj.reconnect(datname) db_dict[datname]['namespaces'] = self.get_namespaces() @@ -958,6 +960,7 @@ class PgClusterInfo(object): db_dict[datname]['languages'] = self.get_lang_info() if self.cursor.connection.server_version >= 100000: db_dict[datname]['publications'] = self.get_pub_info() + db_dict[datname]['subscriptions'] = subscr_info.get(datname, {}) self.pg_info["databases"] = db_dict diff --git a/test/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml b/test/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml index 3d3e39353ac..27cdc5c7cda 100644 --- a/test/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml +++ b/test/integration/targets/postgresql_info/tasks/postgresql_info_initial.yml @@ -67,11 +67,11 @@ - result.databases.{{ db_default }}.languages - result.databases.{{ db_default }}.namespaces - result.databases.{{ db_default }}.extensions + - result.databases.{{ test_db }}.subscriptions.{{ test_subscription }} + - result.databases.{{ test_db }}.subscriptions.{{ test_subscription2 }} - result.settings - result.tablespaces - result.roles - - result.subscriptions.{{ test_db }}.{{ test_subscription }} - - result.subscriptions.{{ test_db }}.{{ test_subscription2 }} - name: postgresql_info - check filter param passed by list <<: *task_parameters @@ -81,14 +81,11 @@ filter: - ver* - rol* - - subscr* - assert: that: - result.version != {} - result.roles - - result.subscriptions.{{ test_db }}.{{ test_subscription }} != {} - - result.subscriptions.{{ test_db }}.{{ test_subscription2 }} != {} - result.databases == {} - result.repl_slots == {} - result.replications == {}