e8d073ad8221510f745e9ff2fc7ec791d2d24de7
[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         [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
44
45         if rejected:
46             raise PLCInvalidArgument, "Cannot add Node with column(s) %r"%rejected
47
48         # Get site information
49         sites = Sites(self.api, [site_id_or_login_base])
50         if not sites:
51             raise PLCInvalidArgument, "No such site"
52
53         site = sites[0]
54
55         # Authenticated function
56         assert self.caller is not None
57
58         # If we are not an admin, make sure that the caller is a
59         # member of the site.
60         if 'admin' not in self.caller['roles']:
61             if site['site_id'] not in self.caller['site_ids']:
62                 assert self.caller['person_id'] not in site['person_ids']
63                 raise PLCPermissionDenied, "Not allowed to add nodes to specified site"
64             else:
65                 assert self.caller['person_id'] in site['person_ids']
66
67         node = Node(self.api, native)
68         node['site_id'] = site['site_id']
69         node.sync()
70
71         for (tagname,value) in tags.iteritems():
72             # the tagtype instance is assumed to exist, just check that
73             if not TagTypes(self.api,{'tagname':tagname}):
74                 raise PLCInvalidArgument,"No such TagType %s"%tagname
75             node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
76             if not node_tags:
77                 AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value)
78             else:
79                 UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value)
80
81         self.event_objects = {'Site': [site['site_id']],
82                              'Node': [node['node_id']]} 
83         self.message = "Node %s created" % node['node_id']
84
85         return node['node_id']