mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Hana query userstore (#3125)
* 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>
This commit is contained in:
parent
a73720c103
commit
e9494c12f2
3 changed files with 72 additions and 8 deletions
2
changelogs/fragments/3125-hana-query-userstore.yaml
Normal file
2
changelogs/fragments/3125-hana-query-userstore.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
minor_changes:
|
||||
- hana_query - added the abillity to use hdbuserstore (https://github.com/ansible-collections/community.general/pull/3125).
|
|
@ -1,4 +1,5 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# 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)
|
||||
|
@ -21,13 +22,21 @@ options:
|
|||
type: str
|
||||
required: true
|
||||
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
|
||||
default: SYSTEM
|
||||
userstore:
|
||||
description: If C(true) the user must be in hdbuserstore.
|
||||
type: bool
|
||||
default: false
|
||||
version_added: 3.5.0
|
||||
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
|
||||
required: true
|
||||
autocommit:
|
||||
description: Autocommit the statement.
|
||||
type: bool
|
||||
|
@ -89,6 +98,17 @@ EXAMPLES = r'''
|
|||
- /tmp/HANA_CPU_UtilizationPerCore_2.00.020+.txt
|
||||
- /tmp/HANA.txt
|
||||
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'''
|
||||
|
@ -117,16 +137,18 @@ def main():
|
|||
argument_spec=dict(
|
||||
sid=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),
|
||||
user=dict(type='str', required=False, default="SYSTEM"),
|
||||
password=dict(type='str', required=True, no_log=True),
|
||||
user=dict(type='str', default="SYSTEM"),
|
||||
userstore=dict(type='bool', default=False),
|
||||
password=dict(type='str', no_log=True),
|
||||
database=dict(type='str', required=False),
|
||||
query=dict(type='list', elements='str', 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_if=[('userstore', False, ['password'])],
|
||||
supports_check_mode=False,
|
||||
)
|
||||
rc, out, err, out_raw = [0, [], "", ""]
|
||||
|
@ -136,6 +158,7 @@ def main():
|
|||
sid = (params['sid']).upper()
|
||||
instance = params['instance']
|
||||
user = params['user']
|
||||
userstore = params['userstore']
|
||||
password = params['password']
|
||||
autocommit = params['autocommit']
|
||||
host = params['host']
|
||||
|
@ -161,7 +184,10 @@ def main():
|
|||
if database is not None:
|
||||
command.extend(['-d', database])
|
||||
# -x Suppresses additional output, such as the number of selected rows in a result set.
|
||||
command.extend(['-x', '-i', instance, '-u', user, '-p', password])
|
||||
if userstore:
|
||||
command.extend(['-x', '-U', user])
|
||||
else:
|
||||
command.extend(['-x', '-i', instance, '-u', user, '-p', password])
|
||||
|
||||
if filepath is not None:
|
||||
command.extend(['-I'])
|
||||
|
|
|
@ -64,3 +64,39 @@ class Testhana_query(ModuleTestCase):
|
|||
{'username': 'myuser', 'name': 'my user'},
|
||||
]])
|
||||
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()
|
||||
|
|
Loading…
Reference in a new issue