bug fixes
[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
33     accepts = [
34         Auth(),
35         Mixed(Node.fields['node_id'],
36               Node.fields['hostname']),
37         accepted_fields
38         ]
39
40     returns = Parameter(int, '1 if successful')
41
42     def call(self, auth, node_id_or_hostname, node_fields):
43
44         # Authenticated function
45         assert self.caller is not None
46
47         # Remove admin only fields
48         if 'admin' not in self.caller['roles']:
49             for key in admin_only:
50                 if node_fields.has_key(key):
51                     del node_fields[key]
52
53         # Get account information
54         nodes = Nodes(self.api, [node_id_or_hostname])
55         if not nodes:
56             raise PLCInvalidArgument, "No such node %r"%node_id_or_hostname
57         node = nodes[0]
58
59         # If we are not an admin, make sure that the caller is a
60         # member of the site at which the node is located.
61         if 'admin' not in self.caller['roles']:
62             if node['site_id'] not in self.caller['site_ids']:
63                 raise PLCPermissionDenied, "Not allowed to delete nodes from specified site"
64
65         node.update(node_fields)
66         node.sync(commit=True)
67
68         # if hostname was modifed make sure to update the hrn
69         # tag
70         if 'hostname' in native:
71             root_auth = self.api.config.PLC_HRN_ROOT
72             # sub auth is the login base of this node's site
73             sites = Sites(self.api, node['site_id'], ['login_base'])
74             site = sites[0]
75             login_base = site['login_base']
76             tags['hrn'] = hostname_to_hrn(root_auth, login_base, node['hostname'])
77
78         for (tagname,value) in tags.iteritems():
79             # the tagtype instance is assumed to exist, just check that
80             tag_types = TagTypes(self.api,{'tagname':tagname})
81             if not tag_types:
82                 raise PLCInvalidArgument,"No such TagType %s"%tagname
83             tag_type = tag_types[0]
84             node_tags=NodeTags(self.api,{'tagname':tagname,'node_id':node['node_id']})
85             if not node_tags:
86                 node_tag = NodeTag(self.api)
87                 node_tag['node_id'] = node['node_id']
88                 node_tag['tag_type_id'] = tag_type['tag_type_id']
89                 node_tag['tagname']  = tagname
90                 node_tag['value'] = value
91                 node_tag.sync()
92             else:
93                 node_tag = node_tags[0]
94                 node_tag['value'] = value
95                 node_tag.sync()
96         return 1