diff --git a/plugins/module_utils/rest_client.py b/plugins/module_utils/rest_client.py index 43edfc2aa..c7a818625 100644 --- a/plugins/module_utils/rest_client.py +++ b/plugins/module_utils/rest_client.py @@ -42,6 +42,21 @@ def list_records( raise errors.ScaleTimeoutError(e) return utils.filter_results(records, query) + def list_records_raw( + self, + endpoint: str, + timeout: Optional[float] = None, + ) -> Any: + """ + Returns all records. No filtering is done. + Returned type is same as type returned by HyperCore API. + """ + try: + records = self.client.get(path=endpoint, timeout=timeout).json + except TimeoutError as e: + raise errors.ScaleTimeoutError(e) + return records + def get_record( self, endpoint: str, diff --git a/plugins/modules/api.py b/plugins/modules/api.py index c521dfb6f..288e446c0 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -277,8 +277,11 @@ def put_record(module, rest_client): def get_records(module, rest_client): - records = rest_client.list_records( - query=module.params["data"], + # records = rest_client.list_records( + # query=module.params["data"], + # endpoint=module.params["endpoint"], + # ) + records = rest_client.list_records_raw( endpoint=module.params["endpoint"], ) return False, records diff --git a/tests/integration/targets/api/tasks/02_corner_cases.yml b/tests/integration/targets/api/tasks/02_corner_cases.yml index 6e299b432..3f85f8c0c 100644 --- a/tests/integration/targets/api/tasks/02_corner_cases.yml +++ b/tests/integration/targets/api/tasks/02_corner_cases.yml @@ -13,7 +13,8 @@ - ansible.builtin.assert: that: - ping_result is not changed - - ping_result.record.0 == "status" + - ping_result.record.status is defined + - ping_result.record.status == "Active" - name: Get HC3 Node scale_computing.hypercore.api: diff --git a/tests/integration/targets/utils_login/tasks/main.yml b/tests/integration/targets/utils_login/tasks/main.yml index 67cb927fc..778c90818 100644 --- a/tests/integration/targets/utils_login/tasks/main.yml +++ b/tests/integration/targets/utils_login/tasks/main.yml @@ -22,7 +22,8 @@ ansible.builtin.assert: that: - ping_result is not changed - - ping_result.record.0 == "status" + - ping_result.record.status is defined + - ping_result.record.status == "Active" # =================================================================== # local user, from cluster_instance variable diff --git a/tests/unit/plugins/module_utils/test_rest_client.py b/tests/unit/plugins/module_utils/test_rest_client.py index 7da03ab3a..f0cdebd60 100644 --- a/tests/unit/plugins/module_utils/test_rest_client.py +++ b/tests/unit/plugins/module_utils/test_rest_client.py @@ -64,6 +64,29 @@ def test_query_passing(self, client): ) +class TestTableListRecordsRaw: + def test_empty_response(self, client): + client.get.return_value = Response( + 200, '{"result": []}', {"X-Total-Count": "0"} + ) + t = rest_client.RestClient(client) + + records = t.list_records_raw("my_table") + + assert {"result": []} == records + client.get.assert_called_once_with(path="my_table", timeout=None) + + def test_non_empty_response(self, client): + client.get.return_value = Response( + 200, '{"result": [{"a": 3, "b": "sys_id"}]}', {"X-Total-Count": "1"} + ) + t = rest_client.RestClient(client) + + records = t.list_records_raw("my_table") + + assert records == {"result": [{"a": 3, "b": "sys_id"}]} + + class TestTableGetRecord: def test_zero_matches(self, client): client.get.return_value = Response( diff --git a/tests/unit/plugins/modules/test_api.py b/tests/unit/plugins/modules/test_api.py index 9a0f57a61..79ac6893c 100644 --- a/tests/unit/plugins/modules/test_api.py +++ b/tests/unit/plugins/modules/test_api.py @@ -23,7 +23,13 @@ class TestGetMethod: - def test_get_method_record_present(self, create_module, rest_client): + @pytest.mark.parametrize( + ("list_records_raw_return"), + [([dict(name="record1"), dict(name="record2")]), (dict(name="record1"))], + ) + def test_get_method_record_present( + self, create_module, rest_client, list_records_raw_return + ): module = create_module( params=dict( cluster_instance=dict( @@ -37,20 +43,16 @@ def test_get_method_record_present(self, create_module, rest_client): ) ) - rest_client.list_records.return_value = [ - dict(name="record1"), - dict(name="record2"), - ] + rest_client.list_records_raw.return_value = list_records_raw_return result = api.get_records(module, rest_client) - rest_client.list_records.assert_called_once() - rest_client.list_records.assert_called_with( + rest_client.list_records_raw.assert_called_once() + rest_client.list_records_raw.assert_called_with( endpoint="/rest/v1/VirDomain/id", - query={}, ) - assert result == (False, [dict(name="record1"), dict(name="record2")]) + assert result == (False, list_records_raw_return) def test_get_method_record_absent(self, create_module, rest_client): module = create_module( @@ -66,14 +68,13 @@ def test_get_method_record_absent(self, create_module, rest_client): ) ) - rest_client.list_records.return_value = [] + rest_client.list_records_raw.return_value = [] result = api.get_records(module, rest_client) - rest_client.list_records.assert_called_once() - rest_client.list_records.assert_called_with( + rest_client.list_records_raw.assert_called_once() + rest_client.list_records_raw.assert_called_with( endpoint="/rest/v1/VirDomain", - query={}, ) assert result == (False, [])