Mostly working, except update
This commit is contained in:
parent
50db3b7deb
commit
acd53fdcfa
1 changed files with 62 additions and 55 deletions
117
scripts/knotctl
117
scripts/knotctl
|
@ -37,19 +37,19 @@ def output(response: Sequence[dict], jsonout: bool = False):
|
||||||
if jsonout:
|
if jsonout:
|
||||||
print(json.dumps(response))
|
print(json.dumps(response))
|
||||||
else:
|
else:
|
||||||
for entry in response:
|
print(nested_out(response))
|
||||||
print(nested_out(entry))
|
|
||||||
except BrokenPipeError:
|
except BrokenPipeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def nested_out(input, tabs="") -> str:
|
def nested_out(input, tabs="") -> str:
|
||||||
string = ""
|
string = ""
|
||||||
if isinstance(input, str):
|
if isinstance(input, str) or isinstance(input, int):
|
||||||
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, nested_out(value, tabs + " "))
|
string += "{}{}: {}".format(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 + " "))
|
||||||
|
@ -58,6 +58,7 @@ def nested_out(input, tabs="") -> str:
|
||||||
|
|
||||||
# Define the parser for each command
|
# Define the parser for each command
|
||||||
def run_add(url: str, jsonout: bool, headers: dict):
|
def run_add(url: str, jsonout: bool, headers: dict):
|
||||||
|
print(url)
|
||||||
response = requests.put(url, headers=headers)
|
response = requests.put(url, headers=headers)
|
||||||
output(response.json(), jsonout)
|
output(response.json(), jsonout)
|
||||||
|
|
||||||
|
@ -77,8 +78,8 @@ def run_config(
|
||||||
for need in needed:
|
for need in needed:
|
||||||
if need == "":
|
if need == "":
|
||||||
output(
|
output(
|
||||||
error("Can not configure without {}".format(need), "No {}".format(need))
|
error("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))
|
||||||
|
|
||||||
|
@ -95,7 +96,11 @@ def run_config(
|
||||||
|
|
||||||
def run_delete(url: str, jsonout: bool, headers: dict):
|
def run_delete(url: str, jsonout: bool, headers: dict):
|
||||||
response = requests.delete(url, headers=headers)
|
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):
|
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
|
# Set up the url
|
||||||
def setup_url(
|
def setup_url(
|
||||||
baseurl: str,
|
baseurl: str,
|
||||||
data: Union[None, Sequence[str]],
|
data: Union[None, str],
|
||||||
name: Union[None, str],
|
name: Union[None, str],
|
||||||
rtype: Union[None, str],
|
rtype: Union[None, str],
|
||||||
|
ttl: Union[None, str],
|
||||||
zone: Union[None, str],
|
zone: Union[None, str],
|
||||||
) -> str:
|
) -> str:
|
||||||
url = baseurl + "/zones"
|
url = baseurl + "/zones"
|
||||||
|
@ -124,13 +130,19 @@ def setup_url(
|
||||||
if zone and name and rtype:
|
if zone and name and rtype:
|
||||||
url += "/{}".format(rtype)
|
url += "/{}".format(rtype)
|
||||||
if data and zone and name and rtype:
|
if data and zone and name and rtype:
|
||||||
url += "?"
|
url += "/{}".format(data)
|
||||||
for arg in data:
|
if ttl and data and zone and name and rtype:
|
||||||
if not url.endswith("?"):
|
url += "/{}".format(ttl)
|
||||||
url += "&"
|
|
||||||
url += urllib.parse.quote_plus(arg)
|
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):
|
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)
|
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"))
|
||||||
|
@ -149,10 +161,10 @@ def main() -> int:
|
||||||
print("You need to configure knotctl before proceeding")
|
print("You need to configure knotctl before proceeding")
|
||||||
run_config(config_filename)
|
run_config(config_filename)
|
||||||
|
|
||||||
config = get_config(config_filename)
|
configcmd = get_config(config_filename)
|
||||||
baseurl = config["baseurl"]
|
baseurl = configcmd["baseurl"]
|
||||||
username = config["username"]
|
username = configcmd["username"]
|
||||||
password = config["password"]
|
password = configcmd["password"]
|
||||||
|
|
||||||
# Authenticate
|
# Authenticate
|
||||||
basic = HTTPBasicAuth(username, password)
|
basic = HTTPBasicAuth(username, password)
|
||||||
|
@ -168,50 +180,45 @@ def main() -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("--json", action=argparse.BooleanOptionalAction)
|
parser.add_argument("--json", action=argparse.BooleanOptionalAction)
|
||||||
subparsers = parser.add_subparsers(dest="command")
|
subparsers = parser.add_subparsers(dest="command")
|
||||||
for sub in ["add", "delete"]:
|
addcmd = subparsers.add_parser("add")
|
||||||
subparser = subparsers.add_parser(sub)
|
addcmd.add_argument("-d", "--data", required=True)
|
||||||
subparser.add_argument(
|
addcmd.add_argument("-n", "--name", required=True)
|
||||||
"-d",
|
addcmd.add_argument("-r", "--rtype", required=True)
|
||||||
"--data",
|
addcmd.add_argument("-t", "--ttl", required=True)
|
||||||
nargs="*",
|
addcmd.add_argument("-z", "--zone", required=True)
|
||||||
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")
|
configcmd = subparsers.add_parser("config")
|
||||||
config.add_argument("-b", "--baseurl")
|
configcmd.add_argument("-b", "--baseurl")
|
||||||
config.add_argument("-p", "--password")
|
configcmd.add_argument("-p", "--password")
|
||||||
config.add_argument("-u", "--username")
|
configcmd.add_argument("-u", "--username")
|
||||||
|
|
||||||
update = subparsers.add_parser("update")
|
deletecmd = subparsers.add_parser("delete")
|
||||||
update.add_argument(
|
deletecmd.add_argument("-d", "--data")
|
||||||
"-d",
|
deletecmd.add_argument("-n", "--name")
|
||||||
"--data",
|
deletecmd.add_argument("-r", "--rtype")
|
||||||
nargs="*",
|
deletecmd.add_argument("-z", "--zone", required=True)
|
||||||
help="Specify any number of key - value pairs: name=dns1.example.com.",
|
|
||||||
required=True,
|
listcmd = subparsers.add_parser("list")
|
||||||
)
|
listcmd.add_argument("-d", "--data")
|
||||||
update.add_argument("-n", "--name", required=True)
|
listcmd.add_argument("-n", "--name")
|
||||||
update.add_argument("-r", "--rtype", required=True)
|
listcmd.add_argument("-r", "--rtype")
|
||||||
update.add_argument("-z", "--zone", required=True)
|
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)
|
argcomplete.autocomplete(parser)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Route based on command
|
# 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":
|
if args.command == "add":
|
||||||
run_add(url, args.json, headers)
|
run_add(url, args.json, headers)
|
||||||
elif args.command == "config":
|
elif args.command == "config":
|
||||||
|
|
Loading…
Add table
Reference in a new issue