3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Table import Row
7 from PLC.Auth import Auth
9 from PLC.Nodes import Node, Nodes
10 from PLC.IpAddresses import IpAddress, IpAddresses
11 from PLC.TagTypes import TagTypes
12 from PLC.InterfaceTags import InterfaceTags
13 from PLC.Interfaces import Interface, Interfaces
14 from PLC.Methods.AddIpAddress import AddIpAddress
15 from PLC.Methods.DeleteIpAddress import DeleteIpAddress
16 from PLC.Methods.DeleteRoute import DeleteRoute
17 from PLC.Methods.AddRoute import AddRoute
18 from PLC.Methods.UpdateNode import UpdateNode
19 from PLC.Methods.AddInterfaceTag import AddInterfaceTag
20 from PLC.Methods.UpdateInterfaceTag import UpdateInterfaceTag
22 cannot_update = ['interface_id','node_id']
24 legacy_interface_fields = {
25 'interface_id': Parameter(int, "Node interface identifier"),
26 'method': Parameter(str, "Addressing method (e.g., 'static' or 'dhcp')"),
27 'type': Parameter(str, "Address type (e.g., 'ipv4')"),
28 'ip': Parameter(str, "IP address", nullok = True),
29 'mac': Parameter(str, "MAC address", nullok = True),
30 'gateway': Parameter(str, "IP address of primary gateway", nullok = True),
31 'network': Parameter(str, "Subnet address", nullok = True),
32 'broadcast': Parameter(str, "Network broadcast address", nullok = True),
33 'netmask': Parameter(str, "Subnet mask", nullok = True),
34 'dns1': Parameter(str, "IP address of primary DNS server", nullok = True),
35 'dns2': Parameter(str, "IP address of secondary DNS server", nullok = True),
36 'bwlimit': Parameter(int, "Bandwidth limit", min = 0, nullok = True),
37 'hostname': Parameter(str, "(Optional) Hostname", nullok = True),
38 'node_id': Parameter(int, "Node associated with this interface"),
39 'is_primary': Parameter(bool, "Is the primary interface for this node"),
40 'interface_tag_ids' : Parameter([int], "List of interface settings"),
41 'last_updated': Parameter(int, "Date and time when the interface entry was last updated"),
45 class UpdateInterface(Method):
47 Updates an existing interface network. Any values specified in
48 interface_fields are used, otherwise defaults are
49 used. Acceptable values for method are dhcp and static. If type is
50 static, then ip, gateway, network, broadcast, netmask, and dns1
51 must all be specified in interface_fields. If type is dhcp,
52 these parameters, even if specified, are ignored.
54 PIs and techs may only update interfaces associated with their own
55 nodes. Admins may update any interface network.
57 Returns 1 if successful, faults otherwise.
60 roles = ['admin', 'pi', 'tech']
62 accepted_fields = Row.accepted_fields(cannot_update, legacy_interface_fields,exclude=True)
63 accepted_fields.update(Interface.tags)
67 Interface.fields['interface_id'],
71 returns = Parameter(int, '1 if successful')
73 def call(self, auth, interface_id, interface_fields):
75 [native,tags,rejected] = Row.split_fields(interface_fields,[legacy_interface_fields,Interface.tags])
78 native= Row.check_fields (native, self.accepted_fields)
80 raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
82 # Get interface information
83 interfaces = Interfaces(self.api, [interface_id])
85 raise PLCInvalidArgument, "No such interface"
86 interface = interfaces[0]
88 # Authenticated function
89 assert self.caller is not None
91 # If we are not an admin, make sure that the caller is a
92 # member of the site where the node exists.
93 if 'admin' not in self.caller['roles']:
94 nodes = Nodes(self.api, [interface['node_id']])
96 raise PLCPermissionDenied, "Interface is not associated with a node"
98 if node['site_id'] not in self.caller['site_ids']:
99 raise PLCPermissionDenied, "Not allowed to update interface"
102 # In case the user is updating one of (ip, type, network), but not the
103 # other two, then fetch the address and get the other two. If the
104 # interface has more than one address, then the behavior is undefined.
105 # we'll update the first IpAddress only
106 ip_address_ids = interface["ip_address_ids"]
107 address_fields = {'type': 'ipv4'}
109 address_fields = IpAddresses(self.api, ip_address_ids[0])[0]
111 def clean_field(obj, key):
118 val = clean_field(native, 'ip')
120 address_fields["ip_addr"] = val
122 val = clean_field(native, 'type')
124 address_fields["type"] = val
126 val = clean_field(native, 'netmask')
128 address_fields["netmask"] = val
130 for key in ("network", "broadcast"):
131 clean_field(native, key)
133 for key in ("last_updated", "ip_address_id", "interface_id"):
134 clean_field(address_fields, key)
138 # check if DNS or gateway is changed if this is primary interface
141 if interface['is_primary'] and native.has_key('gateway'):
142 route_fields['node_id'] = interface['node_id']
143 route_fields['interface_id'] = interface['interface_id']
144 route_fields['subnet'] = '0.0.0.0/0'
145 route_fields['next_hop'] = native['gateway']
148 if native.has_key('dns1'):
149 dns += native['dns1']
150 if native.has_key('dns2'):
151 dns += ",%s" % native['dns2']
154 interface.update(native)
158 # we have no idea which one to delete if there's multiple interfaces,
159 # so delete them all.
160 for ip_address_id in ip_address_ids:
161 DeleteIpAddress(self.api).__call__(auth, ip_address_id)
163 AddIpAddress(self.api).__call__(auth, interface_id, address_fields)
167 # remove routes for interface and add new default gw if this is a primary interface
169 routes = Routes(self.api,
170 {'interface_id' : route_fields['interface_id'],
171 'subnet' : route_fields['subnet']})
173 DeleteRoute(self.api).__call__(auth, route['route_id'])
174 AddRoute(self.api).__call(auth, route_fields)
176 # update dns if this is primary
178 UpdateNode(self.api).__call__(auth, interface['node_id'], {'dns':dns})
181 for (tagname,value) in tags.iteritems():
182 # the tagtype instance is assumed to exist, just check that
183 if not TagTypes(self.api,{'tagname':tagname}):
184 raise PLCInvalidArgument,"No such TagType %s"%tagname
185 interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']})
186 if not interface_tags:
187 AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value)
189 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
191 self.event_objects = {'Interface': [interface['interface_id']]}
192 if 'ip' in interface:
193 self.message = "Interface %s updated"%interface['ip']
195 self.message = "Interface %d updated"%interface['interface_id']
196 self.message += "[%s]." % ", ".join(interface_fields.keys())