530a24f661cfdbcbc2d45cd8dd7d768e6eb4aad3
[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, SessionAuth
5 from PLC.Nodes import Node, Nodes
6 from PLC.Interfaces import Interface, Interfaces
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     roles = ['node']
21
22     interface_fields = dict(filter(can_update, Interface.fields.items()))
23
24     accepts = [
25         Mixed(BootAuth(), SessionAuth()),
26         {'boot_state': Node.fields['boot_state'],
27          'primary_network': interface_fields,
28          'ssh_host_key': Node.fields['ssh_rsa_key']}
29         ]
30
31     returns = Parameter(int, '1 if successful')
32
33     def call(self, auth, node_fields):
34         # Update node state
35         if node_fields.has_key('boot_state'):
36             self.caller['boot_state'] = node_fields['boot_state']
37         if node_fields.has_key('ssh_host_key'):
38             self.caller['ssh_rsa_key'] = node_fields['ssh_host_key']
39
40         # Update primary node network state
41         if node_fields.has_key('primary_network'):
42             primary_network = node_fields['primary_network'] 
43
44             if 'interface_id' not in primary_network:
45                 raise PLCInvalidArgument, "Node network not specified"
46             if primary_network['interface_id'] not in self.caller['interface_ids']:
47                 raise PLCInvalidArgument, "Node network not associated with calling node"
48
49             interfaces = Interfaces(self.api, [primary_network['interface_id']])
50             if not interfaces:
51                 raise PLCInvalidArgument, "No such node network"
52             interface = interfaces[0]
53
54             if not interface['is_primary']:
55                 raise PLCInvalidArgument, "Not the primary node network on record"
56
57             interface_fields = dict(filter(can_update, primary_network.items()))
58             interface.update(interface_fields)
59             interface.sync(commit = False)
60
61         self.caller.sync(commit = True)
62         self.message = "Node updated: %s" % ", ".join(node_fields.keys())
63
64         return 1