inwx/source/macros.py

232 lines
3.9 KiB
Python
Raw Permalink Normal View History

2024-05-31 09:28:32 +02:00
'''
@see https://www.inwx.de/de/help/apidoc/f/ch02.html#account.login
'''
def api_macro_login(
environment : str,
username : str,
password : str
):
if ((username is None) or (password is None)):
raise ValueError("username or password not given")
else:
response = (
api_call(
environment,
None,
"account",
"login",
{
"user": username,
"pass": password,
}
)
)
return response["_accesstoken"]
2024-05-31 09:28:32 +02:00
'''
@see https://www.inwx.de/de/help/apidoc/f/ch02.html#account.logout
'''
def api_macro_logout(
environment : str,
accesstoken : str
):
response = api_call(
environment,
accesstoken,
"account",
"logout",
{
}
)
return None
2024-05-31 09:28:32 +02:00
'''
@see https://www.inwx.de/de/help/apidoc/f/ch02.html#account.info
'''
def api_macro_info(
environment : str,
username : str,
password : str
):
accesstoken = api_macro_login(environment, username, password)
info = api_call(
environment,
accesstoken,
"account",
"info",
{
}
)
api_macro_logout(environment, accesstoken)
return info
2024-05-31 09:28:32 +02:00
'''
@see https://www.inwx.de/de/help/apidoc/f/ch02s15.html#nameserver.info
'''
def api_macro_list(
environment : str,
username : str,
password : str,
domain : str
):
accesstoken = api_macro_login(environment, username, password)
info = api_call(
environment,
accesstoken,
"nameserver",
"info",
{
"domain": domain,
}
)
api_macro_logout(environment, accesstoken)
return info
2024-05-31 09:28:32 +02:00
'''
@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,
password : str,
2024-05-31 14:06:58 +02:00
domain_base : str,
domain_path,
type_ : str,
content : str
):
accesstoken = api_macro_login(environment, username, password)
info = api_call(
environment,
accesstoken,
"nameserver",
"info",
{
2024-05-31 14:06:58 +02:00
"domain": domain_base,
}
)
matching = list(
filter(
2024-05-31 14:06:58 +02:00
lambda record: (
(
(
(domain_path is None)
and
2024-06-01 18:41:13 +02:00
(record["name"] == domain_base)
2024-05-31 14:06:58 +02:00
)
or
(
(domain_path is not None)
and
(record["name"] == (domain_path + "." + domain_base))
)
)
and
(record["type"] == type_)
),
info["record"]
)
)
count = len(matching)
if (count == 0):
result = api_call(
environment,
accesstoken,
"nameserver",
"createRecord",
{
2024-05-31 14:06:58 +02:00
"domain": domain_base,
"name": domain_path,
"type": type_,
"content": content,
}
)
id_ = result["id"]
log("created record %u" % id_)
elif (count == 1):
id_ = matching[0]["id"]
result = api_call(
environment,
accesstoken,
"nameserver",
"updateRecord",
{
"id": id_,
"content": content,
}
)
log("updated record %u" % id_)
else:
log("found multiple records with this name and type")
api_macro_logout(environment, accesstoken)
2024-05-31 09:28:32 +02:00
'''
@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,
2024-05-31 14:06:58 +02:00
domain_base : str,
domain_path,
2024-05-31 09:28:32 +02:00
type_
):
accesstoken = api_macro_login(environment, username, password)
info = api_call(
environment,
accesstoken,
"nameserver",
"info",
{
2024-05-31 14:06:58 +02:00
"domain": domain_base,
2024-05-31 09:28:32 +02:00
}
)
matching = list(
filter(
lambda record: (
2024-05-31 14:06:58 +02:00
(
(
(domain_path is None)
and
(record["name"] == domain_base)
)
or
(
(domain_path is not None)
and
(record["name"] == (domain_path + "." + domain_base))
)
)
2024-05-31 09:28:32 +02:00
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)
2024-05-31 14:06:58 +02:00