fc9a578ff3fc3d225e637022234f6700c4482662
[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: BootStates.py,v 1.1 2006/10/10 20:24:06 mlhuang Exp $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13
14 class BootState(Row):
15     """
16     Representation of a row in the boot_states table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'boot_states'
21     primary_key = 'boot_state'
22     fields = {
23         'boot_state': Parameter(str, "Boot state", max = 20),
24         }
25
26     def __init__(self, api, fields = {}):
27         Row.__init__(self, fields)
28         self.api = api
29
30     def validate_boot_state(self, name):
31         # Remove leading and trailing spaces
32         name = name.strip()
33
34         # Make sure name is not blank after we removed the spaces
35         if not name:
36             raise PLCInvalidArgument, "Boot state must be specified"
37         
38         # Make sure boot state does not alredy exist
39         conflicts = BootStates(self.api, [name])
40         if conflicts:
41             raise PLCInvalidArgument, "Boot state name already in use"
42
43         return name
44
45     def delete(self, commit = True):
46         assert 'boot_state' in self
47
48         # Clean up miscellaneous join tables
49         for table in ['nodes', 'boot_states']:
50             self.api.db.do("DELETE FROM " + table + \
51                            " WHERE boot_state = %(boot_state)s",
52                            self)
53
54         if commit:
55             self.api.db.commit()
56         
57 class BootStates(Table):
58     """
59     Representation of the boot_states table in the database.
60     """
61
62     def __init__(self, api, names = None):
63         sql = "SELECT %s FROM boot_states" % \
64               ", ".join(BootState.fields)
65         
66         if names:
67             # Separate the list into integers and strings
68             sql += " WHERE boot_state IN (%s)" % ", ".join(api.db.quote(names))
69
70         rows = api.db.selectall(sql)
71
72         for row in rows:
73             self[row['boot_state']] = BootState(api, row)