From f1f66aa544638f31d63561a4a9e15eb45bd0c319 Mon Sep 17 00:00:00 2001 From: Tony Mack Date: Mon, 1 Oct 2012 23:03:07 -0400 Subject: [PATCH] assert primary key exists in record before attempting to delete it --- PLC/AddressTypes.py | 3 ++- PLC/Addresses.py | 3 ++- PLC/Interfaces.py | 1 + PLC/Keys.py | 49 +++++++++++++++++----------------------- PLC/Messages.py | 1 + PLC/NetworkMethods.py | 2 +- PLC/NetworkTypes.py | 8 +++++++ PLC/NodeGroups.py | 4 ++++ PLC/NodeTags.py | 3 ++- PLC/NodeTypes.py | 3 ++- PLC/Nodes.py | 8 +++---- PLC/PCUProtocolTypes.py | 49 +++++++++++++++++++++------------------- PLC/PersonTags.py | 50 ++++++++++++++++++++++------------------- PLC/Persons.py | 7 ++++-- 14 files changed, 106 insertions(+), 85 deletions(-) diff --git a/PLC/AddressTypes.py b/PLC/AddressTypes.py index 3e41a92c..34438716 100644 --- a/PLC/AddressTypes.py +++ b/PLC/AddressTypes.py @@ -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): diff --git a/PLC/Addresses.py b/PLC/Addresses.py index cb57eca4..dead3338 100644 --- a/PLC/Addresses.py +++ b/PLC/Addresses.py @@ -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): diff --git a/PLC/Interfaces.py b/PLC/Interfaces.py index 6de99fe3..dcb395bf 100644 --- a/PLC/Interfaces.py +++ b/PLC/Interfaces.py @@ -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) diff --git a/PLC/Keys.py b/PLC/Keys.py index 35db3034..07a05e4f 100644 --- a/PLC/Keys.py +++ b/PLC/Keys.py @@ -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 diff --git a/PLC/Messages.py b/PLC/Messages.py index 09f93356..13214aad 100644 --- a/PLC/Messages.py +++ b/PLC/Messages.py @@ -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): diff --git a/PLC/NetworkMethods.py b/PLC/NetworkMethods.py index 97e2505f..f1e21f76 100644 --- a/PLC/NetworkMethods.py +++ b/PLC/NetworkMethods.py @@ -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)) diff --git a/PLC/NetworkTypes.py b/PLC/NetworkTypes.py index e94b10b1..3006df0e 100644 --- a/PLC/NetworkTypes.py +++ b/PLC/NetworkTypes.py @@ -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. diff --git a/PLC/NodeGroups.py b/PLC/NodeGroups.py index 1cccc286..bbe20a8a 100644 --- a/PLC/NodeGroups.py +++ b/PLC/NodeGroups.py @@ -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 diff --git a/PLC/NodeTags.py b/PLC/NodeTags.py index 22c77281..e9112449 100644 --- a/PLC/NodeTags.py +++ b/PLC/NodeTags.py @@ -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): diff --git a/PLC/NodeTypes.py b/PLC/NodeTypes.py index 15c8cac1..fd5e022f 100644 --- a/PLC/NodeTypes.py +++ b/PLC/NodeTypes.py @@ -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): diff --git a/PLC/Nodes.py b/PLC/Nodes.py index 80de9154..182ef4e6 100644 --- a/PLC/Nodes.py +++ b/PLC/Nodes.py @@ -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): """ diff --git a/PLC/PCUProtocolTypes.py b/PLC/PCUProtocolTypes.py index 9281c46a..9bdeae7f 100644 --- a/PLC/PCUProtocolTypes.py +++ b/PLC/PCUProtocolTypes.py @@ -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) diff --git a/PLC/PersonTags.py b/PLC/PersonTags.py index 4bd38cf1..5581a698 100644 --- a/PLC/PersonTags.py +++ b/PLC/PersonTags.py @@ -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) diff --git a/PLC/Persons.py b/PLC/Persons.py index 518f53cd..76487304 100644 --- a/PLC/Persons.py +++ b/PLC/Persons.py @@ -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']}) -- 2.47.0