revised type-checking on taggable classes - previous code would reject any tag
[plcapi.git] / PLC / Methods / AddSlice.py
1 # $Id$
2 import re
3
4 from PLC.Faults import *
5 from PLC.Auth import Auth
6 from PLC.Method import Method
7 from PLC.Parameter import Parameter, Mixed
8 from PLC.Table import Row
9
10 from PLC.Slices import Slice, Slices
11 from PLC.Sites import Site, Sites
12 from PLC.TagTypes import TagTypes
13 from PLC.SliceTags import SliceTags
14 from PLC.Methods.AddSliceTag import AddSliceTag
15 from PLC.Methods.UpdateSliceTag import UpdateSliceTag
16
17 can_update = ['name', 'instantiation', 'url', 'description', 'max_nodes']
18
19 class AddSlice(Method):
20     """
21     Adds a new slice. Any fields specified in slice_fields are used,
22     otherwise defaults are used.
23
24     Valid slice names are lowercase and begin with the login_base
25     (slice prefix) of a valid site, followed by a single
26     underscore. Thereafter, only letters, numbers, or additional
27     underscores may be used.
28
29     PIs may only add slices associated with their own sites (i.e.,
30     slice prefixes must always be the login_base of one of their
31     sites).
32
33     Returns the new slice_id (> 0) if successful, faults otherwise.
34     """
35
36     roles = ['admin', 'pi']
37
38     accepted_fields = Row.accepted_fields(can_update, Slice.fields)
39     accepted_fields.update(Slice.tags)
40
41     accepts = [
42         Auth(),
43         accepted_fields
44         ]
45
46     returns = Parameter(int, 'New slice_id (> 0) if successful')
47
48     def call(self, auth, slice_fields):
49
50         [native,tags,rejected]=Row.split_fields(slice_fields,[Slice.fields,Slice.tags])
51
52         # type checking
53         native = Row.check_fields (native, self.accepted_fields)
54         if rejected:
55             raise PLCInvalidArgument, "Cannot add Slice with column(s) %r"%rejected
56
57         # Authenticated function
58         assert self.caller is not None
59
60         # 1. Lowercase.
61         # 2. Begins with login_base (letters or numbers).
62         # 3. Then single underscore after login_base.
63         # 4. Then letters, numbers, or underscores.
64         name = slice_fields['name']
65         good_name = r'^[a-z0-9]+_[a-zA-Z0-9_]+$'
66         if not name or \
67            not re.match(good_name, name):
68             raise PLCInvalidArgument, "Invalid slice name"
69
70         # Get associated site details
71         login_base = name.split("_")[0]
72         sites = Sites(self.api, [login_base])
73         if not sites:
74             raise PLCInvalidArgument, "Invalid slice prefix %s in %s"%(login_base,name)
75         site = sites[0]
76
77         if 'admin' not in self.caller['roles']:
78             if site['site_id'] not in self.caller['site_ids']:
79                 raise PLCPermissionDenied, "Slice prefix %s must match one of your sites' login_base"%login_base
80
81         if len(site['slice_ids']) >= site['max_slices']:
82             raise PLCInvalidArgument, \
83                 "Site %s has reached (%d) its maximum allowable slice count (%d)"%(site['name'],
84                                                                                    len(site['slice_ids']),
85                                                                                    site['max_slices'])
86         if not site['enabled']:
87             raise PLCInvalidArgument, "Site %s is disabled and can cannot create slices" % (site['name'])
88          
89         slice = Slice(self.api, native)
90         slice['creator_person_id'] = self.caller['person_id']
91         slice['site_id'] = site['site_id']
92         slice.sync()
93
94         for (tagname,value) in tags.iteritems():
95             # the tagtype instance is assumed to exist, just check that
96             if not TagTypes(self.api,{'tagname':tagname}):
97                 raise PLCInvalidArgument,"No such TagType %s"%tagname
98             slice_tags=SliceTags(self.api,{'tagname':tagname,'slice_id':slice['slice_id']})
99             if not slice_tags:
100                 AddSliceTag(self.api).__call__(auth,slice['slice_id'],tagname,value)
101             else:
102                 UpdateSliceTag(self.api).__call__(auth,slice_tags[0]['slice_tag_id'],value)
103
104         self.event_objects = {'Slice': [slice['slice_id']]}
105         self.message = "Slice %d created" % slice['slice_id']
106
107         return slice['slice_id']