2 # Thierry Parmentelat - INRIA
4 from PLC.Faults import *
5 from PLC.Method import Method
6 from PLC.Parameter import Parameter, Mixed
7 from PLC.Auth import Auth
9 from PLC.TagTypes import TagType, TagTypes
10 from PLC.Slices import Slice, Slices
11 from PLC.Nodes import Node, Nodes
12 from PLC.SliceTags import SliceTag, SliceTags
13 from PLC.NodeGroups import NodeGroup, NodeGroups
14 from PLC.InitScripts import InitScript, InitScripts
16 from PLC.AuthorizeHelpers import AuthorizeHelpers
18 class AddSliceTag(Method):
20 Sets the specified attribute of the slice (or sliver, if
21 node_id_or_hostname is specified) to the specified value.
23 Attributes may require the caller to have a particular role in
24 order to be set or changed. Users may only set attributes of
25 slices or slivers of which they are members. PIs may only set
26 attributes of slices or slivers at their sites, or of which they
27 are members. Admins may set attributes of any slice or sliver.
29 Returns the new slice_tag_id (> 0) if successful, faults
33 roles = ['admin', 'pi', 'user', 'node']
37 Mixed(Slice.fields['slice_id'],
38 Slice.fields['name']),
39 Mixed(SliceTag.fields['tag_type_id'],
40 SliceTag.fields['tagname']),
41 Mixed(SliceTag.fields['value'],
42 InitScript.fields['name']),
43 Mixed(Node.fields['node_id'],
44 Node.fields['hostname'],
46 Mixed(NodeGroup.fields['nodegroup_id'],
47 NodeGroup.fields['groupname'])
50 returns = Parameter(int, 'New slice_tag_id (> 0) if successful')
52 def call(self, auth, slice_id_or_name, tag_type_id_or_name, value, node_id_or_hostname = None, nodegroup_id_or_name = None):
53 slices = Slices(self.api, [slice_id_or_name])
55 raise PLCInvalidArgument, "No such slice %r"%slice_id_or_name
58 tag_types = TagTypes(self.api, [tag_type_id_or_name])
60 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
61 tag_type = tag_types[0]
63 if not isinstance(self.caller, Node):
64 if ('admin' not in self.caller['roles']):
65 if self.caller['person_id'] in slice['person_ids']:
67 elif 'pi' not in self.caller['roles']:
68 raise PLCPermissionDenied, "Not a member of the specified slice"
69 elif slice['site_id'] not in self.caller['site_ids']:
70 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
72 if tag_type['min_role_id'] is not None and \
73 min(self.caller['role_ids']) > tag_type['min_role_id']:
74 raise PLCPermissionDenied, "Not allowed to set the specified slice attribute"
76 ### make node's min_role_id == PI min_role_id
78 if tag_type['min_role_id'] is not None and node_role_id > tag_type['min_role_id']:
79 raise PLCPermissionDenied, "Not allowed to set the specified slice attribute"
81 # if initscript is specified, validate value
82 if tag_type['tagname'] in ['initscript']:
83 initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
85 raise PLCInvalidArgument, "No such plc initscript %r"%value
87 slice_tag = SliceTag(self.api)
88 slice_tag['slice_id'] = slice['slice_id']
89 slice_tag['tag_type_id'] = tag_type['tag_type_id']
90 slice_tag['value'] = unicode(value)
92 # Sliver attribute if node is specified
93 if node_id_or_hostname is not None or isinstance(self.caller, Node):
95 if isinstance(self.caller, Node):
97 node_id = node['node_id']
99 if node_id_or_hostname is not None:
100 nodes = Nodes(self.api, [node_id_or_hostname])
102 raise PLCInvalidArgument, "No such node"
104 if node_id <> None and node_id <> node['node_id']:
105 raise PLCPermissionDenied, "Not allowed to set another node's sliver attribute"
107 node_id = node['node_id']
109 system_slice_tags = SliceTags(self.api, {'tagname': 'system', 'value': '1'}).dict('slice_id')
110 system_slice_ids = system_slice_tags.keys()
111 if slice['slice_id'] not in system_slice_ids and node_id not in slice['node_ids']:
112 raise PLCInvalidArgument, "AddSliceTag: slice %s not on specified node %s nor is it a system slice (%r)"%(slice['name'],node['hostname'],system_slice_ids)
113 slice_tag['node_id'] = node['node_id']
115 # Sliver attribute shared accross nodes if nodegroup is sepcified
116 if nodegroup_id_or_name is not None:
117 if isinstance(self.caller, Node):
118 raise PLCPermissionDenied, "Not allowed to set nodegroup slice attributes"
120 nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
122 raise PLCInvalidArgument, "No such nodegroup %r"%nodegroup_id_or_name
123 nodegroup = nodegroups[0]
125 slice_tag['nodegroup_id'] = nodegroup['nodegroup_id']
127 # Check if slice attribute alreay exists
128 slice_tags_check = SliceTags(self.api, {'slice_id': slice['slice_id'],
129 'tagname': tag_type['tagname'],
131 for slice_tag_check in slice_tags_check:
132 # do not compare between slice tag and sliver tag
133 if 'node_id' not in slice_tag and slice_tag_check['node_id'] is not None:
135 # do not compare between sliver tag and slice tag
136 if 'node_id' in slice_tag and slice_tag['node_id'] is not None and slice_tag_check['node_id'] is None:
138 if 'node_id' in slice_tag and slice_tag['node_id'] == slice_tag_check['node_id']:
139 raise PLCInvalidArgument, "Sliver attribute already exists"
140 if 'nodegroup_id' in slice_tag and slice_tag['nodegroup_id'] == slice_tag_check['nodegroup_id']:
141 raise PLCInvalidArgument, "Slice attribute already exists for this nodegroup"
142 if node_id_or_hostname is None and nodegroup_id_or_name is None:
143 raise PLCInvalidArgument, "Slice attribute already exists"
146 self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
148 return slice_tag['slice_tag_id']