Working list

pull/7/head
Micke Nordin 2 years ago
commit d9b450e1ba
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -0,0 +1,132 @@
#!/usr/bin/env python3
import argparse
import urllib.parse
from collections.abc import Sequence
from typing import Union
import argcomplete
import requests
from argcomplete.completers import subprocess
from requests.models import HTTPBasicAuth
base_url = "https://knotapitest.sunet.se"
user = "kano"
password = subprocess.getoutput("pass show knotapitest.sunet.se/{}".format(user))
# Authenticate
basic = HTTPBasicAuth(user, password)
response = requests.get(base_url + "/user/login", auth=basic)
token = response.json()["token"]
headers = {"Authorization": "Bearer {}".format(token)}
# Parse out information for each command line option
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest="command")
add = subparsers.add_parser("add")
add.add_argument(
"-d",
"--data",
nargs="*",
help="Specify any number of key - value pairs: name=dns1.example.com.",
)
add.add_argument("--name")
add.add_argument("--rtype")
add.add_argument("--zone", required=True)
delete = subparsers.add_parser("delete")
delete.add_argument(
"--data",
nargs="*",
help="Specify any number of key - value pairs: name=dns1.example.com.",
)
delete.add_argument("--name")
delete.add_argument("--rtype")
delete.add_argument("--zone", required=True)
list = subparsers.add_parser("list")
list.add_argument(
"--data",
nargs="*",
help="Specify any number of key - value pairs: name=dns1.example.com.",
)
list.add_argument("--name")
list.add_argument("--rtype")
list.add_argument("--zone")
update = subparsers.add_parser("update")
update.add_argument(
"--data",
nargs="*",
help="Specify any number of key - value pairs: name=dns1.example.com.",
)
update.add_argument("--name")
update.add_argument("--rtype")
update.add_argument("--zone", required=True)
argcomplete.autocomplete(parser)
args = parser.parse_args()
# Define the parser for each command
def parse_add(
data: Union[None, Sequence[str]],
name: Union[None, str],
rtype: Union[None, str],
zone: Union[None, str],
):
print(args)
def parse_delete(
data: Union[None, Sequence[str]],
name: Union[None, str],
rtype: Union[None, str],
zone: Union[None, str],
):
print(args)
def parse_list(
data: Union[None, Sequence[str]],
name: Union[None, str],
rtype: Union[None, str],
zone: Union[None, str],
):
url = base_url + "/zones"
if zone:
url += '/{}'.format(zone)
if name and zone:
url += "/records/{}".format(name)
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)
print(url)
response = requests.get(url, headers=headers)
print(response.json())
def parse_update(
data: Union[None, Sequence[str]],
name: Union[None, str],
rtype: Union[None, str],
zone: Union[None, str],
):
print(args)
# Route based on command
if args.command == "add":
parse_add(args.data, args.name, args.rtype, args.zone)
elif args.command == "delete":
parse_delete(args.data, args.name, args.rtype, args.zone)
elif args.command == "list":
parse_list(args.data, args.name, args.rtype, args.zone)
elif args.command == "update":
parse_update(args.data, args.name, args.rtype, args.zone)
Loading…
Cancel
Save