Fix big leak in type-checking add/update args for taggable classes
[plcapi.git] / PLC / Methods / AddNode.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Auth import Auth
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Table import Row
7
8 from PLC.Sites import Site, Sites
9 from PLC.Nodes import Node, Nodes
10 from PLC.TagTypes import TagTypes
11 from PLC.NodeTags import NodeTags
12 from PLC.Methods.AddNodeTag import AddNodeTag
13 from PLC.Methods.UpdateNodeTag import UpdateNodeTag
14
15 can_update = ['hostname', 'node_type', 'boot_state', 'model', 'version']
16
17 class AddNode(Method):
18     """
19     Adds a new node. Any values specified in node_fields are used,
20     otherwise defaults are used.
21
22     PIs and techs may only add nodes to their own sites. Admins may
23     add nodes to any site.
24
25     Returns the new node_id (> 0) if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'tech']
29
30     accepted_fields = Row.accepted_fields(can_update, [Node.fields,Node.tags])
31
32     accepts = [
33         Auth(),
34         Mixed(Site.fields['site_id'],
35               Site.fields['login_base']),
36         accepted_fields
37         ]
38
39     returns = Parameter(int, 'New node_id (> 0) if successful')
40
41     def call(self, auth, site_id_or_login_base, node_fields):
42
43         node_fields = Row.check_fields (node_fields, self.accepted_fields)
44
45         [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
46
47         if rejected:
48             raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
49
50         # Get site information
51         sites = Sites(self.api, [site_id_or_login_base])
52         if not sites:
53             raise PLCInvalidArgument, "No such site"
54
55         site = sites[0]
56
57         # Authenticated function
58         assert self.caller is not None
59
60         # If we are not an admin, make sure that the caller is a
61         # member of the site.
62         if 'admin' not in self.caller['roles']:
63             if site['site_id'] not in self.caller['site_ids']:
64                 assert self.caller['person_id'] not in site['person_ids']
65                 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
66             else:
67                 assert self.caller['person_id'] in site['person_ids']
68
69         node = Node(self.api, native)
70         node['site_id'] = site['site_id']
71         node.sync()
72
73         for (tagname,value) in tags.iteritems():
74             # the tagtype instance is assumed to exist, just check that
75             if not TagTypes(self.api,{'tagname':tagname}):
76                 raise PLCInvalidArgument,"No such TagType %s"%tagname
77             node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
78             if not node_tags:
79                 AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value)
80             else:
81                 UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value)
82
83         self.event_objects = {'Site': [site['site_id']],
84                              'Node': [node['node_id']]} 
85         self.message = "Node %s created" % node['node_id']
86
87         return node['node_id']