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
12 changes: 6 additions & 6 deletions infrahub_sdk/object_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,18 @@ async def _get_file(self, url: str, identifier: str, tracker: str | None = None)

async def get_file_by_storage_id(self, storage_id: str, tracker: str | None = None) -> str:
"""Retrieve file object content by storage_id."""
url = f"{self.client.address}/api/files/by-storage-id/{storage_id}"
url = f"{self.client.address}/api/storage/files/by-storage-id/{storage_id}"
return await self._get_file(url=url, identifier=storage_id, tracker=tracker)

async def get_file_by_id(self, node_id: str, tracker: str | None = None) -> str:
"""Retrieve file object content by node UUID."""
url = f"{self.client.address}/api/files/{node_id}"
url = f"{self.client.address}/api/storage/files/{node_id}"
return await self._get_file(url=url, identifier=node_id, tracker=tracker)

async def get_file_by_hfid(self, kind: str, hfid: list[str], tracker: str | None = None) -> str:
"""Retrieve file object content by Human-Friendly ID."""
params = "&".join(f"hfid={h}" for h in hfid)
url = f"{self.client.address}/api/files/by-hfid/{kind}?{params}"
url = f"{self.client.address}/api/storage/files/by-hfid/{kind}?{params}"
return await self._get_file(url=url, identifier=f"{kind}:{'/'.join(hfid)}", tracker=tracker)


Expand Down Expand Up @@ -188,16 +188,16 @@ def _get_file(self, url: str, identifier: str, tracker: str | None = None) -> st

def get_file_by_storage_id(self, storage_id: str, tracker: str | None = None) -> str:
"""Retrieve file object content by storage_id."""
url = f"{self.client.address}/api/files/by-storage-id/{storage_id}"
url = f"{self.client.address}/api/storage/files/by-storage-id/{storage_id}"
return self._get_file(url=url, identifier=storage_id, tracker=tracker)

def get_file_by_id(self, node_id: str, tracker: str | None = None) -> str:
"""Retrieve file object content by node UUID."""
url = f"{self.client.address}/api/files/{node_id}"
url = f"{self.client.address}/api/storage/files/{node_id}"
return self._get_file(url=url, identifier=node_id, tracker=tracker)

def get_file_by_hfid(self, kind: str, hfid: list[str], tracker: str | None = None) -> str:
"""Retrieve file object content by Human-Friendly ID."""
params = "&".join(f"hfid={h}" for h in hfid)
url = f"{self.client.address}/api/files/by-hfid/{kind}?{params}"
url = f"{self.client.address}/api/storage/files/by-hfid/{kind}?{params}"
return self._get_file(url=url, identifier=f"{kind}:{'/'.join(hfid)}", tracker=tracker)
45 changes: 44 additions & 1 deletion tests/unit/sdk/test_infrahub_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
pytestmark = pytest.mark.httpx_mock(can_send_already_matched_responses=True)

ARTIFACT_CONTENT_URL = "http://mock/api/storage/object"
FILE_BY_STORAGE_ID_URL = "http://mock/api/files/by-storage-id"
FILE_BY_STORAGE_ID_URL = "http://mock/api/storage/files/by-storage-id"
FILE_BY_ID_URL = "http://mock/api/storage/files"
FILE_BY_HFID_URL = "http://mock/api/storage/files/by-hfid"

CLIENT_FILTER_PARAMS = [
pytest.param(
Expand Down Expand Up @@ -257,6 +259,47 @@ async def test_file_object_content_by_hfid_missing_kind(self, client: InfrahubCl
' — use {{ hfid | file_object_content_by_hfid(kind="MyKind") }}'
)

async def test_file_object_content_by_id_happy_path(self, client: InfrahubClient, httpx_mock: HTTPXMock) -> None:
httpx_mock.add_response(
method="GET",
url=f"{FILE_BY_ID_URL}/node-uuid-1",
text="file node content",
headers={"content-type": "text/plain"},
)
jinja = Jinja2Template(template="{{ node_id | file_object_content_by_id }}", client=client)
result = await jinja.render(variables={"node_id": "node-uuid-1"})
assert result == "file node content"

async def test_file_object_content_by_hfid_happy_path_string(
self, client: InfrahubClient, httpx_mock: HTTPXMock
) -> None:
httpx_mock.add_response(
method="GET",
url=f"{FILE_BY_HFID_URL}/CoreFileObject?hfid=contract-2024",
text="hfid content",
headers={"content-type": "text/plain"},
)
jinja = Jinja2Template(
template='{{ hfid | file_object_content_by_hfid(kind="CoreFileObject") }}', client=client
)
result = await jinja.render(variables={"hfid": "contract-2024"})
assert result == "hfid content"

async def test_file_object_content_by_hfid_happy_path_list(
self, client: InfrahubClient, httpx_mock: HTTPXMock
) -> None:
httpx_mock.add_response(
method="GET",
url=f"{FILE_BY_HFID_URL}/CoreFileObject?hfid=configs&hfid=base.conf",
text="hfid list content",
headers={"content-type": "text/plain"},
)
jinja = Jinja2Template(
template='{{ hfid | file_object_content_by_hfid(kind="CoreFileObject") }}', client=client
)
result = await jinja.render(variables={"hfid": ["configs", "base.conf"]})
assert result == "hfid list content"


class TestFromJsonFilter:
def test_valid_json(self) -> None:
Expand Down