import argparse from .azure import get_dns_nameservers from . import miab def azure_list_command(args): nameservers = get_dns_nameservers(args.resource_group, args.zone_name) print(f"Nameservers for zone {args.zone_name}:") for ns in nameservers: print(f" - {ns}") # Mail-in-a-Box API wrapper functions def miab_set_command(args): return miab.set_custom_dns(args.domain_name, args.record_type, args.record_value) def miab_delete_command(args): return miab.delete_custom_dns(args.domain_name, args.record_type) def miab_add_command(args): return miab.add_custom_dns(args.domain_name, args.record_type, args.record_value) def miab_list_command(args): records = miab.list_custom_dns(args.record_type) if args.record_type: print(f"Custom {args.record_type} records:") else: print("Custom DNS records:") for record in records: print(f" - {record['name']} ({record['type']}): {record['value']}") def miab_configure_command(args): # Get a list of nameservers for the specified Azure DNS zone nameservers = get_dns_nameservers(args.resource_group, args.zone_name) if not nameservers: print("No nameservers found. Aborting configuration.") return # Add each nameserver as an NS record in the Mail-in-a-Box DNS server print(f"Configuring Mail-in-a-Box with nameservers from Azure DNS zone {args.zone_name}...") for ns in nameservers: print(f"Adding NS record for {ns}...") success = miab.add_custom_dns(args.zone_name, "NS", ns) if success: print(f"Successfully added NS record for {ns}") else: print(f"Failed to add NS record for {ns}") if __name__ == "__main__": parser = argparse.ArgumentParser(description="DNS Configuration Tool") subparsers = parser.add_subparsers() # list command - lists nameservers for a given Azure DNS zone list_azure_parser = subparsers.add_parser("list-azure", help="List nameservers for an Azure DNS zone") list_azure_parser.add_argument("--resource-group", required=True, help="Resource group name") list_azure_parser.add_argument("--zone-name", required=True, help="DNS zone name") list_azure_parser.set_defaults(func=azure_list_command) # list-miab command - lists custom DNS records in the Mail-in-a-Box DNS server list_miab_parser = subparsers.add_parser("list-miab", help="List custom DNS records in the Mail-in-a-Box DNS server") list_miab_parser.add_argument("--record-type", help="Filter by DNS record type (e.g., A, CNAME, MX)") list_miab_parser.set_defaults(func=miab_list_command) # set command - sets a record in the Mail-in-a-Box DNS server set_parser = subparsers.add_parser("set", help="Set a DNS record in the Mail-in-a-Box DNS server") set_parser.add_argument("--domain-name", required=True, help="Domain name to set the record for") set_parser.add_argument("--record-type", required=True, help="DNS record type (e.g., A, CNAME, MX)") set_parser.add_argument("--record-value", required=True, help="Value for the DNS record") set_parser.set_defaults(func=miab_set_command) # delete command - deletes a record from the Mail-in-a-Box DNS server delete_parser = subparsers.add_parser("delete", help="Delete a DNS record from the Mail-in-a-Box DNS server") delete_parser.add_argument("--domain-name", required=True, help="Domain name to delete the record for") delete_parser.add_argument("--record-type", required=True, help="DNS record type (e.g., A, CNAME, MX)") delete_parser.add_argument("--record-value", help="Value for the DNS record") delete_parser.set_defaults(func=miab_delete_command) # add command - adds a record to the Mail-in-a-Box DNS server add_parser = subparsers.add_parser("add", help="Add a DNS record to the Mail-in-a-Box DNS server") add_parser.add_argument("--domain-name", required=True, help="Domain name to add the record for") add_parser.add_argument("--record-type", required=True, help="DNS record type (e.g., A, CNAME, MX)") add_parser.add_argument("--record-value", required=True, help="Value for the DNS record") add_parser.set_defaults(func=miab_add_command) # configure-miab - retrieves nameservers for an Azure DNS zone and configures them in the Mail-in-a-Box DNS server configure_miab_parser = subparsers.add_parser("configure-miab", help="Configure Mail-in-a-Box DNS server with nameservers from an Azure DNS zone") configure_miab_parser.add_argument("--resource-group", required=True, help="Azure Resource group name containing the DNS zone") configure_miab_parser.add_argument("--zone-name", required=True, help="DNS zone name") configure_miab_parser.set_defaults(func=miab_configure_command) args = parser.parse_args() args.func(args)