summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSusmitha <susmitha@platform9.com>2016-06-27 16:11:57 -0700
committerSusmitha <susmitha@platform9.com>2016-06-28 11:17:24 -0700
commit2e3869feb25a1575447de186b9b39d2c8d8084bc (patch)
treeaa2ef21f1ffe54905edeb610e5f5e561378bdc5e
parent6c79555d56c2784a68250381d6647fffdb4aa4b1 (diff)
Changes for restricting instance lease to not exceed tenant lease
-rw-r--r--mors/lease_manager.py25
-rw-r--r--test/test_api.py10
2 files changed, 27 insertions, 8 deletions
diff --git a/mors/lease_manager.py b/mors/lease_manager.py
index 26864be..b52c60e 100644
--- a/mors/lease_manager.py
+++ b/mors/lease_manager.py
@@ -93,9 +93,12 @@ class LeaseManager:
93 map(lambda x: get_vm_lease_data(x), self.domain_mgr.get_instance_leases_by_tenant(tenant_uuid)) 93 map(lambda x: get_vm_lease_data(x), self.domain_mgr.get_instance_leases_by_tenant(tenant_uuid))
94 } 94 }
95 95
96 # To Be Implemented
97 def check_instance_lease_violation(self, instance_lease, tenant_lease): 96 def check_instance_lease_violation(self, instance_lease, tenant_lease):
98 return True 97 violation = False
98 expiry = instance_lease['expiry']
99 if (datetime.utcnow() + timedelta(seconds=tenant_lease['expiry_mins']*60)) < expiry:
100 violation = True
101 return violation
99 102
100 def get_instance_lease(self, context, instance_id): 103 def get_instance_lease(self, context, instance_id):
101 data = self.domain_mgr.get_instance_lease(instance_id) 104 data = self.domain_mgr.get_instance_lease(instance_id)
@@ -107,20 +110,26 @@ class LeaseManager:
107 def add_instance_lease(self, context, tenant_uuid, instance_lease_obj): 110 def add_instance_lease(self, context, tenant_uuid, instance_lease_obj):
108 logger.info("Add instance lease %s", instance_lease_obj) 111 logger.info("Add instance lease %s", instance_lease_obj)
109 tenant_lease = self.domain_mgr.get_tenant_lease(tenant_uuid) 112 tenant_lease = self.domain_mgr.get_tenant_lease(tenant_uuid)
110 self.check_instance_lease_violation(instance_lease_obj, tenant_lease) 113 if not self.check_instance_lease_violation(instance_lease_obj, tenant_lease):
111 self.domain_mgr.add_instance_lease(instance_lease_obj['instance_uuid'], 114 self.domain_mgr.add_instance_lease(instance_lease_obj['instance_uuid'],
112 tenant_uuid, 115 tenant_uuid,
113 instance_lease_obj['expiry'], 116 instance_lease_obj['expiry'],
114 context.user_id, 117 context.user_id,
115 datetime.utcnow()) 118 datetime.utcnow())
119 else:
120 raise ValueError("Instance lease exceeds tenant lease")
116 121
117 def update_instance_lease(self, context, tenant_uuid, instance_lease_obj): 122 def update_instance_lease(self, context, tenant_uuid, instance_lease_obj):
118 logger.info("Update instance lease %s", instance_lease_obj) 123 logger.info("Update instance lease %s", instance_lease_obj)
119 self.domain_mgr.update_instance_lease(instance_lease_obj['instance_uuid'], 124 tenant_lease = self.domain_mgr.get_tenant_lease(tenant_uuid)
125 if not self.check_instance_lease_violation(instance_lease_obj, tenant_lease):
126 self.domain_mgr.update_instance_lease(instance_lease_obj['instance_uuid'],
120 tenant_uuid, 127 tenant_uuid,
121 instance_lease_obj['expiry'], 128 instance_lease_obj['expiry'],
122 context.user_id, 129 context.user_id,
123 datetime.utcnow()) 130 datetime.utcnow())
131 else:
132 raise ValueError("Instance lease exceeds tenant lease")
124 133
125 def delete_instance_lease(self, context, instance_uuid): 134 def delete_instance_lease(self, context, instance_uuid):
126 logger.info("Delete instance lease %s", instance_uuid) 135 logger.info("Delete instance lease %s", instance_uuid)
@@ -134,7 +143,7 @@ class LeaseManager:
134 def _get_vms_to_delete_for_tenant(self, tenant_uuid, expiry_mins): 143 def _get_vms_to_delete_for_tenant(self, tenant_uuid, expiry_mins):
135 vms_to_delete = [] 144 vms_to_delete = []
136 vm_ids_to_delete = set() 145 vm_ids_to_delete = set()
137 do_not_delete = set() 146 do_not_delete = set()
138 now = datetime.utcnow() 147 now = datetime.utcnow()
139 add_seconds = timedelta(seconds=expiry_mins*60) 148 add_seconds = timedelta(seconds=expiry_mins*60)
140 instance_leases = self.get_tenant_and_associated_instance_leases(None, tenant_uuid)['all_vms'] 149 instance_leases = self.get_tenant_and_associated_instance_leases(None, tenant_uuid)['all_vms']
@@ -150,8 +159,8 @@ class LeaseManager:
150 tenant_vms = self.lease_handler.get_all_vms(tenant_uuid) 159 tenant_vms = self.lease_handler.get_all_vms(tenant_uuid)
151 for vm in tenant_vms: 160 for vm in tenant_vms:
152 expiry_date = vm['created_at'] + add_seconds 161 expiry_date = vm['created_at'] + add_seconds
153 if now > expiry_date and vm['instance_uuid'] not in do_not_delete \ 162 if now > expiry_date and vm['instance_uuid'] not in vm_ids_to_delete \
154 and vm['instance_uuid'] not in vm_ids_to_delete: 163 and vm['instance_uuid'] not in do_not_delete:
155 logger.info("Instance %s queued up for deletion creation date %s", vm['instance_uuid'], 164 logger.info("Instance %s queued up for deletion creation date %s", vm['instance_uuid'],
156 vm['created_at']) 165 vm['created_at'])
157 vms_to_delete.append(vm) 166 vms_to_delete.append(vm)
diff --git a/test/test_api.py b/test/test_api.py
index 277e29d..076707f 100644
--- a/test/test_api.py
+++ b/test/test_api.py
@@ -163,6 +163,16 @@ def test_update_instance():
163 assert_equal(r.status_code, 200) 163 assert_equal(r.status_code, 200)
164 164
165@test(depends_on=[test_update_instance]) 165@test(depends_on=[test_update_instance])
166def test_update_instance_violation():
167 expiry = datetime.utcnow() + timedelta(days=4)
168 expiry_str = datetime.strftime(expiry, DATE_FORMAT)
169 r = requests.put('http://127.0.0.1:' + port + '/v1/tenant/' + tenant_id1 + '/instance/' + instance_id1,
170 json={"instance_uuid": instance_id1, "expiry": expiry_str},
171 headers=headers)
172 logger.debug(r.text)
173 assert_equal(r.status_code, 422)
174
175@test(depends_on=[test_update_instance])
166def test_get_instance2(): 176def test_get_instance2():
167 r = requests.get('http://127.0.0.1:' + port + '/v1/tenant/' + tenant_id1 + '/instance/' + instance_id1, 177 r = requests.get('http://127.0.0.1:' + port + '/v1/tenant/' + tenant_id1 + '/instance/' + instance_id1,
168 headers=headers) 178 headers=headers)