From 5cb930fd8aa65d88c2a6b1d2c95a8ec3bf686f91 Mon Sep 17 00:00:00 2001 From: Corey Wright Date: Fri, 24 Jun 2022 12:55:14 -0500 Subject: [PATCH] Don't throw an exception if API's JSON response lacks an expected key Instead of throwing an exception, be more defensive in the code: if the API's JSON response lacks a key and we correspondingly set a variable to `None`, then don't call `get()` on the variable, but test the variable first. AttributeError: 'NoneType' object has no attribute 'get' Fixes #272. --- changelogs/fragments/273-Dont_call_get_on_None.yaml | 2 ++ plugins/modules/digital_ocean_droplet.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/273-Dont_call_get_on_None.yaml diff --git a/changelogs/fragments/273-Dont_call_get_on_None.yaml b/changelogs/fragments/273-Dont_call_get_on_None.yaml new file mode 100644 index 00000000..afb16d0a --- /dev/null +++ b/changelogs/fragments/273-Dont_call_get_on_None.yaml @@ -0,0 +1,2 @@ +bugfixes: + - digital_ocean_droplet - if the JSON response lacks a key and the associated variable is set to ``None``, then don't treat that variable like a ``dict`` and call ``get()`` on it without first testing it (https://github.com/ansible-collections/community.digitalocean/issues/272). diff --git a/plugins/modules/digital_ocean_droplet.py b/plugins/modules/digital_ocean_droplet.py index 14f4e35a..791f2891 100644 --- a/plugins/modules/digital_ocean_droplet.py +++ b/plugins/modules/digital_ocean_droplet.py @@ -542,7 +542,7 @@ def wait_status(self, droplet_id, desired_statuses): status_code = response.status_code message = json_data.get("message", "no error message") droplet = json_data.get("droplet", None) - droplet_status = droplet.get("status", None) + droplet_status = droplet.get("status", None) if droplet else None if droplet is None or droplet_status is None: self.module.fail_json(