Skip to content

Commit

Permalink
feat: added get-available-interfaces action
Browse files Browse the repository at this point in the history
  • Loading branch information
Tbaile committed Feb 28, 2024
1 parent 2545e08 commit ee89cc7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
18 changes: 18 additions & 0 deletions imageroot/actions/get-available-interfaces/10fetch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import sys

import network

interfaces = network.list_interfaces()
interfaces_names = [interface["name"] for interface in interfaces]

json.dump({
"data": interfaces_names
}, sys.stdout)
16 changes: 16 additions & 0 deletions imageroot/actions/get-available-interfaces/validate-output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://schema.nethserver.org/dnsmasq/get-available-interfaces.json",
"title": "Interfaces",
"description": "List available interfaces that dnsmasq can be configured on.",
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string",
"description": "Interface name to pass to configure-module."
}
}
}
}
57 changes: 57 additions & 0 deletions imageroot/pypkg/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

#
# Copyright (C) 2024 Nethesis S.r.l.
# SPDX-License-Identifier: GPL-3.0-or-later
#

import json
import subprocess


def __filter_interface(interface):
"""
Filter the interfaces that are not useful for the configuration.
"""
# filter by name
interface_name = interface["ifname"]
if interface_name.startswith("lo"):
return False
if interface_name.startswith("wg"):
return False

# filter by configuration status
if "addr_info" not in interface:
return False
if interface["addr_info"] == []:
return False

# return True if the interface is not filtered
return True


def __format_interface(interface):
"""
Format the interface that can be used internally for responses.
"""
interface_data = {
"name": interface["ifname"],
"addresses": []
}
for address in interface["addr_info"]:
if address["family"] == "inet":
interface_data["addresses"].append({
"address": address["local"],
"netmask": address["prefixlen"],
"broadcast": address["broadcast"]
})

return interface_data


def list_interfaces():
"""
Returns a list of interfaces with their available addresses.
"""
subprocess_result = subprocess.run(["ip", "-j", "addr"], check=True, capture_output=True)
return [__format_interface(interface) for interface in json.loads(subprocess_result.stdout) if __filter_interface(interface)]

0 comments on commit ee89cc7

Please sign in to comment.