From 5123d542e4228422f826fe8a606810f6f4d3a303 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Fri, 19 Jun 2020 09:34:33 +0700 Subject: [PATCH 1/3] Fix #752 - Rollback the removed record if apply operation failed --- powerdnsadmin/models/record.py | 66 +++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/powerdnsadmin/models/record.py b/powerdnsadmin/models/record.py index 201ad82..bfd1918 100644 --- a/powerdnsadmin/models/record.py +++ b/powerdnsadmin/models/record.py @@ -278,6 +278,17 @@ class Record(object): return new_rrsets, del_rrsets + def apply_rrsets(self, domain_name, rrsets): + headers = {'X-API-Key': self.PDNS_API_KEY} + jdata = utils.fetch_json(urljoin( + self.PDNS_STATS_URL, self.API_EXTENDED_URL + + '/servers/localhost/zones/{0}'.format(domain_name)), + headers=headers, + method='PATCH', + verify=Setting().get('verify_ssl_connections'), + data=rrsets) + return jdata + def apply(self, domain_name, submitted_records): """ Apply record changes to a domain. This function @@ -301,44 +312,43 @@ class Record(object): # Submit the changes to PDNS API try: - headers = {'X-API-Key': self.PDNS_API_KEY} - if del_rrsets["rrsets"]: - jdata1 = utils.fetch_json(urljoin( - self.PDNS_STATS_URL, self.API_EXTENDED_URL + - '/servers/localhost/zones/{0}'.format(domain_name)), - headers=headers, - method='PATCH', - verify=Setting().get('verify_ssl_connections'), - data=del_rrsets) - if 'error' in jdata1.keys(): + result = self.apply_rrsets(domain_name, del_rrsets) + if 'error' in result.keys(): current_app.logger.error( 'Cannot apply record changes with deleting rrsets step. PDNS error: {}' - .format(jdata1['error'])) - print(jdata1['error']) + .format(result['error'])) return { 'status': 'error', - 'msg': jdata1['error'].replace("'", "") + 'msg': result['error'].replace("'", "") } if new_rrsets["rrsets"]: - jdata2 = utils.fetch_json( - urljoin( - self.PDNS_STATS_URL, self.API_EXTENDED_URL + - '/servers/localhost/zones/{0}'.format(domain_name)), - headers=headers, - timeout=int(Setting().get('pdns_api_timeout')), - method='PATCH', - verify=Setting().get('verify_ssl_connections'), - data=new_rrsets) - if 'error' in jdata2.keys(): + result = self.apply_rrsets(domain_name, new_rrsets) + if 'error' in result.keys(): current_app.logger.error( 'Cannot apply record changes with adding rrsets step. PDNS error: {}' - .format(jdata2['error'])) - return { - 'status': 'error', - 'msg': jdata2['error'].replace("'", "") - } + .format(result['error'])) + + # rollback - re-add the removed record if the adding operation is failed. + if del_rrsets["rrsets"]: + rollback_rrests = del_rrsets + for r in del_rrsets["rrsets"]: + r['changetype'] = 'REPLACE' + rollback = self.apply_rrsets(domain_name, rollback_rrests) + if 'error' in rollback.keys(): + return dict(status='error', + msg='Failed to apply changes. Cannot rollback previous failed operation: {}' + .format(rollback['error'].replace("'", ""))) + else: + return dict(status='error', + msg='Failed to apply changes. Rolled back previous failed operation: {}' + .format(result['error'].replace("'", ""))) + else: + return { + 'status': 'error', + 'msg': result['error'].replace("'", "") + } self.auto_ptr(domain_name, new_rrsets, del_rrsets) self.update_db_serial(domain_name) From 1f4580a27a6be78bf956bf8d614b2c21002d4026 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Sat, 20 Jun 2020 09:51:30 +0700 Subject: [PATCH 2/3] Log failed domain apply operation --- powerdnsadmin/routes/domain.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/powerdnsadmin/routes/domain.py b/powerdnsadmin/routes/domain.py index 9aaad31..7ccae8f 100644 --- a/powerdnsadmin/routes/domain.py +++ b/powerdnsadmin/routes/domain.py @@ -439,6 +439,15 @@ def record_apply(domain_name): history.add() return make_response(jsonify(result), 200) else: + history = History( + msg='Failed to apply record changes to domain {0}'.format(domain_name), + detail=str( + json.dumps({ + "domain": domain_name, + "msg": result['msg'], + })), + created_by=current_user.username) + history.add() return make_response(jsonify(result), 400) except Exception as e: current_app.logger.error( From f3f91d56e28e66442426101d3c8f4141ad7e03cd Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Sat, 20 Jun 2020 11:08:14 +0700 Subject: [PATCH 3/3] Adjust html code --- powerdnsadmin/templates/domain.html | 4 ++-- powerdnsadmin/templates/domain_setting.html | 10 ++++++---- powerdnsadmin/templates/maintenance.html | 4 ++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/powerdnsadmin/templates/domain.html b/powerdnsadmin/templates/domain.html index c40f4e0..23d684a 100644 --- a/powerdnsadmin/templates/domain.html +++ b/powerdnsadmin/templates/domain.html @@ -74,7 +74,7 @@ {% if record.is_allowed_edit() %} {% else %} - + {% endif %} @@ -243,7 +243,7 @@ // add new row var default_type = records_allow_edit[0] - var nRow = jQuery('#tbl_records').dataTable().fnAddData(['', default_type, 'Active', {{ ttl_options | tojson }}[0][0], '', '', '', '', '0']); + var nRow = jQuery('#tbl_records').dataTable().fnAddData(['', default_type, 'Active', window.ttl_options[0][0], '', '', '', '', '0']); editRow($("#tbl_records").DataTable(), nRow); document.getElementById("edit-row-focus").focus(); nEditing = nRow; diff --git a/powerdnsadmin/templates/domain_setting.html b/powerdnsadmin/templates/domain_setting.html index c09a12b..9312466 100644 --- a/powerdnsadmin/templates/domain_setting.html +++ b/powerdnsadmin/templates/domain_setting.html @@ -113,9 +113,11 @@

-  Allow automatic reverse pointer creation on record updates?{% if - SETTING.get('auto_ptr') %}
Auto-ptr is enabled globally on the PDA - system!{% endif %}

+  Allow automatic reverse pointer creation on record updates? + {% if SETTING.get('auto_ptr') %} +
Auto-ptr is enabled globally on the PDA system! + {% endif %} +

@@ -369,4 +371,4 @@ -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/powerdnsadmin/templates/maintenance.html b/powerdnsadmin/templates/maintenance.html index 16a3c04..a9839e5 100644 --- a/powerdnsadmin/templates/maintenance.html +++ b/powerdnsadmin/templates/maintenance.html @@ -37,7 +37,7 @@

We’ll be back soon!

Sorry for the inconvenience but we’re performing some maintenance at the moment. Please contact the System - Administrator if you need more information, otherwise we’ll be back online shortly!

+ Administrator if you need more information, otherwise we’ll be back online shortly!

— Team

- \ No newline at end of file +