1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Filter import Filter
5 from PLC.Interfaces import Interface, Interfaces
6 from PLC.IpAddresses import IpAddress, IpAddresses
7 from PLC.Routes import Route, Routes
8 from PLC.Nodes import Node, Nodes
9 from PLC.Auth import Auth
12 legacy_interface_fields = {
13 'interface_id': Parameter(int, "Node interface identifier"),
14 'method': Parameter(str, "Addressing method (e.g., 'static' or 'dhcp')"),
15 'type': Parameter(str, "Address type (e.g., 'ipv4')"),
16 'ip': Parameter(str, "IP address", nullok = True),
17 'mac': Parameter(str, "MAC address", nullok = True),
18 'gateway': Parameter(str, "IP address of primary gateway", nullok = True),
19 'network': Parameter(str, "Subnet address", nullok = True),
20 'broadcast': Parameter(str, "Network broadcast address", nullok = True),
21 'netmask': Parameter(str, "Subnet mask", nullok = True),
22 'dns1': Parameter(str, "IP address of primary DNS server", nullok = True),
23 'dns2': Parameter(str, "IP address of secondary DNS server", nullok = True),
24 'bwlimit': Parameter(int, "Bandwidth limit", min = 0, nullok = True),
25 'hostname': Parameter(str, "(Optional) Hostname", nullok = True),
26 'node_id': Parameter(int, "Node associated with this interface"),
27 'is_primary': Parameter(bool, "Is the primary interface for this node"),
28 'interface_tag_ids' : Parameter([int], "List of interface settings"),
29 'last_updated': Parameter(int, "Date and time when the interface entry was last updated"),
32 def clean_interface_fields(interface):
33 remove_keys = [key for key in interface.keys() if key not in legacy_interface_fields.keys()]
34 for key in remove_keys:
39 class GetInterfaces(Method):
41 Returns an array of structs containing details about network
42 interfaces. If interfaces_filter is specified and is an array of
43 interface identifiers, or a struct of interface fields and
44 values, only interfaces matching the filter will be
47 If return_fields is given, only the specified details will be returned.
50 roles = ['admin', 'pi', 'user', 'tech', 'node', 'anonymous']
54 Mixed([Mixed(Interface.fields['interface_id'])],
55 Parameter (int, "interface id"),
56 Filter(legacy_interface_fields)),
57 Parameter([str], "List of fields to return", nullok = True)
60 returns = [Interface.fields] + ["type", "ip", "netmask", "network", "broadcast"]
62 def call(self, auth, interface_filter = None, return_fields = None):
63 if (return_fields is not None):
64 interface_return_fields = return_fields[:]
65 if not ("ip_address_ids" in interface_return_fields):
66 interface_return_fields += ["ip_address_ids"]
67 interface_return_fields = [f for f in interface_return_fields if (f in Interface.fields)]
69 interface_return_fields = return_fields
71 interfaces = Interfaces(self.api, interface_filter, interface_return_fields)
74 for interface in interfaces:
75 ip_address_ids = interface["ip_address_ids"]
77 # we'll use the first IpAddress only
78 addresses = IpAddresses(self.api, ip_address_ids[0])
79 if (not return_fields) or ("type" in return_fields):
80 interface["type"] = addresses[0]["type"]
81 if (not return_fields) or ("ip" in return_fields):
82 interface["ip"] = addresses[0]["ip_addr"]
83 if (not return_fields) or ("netmask" in return_fields):
84 interface["netmask"] = addresses[0]["netmask"]
85 if (not return_fields) or ("network" in return_fields):
86 interface["network"] = addresses[0].get_network()
87 if (not return_fields) or ("broadcast" in return_fields):
88 interface["broadcast"] = addresses[0].get_broadcast()
91 routes = Routes(self.api, {'node_id' : interface['node_id'], 'subnet' : '0.0.0.0/0'})
93 interface['gateway'] = routes[0]['next_hop']
97 nodes = Node(self.api, {'node_id' : interface['node_id']})
98 if nodes and nodes.has_key('dns'):
99 # we'll only get the first two dns records
100 dns_records = nodes[0]['dns'].split(',')
102 dns1 = dns_records[0]
103 if len(dns_records) > 1:
104 dns2 = dns_records[1]
106 interface['dns1'] = dns1
107 interface['dns2'] = dns2
109 interface = clean_interface_fields(interface)