Browse Source

Changes for restricting instance lease to not exceed tenant lease

Susmitha 2 years ago
parent
commit
2e3869feb2
2 changed files with 27 additions and 8 deletions
  1. 17
    8
      mors/lease_manager.py
  2. 10
    0
      test/test_api.py

+ 17
- 8
mors/lease_manager.py View File

@@ -93,9 +93,12 @@ class LeaseManager:
93 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 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 103
     def get_instance_lease(self, context, instance_id):
101 104
         data = self.domain_mgr.get_instance_lease(instance_id)
@@ -107,20 +110,26 @@ class LeaseManager:
107 110
     def add_instance_lease(self, context, tenant_uuid, instance_lease_obj):
108 111
         logger.info("Add instance lease %s", instance_lease_obj)
109 112
         tenant_lease = self.domain_mgr.get_tenant_lease(tenant_uuid)
110
-        self.check_instance_lease_violation(instance_lease_obj, tenant_lease)
111
-        self.domain_mgr.add_instance_lease(instance_lease_obj['instance_uuid'],
113
+        if not self.check_instance_lease_violation(instance_lease_obj, tenant_lease):
114
+            self.domain_mgr.add_instance_lease(instance_lease_obj['instance_uuid'],
112 115
                                            tenant_uuid,
113 116
                                            instance_lease_obj['expiry'],
114 117
                                            context.user_id,
115 118
                                            datetime.utcnow())
119
+        else:
120
+            raise ValueError("Instance lease exceeds tenant lease")
116 121
 
117 122
     def update_instance_lease(self, context, tenant_uuid, instance_lease_obj):
118 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 127
                                               tenant_uuid,
121 128
                                               instance_lease_obj['expiry'],
122 129
                                               context.user_id,
123 130
                                               datetime.utcnow())
131
+        else:
132
+            raise ValueError("Instance lease exceeds tenant lease")
124 133
 
125 134
     def delete_instance_lease(self, context, instance_uuid):
126 135
         logger.info("Delete instance lease %s", instance_uuid)
@@ -134,7 +143,7 @@ class LeaseManager:
134 143
     def _get_vms_to_delete_for_tenant(self, tenant_uuid, expiry_mins):
135 144
         vms_to_delete = []
136 145
         vm_ids_to_delete = set()
137
-        do_not_delete = set()
146
+        do_not_delete = set() 
138 147
         now = datetime.utcnow()
139 148
         add_seconds = timedelta(seconds=expiry_mins*60)
140 149
         instance_leases = self.get_tenant_and_associated_instance_leases(None, tenant_uuid)['all_vms']
@@ -150,8 +159,8 @@ class LeaseManager:
150 159
         tenant_vms = self.lease_handler.get_all_vms(tenant_uuid)
151 160
         for vm in tenant_vms:
152 161
             expiry_date = vm['created_at'] + add_seconds
153
-            if now > expiry_date and vm['instance_uuid'] not in do_not_delete \
154
-                                and vm['instance_uuid'] not in vm_ids_to_delete:
162
+            if now > expiry_date and vm['instance_uuid'] not in vm_ids_to_delete \
163
+                                 and vm['instance_uuid'] not in do_not_delete:
155 164
                 logger.info("Instance %s queued up for deletion creation date %s", vm['instance_uuid'],
156 165
                             vm['created_at'])
157 166
                 vms_to_delete.append(vm)

+ 10
- 0
test/test_api.py View File

@@ -162,6 +162,16 @@ def test_update_instance():
162 162
     logger.debug(r.text)
163 163
     assert_equal(r.status_code, 200)
164 164
 
165
+@test(depends_on=[test_update_instance])
166
+def 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
+
165 175
 @test(depends_on=[test_update_instance])
166 176
 def test_get_instance2():
167 177
     r = requests.get('http://127.0.0.1:' + port + '/v1/tenant/' + tenant_id1 + '/instance/' + instance_id1,

Loading…
Cancel
Save