revised type-checking on taggable classes - previous code would reject any tag
[plcapi.git] / PLC / Methods / UpdateNode.py
1 # $Id$
2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.Table import Row
6 from PLC.Auth import Auth
7
8 from PLC.Nodes import Node, Nodes
9 from PLC.TagTypes import TagTypes
10 from PLC.NodeTags import NodeTags
11 from PLC.Methods.AddNodeTag import AddNodeTag
12 from PLC.Methods.UpdateNodeTag import UpdateNodeTag
13
14 admin_only = [ 'key', 'session', 'boot_nonce', 'site_id']
15 can_update = ['hostname', 'boot_state', 'model', 'version'] + admin_only 
16
17 class UpdateNode(Method):
18     """
19     Updates a node. Only the fields specified in node_fields are
20     updated, all other fields are left untouched.
21     
22     PIs and techs can update only the nodes at their sites. Only
23     admins can update the key, session, and boot_nonce fields.
24
25     Returns 1 if successful, faults otherwise.
26     """
27
28     roles = ['admin', 'pi', 'tech']
29
30     accepted_fields = Row.accepted_fields(can_update,Node.fields)
31     # xxx check the related_fields feature
32     accepted_fields.update(Node.related_fields)
33     accepted_fields.update(Node.tags)
34
35     accepts = [
36         Auth(),
37         Mixed(Node.fields['node_id'],
38               Node.fields['hostname']),
39         accepted_fields
40         ]
41
42     returns = Parameter(int, '1 if successful')
43
44     def call(self, auth, node_id_or_hostname, node_fields):
45         
46         # split provided fields 
47         [native,related,tags,rejected] = Row.split_fields(node_fields,[Node.fields,Node.related_fields,Node.tags])
48
49         # type checking
50         native = Row.check_fields (native, self.accepted_fields)
51         if rejected:
52             raise PLCInvalidArgument, "Cannot update Node column(s) %r"%rejected
53
54         # Authenticated function
55         assert self.caller is not None
56
57         # Remove admin only fields
58         if 'admin' not in self.caller['roles']:
59             for key in admin_only:
60                 if native.has_key(key):
61                     del native[key]
62
63         # Get account information
64         nodes = Nodes(self.api, [node_id_or_hostname])
65         if not nodes:
66             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
67         node = nodes[0]
68
69         if node['peer_id'] is not None:
70             raise PLCInvalidArgument, "Not a local node %r"%node_id_or_hostname
71
72         # If we are not an admin, make sure that the caller is a
73         # member of the site at which the node is located.
74         if 'admin' not in self.caller['roles']:
75             if node['site_id'] not in self.caller['site_ids']:
76                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
77
78         # Make requested associations
79         for (k,v) in related.iteritems():
80             node.associate(auth, k,v)
81
82         node.update(native)
83         node.update_last_updated(commit=False)
84         node.sync(commit=True)
85         
86         for (tagname,value) in tags.iteritems():
87             # the tagtype instance is assumed to exist, just check that
88             if not TagTypes(self.api,{'tagname':tagname}):
89                 raise PLCInvalidArgument,"No such TagType %s"%tagname
90             node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
91             if not node_tags:
92                 AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value)
93             else:
94                 UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value)
95
96         # Logging variables
97         self.event_objects = {'Node': [node['node_id']]}
98         if 'hostname' in node:
99             self.message = 'Node %s updated'%node['hostname']
100         else:
101             self.message = 'Node %d updated'%node['node_id']
102         self.message += " [%s]." % (", ".join(node_fields.keys()),)
103         if 'boot_state' in node_fields.keys():
104                 self.message += ' boot_state updated to %s' % node_fields['boot_state']
105
106         return 1