From: Iustin Pop Date: Mon, 11 Mar 2013 09:13:24 +0000 (+0100) Subject: Introduce better item getter helper in query.py X-Git-Tag: v2.8.0beta1~309 X-Git-Url: https://code.grnet.gr/git/ganeti-local/commitdiff_plain/40b118d3b88e8e47bff3535cb5ecb96284351a4c Introduce better item getter helper in query.py And also use it for simple network fields. Currently the Python library returns jsnull, which is formatted as 'None' by the Python client, which (IMHO) is not nice, since these fields are strings (some of them) or "other". An undeclared gateway is easier to diagnose when properly reported by the server as missing, rather than "existing but null". Signed-off-by: Iustin Pop Reviewed-by: Helga Velroyen --- diff --git a/lib/query.py b/lib/query.py index 37bf114..2b5fc4a 100644 --- a/lib/query.py +++ b/lib/query.py @@ -977,6 +977,23 @@ def _GetItemAttr(attr): return lambda _, item: getter(item) +def _GetItemMaybeAttr(attr): + """Returns a field function to return a not-None attribute of the item. + + If the value is None, then C{_FS_UNAVAIL} will be returned instead. + + @param attr: Attribute name + + """ + def _helper(_, obj): + val = getattr(obj, attr) + if val is None: + return _FS_UNAVAIL + else: + return val + return _helper + + def _GetNDParam(name): """Return a field function to return an ND parameter out of the context. @@ -2614,7 +2631,7 @@ def _BuildNetworkFields(): # Add simple fields fields.extend([ (_MakeField(name, title, kind, doc), - NETQ_CONFIG, 0, _GetItemAttr(name)) + NETQ_CONFIG, 0, _GetItemMaybeAttr(name)) for (name, (title, kind, _, doc)) in _NETWORK_SIMPLE_FIELDS.items()]) def _GetLength(getter):