- added logging variable 'object_type'
[plcapi.git] / PLC / Methods / UpdateSlice.py
1 import time
2
3 from PLC.Faults import *
4 from PLC.Method import Method
5 from PLC.Parameter import Parameter, Mixed
6 from PLC.Slices import Slice, Slices
7 from PLC.Auth import Auth
8 from PLC.Sites import Site, Sites
9
10 can_update = lambda (field, value): field in \
11              ['instantiation', 'url', 'description', 'max_nodes', 'expires']
12
13 class UpdateSlice(Method):
14     """
15     Updates the parameters of an existing slice with the values in
16     slice_fields.
17
18     Users may only update slices of which they are members. PIs may
19     update any of the slices at their sites, or any slices of which
20     they are members. Admins may update any slice.
21
22     Only PIs and admins may update max_nodes. Slices cannot be renewed
23     (by updating the expires parameter) more than 8 weeks into the
24     future.
25
26     Returns 1 if successful, faults otherwise.
27     """
28
29     roles = ['admin', 'pi', 'user']
30
31     slice_fields = dict(filter(can_update, Slice.fields.items()))
32
33     accepts = [
34         Auth(),
35         Mixed(Slice.fields['slice_id'],
36               Slice.fields['name']),
37         slice_fields
38         ]
39
40     returns = Parameter(int, '1 if successful')
41
42     object_type = 'Slice'
43
44     def call(self, auth, slice_id_or_name, slice_fields):
45         slice_fields = dict(filter(can_update, slice_fields.items()))
46
47         slices = Slices(self.api, [slice_id_or_name])
48         if not slices:
49             raise PLCInvalidArgument, "No such slice"
50         slice = slices[0]
51
52         if slice['peer_id'] is not None:
53             raise PLCInvalidArgument, "Not a local slice"
54
55         if 'admin' not in self.caller['roles']:
56             if self.caller['person_id'] in slice['person_ids']:
57                 pass
58             elif 'pi' not in self.caller['roles']:
59                 raise PLCPermissionDenied, "Not a member of the specified slice"
60             elif slice['site_id'] not in self.caller['site_ids']:
61                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
62
63         # Renewing
64         if 'expires' in slice_fields and slice_fields['expires'] > slice['expires']:
65             sites = Sites(self.api, [slice['site_id']])
66             assert sites
67             site = sites[0]
68
69             if site['max_slices'] < 0:
70                 raise PLCInvalidArgument, "Slice creation and renewal have been disabled for the site"
71
72             # Maximum expiration date is 8 weeks from now
73             # XXX Make this configurable
74             max_expires = time.time() + (8 * 7 * 24 * 60 * 60)
75
76             if 'admin' not in self.caller['roles'] and slice_fields['expires'] > max_expires:
77                 raise PLCInvalidArgument, "Cannot renew a slice beyond 8 weeks from now"
78
79         if 'max_nodes' in slice_fields and slice_fields['max_nodes'] != slice['max_nodes']:
80             if 'admin' not in self.caller['roles'] and \
81                'pi' not in self.caller['roles']:
82                 raise PLCInvalidArgument, "Only admins and PIs may update max_nodes"
83
84         slice.update(slice_fields)
85
86         # XXX Make this a configurable policy
87         if slice['description'] is None or not slice['description'].strip() or \
88            slice['url'] is None or not slice['url'].strip():
89             raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"
90
91         slice.sync()
92         self.object_ids = [slice['slice_id']]
93
94         return 1