bring over newinterface branch from Verivue
[plcapi.git] / PLC / Methods / Legacy / UpdateInterface.py
1 # $Id$
2 # $URL$
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
8
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
21
22 cannot_update = ['interface_id','node_id']
23
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"),
42         }
43
44
45 class UpdateInterface(Method):
46     """
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.
53
54     PIs and techs may only update interfaces associated with their own
55     nodes. Admins may update any interface network.
56
57     Returns 1 if successful, faults otherwise.
58     """
59
60     roles = ['admin', 'pi', 'tech']
61
62     accepted_fields = Row.accepted_fields(cannot_update, legacy_interface_fields,exclude=True)
63     accepted_fields.update(Interface.tags)
64
65     accepts = [
66         Auth(),
67         Interface.fields['interface_id'],
68         accepted_fields
69         ]
70
71     returns = Parameter(int, '1 if successful')
72
73     def call(self, auth, interface_id, interface_fields):
74
75         [native,tags,rejected] = Row.split_fields(interface_fields,[legacy_interface_fields,Interface.tags])
76
77         # type checking
78         native= Row.check_fields (native, self.accepted_fields)
79         if rejected:
80             raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
81
82         # Get interface information
83         interfaces = Interfaces(self.api, [interface_id])
84         if not interfaces:
85             raise PLCInvalidArgument, "No such interface"
86         interface = interfaces[0]
87
88         # Authenticated function
89         assert self.caller is not None
90
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']])
95             if not nodes:
96                 raise PLCPermissionDenied, "Interface is not associated with a node"
97             node = nodes[0]
98             if node['site_id'] not in self.caller['site_ids']:
99                 raise PLCPermissionDenied, "Not allowed to update interface"
100
101
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'}
108         if ip_address_ids:
109             address_fields = IpAddresses(self.api, ip_address_ids[0])[0]
110             
111         def clean_field(obj, key):
112             val = None
113             if obj.has_key(key):
114                 val = obj[key]
115                 del obj[key]
116             return val
117
118         val = clean_field(native, 'ip')
119         if val:
120             address_fields["ip_addr"] = val
121
122         val = clean_field(native, 'type')
123         if val:
124             address_fields["type"] = val
125
126         val = clean_field(native, 'netmask')
127         if val:
128             address_fields["netmask"] = val
129
130         for key in ("network", "broadcast"):
131             clean_field(native, key)
132
133         for key in ("last_updated", "ip_address_id", "interface_id"):
134             clean_field(address_fields, key)
135
136
137
138         # check if DNS or gateway is changed if this is primary interface
139         dns = ""
140         route_fields = {}
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']
146
147         dns = ""
148         if native.has_key('dns1'):
149             dns += native['dns1']
150         if native.has_key('dns2'):
151             dns += ",%s" % native['dns2']
152
153
154         interface.update(native)
155         interface.sync()
156
157
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)
162
163         AddIpAddress(self.api).__call__(auth, interface_id, address_fields)
164
165
166
167         # remove routes for interface and add new default gw if this is a primary interface
168         if route_fields:
169             routes = Routes(self.api, 
170                             {'interface_id' : route_fields['interface_id'], 
171                              'subnet' : route_fields['subnet']})
172             for route in routes:
173                 DeleteRoute(self.api).__call__(auth, route['route_id'])
174             AddRoute(self.api).__call(auth, route_fields)
175
176         # update dns if this is primary 
177         if dns:
178             UpdateNode(self.api).__call__(auth, interface['node_id'], {'dns':dns})
179
180
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)
188             else:
189                 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
190
191         self.event_objects = {'Interface': [interface['interface_id']]}
192         if 'ip' in interface:
193             self.message = "Interface %s updated"%interface['ip']
194         else:
195             self.message = "Interface %d updated"%interface['interface_id']
196         self.message += "[%s]." % ", ".join(interface_fields.keys())
197
198         return 1