embed svn Id keyword
[plcapi.git] / PLC / Methods / AddInterface.py
1 # $Id#
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Nodes import Node, Nodes
6 from PLC.Interfaces import Interface, Interfaces
7 from PLC.Auth import Auth
8
9 can_update = lambda (field, value): field not in ['interface_id', 'node_id']
10
11 class AddInterface(Method):
12     """
13
14     Adds a new network for a node. Any values specified in
15     interface_fields are used, otherwise defaults are
16     used. Acceptable values for method may be retrieved via
17     GetNetworkMethods. Acceptable values for type may be retrieved via
18     GetNetworkTypes.
19
20     If type is static, ip, gateway, network, broadcast, netmask, and
21     dns1 must all be specified in interface_fields. If type is dhcp,
22     these parameters, even if specified, are ignored.
23
24     PIs and techs may only add networks to their own nodes. Admins may
25     add networks to any node.
26
27     Returns the new interface_id (> 0) if successful, faults otherwise.
28     """
29
30     roles = ['admin', 'pi', 'tech']
31
32     interface_fields = dict(filter(can_update, Interface.fields.items()))
33
34     accepts = [
35         Auth(),
36         Mixed(Node.fields['node_id'],
37               Node.fields['hostname']),
38         interface_fields
39         ]
40
41     returns = Parameter(int, 'New interface_id (> 0) if successful')
42
43     
44     def call(self, auth, node_id_or_hostname, interface_fields):
45         interface_fields = dict(filter(can_update, interface_fields.items()))
46
47         # Check if node exists
48         nodes = Nodes(self.api, [node_id_or_hostname])
49         if not nodes:
50             raise PLCInvalidArgument, "No such node"
51         node = nodes[0]
52
53         # Authenticated function
54         assert self.caller is not None
55
56         # If we are not an admin, make sure that the caller is a
57         # member of the site where the node exists.
58         if 'admin' not in self.caller['roles']:
59             if node['site_id'] not in self.caller['site_ids']:
60                 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
61
62         # Add node network
63         interface = Interface(self.api, interface_fields)
64         interface['node_id'] = node['node_id']
65         # if this is the first node network, make it primary
66         if not node['interface_ids']:
67                 interface['is_primary'] = True
68         interface.sync()
69         
70         # Logging variables
71         self.object_ids = [node['node_id'], interface['interface_id']]  
72         self.messgage = "Node network %d added" % interface['interface_id']
73
74         return interface['interface_id']