revised type-checking on taggable classes - previous code would reject any tag
[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)
31     accepted_fields.update(Node.tags)
32
33     accepts = [
34         Auth(),
35         Mixed(Site.fields['site_id'],
36               Site.fields['login_base']),
37         accepted_fields
38         ]
39
40     returns = Parameter(int, 'New node_id (> 0) if successful')
41
42     def call(self, auth, site_id_or_login_base, node_fields):
43
44         [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
45
46         # type checking
47         native = Row.check_fields (native, self.accepted_fields)
48         if rejected:
49             raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
50
51         # Get site information
52         sites = Sites(self.api, [site_id_or_login_base])
53         if not sites:
54             raise PLCInvalidArgument, "No such site"
55
56         site = sites[0]
57
58         # Authenticated function
59         assert self.caller is not None
60
61         # If we are not an admin, make sure that the caller is a
62         # member of the site.
63         if 'admin' not in self.caller['roles']:
64             if site['site_id'] not in self.caller['site_ids']:
65                 assert self.caller['person_id'] not in site['person_ids']
66                 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
67             else:
68                 assert self.caller['person_id'] in site['person_ids']
69
70         node = Node(self.api, native)
71         node['site_id'] = site['site_id']
72         node.sync()
73
74         for (tagname,value) in tags.iteritems():
75             # the tagtype instance is assumed to exist, just check that
76             if not TagTypes(self.api,{'tagname':tagname}):
77                 raise PLCInvalidArgument,"No such TagType %s"%tagname
78             node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
79             if not node_tags:
80                 AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value)
81             else:
82                 UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value)
83
84         self.event_objects = {'Site': [site['site_id']],
85                              'Node': [node['node_id']]} 
86         self.message = "Node %s created" % node['node_id']
87
88         return node['node_id']