diff --git a/scripts/fqdn b/scripts/fqdn new file mode 100755 index 0000000..600cb50 --- /dev/null +++ b/scripts/fqdn @@ -0,0 +1,137 @@ +#!/usr/bin/env python3 + +import argparse +import json +import re + + +def assemble_drive( + environment, + function, + instance, + number, +): + domainname = "drive.sunet.se" + if environment != "prod": + domainname = f"drive.{environment}.sunet.se" + if instance not in ["common", "gss", "lookup", "multinode", "internal"]: + domainname = f"{instance}.{domainname}" + return f"{function}{number}.{domainname}" + + +def assemble_platform( + environment, + function, + instance, + location, + number, + service, +): + return f"{instance}-{location}-{environment}-{function}-{number}.{service}.sunet.se" + + +def parse_drive(fqdn: str) -> str: + type_regex = r"-*[1-9]*\..*" + function = re.sub(type_regex, "", fqdn) + + env_regex = r".*(pilot|test).*" + environment = re.sub(env_regex, r"\1", fqdn) + domainname = f"drive.{environment}.sunet.se" + + if environment not in ["pilot", "test"]: + environment = "prod" + domainname = "drive.sunet.se" + + instance = fqdn.split(".")[1] + if instance == "drive": + instance = "common" + elif function in ["gss", "lookup"]: + instance = function + elif function in ["gssbackup", "gss-db"]: + instance = "gss" + elif function in ["lookupbackup", "lookup-db"]: + instance = "lookup" + elif function.startswith("multinode") or function == "kube": + instance = "multinode" + + hostname = fqdn.split(".")[0] + number = hostname[-1] + location = "sto4" + if number == "3": + location = "sto3" + if number == "1": + location = "dco" + return json.dumps({ + "domainname": domainname, + "environment": environment, + "function": function, + "hostname": hostname, + "instance": instance, + "location": location, + "number": number, + "service": "drive", + }) + + +def parse_platform(fqdn) -> str: + domainname = ".".join([x for x in fqdn.split(".")[1:]]) + hostname = fqdn.split(".")[0] + instance, location, environment, function, number = hostname.split("-") + service = fqdn.split(".")[1] + return json.dumps({ + "domainname": domainname, + "environment": environment, + "function": function, + "hostname": hostname, + "instance": instance, + "location": location, + "number": number, + "service": service, + }) + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--flavor", "-f", type=str, default="platform") + subparsers = parser.add_subparsers(title="subcommands", dest="command") + parse = subparsers.add_parser(name="parse") + parse.add_argument("--fqdn", "-F", type=str, required=True) + assemble = subparsers.add_parser(name="assemble") + assemble.add_argument("--environment", "-e", type=str, default="") + assemble.add_argument("--function", "-F", type=str, default="") + assemble.add_argument("--instance", "-i", type=str, default="") + assemble.add_argument("--location", "-l", type=str, default="") + assemble.add_argument("--number", "-n", type=str, default="") + assemble.add_argument("--service", "-s", type=str, default="") + args = parser.parse_args() + flavor = args.flavor + command = args.command + if command == "parse": + if flavor == "drive": + print(parse_drive(args.fqdn)) + else: + print(parse_platform(args.fqdn)) + elif command == "assemble": + if flavor == "drive": + print( + assemble_drive( + args.environment, args.function, args.instance, args.number + ) + ) + else: + print( + assemble_platform( + args.environment, + args.function, + args.instance, + args.location, + args.number, + args.service, + ) + ) + else: + parser.print_help() + + +if __name__ == "__main__": + main()