added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / BootStates.py
index 2cc0b22..0485aa2 100644 (file)
@@ -1,25 +1,57 @@
 #
-# Functions for interacting with the node_bootstates table in the database
+# Functions for interacting with the boot_states table in the database
 #
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id$
-#
 
+from PLC.Faults import *
 from PLC.Parameter import Parameter
+from PLC.Storage.AlchemyObject import AlchemyObj
 
-class BootStates(list):
+class BootState(AlchemyObj):
     """
-    Representation of the node_bootstates table in the database.
+    Representation of a row in the boot_states table. To use,
+    instantiate with a dict of values.
     """
 
+    tablename = 'boot_states'
     fields = {
-        'boot_state': Parameter(int, "Node boot state"),
+        'boot_state': Parameter(str, "Boot state", max = 20, primary_key=True),
         }
 
-    def __init__(self, api):
-        sql = "SELECT * FROM node_bootstates"
-        
-        for row in api.db.selectall(sql):
-            self.append(row['boot_state'])
+    def validate_boot_state(self, name):
+        # Make sure name is not blank
+        if not len(name):
+            raise PLCInvalidArgument, "Boot state must be specified"
+
+        # Make sure boot state does not alredy exist
+        conflicts = BootStates(self.api, name)
+        if conflicts:
+            raise PLCInvalidArgument, "Boot state name already in use"
+
+        return name
+
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit, validate)
+        AlchemyObj.insert(self, dict(self))
+
+    def delete(self, commit=True):
+        assert 'boot_state' in self
+        AlchemyObj.delete(self, dict(self))                
+
+class BootStates(list):
+    """
+    Representation of the boot_states table in the database.
+    """
+
+    def __init__(self, api, filter = None):
+        if not filter:
+            boot_states = BootState().select()
+        elif isinstance(filter, StringTypes) or isinstance(filter, (list, tuple, set)):
+            boot_states = BootState().select(filter={'boot_state': filter})
+        else:
+            raise PLCInvalidArgument, "Wrong boot state filter %r" % filter
+
+        for boot_state in boot_states:
+            self.append(boot_state)