assert primary key exists in record before attempting to delete it
authorTony Mack <tmack@paris.CS.Princeton.EDU>
Tue, 2 Oct 2012 03:03:07 +0000 (23:03 -0400)
committerTony Mack <tmack@paris.CS.Princeton.EDU>
Tue, 2 Oct 2012 03:03:07 +0000 (23:03 -0400)
14 files changed:
PLC/AddressTypes.py
PLC/Addresses.py
PLC/Interfaces.py
PLC/Keys.py
PLC/Messages.py
PLC/NetworkMethods.py
PLC/NetworkTypes.py
PLC/NodeGroups.py
PLC/NodeTags.py
PLC/NodeTypes.py
PLC/Nodes.py
PLC/PCUProtocolTypes.py
PLC/PersonTags.py
PLC/Persons.py

index 3e41a92..3443871 100644 (file)
@@ -9,7 +9,7 @@ from types import StringTypes
 from PLC.Faults import *
 from PLC.Parameter import Parameter
 from PLC.Filter import Filter
-from PLC.Storage.AlchemyObj import AlchemyObj
+from PLC.Storage.AlchemyObject import AlchemyObj
 from PLC.Table import Row, Table
 
 class AddressType(AlchemyObj):
@@ -48,6 +48,7 @@ class AddressType(AlchemyObj):
 
 
     def delete(self, commit=True):
+        assert 'address_type_id' in self
         AlchemyObj.delete(self, dict(self))
 
 class AddressTypes(list):
index cb57eca..dead333 100644 (file)
@@ -1,6 +1,6 @@
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Storage.AlchemyObj import AlchemyObj
+from PLC.Storage.AlchemyObject import AlchemyObj
 from PLC.AddressTypes import AddressType, AddressTypes
 
 class Address(AlchemyObj):
@@ -83,6 +83,7 @@ class Address(AlchemyObj):
             AlchemyObj.update(self, {'address_id': self['address_id']}, dict(self))
 
     def delete(self, commit=True):
+        assert 'address_id' in self
         AlchemyObj.delete(self, dict(self))     
             
 class Addresses(list):
index 6de99fe..dcb395b 100644 (file)
@@ -255,6 +255,7 @@ class Interface(AlchemyObj):
 
     def delete(self,commit=True):
         ### need to cleanup ilinks
