- make Add() calling convention consistent among all functions that
[plcapi.git] / PLC / Methods / AddNodeNetwork.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.Auth import PasswordAuth
7
8 can_update = lambda (field, value): field not in ['nodenetwork_id']
9
10 class AddNodeNetwork(Method):
11     """
12     Adds a new network for a node. Any values specified in
13     nodenetwork_fields are used, otherwise defaults are used. Acceptable
14     values for method are dhcp, static, proxy, tap, and
15     ipmi. Acceptable value for type is ipv4. If type is static, ip,
16     gateway, network, broadcast, netmask, and dns1 must all be
17     specified in nodenetwork_fields. If type is dhcp, these parameters,
18     even if specified, are ignored.
19
20     PIs and techs may only add networks to their own nodes. ins may
21     add networks to any node.
22
23     Returns the new nodenetwork_id (> 0) if successful, faults otherwise.
24     """
25
26     roles = ['admin', 'pi', 'tech']
27
28     nodenetwork_fields = dict(filter(can_update, NodeNetwork.fields.items()))
29
30     accepts = [
31         PasswordAuth(),
32         nodenetwork_fields
33         ]
34
35     returns = Parameter(int, 'New nodenetwork_id (> 0) if successful')
36
37     event_type = 'Add'
38     object_type = 'NodeNetwork'
39     object_ids = []
40
41     def call(self, auth, nodenetwork_fields = {}):
42         nodenetwork_fields = dict(filter(can_update, nodenetwork_fields.items()))
43
44         # Check if node exists
45         nodes = Nodes(self.api, [nodenetwork_fields['node_id']]).values()
46         if not nodes:
47             raise PLCInvalidArgument, "No such node"
48         node = nodes[0]
49
50         # Authenticated function
51         assert self.caller is not None
52
53         # If we are not an admin, make sure that the caller is a
54         # member of the site where the node exists.
55         if 'admin' not in self.caller['roles']:
56             if node['site_id'] not in self.caller['site_ids']:
57                 raise PLCPermissionDenied, "Not allowed to add node network for specified node"
58
59         # Add node network
60         nodenetwork = NodeNetwork(self.api, nodenetwork_fields)
61         nodenetwork.sync()
62
63         self.object_ids = [node['node_id'], nodenetwork['nodenetwork_id']]      
64
65         return nodenetwork['nodenetwork_id']