From a389969ace23befb78589352fb2d7cbf5ec443f0 Mon Sep 17 00:00:00 2001
From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com>
Date: Mon, 25 Jan 2021 13:28:02 +0100
Subject: [PATCH] dnsmadeeasy: Fix HTTP 400 errors when creating a TXT record
 (#1654) (#1675)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* dnsmadeeasy: Fix HTTP 400 errors when creating a TXT record

* When creating a record the module fails on monitor API call
* TXT records are surrounded by quotes in the API response

Fixes: #1237

* dnsmadeeasy: Add changelog fragment

* dnsmadeeasy: Fix pylint error

* Update changelogs/fragments/1654-dnsmadeeasy-http-400-fixes.yaml

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

* Update plugins/modules/net_tools/dnsmadeeasy.py

The dictionary might be empty

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

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit ebaa17f59ffbdf79d7cc9bdbe20deb80332c6d2d)

Co-authored-by: Orosz Dávid <idawko@gmail.com>
---
 .../fragments/1654-dnsmadeeasy-http-400-fixes.yaml |  2 ++
 plugins/modules/net_tools/dnsmadeeasy.py           | 14 +++++++++++---
 2 files changed, 13 insertions(+), 3 deletions(-)
 create mode 100644 changelogs/fragments/1654-dnsmadeeasy-http-400-fixes.yaml

diff --git a/changelogs/fragments/1654-dnsmadeeasy-http-400-fixes.yaml b/changelogs/fragments/1654-dnsmadeeasy-http-400-fixes.yaml
new file mode 100644
index 0000000000..1934228644
--- /dev/null
+++ b/changelogs/fragments/1654-dnsmadeeasy-http-400-fixes.yaml
@@ -0,0 +1,2 @@
+bugfixes:
+  - dnsmadeeasy - fix HTTP 400 errors when creating a TXT record (https://github.com/ansible-collections/community.general/issues/1237).
diff --git a/plugins/modules/net_tools/dnsmadeeasy.py b/plugins/modules/net_tools/dnsmadeeasy.py
index 7d7c78fec6..c6bc70324c 100644
--- a/plugins/modules/net_tools/dnsmadeeasy.py
+++ b/plugins/modules/net_tools/dnsmadeeasy.py
@@ -467,6 +467,9 @@ class DME2(object):
             for result in self.all_records:
                 if record_type == "MX":
                     value = record_value.split(" ")[1]
+                # Note that TXT records are surrounded by quotes in the API response.
+                elif record_type == "TXT":
+                    value = '"{0}"'.format(record_value)
                 elif record_type == "SRV":
                     value = record_value.split(" ")[3]
                 else:
@@ -651,7 +654,9 @@ def main():
     record_changed = False
     if current_record:
         for i in new_record:
-            if str(current_record[i]) != str(new_record[i]):
+            # Remove leading and trailing quote character from values because TXT records
+            # are surrounded by quotes.
+            if str(current_record[i]).strip('"') != str(new_record[i]):
                 record_changed = True
         new_record['id'] = str(current_record['id'])
 
@@ -673,8 +678,11 @@ def main():
         # create record and monitor as the record does not exist
         if not current_record:
             record = DME.createRecord(DME.prepareRecord(new_record))
-            monitor = DME.updateMonitor(record['id'], DME.prepareMonitor(new_monitor))
-            module.exit_json(changed=True, result=dict(record=record, monitor=monitor))
+            if new_monitor.get('monitor') and record_type == "A":
+                monitor = DME.updateMonitor(record['id'], DME.prepareMonitor(new_monitor))
+                module.exit_json(changed=True, result=dict(record=record, monitor=monitor))
+            else:
+                module.exit_json(changed=True, result=dict(record=record, monitor=current_monitor))
 
         # update the record
         updated = False