From aabf3b96a806d835304be3e9d13ff99cfaef8421 Mon Sep 17 00:00:00 2001 From: valievkarim Date: Fri, 31 Jan 2025 13:48:37 +0400 Subject: [PATCH] cloudflare_dns: fix crash when deleting a DNS record or when updating a record with solo=true (#9649) * cloudflare_dns: fix crash when deleting a DNS record or when updating a record with solo=true On 2025-01-27, Cloudflare removed the 'zone_id' field from the DNS record API responses. This caused a KeyError in the delete_dns_records method, which previously relied on rr['zone_id']. This commit ensures the zone ID is retrieved via _get_zone_id() rather than using the no-longer-provided 'zone_id' field in the record response. Reference: https://developers.cloudflare.com/dns/changelog/#2025-01-27 * Add changelog fragment * Update changelogs/fragments/9649-cloudflare_dns-fix-crash-when-deleting-record.yml Co-authored-by: Felix Fontein * Update changelogs/fragments/9649-cloudflare_dns-fix-crash-when-deleting-record.yml Co-authored-by: Felix Fontein --------- Co-authored-by: Felix Fontein (cherry picked from commit 19d0049698822de628b536361e27b4bf0032b840) --- .../9649-cloudflare_dns-fix-crash-when-deleting-record.yml | 2 ++ plugins/modules/cloudflare_dns.py | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/9649-cloudflare_dns-fix-crash-when-deleting-record.yml diff --git a/changelogs/fragments/9649-cloudflare_dns-fix-crash-when-deleting-record.yml b/changelogs/fragments/9649-cloudflare_dns-fix-crash-when-deleting-record.yml new file mode 100644 index 00000000000..c345947575d --- /dev/null +++ b/changelogs/fragments/9649-cloudflare_dns-fix-crash-when-deleting-record.yml @@ -0,0 +1,2 @@ +bugfixes: + - cloudflare_dns - fix crash when deleting a DNS record or when updating a record with ``solo=true`` (https://github.com/ansible-collections/community.general/issues/9652, https://github.com/ansible-collections/community.general/pull/9649). diff --git a/plugins/modules/cloudflare_dns.py b/plugins/modules/cloudflare_dns.py index 6ce2ff8bb40..a51337e3281 100644 --- a/plugins/modules/cloudflare_dns.py +++ b/plugins/modules/cloudflare_dns.py @@ -685,6 +685,7 @@ def delete_dns_records(self, **kwargs): else: search_value = content + zone_id = self._get_zone_id(params['zone']) records = self.get_dns_records(params['zone'], params['type'], search_record, search_value) for rr in records: @@ -692,11 +693,11 @@ def delete_dns_records(self, **kwargs): if not ((rr['type'] == params['type']) and (rr['name'] == search_record) and (rr['content'] == content)): self.changed = True if not self.module.check_mode: - result, info = self._cf_api_call('/zones/{0}/dns_records/{1}'.format(rr['zone_id'], rr['id']), 'DELETE') + result, info = self._cf_api_call('/zones/{0}/dns_records/{1}'.format(zone_id, rr['id']), 'DELETE') else: self.changed = True if not self.module.check_mode: - result, info = self._cf_api_call('/zones/{0}/dns_records/{1}'.format(rr['zone_id'], rr['id']), 'DELETE') + result, info = self._cf_api_call('/zones/{0}/dns_records/{1}'.format(zone_id, rr['id']), 'DELETE') return self.changed def ensure_dns_record(self, **kwargs):