fix PLCAPI doc that was whining about duplicate ids in docbook xml output
[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     # needed for generating the doc and prevent conflicts in the xml ids
74     status = 'legacy'
75
76     def call(self, auth, interface_id, interface_fields):
77
78         [native,tags,rejected] = Row.split_fields(interface_fields,[legacy_interface_fields,Interface.tags])
79
80         # type checking
81         native= Row.check_fields (native, self.accepted_fields)
82         if rejected:
83             raise PLCInvalidArgument, "Cannot update Interface column(s) %r"%rejected
84
85         # Get interface information
86         interfaces = Interfaces(self.api, [interface_id])
87         if not interfaces:
88             raise PLCInvalidArgument, "No such interface"
89         interface = interfaces[0]
90
91         # Authenticated function
92         assert self.caller is not None
93
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']])
98             if not nodes:
99                 raise PLCPermissionDenied, "Interface is not associated with a node"
100             node = nodes[0]
101             if node['site_id'] not in self.caller['site_ids']:
102                 raise PLCPermissionDenied, "Not allowed to update interface"
103
104
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'}
111         if ip_address_ids:
112             address_fields = IpAddresses(self.api, ip_address_ids[0])[0]
113             
114         def clean_field(obj, key):
115             val = None
116             if obj.has_key(key):
117                 val = obj[key]
118                 del obj[key]
119             return val
120
121         val = clean_field(native, 'ip')
122         if val:
123             address_fields["ip_addr"] = val
124
125         val = clean_field(native, 'type')
126         if val:
127             address_fields["type"] = val
128
129         val = clean_field(native, 'netmask')
130         if val:
131             address_fields["netmask"] = val
132
133         for key in ("network", "broadcast"):
134             clean_field(native, key)
135
136         for key in ("last_updated", "ip_address_id", "interface_id"):
137             clean_field(address_fields, key)
138
139
140
141         # check if DNS or gateway is changed if this is primary interface
142         dns = ""
143         route_fields = {}
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']
149
150         dns = ""
151         if native.has_key('dns1'):
152             dns += native['dns1']
153         if native.has_key('dns2'):
154             dns += ",%s" % native['dns2']
155
156
157         interface.update(native)
158         interface.sync()
159
160
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)
165
166         AddIpAddress(self.api).__call__(auth, interface_id, address_fields)
167
168
169
170         # remove routes for interface and add new default gw if this is a primary interface
171         if route_fields:
172             routes = Routes(self.api, 
173                             {'interface_id' : route_fields['interface_id'], 
174                              'subnet' : route_fields['subnet']})
175             for route in routes:
176                 DeleteRoute(self.api).__call__(auth, route['route_id'])
177             AddRoute(self.api).__call(auth, route_fields)
178
179         # update dns if this is primary 
180         if dns:
181             UpdateNode(self.api).__call__(auth, interface['node_id'], {'dns':dns})
182
183
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)
191             else:
192                 UpdateInterfaceTag(self.api).__call__(auth,interface_tags[0]['interface_tag_id'],value)
193
194         self.event_objects = {'Interface': [interface['interface_id']]}
195         if 'ip' in interface:
196             self.message = "Interface %s updated"%interface['ip']
197         else:
198             self.message = "Interface %d updated"%interface['interface_id']
199         self.message += "[%s]." % ", ".join(interface_fields.keys())
200
201         return 1