add boot_states interface
authorMark Huang <mlhuang@cs.princeton.edu>
Tue, 10 Oct 2006 21:54:20 +0000 (21:54 +0000)
committerMark Huang <mlhuang@cs.princeton.edu>
Tue, 10 Oct 2006 21:54:20 +0000 (21:54 +0000)
PLC/BootStates.py
PLC/Methods/AddBootState.py [new file with mode: 0644]
PLC/Methods/DeleteBootState.py [new file with mode: 0644]
PLC/Methods/GetBootStates.py [new file with mode: 0644]

index cabf4fc..fc9a578 100644 (file)
@@ -1,19 +1,73 @@
 #
-# 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: BootStates.py,v 1.2 2006/09/25 14:48:07 mlhuang Exp $
+# $Id: BootStates.py,v 1.1 2006/10/10 20:24:06 mlhuang Exp $
 #
 
-class BootStates(list):
+from PLC.Faults import *
+from PLC.Parameter import Parameter
+from PLC.Table import Row, Table
+
+class BootState(Row):
+    """
+    Representation of a row in the boot_states table. To use,
+    instantiate with a dict of values.
+    """
+
+    table_name = 'boot_states'
+    primary_key = 'boot_state'
+    fields = {
+        'boot_state': Parameter(str, "Boot state", max = 20),
+        }
+
+    def __init__(self, api, fields = {}):
+        Row.__init__(self, fields)
+        self.api = api
+
+    def validate_boot_state(self, name):
+       # Remove leading and trailing spaces
+       name = name.strip()
+
+       # Make sure name is not blank after we removed the spaces
+        if not 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 delete(self, commit = True):
+        assert 'boot_state' in self
+
+        # Clean up miscellaneous join tables
+        for table in ['nodes', 'boot_states']:
+            self.api.db.do("DELETE FROM " + table + \
+                           " WHERE boot_state = %(boot_state)s",
+                           self)
+
+        if commit:
+            self.api.db.commit()
+        
+class BootStates(Table):
     """
-    Representation of the node_bootstates table in the database.
+    Representation of the boot_states table in the database.
     """
 
-    def __init__(self, api):
-        sql = "SELECT * FROM boot_states"
+    def __init__(self, api, names = None):
+        sql = "SELECT %s FROM boot_states" % \
+              ", ".join(BootState.fields)
         
-        for row in api.db.selectall(sql):
-            self.append(row['boot_state'])
+        if names:
+            # Separate the list into integers and strings
+            sql += " WHERE boot_state IN (%s)" % ", ".join(api.db.quote(names))
+
+        rows = api.db.selectall(sql)
+
+        for row in rows:
+            self[row['boot_state']] = BootState(api, row)
diff --git a/PLC/Methods/AddBootState.py b/PLC/Methods/AddBootState.py
new file mode 100644 (file)
index 0000000..2ecd90b
--- /dev/null
@@ -0,0 +1,28 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.BootStates import BootState, BootStates
+from PLC.Auth import PasswordAuth
+
+class AddBootState(Method):
+    """
+    Adds a new node boot state.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        PasswordAuth(),
+        BootState.fields['boot_state']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, name):
+        boot_state = BootState(self.api)
+        boot_state['boot_state'] = name
+        boot_state.sync()
+
+        return 1
diff --git a/PLC/Methods/DeleteBootState.py b/PLC/Methods/DeleteBootState.py
new file mode 100644 (file)
index 0000000..bf4175d
--- /dev/null
@@ -0,0 +1,34 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.BootStates import BootState, BootStates
+from PLC.Auth import PasswordAuth
+
+class DeleteBootState(Method):
+    """
+    Deletes a node boot state.
+
+    WARNING: This will cause the deletion of all nodes in this boot
+    state.
+
+    Returns 1 if successful, faults otherwise.
+    """
+
+    roles = ['admin']
+
+    accepts = [
+        PasswordAuth(),
+        BootState.fields['boot_state']
+        ]
+
+    returns = Parameter(int, '1 if successful')
+
+    def call(self, auth, name):
+        boot_states = BootStates(self.api, [name])
+        if not boot_states:
+            raise PLCInvalidArgument, "No such boot state"
+        boot_state = boot_states.values()[0]
+
+        boot_state.delete()
+
+        return 1
diff --git a/PLC/Methods/GetBootStates.py b/PLC/Methods/GetBootStates.py
new file mode 100644 (file)
index 0000000..fd7bdd2
--- /dev/null
@@ -0,0 +1,21 @@
+from PLC.Faults import *
+from PLC.Method import Method
+from PLC.Parameter import Parameter, Mixed
+from PLC.BootStates import BootState, BootStates
+from PLC.Auth import PasswordAuth
+
+class GetBootStates(Method):
+    """
+    Returns a list of all valid node boot states.
+    """
+
+    roles = ['admin', 'pi', 'user', 'tech']
+
+    accepts = [
+        PasswordAuth()
+        ]
+
+    returns = [BootState.fields['boot_state']]
+
+    def call(self, auth):
+        return [boot_state['boot_state'] for boot_state in BootStates(self.api).values()]