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