From e9494c12f2b21f8f51be92c8e48074f6a697a712 Mon Sep 17 00:00:00 2001 From: rainerleber <39616583+rainerleber@users.noreply.github.com> Date: Thu, 5 Aug 2021 22:42:43 +0200 Subject: [PATCH] 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 * add fragment, fix required if * remove whitespace * add coding fragment * Apply suggestions from code review Co-authored-by: Felix Fontein * added test for userstore * Update plugins/modules/database/saphana/hana_query.py Co-authored-by: Felix Fontein Co-authored-by: Rainer Leber Co-authored-by: quidame Co-authored-by: Felix Fontein --- .../fragments/3125-hana-query-userstore.yaml | 2 + .../modules/database/saphana/hana_query.py | 42 +++++++++++++++---- .../database/saphana/test_hana_query.py | 36 ++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 changelogs/fragments/3125-hana-query-userstore.yaml diff --git a/changelogs/fragments/3125-hana-query-userstore.yaml b/changelogs/fragments/3125-hana-query-userstore.yaml new file mode 100644 index 0000000000..0a626fe7f5 --- /dev/null +++ b/changelogs/fragments/3125-hana-query-userstore.yaml @@ -0,0 +1,2 @@ +minor_changes: + - hana_query - added the abillity to use hdbuserstore (https://github.com/ansible-collections/community.general/pull/3125). diff --git a/plugins/modules/database/saphana/hana_query.py b/plugins/modules/database/saphana/hana_query.py index 9b26134022..ac026d5adc 100644 --- a/plugins/modules/database/saphana/hana_query.py +++ b/plugins/modules/database/saphana/hana_query.py @@ -1,4 +1,5 @@ #!/usr/bin/python +# -*- coding: utf-8 -*- # Copyright: (c) 2021, Rainer Leber # 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']) diff --git a/tests/unit/plugins/modules/database/saphana/test_hana_query.py b/tests/unit/plugins/modules/database/saphana/test_hana_query.py index 4d158c028e..b0fd939604 100644 --- a/tests/unit/plugins/modules/database/saphana/test_hana_query.py +++ b/tests/unit/plugins/modules/database/saphana/test_hana_query.py @@ -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()