3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Auth import Auth, BootAuth, SessionAuth
7 from PLC.Nodes import Node, Nodes
8 from PLC.Interfaces import Interface, Interfaces
10 can_update = lambda (field, value): field in \
11 ['method', 'mac', 'gateway', 'network',
12 'broadcast', 'netmask', 'dns1', 'dns2']
14 class BootUpdateNode(Method):
16 Allows the calling node to update its own record. Only the primary
17 network can be updated, and the node IP cannot be changed.
19 Returns 1 if updated successfully.
24 interface_fields = dict(filter(can_update, Interface.fields.items()))
27 Mixed(BootAuth(), SessionAuth()),
28 {'boot_state': Node.fields['boot_state'],
29 'primary_network': interface_fields,
30 'ssh_host_key': Node.fields['ssh_rsa_key']}
33 returns = Parameter(int, '1 if successful')
35 def call(self, auth, node_fields):
37 if node_fields.has_key('boot_state'):
38 self.caller['boot_state'] = node_fields['boot_state']
39 if node_fields.has_key('ssh_host_key'):
40 self.caller['ssh_rsa_key'] = node_fields['ssh_host_key']
42 # Update primary interface state
43 if node_fields.has_key('primary_network'):
44 primary_network = node_fields['primary_network']
46 if 'interface_id' not in primary_network:
47 raise PLCInvalidArgument, "Interface not specified"
48 if primary_network['interface_id'] not in self.caller['interface_ids']:
49 raise PLCInvalidArgument, "Interface not associated with calling node"
51 interfaces = Interfaces(self.api, [primary_network['interface_id']])
53 raise PLCInvalidArgument, "No such interface %r"%interface_id
54 interface = interfaces[0]
56 if not interface['is_primary']:
57 raise PLCInvalidArgument, "Not the primary interface on record"
59 interface_fields = dict(filter(can_update, primary_network.items()))
60 interface.update(interface_fields)
61 interface.sync(commit = False)
63 # indicate that node has booted & contacted PLC.
64 if isinstance(self.caller, Node):
66 node.update_last_contact()
68 self.caller.sync(commit = True)
69 self.message = "Node updated: %s" % ", ".join(node_fields.keys())