pull/7/head
Micke Nordin 2 years ago
parent d5902b57be
commit 570b6e0ff8
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -41,8 +41,9 @@ def nested_out(input, tabs="") -> str:
string += "{}\n".format(input) string += "{}\n".format(input)
elif isinstance(input, dict): elif isinstance(input, dict):
for key, value in input.items(): for key, value in input.items():
string += "{}{}: {}".format(tabs, key, string += "{}{}: {}".format(
nested_out(value, tabs + " ")) tabs, key, nested_out(value, tabs + " ")
)
elif isinstance(input, list): elif isinstance(input, list):
for entry in input: for entry in input:
string += "{}\n{}".format(tabs, nested_out(entry, tabs + " ")) string += "{}\n{}".format(tabs, nested_out(entry, tabs + " "))
@ -90,8 +91,11 @@ def run_config(
for need in needed: for need in needed:
if need == "": if need == "":
output( output(
error("Can not configure without {}".format(need), error(
"No {}".format(need))) "Can not configure without {}".format(need),
"No {}".format(need),
)
)
sys.exit(1) sys.exit(1)
config[need] = input("Enter {}:".format(need)) config[need] = input("Enter {}:".format(need))
@ -115,10 +119,9 @@ def run_delete(url: str, jsonout: bool, headers: dict):
output(reply, jsonout) output(reply, jsonout)
def run_list(url: str, def run_list(
jsonout: bool, url: str, jsonout: bool, headers: dict, ret=False
headers: dict, ) -> Union[None, str]:
ret=False) -> Union[None, str]:
response = requests.get(url, headers=headers) response = requests.get(url, headers=headers)
string = response.json() string = response.json()
if ret: if ret:
@ -165,13 +168,19 @@ def setup_url(
if ttl and (not rtype or not name or not zone): if ttl and (not rtype or not name or not zone):
output( output(
error("ttl only makes sense with rtype, name and zone", error(
"Missing parameter")) "ttl only makes sense with rtype, name and zone",
"Missing parameter",
)
)
sys.exit(1) sys.exit(1)
if rtype and (not name or not zone): if rtype and (not name or not zone):
output( output(
error("rtype only makes sense with name and zone", error(
"Missing parameter")) "rtype only makes sense with name and zone",
"Missing parameter",
)
)
sys.exit(1) sys.exit(1)
if name and not zone: if name and not zone:
output(error("name only makes sense with a zone", "Missing parameter")) output(error("name only makes sense with a zone", "Missing parameter"))
@ -264,23 +273,29 @@ def main() -> int:
# Route based on command # Route based on command
ttl = None ttl = None
if 'ttl' in args: if "ttl" in args:
ttl = args.ttl ttl = args.ttl
if args.command != "update": if args.command != "update":
args.argument = None args.argument = None
if args.command == "add" and not ttl: if args.command == "add" and not ttl:
if args.zone.endswith('.'): if args.zone.endswith("."):
zname = args.zone zname = args.zone
else: else:
zname = args.zone + '.' zname = args.zone + "."
soa_url = setup_url(baseurl, None, None, zname, "SOA", None, soa_url = setup_url(baseurl, None, None, zname, "SOA", None, args.zone)
args.zone)
soa_json = run_list(soa_url, True, headers, ret=True) soa_json = run_list(soa_url, True, headers, ret=True)
ttl = soa_json[0]["ttl"] ttl = soa_json[0]["ttl"]
try: try:
url = setup_url(baseurl, args.argument, args.data, args.name, url = setup_url(
args.rtype, ttl, args.zone) baseurl,
args.argument,
args.data,
args.name,
args.rtype,
ttl,
args.zone,
)
except AttributeError: except AttributeError:
parser.print_help(sys.stderr) parser.print_help(sys.stderr)
return 1 return 1
@ -296,7 +311,8 @@ def main() -> int:
run_update(url, args.json, headers) run_update(url, args.json, headers)
except (RequestsJSONDecodeError, SimplejsonJSONDecodeError): except (RequestsJSONDecodeError, SimplejsonJSONDecodeError):
output( output(
error("Could not decode api response as JSON", "Could not decode")) error("Could not decode api response as JSON", "Could not decode")
)
return 0 return 0

Loading…
Cancel
Save