summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaustubh Phatak <kphatak@platform9.com>2017-04-28 14:38:38 -0700
committerKaustubh Phatak <kphatak@platform9.com>2017-04-28 14:38:57 -0700
commit46c639537237f8d7fea5c17872dd73baa80556a2 (patch)
treeba9870ebdc8daeb587688d5c64038bf271077024
parent3eb6c954248428e46e31c2dc6930da86229ef356 (diff)
Fixing tenant deletes bug
Notes
Notes (review): Code-Review+1: Roopak Parikh <rparikh@platform9.net> Code-Review+1: Amrish <amrish.kapoor@gmail.com> Code-Review+1: Blake Covarrubias <blake@platform9.com> Code-Review+2: Kaustubh Phatak <kphatak@platform9.com> Workflow+1: Kaustubh Phatak <kphatak@platform9.com> Verified+2: Jenkins Submitted-by: Jenkins Submitted-at: Fri, 09 Jun 2017 22:53:33 +0000 Reviewed-on: https://review.openstack.org/461165 Project: openstack/mors Branch: refs/heads/master
-rw-r--r--mors/persistence.py37
1 files changed, 20 insertions, 17 deletions
diff --git a/mors/persistence.py b/mors/persistence.py
index 7677049..f6f89b5 100644
--- a/mors/persistence.py
+++ b/mors/persistence.py
@@ -13,10 +13,12 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and 13See the License for the specific language governing permissions and
14limitations under the License. 14limitations under the License.
15""" 15"""
16import functools
17import logging
18
19from sqlalchemy import Table, MetaData
20from sqlalchemy import create_engine
16from sqlalchemy.pool import QueuePool 21from sqlalchemy.pool import QueuePool
17from sqlalchemy import create_engine, text
18from sqlalchemy import Table, Column, Integer, String, MetaData, DateTime
19import logging, functools
20 22
21logger = logging.getLogger(__name__) 23logger = logging.getLogger(__name__)
22 24
@@ -25,7 +27,7 @@ def db_connect(transaction=False):
25 """ 27 """
26 Generates a decorator that get connection from a pool and returns 28 Generates a decorator that get connection from a pool and returns
27 it to the pool when the internal function is done 29 it to the pool when the internal function is done
28 :param transaction bool: should this function create and end transaction. 30 :param transaction: (boolean) should this function create and end transaction.
29 """ 31 """
30 32
31 def _db_connect(fun): 33 def _db_connect(fun):
@@ -36,6 +38,7 @@ def db_connect(transaction=False):
36 38
37 @functools.wraps(fun) 39 @functools.wraps(fun)
38 def newfun(self, *args, **kwargs): 40 def newfun(self, *args, **kwargs):
41 trans = None
39 conn = self.engine.connect() 42 conn = self.engine.connect()
40 if transaction: 43 if transaction:
41 trans = conn.begin() 44 trans = conn.begin()
@@ -44,7 +47,7 @@ def db_connect(transaction=False):
44 if transaction: 47 if transaction:
45 trans.commit() 48 trans.commit()
46 return ret 49 return ret
47 except Exception as e: 50 except Exception:
48 if transaction: 51 if transaction:
49 trans.rollback() 52 trans.rollback()
50 logger.exception("Error during transaction ") 53 logger.exception("Error during transaction ")
@@ -81,26 +84,26 @@ class DbPersistence:
81 @db_connect(transaction=True) 84 @db_connect(transaction=True)
82 def update_tenant_lease(self, conn, tenant_uuid, expiry_mins, updated_by, updated_at): 85 def update_tenant_lease(self, conn, tenant_uuid, expiry_mins, updated_by, updated_at):
83 logger.debug("Updating tenant lease %s %d %s %s", tenant_uuid, expiry_mins, str(updated_at), updated_by) 86 logger.debug("Updating tenant lease %s %d %s %s", tenant_uuid, expiry_mins, str(updated_at), updated_by)
84 conn.execute(self.tenant_lease.update().\ 87 conn.execute(self.tenant_lease.update().where(
85 where(self.tenant_lease.c.tenant_uuid == tenant_uuid).\ 88 self.tenant_lease.c.tenant_uuid == tenant_uuid).
86 values(expiry_mins=expiry_mins, 89 values(expiry_mins=expiry_mins,
87 updated_at=updated_at, updated_by=updated_by)) 90 updated_at=updated_at, updated_by=updated_by))
88 91
89 @db_connect(transaction=True) 92 @db_connect(transaction=True)
90 def delete_tenant_lease(self, conn, tenant_uuid): 93 def delete_tenant_lease(self, conn, tenant_uuid):
91 # Should we just soft delete ? 94 # Should we just soft delete ?
92 logger.debug("Deleting tenant lease %s", tenant_uuid) 95 logger.debug("Deleting tenant lease %s", tenant_uuid)
93 conn.execute(self.tenant_lease.delete().where(tenant_uuid == tenant_uuid)) 96 conn.execute(self.tenant_lease.delete().where(self.tenant_lease.c.tenant_uuid == tenant_uuid))
94 conn.execute(self.instance_lease.delete().where(tenant_uuid == tenant_uuid)) 97 conn.execute(self.instance_lease.delete().where(self.instance_lease.c.tenant_uuid == tenant_uuid))
95 98
96 @db_connect(transaction=False) 99 @db_connect(transaction=False)
97 def get_instance_leases_by_tenant(self, conn, tenant_uuid): 100 def get_instance_leases_by_tenant(self, conn, tenant_uuid):
98 return conn.execute(self.instance_lease.select(\ 101 return conn.execute(self.instance_lease.select(
99 self.instance_lease.c.tenant_uuid == tenant_uuid)).fetchall() 102 self.instance_lease.c.tenant_uuid == tenant_uuid)).fetchall()
100 103
101 @db_connect(transaction=False) 104 @db_connect(transaction=False)
102 def get_instance_lease(self, conn, instance_uuid): 105 def get_instance_lease(self, conn, instance_uuid):
103 return conn.execute(self.instance_lease.select((\ 106 return conn.execute(self.instance_lease.select((
104 self.instance_lease.c.instance_uuid == instance_uuid))).first() 107 self.instance_lease.c.instance_uuid == instance_uuid))).first()
105 108
106 @db_connect(transaction=True) 109 @db_connect(transaction=True)
@@ -113,10 +116,10 @@ class DbPersistence:
113 @db_connect(transaction=True) 116 @db_connect(transaction=True)
114 def update_instance_lease(self, conn, instance_uuid, tenant_uuid, expiry, updated_by, updated_at): 117 def update_instance_lease(self, conn, instance_uuid, tenant_uuid, expiry, updated_by, updated_at):
115 logger.debug("Updating instance lease %s %s %s %s", instance_uuid, tenant_uuid, expiry, updated_by) 118 logger.debug("Updating instance lease %s %s %s %s", instance_uuid, tenant_uuid, expiry, updated_by)
116 conn.execute(self.instance_lease.update().\ 119 conn.execute(self.instance_lease.update().where(
117 where(self.instance_lease.c.instance_uuid == instance_uuid).\ 120 self.instance_lease.c.instance_uuid == instance_uuid).values
118 values(tenant_uuid=tenant_uuid, expiry=expiry, 121 (tenant_uuid=tenant_uuid, expiry=expiry,
119 updated_at=updated_at, updated_by=updated_by)) 122 updated_at=updated_at, updated_by=updated_by))
120 123
121 @db_connect(transaction=True) 124 @db_connect(transaction=True)
122 def delete_instance_leases(self, conn, instance_uuids): 125 def delete_instance_leases(self, conn, instance_uuids):