Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

cockroachdb_info: new module #18

Merged
merged 21 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
d06cfd5
cockroachdb_info: new module
Andersson007 Nov 9, 2021
7659807
Get server version
Andersson007 Nov 9, 2021
7231602
CI: Remove redundant check
Andersson007 Nov 10, 2021
b872159
Fix CI
Andersson007 Nov 10, 2021
1b44d16
Remove unnecessary try-except
Andersson007 Nov 10, 2021
53e848f
Add comments to functions
Andersson007 Nov 10, 2021
5ee014c
Add unit tests for extract_server_ver function
Andersson007 Nov 10, 2021
601638d
Fix formatting sanity issues
Andersson007 Nov 10, 2021
e513542
Add unit tests for exec_query function
Andersson007 Nov 10, 2021
b480408
Unit tests: replace dummy Module class with monkeypatch
Andersson007 Nov 10, 2021
e48dcf0
Unit tests: replace dummy Cursor classes with monkeypatch
Andersson007 Nov 10, 2021
a031b79
Add get_databases function + its unit and CI tests
Andersson007 Nov 11, 2021
508c1a8
cockroachdb_info unit tests: replace mock funcs with lambdas
Andersson007 Nov 11, 2021
e3bf23d
Switch to DictCursor. Add fetching more fields to get_databases.
Andersson007 Nov 11, 2021
2b6aa54
Add get_users function
Andersson007 Nov 12, 2021
fdac33b
Replace get_databases and get_users func with more general get_info
Andersson007 Nov 15, 2021
db22c71
Add fetching settings
Andersson007 Nov 16, 2021
1e24db2
Add success unit test for get_server_version func
Andersson007 Nov 16, 2021
832425c
Add fail unit test for get_server_version func
Andersson007 Nov 16, 2021
da39a90
Add fetching cluster regions info
Andersson007 Nov 16, 2021
acf8bb7
Fix sanity
Andersson007 Nov 16, 2021
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
Prev Previous commit
Next Next commit
Replace get_databases and get_users func with more general get_info
  • Loading branch information
Andersson007 committed Nov 15, 2021
commit fdac33bba940d25d042157f56607fa7e1ea2f857
90 changes: 36 additions & 54 deletions plugins/modules/cockroachdb_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,65 +147,42 @@ def get_server_version(module, cursor):
return v_info


def get_databases(module, cursor):
"""Get database info from a server.

Return a dictionary containing database info.
"""
res = exec_query(module, cursor, 'SHOW DATABASES WITH COMMENT')

if not res:
return {}

db_info = {}
for d in res:
dbname = d['database_name']
db_info[dbname] = {}

FIELDS = [
'comment',
'owner',
'primary_region',
'regions',
'survival_goal',
]

for field in FIELDS:
if field in d:
db_info[dbname][field] = d[field]

return db_info


def get_users(module, cursor):
"""Get user info from a server.

NOTE: This one and get_databases functions technically
can be replaced by one function with more arguments
but I would keep them separate for better readability.

Return a dictionary containing user info.
def get_info(module, cursor, query, root_key, fields):
"""Get info from a server.

As the rows returned by exec_query are a list of dictionaries,
for example, [{'user_name': 'Bob', 'member_of': [], 'options': None}],
you want to get user info you need pass 'SHOW USERS'
as the query argument, 'user_name' as the root_key argument,
and ['member_of', 'options'] as the fields argument, so that
you'll get the {'Bob': {'member_of': [], 'options': None}} dict
as a return value.

Args:
module (AnsibleModule) - AnsibleModule class object
cursor (psycopg2.Cursor) - psycopg2.Cursor class object
query (string) - query to pass to the exec_query function
root_key (string) - key that should be a root key of the ret dict
fields (list) - list of strings that represents fields
we wanna get

