X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FBootUpdateNode.py;h=c57048a8936d4b41447d1ee12b1a4f8b74b251f1;hb=49b6557372cb6cf7251755f8170e0fc884eddf88;hp=48e02193bdc94e51bb059f3a0ee172b9b4d580a4;hpb=1f8c38dd1357c93e4be8d94456b7274a591d2db4;p=plcapi.git diff --git a/PLC/Methods/BootUpdateNode.py b/PLC/Methods/BootUpdateNode.py index 48e0219..c57048a 100644 --- a/PLC/Methods/BootUpdateNode.py +++ b/PLC/Methods/BootUpdateNode.py @@ -1,6 +1,15 @@ +# $Id$ +# $URL$ +from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed -from PLC.Auth import Auth, BootAuth +from PLC.Auth import Auth, BootAuth, SessionAuth +from PLC.Nodes import Node, Nodes +from PLC.Interfaces import Interface, Interfaces + +can_update = lambda (field, value): field in \ + ['method', 'mac', 'gateway', 'network', + 'broadcast', 'netmask', 'dns1', 'dns2'] class BootUpdateNode(Method): """ @@ -10,9 +19,53 @@ class BootUpdateNode(Method): Returns 1 if updated successfully. """ - accepts = [BootAuth(), dict] + roles = ['node'] + + interface_fields = dict(filter(can_update, Interface.fields.items())) + + accepts = [ + Mixed(BootAuth(), SessionAuth()), + {'boot_state': Node.fields['boot_state'], + 'primary_network': interface_fields, + 'ssh_host_key': Node.fields['ssh_rsa_key']} + ] + returns = Parameter(int, '1 if successful') - def call(self, auth, update_fields): - # XXX + def call(self, auth, node_fields): + # Update node state + if node_fields.has_key('boot_state'): + self.caller['boot_state'] = node_fields['boot_state'] + if node_fields.has_key('ssh_host_key'): + self.caller['ssh_rsa_key'] = node_fields['ssh_host_key'] + + # Update primary interface state + if node_fields.has_key('primary_network'): + primary_network = node_fields['primary_network'] + + if 'interface_id' not in primary_network: + raise PLCInvalidArgument, "Interface not specified" + if primary_network['interface_id'] not in self.caller['interface_ids']: + raise PLCInvalidArgument, "Interface not associated with calling node" + + interfaces = Interfaces(self.api, [primary_network['interface_id']]) + if not interfaces: + raise PLCInvalidArgument, "No such interface %r"%interface_id + interface = interfaces[0] + + if not interface['is_primary']: + raise PLCInvalidArgument, "Not the primary interface on record" + + interface_fields = dict(filter(can_update, primary_network.items())) + interface.update(interface_fields) + interface.sync(commit = False) + + # indicate that node has booted & contacted PLC. + if isinstance(self.caller, Node): + node = self.caller + node.update_last_contact() + + self.caller.sync(commit = True) + self.message = "Node updated: %s" % ", ".join(node_fields.keys()) + return 1