Skip to content

Commit f1c8fab

Browse files
committed
feat: use new filter on dns api ?item_hash=
1 parent 9c73a06 commit f1c8fab

File tree

2 files changed

+8
-45
lines changed

2 files changed

+8
-45
lines changed

src/aleph/sdk/client/services/dns.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ async def get_public_dns_by_host(self, crn_hostname):
4444

4545
return DnsListAdapter.validate_json(raw)
4646

47-
async def get_dns_for_instance(self, vm_hash: ItemHash) -> Optional[Dns]:
48-
dns_list: List[Dns] = await self.get_public_dns()
49-
for dns in dns_list:
50-
if dns.item_hash == vm_hash:
51-
return dns
52-
return None
47+
async def get_dns_for_instance(self, vm_hash: ItemHash) -> Optional[List[Dns]]:
48+
async with aiohttp.ClientSession() as session:
49+
async with session.get(
50+
sanitize_url(settings.DNS_API), params={"item_hash": vm_hash}
51+
) as resp:
52+
resp.raise_for_status()
53+
raw = await resp.json()
54+
return DnsListAdapter.validate_json(raw)

tests/unit/test_services.py

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -371,45 +371,6 @@ async def test_dns_service_get_public_dns():
371371
assert result[0].ipv4 is not None and result[0].ipv4.public == "192.0.2.1"
372372

373373

374-
@pytest.mark.asyncio
375-
async def test_dns_service_get_dns_for_instance():
376-
"""Test the DNSService get_dns_for_instance method"""
377-
mock_client = MagicMock()
378-
dns_service = DNS(mock_client)
379-
380-
# Use a valid format for ItemHash (64-character hex string for storage hash)
381-
dns1 = Dns(
382-
name="test1.aleph.sh",
383-
item_hash="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
384-
ipv6="2001:db8::1",
385-
ipv4=IPV4(public="192.0.2.1", local="10.0.0.1"),
386-
)
387-
388-
dns2 = Dns(
389-
name="test2.aleph.sh",
390-
item_hash="fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210",
391-
ipv6="2001:db8::2",
392-
ipv4=IPV4(public="192.0.2.2", local="10.0.0.2"),
393-
)
394-
395-
# Use AsyncMock instead of a regular async function
396-
with patch.object(
397-
dns_service, "get_public_dns", AsyncMock(return_value=[dns1, dns2])
398-
):
399-
# Test finding a DNS entry (use the same hash as dns1)
400-
result = await dns_service.get_dns_for_instance(
401-
vm_hash="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
402-
)
403-
assert result is not None
404-
assert result.name == "test1.aleph.sh"
405-
406-
# Test not finding a DNS entry
407-
result = await dns_service.get_dns_for_instance(
408-
vm_hash="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
409-
)
410-
assert result is None
411-
412-
413374
@pytest.mark.asyncio
414375
async def test_crn_service_get_last_crn_version():
415376
"""Test the CrnService get_last_crn_version method"""

0 commit comments

Comments
 (0)