Skip to content

Commit

Permalink
Merge pull request #4 from diijkstra/detect_auth_failure
Browse files Browse the repository at this point in the history
Detect authorization failure
  • Loading branch information
tobias-richter authored Apr 18, 2020
2 parents e42d6d2 + 48eeeef commit 15a883e
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions action_plugins/tasmota.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from ansible.module_utils._text import to_native
from ansible.plugins.action import ActionBase
from ansible.errors import AnsibleOptionsError
from ansible.errors import AnsibleOptionsError, AnsibleAuthenticationFailure, AnsibleRuntimeError

try:
from __main__ import display
Expand Down Expand Up @@ -84,7 +84,19 @@ def run(self, tmp=None, task_vars=None):
status_response = requests.get(url = endpoint_uri, params = status_params)
# get response data
data = status_response.json()
display.v("data: %s" % (data))
display.v("data: %s, response code: %s" % (data, status_response.status_code))

warnings = []
resp_warn = data.get("WARNING")
if resp_warn:
# Prior to v8.2.3 authorization error has 200 ok status
if status_response.status_code == 401 or resp_warn == "Need user=<username>&password=<password>":
raise AnsibleAuthenticationFailure("Missing/Invalid credentials")
warnings.append(resp_warn)

if status_response.status_code != 200:
raise AnsibleRuntimeError("Unexpected response code: %s" % (status_response.status_code))

existing_value = unicode(data.get(command))

if (command.startswith('Rule')):
Expand Down Expand Up @@ -120,6 +132,10 @@ def run(self, tmp=None, task_vars=None):
change_params.update( { 'cmnd' : ("%s %s" % (command, incoming_value)) } )
change_response = requests.get(url = endpoint_uri, params = change_params)

if warnings:
display.warning(warnings)
result["warning"] = warnings

result["changed"] = changed
result["command"] = command
result["tasmota_host"] = tasmota_host
Expand Down

0 comments on commit 15a883e

Please sign in to comment.