1 from PLC.Faults import *
2 from PLC.Method import Method
3 from PLC.Parameter import Parameter, Mixed
4 from PLC.SliceAttributeTypes import SliceAttributeType, SliceAttributeTypes
5 from PLC.Slices import Slice, Slices
6 from PLC.Nodes import Node, Nodes
7 from PLC.SliceAttributes import SliceAttribute, SliceAttributes
8 from PLC.NodeGroups import NodeGroup, NodeGroups
9 from PLC.InitScripts import InitScript, InitScripts
10 from PLC.Auth import Auth
12 class AddSliceAttribute(Method):
14 Sets the specified attribute of the slice (or sliver, if
15 node_id_or_hostname is specified) to the specified value.
17 Attributes may require the caller to have a particular role in
18 order to be set or changed. Users may only set attributes of
19 slices or slivers of which they are members. PIs may only set
20 attributes of slices or slivers at their sites, or of which they
21 are members. Admins may set attributes of any slice or sliver.
23 Returns the new slice_attribute_id (> 0) if successful, faults
27 roles = ['admin', 'pi', 'user']
31 Mixed(SliceAttribute.fields['slice_id'],
32 SliceAttribute.fields['name']),
33 Mixed(SliceAttribute.fields['attribute_type_id'],
34 SliceAttribute.fields['name']),
35 Mixed(SliceAttribute.fields['value'],
36 InitScript.fields['initscript_id']),
37 Mixed(Node.fields['node_id'],
38 Node.fields['hostname'],
40 Mixed(NodeGroup.fields['nodegroup_id'],
41 NodeGroup.fields['name'])
44 returns = Parameter(int, 'New slice_attribute_id (> 0) if successful')
46 def call(self, auth, slice_id_or_name, attribute_type_id_or_name, value, node_id_or_hostname = None, nodegroup_id_or_name = None):
47 slices = Slices(self.api, [slice_id_or_name])
49 raise PLCInvalidArgument, "No such slice"
52 attribute_types = SliceAttributeTypes(self.api, [attribute_type_id_or_name])
53 if not attribute_types:
54 raise PLCInvalidArgument, "No such slice attribute type"
55 attribute_type = attribute_types[0]
57 if 'admin' not in self.caller['roles']:
58 if self.caller['person_id'] in slice['person_ids']:
60 elif 'pi' not in self.caller['roles']:
61 raise PLCPermissionDenied, "Not a member of the specified slice"
62 elif slice['site_id'] not in self.caller['site_ids']:
63 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
65 if attribute_type['min_role_id'] is not None and \
66 min(self.caller['role_ids']) > attribute_type['min_role_id']:
67 raise PLCPermissionDenied, "Not allowed to set the specified slice attribute"
69 # if initscript is specified, validate value
70 if attribute_type['name'] in ['plc_initscript_id']:
71 initscripts = InitScripts(self.api, {'enabled': True, 'initscript_id': int(value)})
73 raise PLCInvalidArgument, "No such plc initscript"
75 slice_attribute = SliceAttribute(self.api)
76 slice_attribute['slice_id'] = slice['slice_id']
77 slice_attribute['attribute_type_id'] = attribute_type['attribute_type_id']
78 slice_attribute['value'] = value
80 # Sliver attribute if node is specified
81 if node_id_or_hostname is not None:
82 nodes = Nodes(self.api, [node_id_or_hostname])
84 raise PLCInvalidArgument, "No such node"
87 if node['node_id'] not in slice['node_ids']:
88 raise PLCInvalidArgument, "Node not in the specified slice"
90 slice_attribute['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"
97 nodegroup = nodegroups[0]
99 slice_attribute['nodegroup_id'] = nodegroup['nodegroup_id']
101 slice_attribute.sync()
102 self.event_objects = {'SliceAttribute': [slice_attribute['slice_attribute_id']]}
104 return slice_attribute['slice_attribute_id']