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