From cf33b4c71d757800f5998b3c3b8585e9f0d9298c Mon Sep 17 00:00:00 2001 From: Gonza Rafuls Date: Tue, 15 Feb 2022 13:04:10 +0100 Subject: [PATCH] fix: add-schedule on non existing host This was failing with a non descriptive exception as we were trying to access the host.broken parameter on a None type object when the host is not found on DB. Resolved to display a proper validation error. Change-Id: I5f820180e6795fd5b16a3c3d765d2ca1c53471cf --- quads/api_v2.py | 4 +++- quads/model.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/quads/api_v2.py b/quads/api_v2.py index 32b59da51..c5ae08172 100644 --- a/quads/api_v2.py +++ b/quads/api_v2.py @@ -379,10 +379,12 @@ def POST(self, **data): _host = data["host"] _host_obj = Host.objects(name=_host).first() + if not _host_obj: + result.append(f"Host {_host} is not defined") broken_hosts = Host.objects(broken=True) if _host_obj in broken_hosts: - result.append(f"Host {_host_obj.name} is in broken state") + result.append(f"Host {_host_obj.name} does not exist") # Check if there were data validation errors if result: diff --git a/quads/model.py b/quads/model.py index c27a2d07a..a09efcd87 100644 --- a/quads/model.py +++ b/quads/model.py @@ -283,6 +283,8 @@ def insert_schedule(self, cloud, host, start, end): @queryset_manager def is_host_available(self, queryset, host, start, end, exclude=None): _host = Host.objects(name=host).first() + if not _host: + return False if _host.broken or _host.retired: return False _query = Q(host=_host)