From 06a46360cfb98195ad3aa752dcae19ffde643af0 Mon Sep 17 00:00:00 2001 From: "John D. Rowell" Date: Wed, 22 May 2024 12:30:55 +0000 Subject: [PATCH] Moved http status check up to catch more cases (#362) Co-authored-by: Mark Mercado --- plugins/modules/digital_ocean_droplet.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/plugins/modules/digital_ocean_droplet.py b/plugins/modules/digital_ocean_droplet.py index 495aca7b..ea915027 100644 --- a/plugins/modules/digital_ocean_droplet.py +++ b/plugins/modules/digital_ocean_droplet.py @@ -621,6 +621,17 @@ def wait_action(self, droplet_id, desired_action_data): json_data = response.json status_code = response.status_code message = json_data.get("message", "no error message") + + # action and other fields may not be available in case of error, check first + # will catch Not Authorized due to restrictive Scopes + if status_code >= 400: + self.module.fail_json( + changed=False, + msg=DODroplet.failure_message["failed_to"].format( + "post", "action", status_code, message + ), + ) + action = json_data.get("action", None) action_id = action.get("id", None) action_status = action.get("status", None) @@ -633,14 +644,6 @@ def wait_action(self, droplet_id, desired_action_data): ), ) - if status_code >= 400: - self.module.fail_json( - changed=False, - msg=DODroplet.failure_message["failed_to"].format( - "post", "action", status_code, message - ), - ) - # Keep checking till it is done or times out self.wait_check_action(droplet_id, action_id)