|
|
|
@ -37,19 +37,19 @@ def output(response: Sequence[dict], jsonout: bool = False):
|
|
|
|
|
if jsonout:
|
|
|
|
|
print(json.dumps(response))
|
|
|
|
|
else:
|
|
|
|
|
for entry in response:
|
|
|
|
|
print(nested_out(entry))
|
|
|
|
|
print(nested_out(response))
|
|
|
|
|
except BrokenPipeError:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def nested_out(input, tabs="") -> str:
|
|
|
|
|
string = ""
|
|
|
|
|
if isinstance(input, str):
|
|
|
|
|
if isinstance(input, str) or isinstance(input, int):
|
|
|
|
|
string += "{}\n".format(input)
|
|
|
|
|
elif isinstance(input, dict):
|
|
|
|
|
for key, value in input.items():
|
|
|
|
|
string += "{}{}: {}".format(tabs, key, nested_out(value, tabs + " "))
|
|
|
|
|
string += "{}{}: {}".format(tabs, key,
|
|
|
|
|
nested_out(value, tabs + " "))
|
|
|
|
|
elif isinstance(input, list):
|
|
|
|
|
for entry in input:
|
|
|
|
|
string += "{}\n{}".format(tabs, nested_out(entry, tabs + " "))
|
|
|
|
@ -58,6 +58,7 @@ def nested_out(input, tabs="") -> str:
|
|
|
|
|
|
|
|
|
|
# Define the parser for each command
|
|
|
|
|
def run_add(url: str, jsonout: bool, headers: dict):
|
|
|
|
|
print(url)
|
|
|
|
|
response = requests.put(url, headers=headers)
|
|
|
|
|
output(response.json(), jsonout)
|
|
|
|
|
|
|
|
|
@ -77,8 +78,8 @@ def run_config(
|
|
|
|
|
for need in needed:
|
|
|
|
|
if need == "":
|
|
|
|
|
output(
|
|
|
|
|
error("Can not configure without {}".format(need), "No {}".format(need))
|
|
|
|
|
)
|
|
|
|
|
error("Can not configure without {}".format(need),
|
|
|
|
|
"No {}".format(need)))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
config[need] = input("Enter {}:".format(need))
|
|
|
|
|
|
|
|
|
@ -95,7 +96,11 @@ def run_config(
|
|
|
|
|
|
|
|
|
|
def run_delete(url: str, jsonout: bool, headers: dict):
|
|
|
|
|
response = requests.delete(url, headers=headers)
|
|
|
|
|
output(response.json(), jsonout)
|
|
|
|
|
reply = response.json()
|
|
|
|
|
if not reply and response.status_code == requests.codes.ok:
|
|
|
|
|
reply = [{"Code": 200, "Description": "{} deleted".format(url)}]
|
|
|
|
|
|
|
|
|
|
output(reply, jsonout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run_list(url: str, jsonout: bool, headers: dict):
|
|
|
|
@ -111,9 +116,10 @@ def run_update(url: str, jsonout: bool, headers: dict):
|
|
|
|
|
# Set up the url
|
|
|
|
|
def setup_url(
|
|
|
|
|
baseurl: str,
|
|
|
|
|
data: Union[None, Sequence[str]],
|
|
|
|
|
data: Union[None, str],
|
|
|
|
|
name: Union[None, str],
|
|
|
|
|
rtype: Union[None, str],
|
|
|
|
|
ttl: Union[None, str],
|
|
|
|
|
zone: Union[None, str],
|
|
|
|
|
) -> str:
|
|
|
|
|
url = baseurl + "/zones"
|
|
|
|
@ -124,13 +130,19 @@ def setup_url(
|
|
|
|
|
if zone and name and rtype:
|
|
|
|
|
url += "/{}".format(rtype)
|
|
|
|
|
if data and zone and name and rtype:
|
|
|
|
|
url += "?"
|
|
|
|
|
for arg in data:
|
|
|
|
|
if not url.endswith("?"):
|
|
|
|
|
url += "&"
|
|
|
|
|
url += urllib.parse.quote_plus(arg)
|
|
|
|
|
url += "/{}".format(data)
|
|
|
|
|
if ttl and data and zone and name and rtype:
|
|
|
|
|
url += "/{}".format(ttl)
|
|
|
|
|
|
|
|
|
|
if ttl and (not rtype or not name or not zone):
|
|
|
|
|
output(
|
|
|
|
|
error("ttl only makes sense with rtype, name and zone",
|
|
|
|
|
"Missing parameter"))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
if rtype and (not name or not zone):
|
|
|
|
|
output(error("rtype only makes sense with name and zone", "Missing parameter"))
|
|
|
|
|
output(
|
|
|
|
|
error("rtype only makes sense with name and zone",
|
|
|
|
|
"Missing parameter"))
|
|
|
|
|
sys.exit(1)
|
|
|
|
|
if name and not zone:
|
|
|
|
|
output(error("name only makes sense with a zone", "Missing parameter"))
|
|
|
|
@ -149,10 +161,10 @@ def main() -> int:
|
|
|
|
|
print("You need to configure knotctl before proceeding")
|
|
|
|
|
run_config(config_filename)
|
|
|
|
|
|
|
|
|
|
config = get_config(config_filename)
|
|
|
|
|
baseurl = config["baseurl"]
|
|
|
|
|
username = config["username"]
|
|
|
|
|
password = config["password"]
|
|
|
|
|
configcmd = get_config(config_filename)
|
|
|
|
|
baseurl = configcmd["baseurl"]
|
|
|
|
|
username = configcmd["username"]
|
|
|
|
|
password = configcmd["password"]
|
|
|
|
|
|
|
|
|
|
# Authenticate
|
|
|
|
|
basic = HTTPBasicAuth(username, password)
|
|
|
|
@ -168,50 +180,45 @@ def main() -> int:
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
parser.add_argument("--json", action=argparse.BooleanOptionalAction)
|
|
|
|
|
subparsers = parser.add_subparsers(dest="command")
|
|
|
|
|
for sub in ["add", "delete"]:
|
|
|
|
|
subparser = subparsers.add_parser(sub)
|
|
|
|
|
subparser.add_argument(
|
|
|
|
|
"-d",
|
|
|
|
|
"--data",
|
|
|
|
|
nargs="*",
|
|
|
|
|
help="Specify any number of key - value pairs: name=dns1.example.com.",
|
|
|
|
|
)
|
|
|
|
|
subparser.add_argument("-n", "--name")
|
|
|
|
|
subparser.add_argument("-r", "--rtype")
|
|
|
|
|
subparser.add_argument("-z", "--zone", required=True)
|
|
|
|
|
list = subparsers.add_parser("list")
|
|
|
|
|
list.add_argument(
|
|
|
|
|
"-d",
|
|
|
|
|
"--data",
|
|
|
|
|
nargs="*",
|
|
|
|
|
help="Specify any number of key - value pairs: name=dns1.example.com.",
|
|
|
|
|
)
|
|
|
|
|
list.add_argument("-n", "--name")
|
|
|
|
|
list.add_argument("-r", "--rtype")
|
|
|
|
|
list.add_argument("-z", "--zone")
|
|
|
|
|
|
|
|
|
|
config = subparsers.add_parser("config")
|
|
|
|
|
config.add_argument("-b", "--baseurl")
|
|
|
|
|
config.add_argument("-p", "--password")
|
|
|
|
|
config.add_argument("-u", "--username")
|
|
|
|
|
|
|
|
|
|
update = subparsers.add_parser("update")
|
|
|
|
|
update.add_argument(
|
|
|
|
|
"-d",
|
|
|
|
|
"--data",
|
|
|
|
|
nargs="*",
|
|
|
|
|
help="Specify any number of key - value pairs: name=dns1.example.com.",
|
|
|
|
|
required=True,
|
|
|
|
|
)
|
|
|
|
|
update.add_argument("-n", "--name", required=True)
|
|
|
|
|
update.add_argument("-r", "--rtype", required=True)
|
|
|
|
|
update.add_argument("-z", "--zone", required=True)
|
|
|
|
|
addcmd = subparsers.add_parser("add")
|
|
|
|
|
addcmd.add_argument("-d", "--data", required=True)
|
|
|
|
|
addcmd.add_argument("-n", "--name", required=True)
|
|
|
|
|
addcmd.add_argument("-r", "--rtype", required=True)
|
|
|
|
|
addcmd.add_argument("-t", "--ttl", required=True)
|
|
|
|
|
addcmd.add_argument("-z", "--zone", required=True)
|
|
|
|
|
|
|
|
|
|
configcmd = subparsers.add_parser("config")
|
|
|
|
|
configcmd.add_argument("-b", "--baseurl")
|
|
|
|
|
configcmd.add_argument("-p", "--password")
|
|
|
|
|
configcmd.add_argument("-u", "--username")
|
|
|
|
|
|
|
|
|
|
deletecmd = subparsers.add_parser("delete")
|
|
|
|
|
deletecmd.add_argument("-d", "--data")
|
|
|
|
|
deletecmd.add_argument("-n", "--name")
|
|
|
|
|
deletecmd.add_argument("-r", "--rtype")
|
|
|
|
|
deletecmd.add_argument("-z", "--zone", required=True)
|
|
|
|
|
|
|
|
|
|
listcmd = subparsers.add_parser("list")
|
|
|
|
|
listcmd.add_argument("-d", "--data")
|
|
|
|
|
listcmd.add_argument("-n", "--name")
|
|
|
|
|
listcmd.add_argument("-r", "--rtype")
|
|
|
|
|
listcmd.add_argument("-z", "--zone")
|
|
|
|
|
|
|
|
|
|
updatecmd = subparsers.add_parser("update")
|
|
|
|
|
updatecmd.add_argument("-d", "--data", required=True)
|
|
|
|
|
updatecmd.add_argument("-n", "--name", required=True)
|
|
|
|
|
updatecmd.add_argument("-r", "--rtype", required=True)
|
|
|
|
|
updatecmd.add_argument("-t", "--ttl", required=True)
|
|
|
|
|
updatecmd.add_argument("-z", "--zone", required=True)
|
|
|
|
|
|
|
|
|
|
argcomplete.autocomplete(parser)
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# Route based on command
|
|
|
|
|
url = setup_url(baseurl, args.data, args.name, args.rtype, args.zone)
|
|
|
|
|
ttl = None
|
|
|
|
|
if 'ttl' in args:
|
|
|
|
|
ttl = args.ttl
|
|
|
|
|
url = setup_url(baseurl, args.data, args.name, args.rtype, ttl, args.zone)
|
|
|
|
|
if args.command == "add":
|
|
|
|
|
run_add(url, args.json, headers)
|
|
|
|
|
elif args.command == "config":
|
|
|
|
|