def _verify_with_reconciled_data(exist): if not exist.launched_at: raise VerificationException("Exists without a launched_at") query = models.InstanceReconcile.objects.filter(instance=exist.instance) if query.count() > 0: recs = models.InstanceReconcile.find( exist.instance, dt.dt_from_decimal((exist.launched_at))) search_query = { 'instance': exist.instance, 'launched_at': exist.launched_at } count = recs.count() if count > 1: raise AmbiguousResults('InstanceReconcile', search_query) elif count == 0: raise NotFound('InstanceReconcile', search_query) reconcile = recs[0] else: raise NotFound('InstanceReconcile', {'instance': exist.instance}) _verify_for_launch(exist, launch=reconcile, launch_type="InstanceReconcile") delete = None if reconcile.deleted_at is not None: delete = reconcile _verify_for_delete(exist, delete=delete, delete_type="InstanceReconcile")
def _verify_for_delete(exist, delete=None): delete_type = "ImageDelete" if not delete and exist.delete: # We know we have a delete and we have it's id delete = exist.delete elif not delete: if exist.deleted_at: # We received this exists before the delete, go find it deletes = models.ImageDeletes.find(uuid=exist.uuid) if deletes.count() == 1: delete = deletes[0] else: query = { 'instance': exist.instance, 'launched_at': exist.launched_at } raise NotFound(delete_type, query) else: # We don't know if this is supposed to have a delete or not. # Thus, we need to check if we have a delete for this instance. # We need to be careful though, since we could be verifying an # exist event that we got before the delete. So, we restrict the # search to only deletes before this exist's audit period ended. # If we find any, we fail validation deleted_at_max = dt.dt_from_decimal(exist.audit_period_ending) deletes = models.ImageDeletes.find(exist.uuid, deleted_at_max) if deletes.count() > 0: reason = 'Found %ss for non-delete exist' % delete_type raise VerificationException(reason) if delete: if not base_verifier._verify_date_field( delete.deleted_at, exist.deleted_at, same_second=True): raise FieldMismatch('deleted_at', exist.deleted_at, delete.deleted_at, exist.uuid)
def _verify_for_usage(exist, usage=None): usage_type = "ImageUsage" if not usage and exist.usage: usage = exist.usage elif not usage: usages = models.ImageUsage.objects.filter(uuid=exist.uuid) usage_count = usages.count() if usage_count == 0: query = {'uuid': exist.uuid} raise NotFound(usage_type, query) usage = usages[0] _verify_field_mismatch(exist, usage)
def _verify_for_launch(exist): if exist.usage: launch = exist.usage else: if models.InstanceUsage.objects\ .filter(instance=exist.instance).count() > 0: launches = _find_launch(exist.instance, dt.dt_from_decimal(exist.launched_at)) count = launches.count() query = { 'instance': exist.instance, 'launched_at': exist.launched_at } if count > 1: raise AmbiguousResults('InstanceUsage', query) elif count == 0: raise NotFound('InstanceUsage', query) launch = launches[0] else: raise NotFound('InstanceUsage', {'instance': exist.instance}) _verify_field_mismatch(exist, launch)
def test_not_found_exception(self): exception = NotFound('object_type', 'search_params') self.assertEqual(exception.reason, "Couldn't find object_type using search_params")