added create_network(), delete_network(), create_subnet(), delete_subnet(), process_t...
[plcapi.git] / PLC / ConfFiles.py
index 78853df..4944cdc 100644 (file)
@@ -4,27 +4,23 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: ConfFiles.py,v 1.9 2006/11/09 03:07:42 mlhuang Exp $
-#
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Filter import Filter
-from PLC.Table import Row, Table
 from PLC.Nodes import Node, Nodes
 from PLC.NodeGroups import NodeGroup, NodeGroups
+from PLC.Storage.AlchemyObject import AlchemyObj
 
-class ConfFile(Row):
+class ConfFile(AlchemyObj):
     """
     Representation of a row in the conf_files table. To use,
     instantiate with a dict of values.
     """
 
-    table_name = 'conf_files'
-    primary_key = 'conf_file_id'
+    tablename = 'conf_files'
     join_tables = ['conf_file_node', 'conf_file_nodegroup']
     fields = {
-        'conf_file_id': Parameter(int, "Configuration file identifier"),
+        'conf_file_id': Parameter(int, "Configuration file identifier", primary_key=True),
         'enabled': Parameter(bool, "Configuration file is active"),
         'source': Parameter(str, "Relative path on the boot server where file can be downloaded", max = 255),
         'dest': Parameter(str, "Absolute path where file should be installed", max = 255),
@@ -36,8 +32,8 @@ class ConfFile(Row):
         'error_cmd': Parameter(str, "Shell command to execute if any error occurs", max = 1024, nullok = True),
         'ignore_cmd_errors': Parameter(bool, "Install file anyway even if an error occurs"),
         'always_update': Parameter(bool, "Always attempt to install file even if unchanged"),
-        'node_ids': Parameter(int, "List of nodes linked to this file"),
-        'nodegroup_ids': Parameter(int, "List of node groups linked to this file"),
+        'node_ids': Parameter(int, "List of nodes linked to this file", joined=True),
+        'nodegroup_ids': Parameter(int, "List of node groups linked to this file", joined=True),
         }
 
     def add_node(self, node, commit = True):
@@ -134,22 +130,29 @@ class ConfFile(Row):
             self['nodegroup_ids'].remove(nodegroup_id)
             nodegroup['conf_file_ids'].remove(conf_file_id)
 
-class ConfFiles(Table):
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit, validate)
+        if 'conf_file_id' not in self:
+            AlchemyObj.insert(self, dict(self))
+        else:
+            AlchemyObj.update(self, {'conf_file_id': self['conf_file_id']}, dict(self))
+
+    def delete(self, commit=True):
+        assert 'conf_file_id' in self
+        AlchemyObj.delete(self, dict(self))
+
+class ConfFiles(list):
     """
     Representation of the conf_files table in the database.
     """
 
     def __init__(self, api, conf_file_filter = None, columns = None):
-       Table.__init__(self, api, ConfFile, columns)
-
-        sql = "SELECT %s FROM view_conf_files WHERE True" % \
-              ", ".join(self.columns)
-
-        if conf_file_filter is not None:
-            if isinstance(conf_file_filter, (list, tuple, set)):
-                conf_file_filter = Filter(ConfFile.fields, {'conf_file_id': conf_file_filter})
-            elif isinstance(conf_file_filter, dict):
-                conf_file_filter = Filter(ConfFile.fields, conf_file_filter)
-            sql += " AND (%s)" % conf_file_filter.sql(api)
-
-        self.selectall(sql)
+        if not conf_file_filter:
+            conf_files = ConfFile().select()
+        elif isinstance(conf_file_filter, (list, tuple, set, int, long)):
+            conf_files = ConfFile().select(filter={'conf_file_id': conf_file_filter})
+        elif isinstance(conf_file_filter, dict):
+            conf_files = ConfFile().select(filter=conf_file_filter)
+
+        for conf_file in conf_files:
+            self.append(conf_file)