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 # needed for generating the doc and prevent conflicts in the xml ids
76 def call(self, auth, interface_id, interface_fields):
78 [native,tags,rejected] = Row.split_fields(interface_fields,[legacy_interface_fields,Interface.tags])
81 native= Row.check_fields (native, self.accepted_fields)
83 raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
85 # Get interface information
86 interfaces = Interfaces(self.api, [interface_id])
88 raise PLCInvalidArgument, "No such interface"
89 interface = interfaces[0]
91 # Authenticated function
92 assert self.caller is not None
94 # If we are not an admin, make sure that the caller is a
95 # member of the site where the node exists.
96 if 'admin' not in self.caller['roles']:
97 nodes = Nodes(self.api, [interface['node_id']])
99 raise PLCPermissionDenied, "Interface is not associated with a node"
101 if node['site_id'] not in self.caller['site_ids']:
102 raise PLCPermissionDenied, "Not allowed to update interface"
105 # In case the user is updating one of (ip, type, network), but not the
106 # other two, then fetch the address and get the other two. If the
107 # interface has more than one address, then the behavior is undefined.
108 # we'll update the first IpAddress only
109 ip_address_ids = interface["ip_address_ids"]
110 address_fields = {'type': 'ipv4'}
112 address_fields = IpAddresses(self.api, ip_address_ids[0])[0]
114 def clean_field(obj, key):
121 val = clean_field(native, 'ip')
123 address_fields["ip_addr"] = val
125 val = clean_field(native, 'type')
127 address_fields["type"] = val
129 val = clean_field(native, 'netmask')
131 address_fields["netmask"] = val
133 for key in ("network", "broadcast"):
134 clean_field(native, key)
136 for key in ("last_updated", "ip_address_id", "interface_id"):
137 clean_field(address_fields, key)
141 # check if DNS or gateway is changed if this is primary interface
144 if interface['is_primary'] and native.has_key('gateway'):
145 route_fields['node_id'] = interface['node_id']
146 route_fields['interface_id'] = interface['interface_id']
147 route_fields['subnet'] = '0.0.0.0/0'
148 route_fields['next_hop'] = native['gateway']
151 if native.has_key('dns1'):
152 dns += native['dns1']
153 if native.has_key('dns2'):
154 dns += ",%s" % native['dns2']
157 interface.update(native)
161 # we have no idea which one to delete if there's multiple interfaces,
162 # so delete them all.
163 for ip_address_id in ip_address_ids:
164 DeleteIpAddress(self.api).__call__(auth, ip_address_id)
166 AddIpAddress(self.api).__call__(auth, interface_id, address_fields)
170 # remove routes for interface and add new default gw if this is a primary interface
172 routes = Routes(self.api,
173 {'interface_id' : route_fields['interface_id'],
174 'subnet' : route_fields['subnet']})
176 DeleteRoute(self.api).__call__(auth, route['route_id'])
177 AddRoute(self.api).__call(auth, route_fields)
179 # update dns if this is primary
181 UpdateNode(self.api).__call__(auth, interface['node_id'], {'dns':dns})
184 for (tagname,value) in tags.iteritems():
185 # the tagtype instance is assumed to exist, just check that
186 if not TagTypes(self.api,{'tagname':tagname}):
187 raise PLCInvalidArgument,"No such TagType %s"%tagname
188 interface_tags=InterfaceTags(self.api,{'tagname':tagname,'interface_id':interface['interface_id']})
189 if not interface_tags:
190 AddInterfaceTag(self.api).__call__(auth,interface['interface_id'],tagname,value)
192 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
194 self.event_objects = {'Interface': [interface['interface_id']]}
195 if 'ip' in interface:
196 self.message = "Interface %s updated"%interface['ip']
198 self.message = "Interface %d updated"%interface['interface_id']
199 self.message += "[%s]." % ", ".join(interface_fields.keys())