Initial checkin of new API implementation
[plcapi.git] / PLC / Methods / AdmAddNodeNetwork.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Nodes import Node, Nodes
5 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
6 from PLC.Sites import Site, Sites
7 from PLC.Auth import PasswordAuth
8
9 class AdmAddNodeNetwork(Method):
10     """
11     Adds a new newtwork for a node. Any values specified in optional_vals are used,
12     otherwise defaults are used. Acceptable values for method are dhcp, static, 
13     proxy, tap, and ipmi. Acceptable value for type is ipv4. If type is static, 
14     the parameter optional_vals must be present and ip, gateway, network, broadcast, 
15     netmask, and dns1 must all be specified. If type is dhcp, these parameters, even 
16     if specified, are ignored. Returns the new nodenetwork_id (>0) if successful.
17
18     PIs and techs may only add networks to their own nodes. Admins may
19     add networks to any node.
20
21     Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
22     """
23
24     roles = ['admin', 'pi', 'tech']
25
26     cant_update = lambda (field, value): field not in \
27                  ['nodenetwork_id']
28     update_fields = dict(filter(cant_update, NodeNetwork.all_fields.items()))
29
30     accepts = [
31         PasswordAuth(),
32         NodeNetwork.all_fields['node_id'],
33         NodeNetwork.all_fields['method'],
34         NodeNetwork.all_fields['type'],
35         update_fields
36         ]
37
38     returns = Parameter(int, '1 if successful')
39
40     def call(self, auth, node_id, method, type, optional_vals = {}):
41         if filter(lambda field: field not in self.update_fields, optional_vals):
42             raise PLCInvalidArgument, "Invalid fields specified"
43
44         # check if node exists
45         nodes = Nodes(self.api, [node_id], Node.extra_fields).values()
46         if not nodes:
47             raise PLCInvalidArgument, "No such node"
48         node = nodes[0]
49         
50         # Make sure node network doesnt already exist
51         nodenetworks = NodeNetworks(self.api).values()
52         if nodenetworks:
53                 for nodenetwork in nodenetworks:
54                         if nodenetwork['node_id'] == node_id and nodenetwork['method'] == method and nodenetwork['type'] == type:
55                                 raise PLCInvalidArgument, "Node Network already exists"
56
57         # Authenticated function
58         assert self.caller is not None
59
60         # If we are not an admin, make sure that the caller is a
61         # member of the site where the node exists.
62         if 'admin' not in self.caller['roles']:
63                 if node['site_id'] not in self.caller['site_ids']:
64                         raise PLCPermissionDenied, "Not allowed to add node network for specified node"
65                 if 'tech' not in self.caller['roles']:
66                         raise PLCPermissionDenied, "Not allowed to add node network for specified node"
67         
68
69         # add node network
70         nodenetwork = NodeNetwork(self.api, optional_vals)
71         nodenetwork['node_id'] = node_id
72         nodenetwork['method'] = method
73         nodenetwork['type'] = type
74         nodenetwork.flush()
75
76         return nodenetwork['nodenetwork_id']