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