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
10 can_update = lambda (field, value): field in \
11 ['instantiation', 'url', 'description', 'max_nodes', 'expires']
13 class UpdateSlice(Method):
15 Updates the parameters of an existing slice with the values in
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.
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
26 Returns 1 if successful, faults otherwise.
29 roles = ['admin', 'pi', 'user']
31 slice_fields = dict(filter(can_update, Slice.fields.items()))
35 Mixed(Slice.fields['slice_id'],
36 Slice.fields['name']),
40 returns = Parameter(int, '1 if successful')
42 def call(self, auth, slice_id_or_name, slice_fields):
43 slice_fields = dict(filter(can_update, slice_fields.items()))
45 slices = Slices(self.api, [slice_id_or_name]).values()
47 raise PLCInvalidArgument, "No such slice"
50 if 'admin' not in self.caller['roles']:
51 if self.caller['person_id'] in slice['person_ids']:
53 elif 'pi' not in self.caller['roles']:
54 raise PLCPermissionDenied, "Not a member of the specified slice"
55 elif slice['site_id'] not in self.caller['site_ids']:
56 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
59 if 'expires' in slice_fields and slice_fields['expires'] > slice['expires']:
60 sites = Sites(self.api, [slice['site_id']]).values()
64 if site['max_slices'] < 0:
65 raise PLCInvalidArgument, "Slice creation and renewal have been disabled for the site"
67 # Maximum expiration date is 8 weeks from now
68 # XXX Make this configurable
69 max_expires = time.time() + (8 * 7 * 24 * 60 * 60)
71 if 'admin' not in self.caller['roles'] and slice_fields['expires'] > max_expires:
72 raise PLCInvalidArgument, "Cannot renew a slice beyond 8 weeks from now"
74 if 'max_nodes' in slice_fields and slice_fields['max_nodes'] != slice['max_nodes']:
75 if 'admin' not in self.caller['roles'] and \
76 'pi' not in self.caller['roles']:
77 raise PLCInvalidArgument, "Only admins and PIs may update max_nodes"
79 slice.update(slice_fields)
81 # XXX Make this a configurable policy
82 if slice['description'] is None or not slice['description'].strip() or \
83 slice['url'] is None or not slice['url'].strip():
84 raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"