From c123b4e7ef6efe8d9ea777ae542e8995b6bd1ffd Mon Sep 17 00:00:00 2001 From: Tom Hodder Date: Sat, 22 Sep 2018 08:13:20 +0100 Subject: [PATCH] digital_ocean_domain: fix to allow updating the module param ip between calls (#42009) * update the domain record with the new ip from params if resource exists * fixes for pylint and pep8 checks --- .../digital_ocean/digital_ocean_domain.py | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/cloud/digital_ocean/digital_ocean_domain.py b/lib/ansible/modules/cloud/digital_ocean/digital_ocean_domain.py index f0349ed168..9be58956e9 100644 --- a/lib/ansible/modules/cloud/digital_ocean/digital_ocean_domain.py +++ b/lib/ansible/modules/cloud/digital_ocean/digital_ocean_domain.py @@ -134,10 +134,22 @@ class DoManager(DigitalOceanHelper, object): else: return json - def edit_domain_record(self): - params = {'name': self.domain_name} - resp = self.put('domains/%s/records/%s' % (self.domain_name, self.domain_id), data=params) + def edit_domain_record(self, record): + params = {'name': '@', + 'data': self.module.params.get('ip')} + resp = self.put('domains/%s/records/%s' % (self.domain_name, record['id']), data=params) status, json = self.jsonify(resp) + + return json['domain_record'] + + def create_domain_record(self): + params = {'name': '@', + 'type': 'A', + 'data': self.module.params.get('ip')} + + resp = self.post('domains/%s/records' % (self.domain_name), data=params) + status, json = self.jsonify(resp) + return json['domain_record'] @@ -160,8 +172,11 @@ def core(module): if record['name'] == "@" and record['type'] == 'A': at_record = record - if not at_record['data'] == module.params.get('ip'): - do_manager.edit_domain_record() + if not at_record: + do_manager.create_domain_record() + module.exit_json(changed=True, domain=do_manager.find()) + elif not at_record['data'] == module.params.get('ip'): + do_manager.edit_domain_record(at_record) module.exit_json(changed=True, domain=do_manager.find()) else: module.exit_json(changed=False, domain=do_manager.domain_record())