From 8dcc0a761d0cb14d82e0805eb7d143f3c5a46139 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fra=C3=9F?= Date: Fri, 31 May 2024 09:28:32 +0200 Subject: [PATCH] [add] record-deletion --- source/macros.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ source/main.py | 16 ++++++++++-- 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/source/macros.py b/source/macros.py index 6346bd0..68ceaaf 100644 --- a/source/macros.py +++ b/source/macros.py @@ -1,3 +1,6 @@ +''' +@see https://www.inwx.de/de/help/apidoc/f/ch02.html#account.login +''' def api_macro_login( environment : str, username : str, @@ -21,6 +24,9 @@ def api_macro_login( return response["_accesstoken"] +''' +@see https://www.inwx.de/de/help/apidoc/f/ch02.html#account.logout +''' def api_macro_logout( environment : str, accesstoken : str @@ -36,6 +42,9 @@ def api_macro_logout( return None +''' +@see https://www.inwx.de/de/help/apidoc/f/ch02.html#account.info +''' def api_macro_info( environment : str, username : str, @@ -54,6 +63,9 @@ def api_macro_info( return info +''' +@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.info +''' def api_macro_list( environment : str, username : str, @@ -74,6 +86,11 @@ def api_macro_list( return info +''' +@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.info +@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.createRecord +@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.updateRecord +''' def api_macro_save( environment : str, username : str, @@ -133,3 +150,53 @@ def api_macro_save( api_macro_logout(environment, accesstoken) + +''' +@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.info +@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.deleteRecord +''' +def api_macro_delete( + environment : str, + username : str, + password : str, + domain : str, + name : str, + type_ +): + accesstoken = api_macro_login(environment, username, password) + info = api_call( + environment, + accesstoken, + "nameserver", + "info", + { + "domain": domain, + } + ) + matching = list( + filter( + lambda record: ( + (record["name"] == (name + "." + domain)) + and + ( + (type_ is None) + or + (record["type"] == type_) + ) + ), + info["record"] + ) + ) + for entry in matching: + id_ = entry["id"] + result = api_call( + environment, + accesstoken, + "nameserver", + "deleteRecord", + { + "id": id_, + } + ) + api_macro_logout(environment, accesstoken) + diff --git a/source/main.py b/source/main.py index 2e3656c..4ca10b5 100644 --- a/source/main.py +++ b/source/main.py @@ -66,9 +66,9 @@ def args( argumentparser.add_argument( "action", type = str, - choices = ["info", "list", "save", "certbot-hook"], + choices = ["info", "list", "save", "delete", "certbot-hook"], metavar = "", - help = "action to execute", + help = "action to execute; options: info,list,save,delete,certbot-hook", ) argumentparser.add_argument( "parameter", @@ -121,6 +121,18 @@ def main( content ) # print(_json.dumps(result, indent = "\t")) + elif (arguments.action == "delete"): + domain = arguments.parameter[0] + name = arguments.parameter[1] + type_ = (arguments.parameter[2] if (len(arguments.parameter) >= 2) else None) + api_macro_delete( + conf_get("environment"), + conf_get("account.username"), + conf_get("account.password"), + domain, + name, + type_ + ) elif (arguments.action == "certbot-hook"): domain_full_parts = _os.environ["CERTBOT_DOMAIN"].split(".") account = ".".join(domain_full_parts[-2:])