X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FMethods%2FAddNode.py;h=e8d073ad8221510f745e9ff2fc7ec791d2d24de7;hb=dd73bbbd06bbd35f5f2c9b45421bee889b70bc48;hp=fc1b18a4218f4d2c86b2b8f0daece5273d55d6e3;hpb=2030159bddcc26c81a29f7b4791e65fede1ee9f0;p=plcapi.git diff --git a/PLC/Methods/AddNode.py b/PLC/Methods/AddNode.py index fc1b18a..e8d073a 100644 --- a/PLC/Methods/AddNode.py +++ b/PLC/Methods/AddNode.py @@ -1,14 +1,22 @@ +# $Id$ from PLC.Faults import * +from PLC.Auth import Auth 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.Table import Row + from PLC.Sites import Site, Sites -from PLC.Auth import PasswordAuth +from PLC.Nodes import Node, Nodes +from PLC.TagTypes import TagTypes +from PLC.NodeTags import NodeTags +from PLC.Methods.AddNodeTag import AddNodeTag +from PLC.Methods.UpdateNodeTag import UpdateNodeTag + +can_update = ['hostname', 'node_type', '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. Admins may @@ -19,30 +27,30 @@ class AddNode(Method): roles = ['admin', 'pi', 'tech'] - can_update = lambda (field, value): field in \ - ['boot_state', 'model', 'version'] - update_fields = dict(filter(can_update, Node.fields.items())) + accepted_fields = Row.accepted_fields(can_update, [Node.fields,Node.tags]) accepts = [ - PasswordAuth(), + Auth(), Mixed(Site.fields['site_id'], Site.fields['login_base']), - Node.fields['hostname'], - update_fields + accepted_fields ] returns = Parameter(int, 'New node_id (> 0) if successful') - def call(self, auth, site_id_or_login_base, hostname, 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): + + [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags]) + + if rejected: + raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected # 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 @@ -56,9 +64,22 @@ class AddNode(Method): else: assert self.caller['person_id'] in site['person_ids'] - node = Node(self.api, optional_vals) - node['hostname'] = hostname + node = Node(self.api, native) node['site_id'] = site['site_id'] node.sync() + for (tagname,value) in tags.iteritems(): + # the tagtype instance is assumed to exist, just check that + if not TagTypes(self.api,{'tagname':tagname}): + raise PLCInvalidArgument,"No such TagType %s"%tagname + node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']}) + if not node_tags: + AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value) + else: + UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value) + + self.event_objects = {'Site': [site['site_id']], + 'Node': [node['node_id']]} + self.message = "Node %s created" % node['node_id'] + return node['node_id']