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