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 PasswordAuth
8 from PLC.Sites import Site, Sites
10 class UpdateSlice(Method):
12 Updates the parameters of an existing slice with the values in
15 Users may only update slices of which they are members. PIs may
16 update any of the slices at their sites, or any slices of which
17 they are members. Admins may update any slice.
19 Only PIs and admins may update max_nodes. Slices cannot be renewed
20 (by updating the expires parameter) more than 8 weeks into the
23 Returns 1 if successful, faults otherwise.
26 roles = ['admin', 'pi', 'user']
28 can_update = lambda (field, value): field in \
29 ['instantiation', 'url', 'description', 'max_nodes', 'expires']
30 update_fields = dict(filter(can_update, Slice.fields.items()))
34 Mixed(Slice.fields['slice_id'],
35 Slice.fields['name']),
39 returns = Parameter(int, '1 if successful')
41 def call(self, auth, slice_id_or_name, update_fields):
42 if filter(lambda field: field not in self.update_fields, update_fields):
43 raise PLCInvalidArgument, "Invalid field specified"
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 update_fields and update_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 update_fields['expires'] > max_expires:
72 raise PLCInvalidArgument, "Cannot renew a slice beyond 8 weeks from now"
74 if 'max_nodes' in update_fields and update_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(update_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"