prevents side-effect on foreign objects when appropriate
[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     def call(self, auth, slice_id_or_name, slice_fields):
43         slice_fields = dict(filter(can_update, slice_fields.items()))
44
45         slices = Slices(self.api, [slice_id_or_name])
46         if not slices:
47             raise PLCInvalidArgument, "No such slice"
48         slice = slices[0]
49         PLCCheckLocalSlice(slice,"UpdateSlice")
50
51         if 'admin' not in self.caller['roles']:
52             if self.caller['person_id'] in slice['person_ids']:
53                 pass
54             elif 'pi' not in self.caller['roles']:
55                 raise PLCPermissionDenied, "Not a member of the specified slice"
56             elif slice['site_id'] not in self.caller['site_ids']:
57                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
58
59         # Renewing
60         if 'expires' in slice_fields and slice_fields['expires'] > slice['expires']:
61             sites = Sites(self.api, [slice['site_id']])
62             assert sites
63             site = sites[0]
64
65             if site['max_slices'] < 0:
66                 raise PLCInvalidArgument, "Slice creation and renewal have been disabled for the site"
67
68             # Maximum expiration date is 8 weeks from now
69             # XXX Make this configurable
70             max_expires = time.time() + (8 * 7 * 24 * 60 * 60)
71
72             if 'admin' not in self.caller['roles'] and slice_fields['expires'] > max_expires:
73                 raise PLCInvalidArgument, "Cannot renew a slice beyond 8 weeks from now"
74
75         if 'max_nodes' in slice_fields and slice_fields['max_nodes'] != slice['max_nodes']:
76             if 'admin' not in self.caller['roles'] and \
77                'pi' not in self.caller['roles']:
78                 raise PLCInvalidArgument, "Only admins and PIs may update max_nodes"
79
80         slice.update(slice_fields)
81
82         # XXX Make this a configurable policy
83         if slice['description'] is None or not slice['description'].strip() or \
84            slice['url'] is None or not slice['url'].strip():
85             raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"
86
87         slice.sync()
88         self.object_ids = [slice['slice_id']]
89
90         return 1