Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions plugins/module_utils/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 5 additions & 2 deletions plugins/modules/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/targets/api/tasks/02_corner_cases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/targets/utils_login/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/plugins/module_utils/test_rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
27 changes: 14 additions & 13 deletions tests/unit/plugins/modules/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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, [])
Expand Down