Merge remote-tracking branch 'origin/pycurl' into planetlab-4_0-branch
[plcapi.git] / PLC / SliceInstantiations.py
1 #
2 # Functions for interacting with the slice_instantiations table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id: SliceInstantiations.py 5574 2007-10-25 20:33:17Z thierry $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13
14 class SliceInstantiation(Row):
15     """
16     Representation of a row in the slice_instantiations table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'slice_instantiations'
21     primary_key = 'instantiation'
22     join_tables = ['slices']
23     fields = {
24         'instantiation': Parameter(str, "Slice instantiation state", max = 100),
25         }
26
27     def validate_instantiation(self, instantiation):
28         # Make sure name is not blank
29         if not len(instantiation):
30             raise PLCInvalidArgument, "Slice instantiation state name must be specified"
31         
32         # Make sure slice instantiation does not alredy exist
33         conflicts = SliceInstantiations(self.api, [instantiation])
34         if conflicts:
35             raise PLCInvalidArgument, "Slice instantiation state name already in use"
36
37         return instantiation
38         
39 class SliceInstantiations(Table):
40     """
41     Representation of the slice_instantiations table in the database.
42     """
43
44     def __init__(self, api, instantiations = None):
45         Table.__init__(self, api, SliceInstantiation)
46
47         sql = "SELECT %s FROM slice_instantiations" % \
48               ", ".join(SliceInstantiation.fields)
49         
50         if instantiations:
51             sql += " WHERE instantiation IN (%s)" % ", ".join(map(api.db.quote, instantiations))
52
53         self.selectall(sql)