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