Add node_id to added_fields
[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
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Storage.AlchemyObject import AlchemyObj
11
12 class BootState(AlchemyObj):
13     """
14     Representation of a row in the boot_states table. To use,
15     instantiate with a dict of values.
16     """
17
18     tablename = 'boot_states'
19     fields = {
20         'boot_state': Parameter(str, "Boot state", max = 20, primary_key=True),
21         }
22
23     def validate_boot_state(self, name):
24         # Make sure name is not blank
25         if not len(name):
26             raise PLCInvalidArgument, "Boot state must be specified"
27
28         # Make sure boot state does not alredy exist
29         conflicts = BootStates(self.api, name)
30         if conflicts:
31             raise PLCInvalidArgument, "Boot state name already in use"
32
33         return name
34
35     def sync(self, commit=True, validate=True):
36         AlchemyObj.sync(self, commit, validate)
37         AlchemyObj.insert(self, dict(self))
38
39     def delete(self, commit=True):
40         assert 'boot_state' in self
41         AlchemyObj.delete(self, dict(self))                
42
43 class BootStates(list):
44     """
45     Representation of the boot_states table in the database.
46     """
47
48     def __init__(self, api, filter = None):
49         if not filter:
50             boot_states = BootState().select()
51         elif isinstance(filter, StringTypes) or isinstance(filter, (list, tuple, set)):
52             boot_states = BootState().select(filter={'boot_state': filter})
53         else:
54             raise PLCInvalidArgument, "Wrong boot state filter %r" % filter
55
56         for boot_state in boot_states:
57             self.append(boot_state)