2 from PLC.Faults import *
3 from PLC.Method import Method
4 from PLC.Parameter import Parameter, Mixed
5 from PLC.TagTypes import TagType, TagTypes
6 from PLC.Slices import Slice, Slices
7 from PLC.Nodes import Node, Nodes
8 from PLC.SliceTags import SliceTag, SliceTags
9 from PLC.NodeGroups import NodeGroup, NodeGroups
10 from PLC.InitScripts import InitScript, InitScripts
11 from PLC.Auth import Auth
13 class AddSliceTag(Method):
15 Sets the specified attribute of the slice (or sliver, if
16 node_id_or_hostname is specified) to the specified value.
18 Attributes may require the caller to have a particular role in
19 order to be set or changed. Users may only set attributes of
20 slices or slivers of which they are members. PIs may only set
21 attributes of slices or slivers at their sites, or of which they
22 are members. Admins may set attributes of any slice or sliver.
24 Returns the new slice_tag_id (> 0) if successful, faults
28 roles = ['admin', 'pi', 'user']
32 Mixed(Slice.fields['slice_id'],
33 Slice.fields['name']),
34 Mixed(SliceTag.fields['tag_type_id'],
35 SliceTag.fields['tagname']),
36 Mixed(SliceTag.fields['value'],
37 InitScript.fields['name']),
38 Mixed(Node.fields['node_id'],
39 Node.fields['hostname'],
41 Mixed(NodeGroup.fields['nodegroup_id'],
42 NodeGroup.fields['groupname'])
45 returns = Parameter(int, 'New slice_tag_id (> 0) if successful')
47 def call(self, auth, slice_id_or_name, tag_type_id_or_name, value, node_id_or_hostname = None, nodegroup_id_or_name = None):
48 slices = Slices(self.api, [slice_id_or_name])
50 raise PLCInvalidArgument, "No such slice %r"%slice_id_or_name
53 tag_types = TagTypes(self.api, [tag_type_id_or_name])
55 raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
56 tag_type = tag_types[0]
58 if 'admin' not in self.caller['roles']:
59 if self.caller['person_id'] in slice['person_ids']:
61 elif 'pi' not in self.caller['roles']:
62 raise PLCPermissionDenied, "Not a member of the specified slice"
63 elif slice['site_id'] not in self.caller['site_ids']:
64 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
66 if tag_type['min_role_id'] is not None and \
67 min(self.caller['role_ids']) > tag_type['min_role_id']:
68 raise PLCPermissionDenied, "Not allowed to set the specified slice attribute"
70 # if initscript is specified, validate value
71 if tag_type['tagname'] in ['initscript']:
72 initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
74 raise PLCInvalidArgument, "No such plc initscript %r"%value
76 slice_tag = SliceTag(self.api)
77 slice_tag['slice_id'] = slice['slice_id']
78 slice_tag['tag_type_id'] = tag_type['tag_type_id']
79 slice_tag['value'] = unicode(value)
81 # Sliver attribute if node is specified
82 if node_id_or_hostname is not None:
83 nodes = Nodes(self.api, [node_id_or_hostname])
85 raise PLCInvalidArgument, "No such node"
88 if node['node_id'] not in slice['node_ids']:
89 raise PLCInvalidArgument, "Node not in the specified slice"
90 slice_tag['node_id'] = node['node_id']
92 # Sliver attribute shared accross nodes if nodegroup is sepcified
93 if nodegroup_id_or_name is not None:
94 nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
96 raise PLCInvalidArgument, "No such nodegroup %r"%nodegroup_id_or_name
97 nodegroup = nodegroups[0]
99 slice_tag['nodegroup_id'] = nodegroup['nodegroup_id']
101 # Check if slice attribute alreay exists
102 slice_tags_check = SliceTags(self.api, {'slice_id': slice['slice_id'],
103 'tagname': tag_type['tagname'],
105 for slice_tag_check in slice_tags_check:
106 if 'node_id' in slice_tag and slice_tag['node_id'] == slice_tag_check['node_id']:
107 raise PLCInvalidArgument, "Sliver attribute already exists"
108 if 'nodegroup_id' in slice_tag and slice_tag['nodegroup_id'] == slice_tag_check['nodegroup_id']:
109 raise PLCInvalidArgument, "Slice attribute already exists for this nodegroup"
110 if node_id_or_hostname is None and nodegroup_id_or_name is None:
111 raise PLCInvalidArgument, "Slice attribute already exists"
114 self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
116 return slice_tag['slice_tag_id']