Return a dictionary containing info.
"""
res = exec_query(module, cursor, 'SHOW USERS')
res = exec_query(module, cursor, query)

if not res:
return {}

user_info = {}
for d in res:
username = d['username']
user_info[username] = {}

FIELDS = [
'member_of',
'options',
]
info = {}
for row in res:
root = row[root_key]
info[root] = {}

for field in FIELDS:
if field in d:
user_info[username][field] = d[field]
for field in fields:
if field in row:
info[root][field] = row[field]

return user_info
return info


def main():
Expand Down Expand Up @@ -235,8 +212,13 @@ def main():
# Collect info
# TODO: implement via loop with filtering
server_info['version'] = get_server_version(module, cursor)
server_info['databases'] = get_databases(module, cursor)
server_info['users'] = get_users(module, cursor)
server_info['databases'] = get_info(module, cursor,
'SHOW DATABASES WITH COMMENT',
'database_name',
['comment', 'owner', 'primary_region',
'regions', 'survival_goal'])
server_info['users'] = get_info(module, cursor, 'SHOW USERS', 'username',
['member_of', 'options'])

# Close cursor and conn
cursor.close()
Expand Down
64 changes: 36 additions & 28 deletions tests/unit/plugins/modules/test_cockroachdb_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from ansible_collections.community.cockroachdb.plugins.modules.cockroachdb_info import (
exec_query,
extract_server_ver,
get_databases,
get_server_version,
get_users,
get_info,
)


Expand Down Expand Up @@ -119,45 +118,51 @@ def test_exec_query_fail_fetchall(monkeypatch):
'from cursor: Fake cursor.fetchall() failing.')


@pytest.mark.parametrize('fetchall_out,expected', [
([{'database_name': 'postgres', 'comment': None}], {'postgres': {'comment': None}}),
([{'database_name': 'postgres', 'comment': 'test'}], {'postgres': {'comment': 'test'}}),
(
[
{'database_name': 'postgres', 'comment': 'test0'},
{'database_name': 'test', 'comment': 'test1'},
],
{'postgres': {'comment': 'test0'}, 'test': {'comment': 'test1'}},
)]
)
def test_get_databases(monkeypatch, fetchall_out, expected):
monkeypatch.setattr(Cursor, 'execute', lambda self, x: None)
monkeypatch.setattr(Cursor, 'fetchall', lambda self: fetchall_out)
monkeypatch.setattr(AnsibleModule, '__init__', mock__init__)
monkeypatch.setattr(AnsibleModule, 'fail_json', mock_fail_json)

module = AnsibleModule()
cursor = Cursor()

assert get_databases(module, cursor) == expected


@pytest.mark.parametrize('fetchall_out,expected', [
@pytest.mark.parametrize('query,root_key,fields,fetchall_out,expected', [
(
'SHOW USERS',
'username',
['member_of', 'options'],
[
{'username': 'admin', 'member_of': [], 'options': ''},
],
{'admin': {'member_of': [], 'options': ''}},
),
(
'SHOW USERS',
'username',
['member_of', 'options'],
[
{'username': 'admin', 'member_of': [], 'options': ''},
{'username': 'root', 'member_of': ['admin'], 'options': ''},
],
{'admin': {'member_of': [], 'options': ''}, 'root': {'member_of': ['admin'], 'options': ''}},
),
(
'SHOW DATABASES WITH COMMENT',
'database_name',
['comment'],
[{'database_name': 'postgres', 'comment': None}],
{'postgres': {'comment': None}}
),
(
'SHOW DATABASES WITH COMMENT',
'database_name',
['comment'],
[{'database_name': 'postgres', 'comment': 'test'}], {'postgres': {'comment': 'test'}}
),
(
'SHOW DATABASES WITH COMMENT',
'database_name',
['comment'],
[
{'database_name': 'postgres', 'comment': 'test0'},
{'database_name': 'test', 'comment': 'test1'},
],
{'postgres': {'comment': 'test0'}, 'test': {'comment': 'test1'}},
)]
)
def test_get_users(monkeypatch, fetchall_out, expected):
def test_get_info(monkeypatch, query, root_key, fields, fetchall_out, expected):
monkeypatch.setattr(Cursor, 'execute', lambda self, x: None)
monkeypatch.setattr(Cursor, 'fetchall', lambda self: fetchall_out)
monkeypatch.setattr(AnsibleModule, '__init__', mock__init__)
Expand All @@ -166,4 +171,7 @@ def test_get_users(monkeypatch, fetchall_out, expected):
module = AnsibleModule()
cursor = Cursor()

assert get_users(module, cursor) == expected
assert get_info(module, cursor, query, root_key, fields) == expected


# TODO: test_get_server_version