6f60a46298caa342cadcb21666cb40de9e81876b
[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 can_update = ['hostname', 'boot_state', 'model', 'version','key', 'session', 'boot_nonce', 'site_id'] + \
15               Node.related_fields.keys()
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,Node.related_fields,Node.tags])
31
32     accepts = [
33         Auth(),
34         Mixed(Node.fields['node_id'],
35               Node.fields['hostname']),
36         accepted_fields
37         ]
38
39     returns = Parameter(int, '1 if successful')
40
41     def call(self, auth, node_id_or_hostname, node_fields):
42         
43         node_fields = Row.check_fields (node_fields, self.accepted_fields)
44
45         # split provided fields 
46         [native,related,tags,rejected] = Row.split_fields(node_fields,[Node.fields,Node.related_fields,Node.tags])
47
48         if rejected:
49             raise PLCInvalidArgument, "Cannot update Node column(s) %r"%rejected
50
51         # Remove admin only fields
52         if 'admin' not in self.caller['roles']:
53             for key in 'key', 'session', 'boot_nonce', 'site_id':
54                 if native.has_key(key):
55                     del native[key]
56
57         # Get account information
58         nodes = Nodes(self.api, [node_id_or_hostname])
59         if not nodes:
60             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
61         node = nodes[0]
62
63         if node['peer_id'] is not None:
64             raise PLCInvalidArgument, "Not a local node %r"%node_id_or_hostname
65
66         # Authenticated function
67         assert self.caller is not None
68
69         # If we are not an admin, make sure that the caller is a
70         # member of the site at which the node is located.
71         if 'admin' not in self.caller['roles']:
72             if node['site_id'] not in self.caller['site_ids']:
73                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
74
75         # Make requested associations
76         for (k,v) in related.iteritems():
77             node.associate(auth, k,v)
78
79         node.update(native)
80         node.update_last_updated(commit=False)
81         node.sync(commit=True)
82         
83         for (tagname,value) in tags.iteritems():
84             # the tagtype instance is assumed to exist, just check that
85             if not TagTypes(self.api,{'tagname':tagname}):
86                 raise PLCInvalidArgument,"No such TagType %s"%tagname
87             node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
88             if not node_tags:
89                 AddNodeTag(self.api).__call__(auth,node['node_id'],tagname,value)
90             else:
91                 UpdateNodeTag(self.api).__call__(auth,node_tags[0]['node_tag_id'],value)
92
93         # Logging variables
94         self.event_objects = {'Node': [node['node_id']]}
95         if 'hostname' in node:
96             self.message = 'Node %s updated'%node['hostname']
97         else:
98             self.message = 'Node %d updated'%node['node_id']
99         self.message += " [%s]." % (", ".join(node_fields.keys()),)
100         if 'boot_state' in node_fields.keys():
101                 self.message += ' boot_state updated to %s' % node_fields['boot_state']
102
103         return 1