X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FAddNode.py;h=25e1172a2ea107aac467c99889c87acd4a83ddf8;hb=e0b2f2c5b77f0dd34c3f4f685225819c6b521456;hp=51794644d0569689b1a3c26827acecb37d1bdd29;hpb=7e14913168a5b321d1260ce8ba2a759326d49857;p=plcapi.git diff --git a/PLC/Methods/AddNode.py b/PLC/Methods/AddNode.py index 5179464..25e1172 100644 --- a/PLC/Methods/AddNode.py +++ b/PLC/Methods/AddNode.py @@ -1,17 +1,21 @@ +# $Id# from PLC.Faults import * from PLC.Method import Method from PLC.Parameter import Parameter, Mixed from PLC.Nodes import Node, Nodes from PLC.NodeGroups import NodeGroup, NodeGroups from PLC.Sites import Site, Sites -from PLC.Auth import PasswordAuth +from PLC.Auth import Auth + +can_update = lambda (field, value): field in \ + ['hostname', 'boot_state', 'model', 'version'] class AddNode(Method): """ - Adds a new node. Any values specified in optional_vals are used, + Adds a new node. Any values specified in node_fields are used, otherwise defaults are used. - PIs and techs may only add nodes to their own sites. ins may + PIs and techs may only add nodes to their own sites. Admins may add nodes to any site. Returns the new node_id (> 0) if successful, faults otherwise. @@ -19,31 +23,26 @@ class AddNode(Method): roles = ['admin', 'pi', 'tech'] - can_update = lambda (field, value): field in \ - ['model', 'version'] - update_fields = dict(filter(can_update, Node.fields.items())) + node_fields = dict(filter(can_update, Node.fields.items())) accepts = [ - PasswordAuth(), + Auth(), Mixed(Site.fields['site_id'], Site.fields['login_base']), - Node.fields['hostname'], - Node.fields['boot_state'], - update_fields + node_fields ] returns = Parameter(int, 'New node_id (> 0) if successful') - def call(self, auth, site_id_or_login_base, hostname, boot_state, optional_vals = {}): - if filter(lambda field: field not in self.update_fields, optional_vals): - raise PLCInvalidArgument, "Invalid fields specified" + def call(self, auth, site_id_or_login_base, node_fields): + node_fields = dict(filter(can_update, node_fields.items())) # Get site information sites = Sites(self.api, [site_id_or_login_base]) if not sites: raise PLCInvalidArgument, "No such site" - site = sites.values()[0] + site = sites[0] # Authenticated function assert self.caller is not None @@ -57,10 +56,12 @@ class AddNode(Method): else: assert self.caller['person_id'] in site['person_ids'] - node = Node(self.api, optional_vals) - node['hostname'] = hostname - node['boot_state'] = boot_state + node = Node(self.api, node_fields) node['site_id'] = site['site_id'] node.sync() + self.event_objects = {'Site': [site['site_id']], + 'Node': [node['node_id']]} + self.message = "Node %s created" % node['node_id'] + return node['node_id']