- make Add() calling convention consistent among all functions that
[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 PasswordAuth
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     for field in slice_fields.values():
33         field.optional = True
34
35     accepts = [
36         PasswordAuth(),
37         Mixed(Slice.fields['slice_id'],
38               Slice.fields['name']),
39         slice_fields
40         ]
41
42     returns = Parameter(int, '1 if successful')
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]).values()
48         if not slices:
49             raise PLCInvalidArgument, "No such slice"
50         slice = slices[0]
51
52         if 'admin' not in self.caller['roles']:
53             if self.caller['person_id'] in slice['person_ids']:
54                 pass
55             elif 'pi' not in self.caller['roles']:
56                 raise PLCPermissionDenied, "Not a member of the specified slice"
57             elif slice['site_id'] not in self.caller['site_ids']:
58                 raise PLCPermissionDenied, "Specified slice not associated with any of your sites"
59
60         # Renewing
61         if 'expires' in slice_fields and slice_fields['expires'] > slice['expires']:
62             sites = Sites(self.api, [slice['site_id']]).values()
63             assert sites
64             site = sites[0]
65
66             if site['max_slices'] < 0:
67                 raise PLCInvalidArgument, "Slice creation and renewal have been disabled for the site"
68
69             # Maximum expiration date is 8 weeks from now
70             # XXX Make this configurable
71             max_expires = time.time() + (8 * 7 * 24 * 60 * 60)
72
73             if 'admin' not in self.caller['roles'] and slice_fields['expires'] > max_expires:
74                 raise PLCInvalidArgument, "Cannot renew a slice beyond 8 weeks from now"
75
76         if 'max_nodes' in slice_fields and slice_fields['max_nodes'] != slice['max_nodes']:
77             if 'admin' not in self.caller['roles'] and \
78                'pi' not in self.caller['roles']:
79                 raise PLCInvalidArgument, "Only admins and PIs may update max_nodes"
80
81         slice.update(slice_fields)
82
83         # XXX Make this a configurable policy
84         if slice['description'] is None or not slice['description'].strip() or \
85            slice['url'] is None or not slice['url'].strip():
86             raise PLCInvalidArgument, "Cannot renew a slice with an empty description or URL"
87
88         slice.sync()
89
90         return 1