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