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.Table import Row
6 from PLC.Namespace import hostname_to_hrn
7 from PLC.Peers import Peers
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, NodeTag
12 from PLC.Methods.AddNodeTag import AddNodeTag
13 from PLC.Methods.UpdateNodeTag import UpdateNodeTag
15 can_update = ['hostname', 'node_type', 'boot_state', 'model', 'version']
17 class AddNode(Method):
19 Adds a new node. Any values specified in node_fields are used,
20 otherwise defaults are used.
22 PIs and techs may only add nodes to their own sites. Admins may
23 add nodes to any site.
25 Returns the new node_id (> 0) if successful, faults otherwise.
28 roles = ['admin', 'pi', 'tech']
30 accepted_fields = Row.accepted_fields(can_update,Node.fields)
31 accepted_fields.update(Node.tags)
35 Mixed(Site.fields['site_id'],
36 Site.fields['login_base']),
40 returns = Parameter(int, 'New node_id (> 0) if successful')
42 def call(self, auth, site_id_or_login_base, node_fields):
44 [native,tags,rejected]=Row.split_fields(node_fields,[Node.fields,Node.tags])
47 native = Row.check_fields(native, self.accepted_fields)
49 raise PLCInvalidArgument("Cannot add Node with column(s) %r"%rejected)
51 # Get site information
52 sites = Sites(self.api, [site_id_or_login_base])
54 raise PLCInvalidArgument("No such site")
58 # Authenticated function
59 assert self.caller is not None
61 # If we are not an admin, make sure that the caller is a
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")
68 assert self.caller['person_id'] in site['person_ids']
70 node = Node(self.api, native)
71 node['site_id'] = site['site_id']
74 # since hostname was specified lets add the 'hrn' node tag
75 root_auth = self.api.config.PLC_HRN_ROOT
76 login_base = site['login_base']
77 tags['hrn'] = hostname_to_hrn(root_auth, login_base, node['hostname'])
79 for (tagname,value) in tags.items():
80 # the tagtype instance is assumed to exist, just check that
81 tag_types = TagTypes(self.api,{'tagname':tagname})
83 raise PLCInvalidArgument("No such TagType %s"%tagname)
84 tag_type = tag_types[0]
85 node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
87 node_tag = NodeTag(self.api)
88 node_tag['node_id'] = node['node_id']
89 node_tag['tag_type_id'] = tag_type['tag_type_id']
90 node_tag['tagname'] = tagname
91 node_tag['value'] = value
94 node_tag = node_tags[0]
95 node_tag['value'] = value
98 self.event_objects = {'Site': [site['site_id']],
99 'Node': [node['node_id']]}
100 self.message = "Node %d=%s created" % (node['node_id'],node['hostname'])
102 return node['node_id']