Return default network connection (#2115)

This commit is contained in:
Pascal Vizeli 2020-10-12 09:13:27 +02:00 committed by GitHub
parent 4c525de5e2
commit ac4277cd7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 2 deletions

View File

@ -39,6 +39,7 @@ SCHEMA_UPDATE = vol.Schema(
def interface_information(interface: NetworkInterface) -> dict: def interface_information(interface: NetworkInterface) -> dict:
"""Return a dict with information of a interface to be used in th API.""" """Return a dict with information of a interface to be used in th API."""
return { return {
ATTR_INTERFACE: interface.name,
ATTR_IP_ADDRESS: f"{interface.ip_address}/{interface.prefix}", ATTR_IP_ADDRESS: f"{interface.ip_address}/{interface.prefix}",
ATTR_GATEWAY: interface.gateway, ATTR_GATEWAY: interface.gateway,
ATTR_ID: interface.id, ATTR_ID: interface.id,
@ -69,8 +70,19 @@ class APINetwork(CoreSysAttributes):
async def interface_info(self, request: web.Request) -> Dict[str, Any]: async def interface_info(self, request: web.Request) -> Dict[str, Any]:
"""Return network information for a interface.""" """Return network information for a interface."""
req_interface = request.match_info.get(ATTR_INTERFACE) req_interface = request.match_info.get(ATTR_INTERFACE)
for interface in self.sys_host.network.interfaces:
if req_interface == self.sys_host.network.interfaces[interface].name: if req_interface.lower() == "default":
for interface in self.sys_host.network.interfaces:
if not self.sys_host.network.interfaces[interface].primary:
continue
return interface_information(
self.sys_host.network.interfaces[interface]
)
else:
for interface in self.sys_host.network.interfaces:
if req_interface != self.sys_host.network.interfaces[interface].name:
continue
return interface_information( return interface_information(
self.sys_host.network.interfaces[interface] self.sys_host.network.interfaces[interface]
) )

View File

@ -18,6 +18,16 @@ async def test_api_network_interface_info(api_client):
resp = await api_client.get(f"/network/interface/{TEST_INTERFACE}/info") resp = await api_client.get(f"/network/interface/{TEST_INTERFACE}/info")
result = await resp.json() result = await resp.json()
assert result["data"]["ip_address"] == "192.168.2.148/24" assert result["data"]["ip_address"] == "192.168.2.148/24"
assert result["data"]["interface"] == TEST_INTERFACE
@pytest.mark.asyncio
async def test_api_network_interface_info_default(api_client):
"""Test network manager default api."""
resp = await api_client.get("/network/interface/default/info")
result = await resp.json()
assert result["data"]["ip_address"] == "192.168.2.148/24"
assert result["data"]["interface"] == TEST_INTERFACE
@pytest.mark.asyncio @pytest.mark.asyncio