- implement for backward compatibility
[plcapi.git] / PLC / Methods / BootUpdateNode.py
1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.Auth import Auth, BootAuth
5 from PLC.Nodes import Node, Nodes
6 from PLC.NodeNetworks import NodeNetwork, NodeNetworks
7
8 can_update = lambda (field, value): field in \
9              ['method', 'mac', 'gateway', 'network',
10               'broadcast', 'netmask', 'dns1', 'dns2']
11
12 class BootUpdateNode(Method):
13     """
14     Allows the calling node to update its own record. Only the primary
15     network can be updated, and the node IP cannot be changed.
16
17     Returns 1 if updated successfully.
18     """
19
20     nodenetwork_fields = dict(filter(can_update, NodeNetwork.fields.items()))
21
22     accepts = [
23         BootAuth(),
24         {'boot_state': Node.fields['boot_state'],
25          'primary_network': nodenetwork_fields,
26          'ssh_host_key': Node.fields['ssh_rsa_key']}
27         ]
28     returns = Parameter(int, '1 if successful')
29
30     def call(self, auth, node_fields):
31         # Update node state
32         if node_fields.has_key('boot_state'):
33             self.caller['boot_state'] = node_fields['boot_state']
34         if node_fields.has_key('ssh_host_key'):
35             self.caller['ssh_rsa_key'] = node_fields['ssh_host_key']
36
37         # Update primary node network state
38         if node_fields.has_key('primary_network'):
39             primary_network = node_fields['primary_network'] 
40
41             if 'nodenetwork_id' not in primary_network:
42                 raise PLCInvalidArgument, "Node network not specified"
43             if primary_network['nodenetwork_id'] not in self.caller['nodenetwork_ids']:
44                 raise PLCInvalidArgument, "Node network not associated with calling node"
45
46             nodenetworks = NodeNetworks(self.api, [primary_network['nodenetwork_id']]).values()
47             if not nodenetworks:
48                 raise PLCInvalidArgument, "No such node network"
49             nodenetwork = nodenetworks[0]
50
51             if not nodenetwork['is_primary']:
52                 raise PLCInvalidArgument, "Not the primary node network on record"
53
54             nodenetwork_fields = dict(filter(can_update, primary_network.items()))
55             nodenetwork.update(nodenetwork_fields)
56             nodenetwork.sync(commit = False)
57
58         self.caller.sync(commit = True)
59
60         return 1