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