From 70ba401602901857db424e1bf18043fae171ab63 Mon Sep 17 00:00:00 2001
From: Martin <spleefer90@gmail.com>
Date: Wed, 2 Dec 2020 07:38:56 +0100
Subject: [PATCH] [icinga2_host.py] Actually return codes instead of data
 (#335)

* [icinga2_host.py] Actually return codes instead of data

Currently the module tries to return the `data`, which can result in a blank message instead of the code being shown.

```
 "msg": "bad return code creating host: "
```

* add changelog fragment

* Update changelogs/fragments/335-icinga2_host-return-error-code.yaml

Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>

* return code and data on fail

Co-authored-by: Felix Fontein <felix@fontein.de>

Co-authored-by: John R Barker <john@johnrbarker.com>
Co-authored-by: Andrew Klychkov <aaklychkov@mail.ru>
Co-authored-by: Deric Crago <deric.crago@gmail.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
---
 .../fragments/335-icinga2_host-return-error-code.yaml       | 2 ++
 plugins/modules/monitoring/icinga2_host.py                  | 6 +++---
 2 files changed, 5 insertions(+), 3 deletions(-)
 create mode 100644 changelogs/fragments/335-icinga2_host-return-error-code.yaml

diff --git a/changelogs/fragments/335-icinga2_host-return-error-code.yaml b/changelogs/fragments/335-icinga2_host-return-error-code.yaml
new file mode 100644
index 0000000000..26e2d2f5c9
--- /dev/null
+++ b/changelogs/fragments/335-icinga2_host-return-error-code.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+  - icinga2_host - fix returning error codes (https://github.com/ansible-collections/community.general/pull/335).
diff --git a/plugins/modules/monitoring/icinga2_host.py b/plugins/modules/monitoring/icinga2_host.py
index 6bdbb7179c..e2acdb2a6a 100644
--- a/plugins/modules/monitoring/icinga2_host.py
+++ b/plugins/modules/monitoring/icinga2_host.py
@@ -289,7 +289,7 @@ def main():
                     if ret['code'] == 200:
                         changed = True
                     else:
-                        module.fail_json(msg="bad return code deleting host: %s" % (ret['data']))
+                        module.fail_json(msg="bad return code (%s) deleting host: '%s'" % (ret['code'], ret['data']))
                 except Exception as e:
                     module.fail_json(msg="exception deleting host: " + str(e))
 
@@ -305,7 +305,7 @@ def main():
             if ret['code'] == 200:
                 changed = True
             else:
-                module.fail_json(msg="bad return code modifying host: %s" % (ret['data']))
+                module.fail_json(msg="bad return code (%s) modifying host: '%s'" % (ret['code'], ret['data']))
 
     else:
         if state == "present":
@@ -317,7 +317,7 @@ def main():
                     if ret['code'] == 200:
                         changed = True
                     else:
-                        module.fail_json(msg="bad return code creating host: %s" % (ret['data']))
+                        module.fail_json(msg="bad return code (%s) creating host: '%s'" % (ret['code'], ret['data']))
                 except Exception as e:
                     module.fail_json(msg="exception creating host: " + str(e))