|
|
@ -9,6 +9,7 @@ import urllib.parse
|
|
|
|
from os import environ, mkdir
|
|
|
|
from os import environ, mkdir
|
|
|
|
from os.path import isdir, isfile, join
|
|
|
|
from os.path import isdir, isfile, join
|
|
|
|
from typing import Union
|
|
|
|
from typing import Union
|
|
|
|
|
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
|
|
|
|
|
|
|
import argcomplete
|
|
|
|
import argcomplete
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
@ -61,9 +62,18 @@ def output(response: list[dict], jsonout: bool = False):
|
|
|
|
|
|
|
|
|
|
|
|
# Define the runner for each command
|
|
|
|
# Define the runner for each command
|
|
|
|
def run_add(url: str, jsonout: bool, headers: dict):
|
|
|
|
def run_add(url: str, jsonout: bool, headers: dict):
|
|
|
|
print(url)
|
|
|
|
parsed = split_url(url)
|
|
|
|
response = requests.put(url, headers=headers)
|
|
|
|
response = requests.put(url, headers=headers)
|
|
|
|
output(response.json(), jsonout)
|
|
|
|
out = response.json()
|
|
|
|
|
|
|
|
if isinstance(out, list):
|
|
|
|
|
|
|
|
for record in out:
|
|
|
|
|
|
|
|
if (record["data"] == parsed["data"]
|
|
|
|
|
|
|
|
and record["name"] == parsed["name"]
|
|
|
|
|
|
|
|
and record["rtype"] == parsed["rtype"]):
|
|
|
|
|
|
|
|
output(record, jsonout)
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
output(out, jsonout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_complete(shell: Union[None, str]):
|
|
|
|
def run_complete(shell: Union[None, str]):
|
|
|
@ -90,10 +100,12 @@ 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))
|
|
|
|
|
|
|
|
|
|
|
|
if not password:
|
|
|
|
if not password:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
@ -165,13 +177,17 @@ 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"))
|
|
|
@ -179,6 +195,38 @@ def setup_url(
|
|
|
|
return url
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def split_url(url: str) -> dict:
|
|
|
|
|
|
|
|
parsed = urlparse(url, allow_fragments=False)
|
|
|
|
|
|
|
|
path = parsed.path
|
|
|
|
|
|
|
|
query = parsed.query
|
|
|
|
|
|
|
|
arguments: Union[None, list[str]] = query.split("&")
|
|
|
|
|
|
|
|
path_arr = path.split("/")
|
|
|
|
|
|
|
|
data: Union[None, str] = None
|
|
|
|
|
|
|
|
name: Union[None, str] = None
|
|
|
|
|
|
|
|
rtype: Union[None, str] = None
|
|
|
|
|
|
|
|
ttl: Union[None, str] = None
|
|
|
|
|
|
|
|
zone: Union[None, str] = None
|
|
|
|
|
|
|
|
if len(path_arr) > 2:
|
|
|
|
|
|
|
|
zone = path_arr[2]
|
|
|
|
|
|
|
|
if len(path_arr) > 4:
|
|
|
|
|
|
|
|
name = path_arr[4]
|
|
|
|
|
|
|
|
if len(path_arr) > 5:
|
|
|
|
|
|
|
|
rtype = path_arr[5]
|
|
|
|
|
|
|
|
if len(path_arr) > 6:
|
|
|
|
|
|
|
|
data = path_arr[6]
|
|
|
|
|
|
|
|
if len(path_arr) > 7:
|
|
|
|
|
|
|
|
ttl = path_arr[7]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
"arguments": arguments,
|
|
|
|
|
|
|
|
"data": data,
|
|
|
|
|
|
|
|
"name": name,
|
|
|
|
|
|
|
|
"rtype": rtype,
|
|
|
|
|
|
|
|
"ttl": ttl,
|
|
|
|
|
|
|
|
"zone": zone,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Entry point to program
|
|
|
|
# Entry point to program
|
|
|
|
def main() -> int:
|
|
|
|
def main() -> int:
|
|
|
|
# Grab user input
|
|
|
|
# Grab user input
|
|
|
@ -240,7 +288,7 @@ def main() -> int:
|
|
|
|
mkdir(config_basepath)
|
|
|
|
mkdir(config_basepath)
|
|
|
|
|
|
|
|
|
|
|
|
if args.command == "config":
|
|
|
|
if args.command == "config":
|
|
|
|
run_config(args.baseurl, args.username, args.password)
|
|
|
|
run_config(config_filename, args.baseurl, args.username, args.password)
|
|
|
|
return 0
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|
|
|
if not isfile(config_filename):
|
|
|
|
if not isfile(config_filename):
|
|
|
@ -260,27 +308,37 @@ def main() -> int:
|
|
|
|
except KeyError:
|
|
|
|
except KeyError:
|
|
|
|
output(response.json())
|
|
|
|
output(response.json())
|
|
|
|
return 1
|
|
|
|
return 1
|
|
|
|
|
|
|
|
except requests.exceptions.JSONDecodeError:
|
|
|
|
|
|
|
|
output(
|
|
|
|
|
|
|
|
error("Could not decode api response as JSON", "Could not decode"))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
headers = {"Authorization": "Bearer {}".format(token)}
|
|
|
|
headers = {"Authorization": "Bearer {}".format(token)}
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|