from PLC.Method import Method
from PLC.Parameter import Parameter, Mixed
from PLC.Slices import Slice, Slices
-from PLC.Auth import PasswordAuth
+from PLC.Auth import Auth
from PLC.Sites import Site, Sites
+related_fields = Slice.related_fields.keys()
+can_update = lambda (field, value): field in \
+ ['instantiation', 'url', 'description', 'max_nodes', 'expires'] + \
+ related_fields
+
+
class UpdateSlice(Method):
"""
Updates the parameters of an existing slice with the values in
- update_fields.
+ slice_fields.
Users may only update slices of which they are members. PIs may
- update any of the slices at their slices. Admins may update any
- slice.
+ update any of the slices at their sites, or any slices of which
+ they are members. Admins may update any slice.
Only PIs and admins may update max_nodes. Slices cannot be renewed
(by updating the expires parameter) more than 8 weeks into the
roles = ['admin', 'pi', 'user']
- can_update = lambda (field, value): field in \
- ['instantiation', 'url', 'description', 'max_nodes', 'expires']
- update_fields = dict(filter(can_update, Slice.fields.items()))
+ slice_fields = dict(filter(can_update, Slice.fields.items() + Slice.related_fields.items()))
accepts = [
- PasswordAuth(),
+ Auth(),
Mixed(Slice.fields['slice_id'],
Slice.fields['name']),
- update_fields
+ slice_fields
]
returns = Parameter(int, '1 if successful')
- def call(self, auth, slice_id_or_name, update_fields):
- if filter(lambda field: field not in self.update_fields, update_fields):
- raise PLCInvalidArgument, "Invalid field specified"
-
- slices = Slices(self.api, [slice_id_or_name]).values()
+ def call(self, auth, slice_id_or_name, slice_fields):
+ slice_fields = dict(filter(can_update, slice_fields.items()))
+
+ slices = Slices(self.api, [slice_id_or_name])
if not slices:
raise PLCInvalidArgument, "No such slice"
slice = slices[0]
+ if slice['peer_id'] is not None:
+ raise PLCInvalidArgument, "Not a local slice"
+
if 'admin' not in self.caller['roles']:
if self.caller['person_id'] in slice['person_ids']:
pass
raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
# Renewing
- if 'expires' in update_fields and update_fields['expires'] > slice['expires']:
- sites = Sites(self.api, [slice['site_id']]).values()
+ if 'expires' in slice_fields and slice_fields['expires'] > slice['expires']:
+ sites = Sites(self.api, [slice['site_id']])
assert sites
site = sites[0]
# XXX Make this configurable
max_expires = time.time() + (8 * 7 * 24 * 60 * 60)
- if 'admin' not in self.caller['roles'] and update_fields['expires'] > max_expires:
+ if 'admin' not in self.caller['roles'] and slice_fields['expires'] > max_expires:
raise PLCInvalidArgument, "Cannot renew a slice beyond 8 weeks from now"
- if 'max_nodes' in update_fields and update_fields['max_nodes'] != slice['max_nodes']:
+ # XXX Make this a configurable policy
+ if slice['description'] is None or not slice['description'].strip():
+ if 'description' not in slice_fields or slice_fields['description'] is None or \
+ not slice_fields['description'].strip():
+ raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"
+
+ if slice['url'] is None or not slice['url'].strip():
+ if 'url' not in slice_fields or slice_fields['url'] is None or \
+ not slice_fields['url'].strip():
+ raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"
+
+ if 'max_nodes' in slice_fields and slice_fields['max_nodes'] != slice['max_nodes']:
if 'admin' not in self.caller['roles'] and \
'pi' not in self.caller['roles']:
raise PLCInvalidArgument, "Only admins and PIs may update max_nodes"
- slice.update(update_fields)
-
- # XXX Make this a configurable policy
- if slice['description'] is None or not slice['description'].strip() or \
- slice['url'] is None or not slice['url'].strip():
- raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"
+ # Make requested associations
+ for field in related_fields:
+ if field in slice_fields:
+ slice.associate(auth, field, slice_fields[field])
+ slice_fields.pop(field)
+ slice.update(slice_fields)
slice.sync()
+ self.event_objects = {'Slice': [slice['slice_id']]}
+
return 1