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