ba4d0eaa541fcc6d2432ef61983d3a23bb377f86
[plcapi.git] / PLC / Methods / AddSliceTag.py
1 # $Id$
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
12
13 class AddSliceTag(Method):
14     """
15     Sets the specified attribute of the slice (or sliver, if
16     node_id_or_hostname is specified) to the specified value.
17
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.
23
24     Returns the new slice_tag_id (> 0) if successful, faults
25     otherwise.
26     """
27
28     roles = ['admin', 'pi', 'user', 'node']
29
30     accepts = [
31         Auth(),
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'],
40               None),
41         Mixed(NodeGroup.fields['nodegroup_id'],
42               NodeGroup.fields['groupname'])
43         ]
44
45     returns = Parameter(int, 'New slice_tag_id (> 0) if successful')
46
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])
49         if not slices:
50             raise PLCInvalidArgument, "No such slice %r"%slice_id_or_name
51         slice = slices[0]
52
53         tag_types = TagTypes(self.api, [tag_type_id_or_name])
54         if not tag_types:
55             raise PLCInvalidArgument, "No such tag type %r"%tag_type_id_or_name
56         tag_type = tag_types[0]
57
58         if not isinstance(self.caller, Node):
59             if ('admin' not in self.caller['roles']):
60                 if self.caller['person_id'] in slice['person_ids']:
61                     pass
62                 elif 'pi' not in self.caller['roles']:
63                     raise PLCPermissionDenied, "Not a member of the specified slice"
64                 elif slice['site_id'] not in self.caller['site_ids']:
65                     raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
66
67                 if tag_type['min_role_id'] is not None and \
68                        min(self.caller['role_ids']) > tag_type['min_role_id']:
69                     raise PLCPermissionDenied, "Not allowed to set the specified slice attribute"
70         else:
71             ### make node's min_role_id == PI min_role_id
72             node_role_id = 20
73             if tag_type['min_role_id'] is not None and node_role_id > tag_type['min_role_id']:
74                 raise PLCPermissionDenied, "Not allowed to set the specified slice attribute"
75             
76         # if initscript is specified, validate value
77         if tag_type['tagname'] in ['initscript']:
78             initscripts = InitScripts(self.api, {'enabled': True, 'name': value})
79             if not initscripts: 
80                 raise PLCInvalidArgument, "No such plc initscript %r"%value
81
82         slice_tag = SliceTag(self.api)
83         slice_tag['slice_id'] = slice['slice_id']
84         slice_tag['tag_type_id'] = tag_type['tag_type_id']
85         slice_tag['value'] = unicode(value)
86
87         # Sliver attribute if node is specified
88         if node_id_or_hostname is not None or isinstance(self.caller, Node):
89             node_id = None
90             if isinstance(self.caller, Node):
91                 node = self.caller
92                 node_id = node['node_id']
93
94             if node_id_or_hostname is not None:
95                 nodes = Nodes(self.api, [node_id_or_hostname])
96                 if not nodes:
97                     raise PLCInvalidArgument, "No such node"
98                 node = nodes[0]
99                 if node_id <> None and node_id <> node['node_id']:
100                     raise PLCPermissionDenied, "Not allowed to set another node's sliver attribute"
101                 else:                    
102                     node_id = node['node_id']
103             
104             system_slice_tags = SliceTags(self.api, {'tagname': 'system', 'value': '1'}).dict('slice_id')
105             system_slice_ids = system_slice_tags.keys()
106             if slice['slice_id'] not in system_slice_ids and node_id not in slice['node_ids']:
107                 raise PLCInvalidArgument, "Node not in the specified slice %s not in %s"%(slice['slice_id'],system_slice_ids)
108             slice_tag['node_id'] = node['node_id']
109
110         # Sliver attribute shared accross nodes if nodegroup is sepcified
111         if nodegroup_id_or_name is not None:
112             if isinstance(self.caller, Node):
113                     raise PLCPermissionDenied, "Not allowed to set nodegroup slice attributes"
114                 
115             nodegroups = NodeGroups(self.api, [nodegroup_id_or_name])
116             if not nodegroups:
117                 raise PLCInvalidArgument, "No such nodegroup %r"%nodegroup_id_or_name
118             nodegroup = nodegroups[0]
119         
120             slice_tag['nodegroup_id'] = nodegroup['nodegroup_id']
121
122         # Check if slice attribute alreay exists
123         slice_tags_check = SliceTags(self.api, {'slice_id': slice['slice_id'], 
124                                                             'tagname': tag_type['tagname'], 
125                                                             'value': value})
126         for slice_tag_check in slice_tags_check:
127             if 'node_id' in slice_tag and slice_tag['node_id'] == slice_tag_check['node_id']:
128                 raise PLCInvalidArgument, "Sliver attribute already exists"
129             if 'nodegroup_id' in slice_tag and slice_tag['nodegroup_id'] == slice_tag_check['nodegroup_id']:
130                 raise PLCInvalidArgument, "Slice attribute already exists for this nodegroup"
131             if node_id_or_hostname is None and nodegroup_id_or_name is None:
132                 raise PLCInvalidArgument, "Slice attribute already exists"
133
134         slice_tag.sync()
135         self.event_objects = {'SliceTag': [slice_tag['slice_tag_id']]}
136
137         return slice_tag['slice_tag_id']