+        assert 'interface_id' in self
         AlchemyObj.delete(self, dict(self)
 
 
index 35db303..07a05e4 100644 (file)
@@ -15,50 +15,43 @@ class Key(NovaObject):
     with sync().
     """
 
-    table_name = 'keys'
-    primary_key = 'key_id'
+    tablename = 'keys'
     join_tables = ['person_key', 'peer_key']
     fields = {
-        'key_id': Parameter(str, "Key identifier"),
-        'key_type': Parameter(str, "Key type"),
-        'key': Parameter(str, "Key value", max = 4096),
-        'person_id': Parameter(int, "User to which this key belongs", nullok = True),
-        'peer_id': Parameter(int, "Peer to which this key belongs", nullok = True),
-        'peer_key_id': Parameter(int, "Foreign key identifier at peer", nullok = True),
+        'id': Parameter(str, "Key identifier", primary_key=True),
+        #'key_type': Parameter(str, "Key type"),
+        'public_key': Parameter(str, "Key string", max = 4096),
+        'name': Parameter(str, "Key name",)
         }
 
     def sync(self, insert = False, validate = True):
         NovaObject.sync(self, insert, validate)
-        if insert == True or self.key_id not in self:
-            self.object = self.api.client_shell.nova.keypairs.create(self.key_id,
+        if insert == True or 'id' not in self:
+            self.object = self.api.client_shell.nova.keypairs.create(self.id,
                                                                      self.key)
+        else:
+            self.object = self.api.client_shell.nova.keypairs.update(self.id, dict(self))
 
     def delete(self):
-        self.api.client_shell.nova.keypairs.delete(self.key_id)
+        assert 'id' in self
+        self.api.client_shell.nova.keypairs.delete(self.id)
 
-    def validate_key_type(self, key_type):
-        key_types = [row['key_type'] for row in KeyTypes(self.api)]
-        if key_type not in key_types:
-            raise PLCInvalidArgument, "Invalid key type"
-        return key_type
-
-    def validate_key(self, key):
+    def validate_public_key(self, key):
         # Key must not be blacklisted
-        rows = self.api.db.selectall("SELECT 1 from keys" \
-                                     " WHERE key = %(key)s" \
-                                     " AND is_blacklisted IS True",
-                                     locals())
-        if rows:
-            raise PLCInvalidArgument, "Key is blacklisted and cannot be used"
-
+        pass
         return key
 
+    def validate_name(self, name)
+        keys = Keys(self.api, name)
+        if keys:
+            raise PLCInvalidArgument, "Key name alredy in use"
+
     def validate(self):
         # Basic validation
-        Row.validate(self)
+        NovaObject.validate(self)
 
-        assert 'key' in self
-        key = self['key']
+        assert 'public_key' in self
+        key = self['public_key']
 
         if self['key_type'] == 'ssh':
             # Accept only SSH version 2 keys without options. From
index 09f9335..13214aa 100644 (file)
@@ -35,6 +35,7 @@ class Message(AlchemyObj):
             AlchemyObj.update(self, {'message_id': self['message_id']}, dict(self))
 
     def delete(self, commit = True):
+        assert 'message_id' in self
         AlchemyObj.delete(self, dict(self))
 
 class Messages(list):
index 97e2505..f1e21f7 100644 (file)
@@ -36,7 +36,7 @@ class NetworkMethod(AlchemyObj):
         AlchemyObj.insert(self, dict(self))
 
     def delete(self, commit=True):
-        assert method in self
+        assert 'method' in self
         AlchemyObj.delete(self, dict(self))
         
 
index e94b10b..3006df0 100644 (file)
@@ -30,6 +30,14 @@ class NetworkType(AlchemyObj):
 
         return name
 
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit=commit, validate=validate)
+        AlchemyObj.insert(self, dict(self)) 
+
+    def delete(self, commit=True):
+        assert 'type' in self
+        AlchemyObj.delete(self, dict(self))
+
 class NetworkTypes(list):
     """
     Representation of the network_types table in the database.
index 1cccc28..bbe20a8 100644 (file)
@@ -52,6 +52,10 @@ class NodeGroup(Row):
         else:
             AlchemyObj.update(self, {'nodegroup_id': self['nodegroup_id']}, dict(self))
 
+    def delete(self, commit=True):
+        assert 'nodegroup_id' in self
+        AlchemyObj.delete(self, dict(self))
+
 class NodeGroups(list):
     """
     Representation of row(s) from the nodegroups table in the
index 22c7728..e911244 100644 (file)
@@ -3,7 +3,7 @@
 #
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Storage.AlchemyObj import AlchemyObj
+from PLC.Storage.AlchemyObject import AlchemyObj
 from PLC.Nodes import Node, Nodes
 from PLC.TagTypes import TagType, TagTypes
 
@@ -33,6 +33,7 @@ class NodeTag(AlchemyObj):
             AlchemyObj.update(self, {'node_tag_id': self['node_tag_id']}, dict(self))
 
     def delete(self, commit=True):
+        assert 'node_tag_id' in self
         AlchemyObj.delete(self, dict(self))        
 
 class NodeTags(list):
index 15c8cac..fd5e022 100644 (file)
@@ -5,7 +5,7 @@
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Storage.AlchemyObj import AlchemyObj
+from PLC.Storage.AlchemyObject import AlchemyObj
 
 class NodeType(AlchemyObj):
     """
@@ -36,6 +36,7 @@ class NodeType(AlchemyObj):
         AlchemyObj.insert(self, dict(self)
 
     def delete(self, commit=True):
+        assert 'node_type' in self
         AlcemyObj.delete(self, dict(self))  
 
 class NodeTypes(list):
index 80de915..182ef4e 100644 (file)
@@ -263,12 +263,12 @@ class Node(AlchemyObj):
 
 
 
-    def sync(self, insert=False, validate=True):
-        AlchemyObj.sync(self, insert, validate)
-        if insert == True or 'node_id' not in self:
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit=commit, validate=validate)
+        if 'node_id' not in self:
             AlchemyObj.insert(self, dict(self))
         else:
-            AlchemyObj.update(self, dict(self))
+            AlchemyObj.update(self, {'node_id': self['node_id']}, dict(self))
 
     def delete(self, commit = True):
         """
index 9281c46..9bdeae7 100644 (file)
@@ -7,20 +7,18 @@
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Table import Row, Table
-from PLC.Filter import Filter
+from PLC.Storage.AlchemyObject import AlchemyObj
 
-class PCUProtocolType(Row):
+class PCUProtocolType(AlchemyObj):
     """
     Representation of a row in the pcu_protocol_type table. To use,
     instantiate with a dict of values.
     """
 
-    table_name = 'pcu_protocol_type'
-    primary_key = 'pcu_protocol_type_id'
+    tablename = 'pcu_protocol_type'
     join_tables = []
     fields = {
-        'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier"),
+        'pcu_protocol_type_id': Parameter(int, "PCU protocol type identifier", primary_key=True),
         'pcu_type_id': Parameter(int, "PCU type identifier"),
         'port': Parameter(int, "PCU port"),
         'protocol': Parameter(str, "Protocol"),
@@ -42,26 +40,31 @@ class PCUProtocolType(Row):
 
         return protocol
 
-class PCUProtocolTypes(Table):
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit=commit, validate=validate)
+        if 'pcu_protocol_type_id' not in self:
+            AlchemyObj.insert(self, dict(self))
+        else:
+            filter = {'pcu_protocol_type_id', self['pcu_protocol_type_id']}
+            AlchemyObj.update(self, filter, dict(self))
+
+    def delete(self, commit=True):
+        assert 'pcu_protocol_type_id' in self
+        AlchemyObj.delete(self, dict(self))
+
+class PCUProtocolTypes(list):
     """
     Representation of the pcu_protocol_types table in the database.
     """
 
     def __init__(self, api, protocol_type_filter = None, columns = None):
-        Table.__init__(self, api, PCUProtocolType, columns)
-
-        sql = "SELECT %s FROM pcu_protocol_type WHERE True" % \
-              ", ".join(self.columns)
-
-        if protocol_type_filter is not None:
-            if isinstance(protocol_type_filter, (list, tuple, set, int, long)):
-                protocol_type_filter = Filter(PCUProtocolType.fields, {'pcu_protocol_type_id': protocol_type_filter})
-                sql += " AND (%s) %s" % protocol_type_filter.sql(api, "OR")
-            elif isinstance(protocol_type_filter, dict):
-                protocol_type_filter = Filter(PCUProtocolType.fields, protocol_type_filter)
-                sql += " AND (%s) %s" % protocol_type_filter.sql(api, "AND")
-            else:
-                raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter
-
+        if not  protocol_type_filter:
+            protocol_types = PCUProtocolType().select()
+        elif isinstance(protocol_type_filter, (list, tuple, set, int, long)):
+            protocol_types = PCUProtocolType.select(filter={'pcu_protocol_type_id': protocol_type_filter})
+        elif isinstance(protocol_type_filter, dict):
+            protocol_types = PCUProtocolType.select(filter= protocol_type_filter)
+        else:
+            raise PLCInvalidArgument, "Wrong pcu_protocol_type filter %r"%protocol_type_filter
 
-        self.selectall(sql)
+        self.extend(protocol_types)
index 4bd38cf..5581a69 100644 (file)
@@ -3,21 +3,19 @@
 #
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Filter import Filter
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
 from PLC.TagTypes import TagType, TagTypes
 from PLC.Persons import Person
 
-class PersonTag(Row):
+class PersonTag(AlchemyObj):
     """
     Representation of a row in the person_tag.
     To use, instantiate with a dict of values.
     """
 
-    table_name = 'person_tag'
-    primary_key = 'person_tag_id'
+    tablename = 'person_tag'
     fields = {
-        'person_tag_id': Parameter(int, "Person setting identifier"),
+        'person_tag_id': Parameter(int, "Person setting identifier", primary_key=True),
         'person_id': Person.fields['id'],
         'email': Person.fields['email'],
         'tag_type_id': TagType.fields['tag_type_id'],
@@ -29,26 +27,32 @@ class PersonTag(Row):
 
         }
 
-class PersonTags(Table):
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit=Ture, validate=True)
+        if 'person_tag_id' not in self:
+            AlchemyObj.insert(self, dict(self))
+        else:
+            AlchemyObj.update(self, {'person_tag_id': self['person_tag_id']}, dict(self))
+            
+        
+    def delete(self, commit=True):
+        assert 'person_tag_id' in self
+        AlchemyObj.delete(self, dict(self))
+
+class PersonTags(list):
     """
     Representation of row(s) from the person_tag table in the
     database.
     """
 
     def __init__(self, api, person_tag_filter = None, columns = None):
-        Table.__init__(self, api, PersonTag, columns)
-
-        sql = "SELECT %s FROM view_person_tags WHERE True" % \
-              ", ".join(self.columns)
-
-        if person_tag_filter is not None:
-            if isinstance(person_tag_filter, (list, tuple, set, int, long)):
-                person_tag_filter = Filter(PersonTag.fields, {'person_tag_id': person_tag_filter})
-            elif isinstance(person_tag_filter, dict):
-                person_tag_filter = Filter(PersonTag.fields, person_tag_filter)
-            else:
-                raise PLCInvalidArgument, "Wrong person setting filter %r"%person_tag_filter
-            sql += " AND (%s) %s" % person_tag_filter.sql(api)
-
-
-        self.selectall(sql)
+        if not person_tag_filter:
+            person_tags = PersonTag().select()
+        elif isinstance(person_tag_filter, (list, tuple, set, int, long)):
+            person_tags = PersonTag().select(filter={'person_tag_id': person_tag_filter})
+        elif isinstance(person_tag_filter, dict):
+            person_tags = PersonTag().select(filter=person_tag_filter)
+        else:
+            raise PLCInvalidArgument, "Wrong person setting filter %r"%person_tag_filter
+
+        self.extend(person_tags)
index 518f53c..7648730 100644 (file)
@@ -19,7 +19,7 @@ from PLC.Table import Row, Table
 from PLC.Roles import Role, Roles
 from PLC.Keys import Key, Keys
 from PLC.Messages import Message, Messages
-from PLC.Storage.AlchemyObj import AlchemyObj
+from PLC.Storage.AlchemyObject import AlchemyObj
 
 class Person(AlchemyObj):
     """
@@ -28,8 +28,10 @@ class Person(AlchemyObj):
     dict. Commit to the database with sync().
     """
 
+    tablename = 'persons'
+
     fields = {
-        'person_id': Parameter(int, "User identifier"),
+        'person_id': Parameter(int, "User identifier", primary_key=True),
         'keystone_id': Parameter(int, "Keystone User identifier"),
         'first_name': Parameter(str, "Given name", max = 128),
         'last_name': Parameter(str, "Surname", max = 128),
@@ -141,6 +143,7 @@ class Person(AlchemyObj):
         
 
     def delete(self):
+        assert 'person_id' in self
         # delete relationships
         SlicePerson().delete.filter({'person_id': self['person_id']})