1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Hana query userstore (#3125) (#3152)

* add hdbuserstore ability

* add description

* fix

* add default

* add description

* add sample

* Apply suggestions from code review

Co-authored-by: quidame <quidame@poivron.org>

* add fragment, fix required if

* remove whitespace

* add coding fragment

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* added test for userstore

* Update plugins/modules/database/saphana/hana_query.py

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: Rainer Leber <rainer.leber@sva.de>
Co-authored-by: quidame <quidame@poivron.org>
Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit e9494c12f2)

Co-authored-by: rainerleber <39616583+rainerleber@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2021-08-05 23:03:05 +02:00 committed by GitHub
parent f493110651
commit 3495823a72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 8 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- hana_query - added the abillity to use hdbuserstore (https://github.com/ansible-collections/community.general/pull/3125).

View file

@ -1,4 +1,5 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Rainer Leber <rainerleber@gmail.com> # Copyright: (c) 2021, Rainer Leber <rainerleber@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
@ -21,13 +22,21 @@ options:
type: str type: str
required: true required: true
user: user:
description: A dedicated username. Defaults to C(SYSTEM). description: A dedicated username. The user could be also in hdbuserstore. Defaults to C(SYSTEM).
type: str type: str
default: SYSTEM default: SYSTEM
userstore:
description: If C(true) the user must be in hdbuserstore.
type: bool
default: false
version_added: 3.5.0
password: password:
description: The password to connect to the database. description:
- The password to connect to the database.
- "B(Note:) Since the passwords have to be passed as command line arguments, I(userstore=true) should
be used whenever possible, as command line arguments can be seen by other users
on the same machine."
type: str type: str
required: true
autocommit: autocommit:
description: Autocommit the statement. description: Autocommit the statement.
type: bool type: bool
@ -89,6 +98,17 @@ EXAMPLES = r'''
- /tmp/HANA_CPU_UtilizationPerCore_2.00.020+.txt - /tmp/HANA_CPU_UtilizationPerCore_2.00.020+.txt
- /tmp/HANA.txt - /tmp/HANA.txt
host: "localhost" host: "localhost"
- name: Run several queries from user store
community.general.hana_query:
sid: "hdb"
instance: "01"
user: hdbstoreuser
userstore: true
query:
- "select user_name from users;"
- select * from users;
autocommit: False
''' '''
RETURN = r''' RETURN = r'''
@ -117,16 +137,18 @@ def main():
argument_spec=dict( argument_spec=dict(
sid=dict(type='str', required=True), sid=dict(type='str', required=True),
instance=dict(type='str', required=True), instance=dict(type='str', required=True),
encrypted=dict(type='bool', required=False, default=False), encrypted=dict(type='bool', default=False),
host=dict(type='str', required=False), host=dict(type='str', required=False),
user=dict(type='str', required=False, default="SYSTEM"), user=dict(type='str', default="SYSTEM"),
password=dict(type='str', required=True, no_log=True), userstore=dict(type='bool', default=False),
password=dict(type='str', no_log=True),
database=dict(type='str', required=False), database=dict(type='str', required=False),
query=dict(type='list', elements='str', required=False), query=dict(type='list', elements='str', required=False),
filepath=dict(type='list', elements='path', required=False), filepath=dict(type='list', elements='path', required=False),
autocommit=dict(type='bool', required=False, default=True), autocommit=dict(type='bool', default=True),
), ),
required_one_of=[('query', 'filepath')], required_one_of=[('query', 'filepath')],
required_if=[('userstore', False, ['password'])],
supports_check_mode=False, supports_check_mode=False,
) )
rc, out, err, out_raw = [0, [], "", ""] rc, out, err, out_raw = [0, [], "", ""]
@ -136,6 +158,7 @@ def main():
sid = (params['sid']).upper() sid = (params['sid']).upper()
instance = params['instance'] instance = params['instance']
user = params['user'] user = params['user']
userstore = params['userstore']
password = params['password'] password = params['password']
autocommit = params['autocommit'] autocommit = params['autocommit']
host = params['host'] host = params['host']
@ -161,6 +184,9 @@ def main():
if database is not None: if database is not None:
command.extend(['-d', database]) command.extend(['-d', database])
# -x Suppresses additional output, such as the number of selected rows in a result set. # -x Suppresses additional output, such as the number of selected rows in a result set.
if userstore:
command.extend(['-x', '-U', user])
else:
command.extend(['-x', '-i', instance, '-u', user, '-p', password]) command.extend(['-x', '-i', instance, '-u', user, '-p', password])
if filepath is not None: if filepath is not None:

View file

@ -64,3 +64,39 @@ class Testhana_query(ModuleTestCase):
{'username': 'myuser', 'name': 'my user'}, {'username': 'myuser', 'name': 'my user'},
]]) ]])
self.assertEqual(run_command.call_count, 1) self.assertEqual(run_command.call_count, 1)
def test_hana_userstore_query(self):
"""Check that result is processed with userstore."""
set_module_args({
'sid': "HDB",
'instance': "01",
'encrypted': False,
'host': "localhost",
'user': "SYSTEM",
'userstore': True,
'database': "HDB",
'query': "SELECT * FROM users;"
})
with patch.object(basic.AnsibleModule, 'run_command') as run_command:
run_command.return_value = 0, 'username,name\n testuser,test user \n myuser, my user \n', ''
with self.assertRaises(AnsibleExitJson) as result:
hana_query.main()
self.assertEqual(result.exception.args[0]['query_result'], [[
{'username': 'testuser', 'name': 'test user'},
{'username': 'myuser', 'name': 'my user'},
]])
self.assertEqual(run_command.call_count, 1)
def test_hana_failed_no_passwd(self):
"""Check that result is failed with no password."""
with self.assertRaises(AnsibleFailJson):
set_module_args({
'sid': "HDB",
'instance': "01",
'encrypted': False,
'host': "localhost",
'user': "SYSTEM",
'database': "HDB",
'query': "SELECT * FROM users;"
})
self.module.main()