123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- # This script requires:
- # pip install --upgrade google google-cloud-dns
- # Thanks for using this software! DL.
- import sys, getopt, time
- try:
- import google
- from google.cloud import dns
- except ImportError:
- print('\nYou must install google and google-cloud-dns modules to run this script. You may run:\n')
- print('pip install --upgrade google google-cloud-dns\n')
- sys.exit(2)
- # Globals
- google_client = None
- app_subdomains = ['base', 'radius', 'cablemodem', 'ftth', 'dhcp', 'mapas', 'stats', 'grafana', 'geoserver', 'api', 'swagger', 'pma']
- def main(argv):
- global google_client
- json_path = action = client = ip_address = domain = opts = args = None
- try:
- opts, args = getopt.getopt(argv, None, ["key=", "action=", "client=", "ip_address=", "domain="])
- except getopt.GetoptError:
- print_usage()
- sys.exit(2)
- for opt, arg in opts:
- if opt in ('--key'):
- json_path = arg
- if opt in ('--action'):
- action = arg
- if opt in ('--client'):
- client = arg
- if opt in ('--ip_address'):
- ip_address = arg
- if opt in ('--domain'):
- domain = arg
- if not json_path:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nERROR: You must provide a path to the JSON Key File! Check documentation for further assistance.'+
- bcolors.ENDC + bcolors.ENDC)
- print_usage()
- sys.exit(2)
- google_client = dns.Client.from_service_account_json(json_path)
- if not google_client:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nERROR: Failed to create Google Client object.\n'+
- bcolors.ENDC + bcolors.ENDC)
- sys.exit(2)
- if action == 'create':
- if not client or not ip_address or not domain:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nERROR: create requires client, ip_address and domain!\n'+
- bcolors.ENDC + bcolors.ENDC)
- print_usage()
- sys.exit(2)
- if create(client=client, ip_address=ip_address, domain=domain):
- print(bcolors.OKGREEN + bcolors.BOLD +
- '\nEntities have been successfully created!\n' +
- bcolors.ENDC + bcolors.ENDC)
- elif action == 'delete':
- if not client or not ip_address or not domain:
- print(bcolors.FAIL + bcolors.BOLD +
- '\nERROR: delete requires client and domain!\n'+
- bcolors.ENDC + bcolors.ENDC)
- print_usage()
- sys.exit(2)
- if delete(client=client, domain=domain):
- print(bcolors.OKGREEN + bcolors.BOLD +
- '\nEntities have been successfully deleted!\n' +
- bcolors.ENDC + bcolors.ENDC)
- elif action == 'list':
- list()
- def list():
- zone = google_client.zone('flowdatnet')
- records = zone.list_resource_record_sets() # API request
- for record in records:
- print(record.name, record.record_type, record.ttl, record.rrdatas)
- return True
- def create(client=None, ip_address=None, domain='flowdat.net'):
- zone = google_client.zone('flowdatnet')
- changes = zone.changes()
- # Add "A" records for domain
- record_set = zone.resource_record_set('{}.{}.'.format(client, domain), 'A', '3600', ip_address)
- changes.add_record_set(record_set)
- # Add "A" records for subdomains
- for subdomain in app_subdomains:
- record_set = zone.resource_record_set('{}.{}.{}.'.format(subdomain, client, domain), 'A', '3600', ip_address)
- changes.add_record_set(record_set)
- try:
- changes.create()
- except google.api_core.exceptions.Conflict as e:
- print(bcolors.FAIL + bcolors.BOLD +
- "\nError: Entity already exist. Please use the delete action to make sure there are no existing records and try again.\n" +
- bcolors.ENDC + bcolors.ENDC)
- print("{}\n".format(e))
- sys.exit(2)
- return True
- def delete(client=None, domain='flowdat.net'):
- zone = google_client.zone('flowdatnet')
- changes = zone.changes()
- records = []
- all_records = zone.list_resource_record_sets()
- for record in all_records:
- # Match "A" record for main domain
- if record.name == '{}.{}.'.format(client, domain):
- records.append(record)
- continue
- # Match "A" records for subdomains
- for subdomain in app_subdomains:
- if record.name == '{}.{}.{}.'.format(subdomain, client, domain):
- records.append(record)
- break
- for record in records:
- changes.delete_record_set(record)
- try:
- changes.create()
- except ValueError as e:
- print(bcolors.OKBLUE + bcolors.BOLD +
- '\nNothing to change.\n' +
- bcolors.ENDC + bcolors.ENDC)
- return False
- return True
- def print_usage():
- print("\n")
- print(bcolors.HEADER + "-- Usage --" + bcolors.ENDC)
- print(
- """
- python googledns.py --key=<Path to JSON Key File> --action=create --client=<Client Name> --ip_address=<IP Address> --domain=<Domain>
- python googledns.py --key=<Path to JSON Key File> --action=delete --client=<Client Name> --domain=<Domain>
- python googledns.py --key=<Path to JSON Key File> --action=list
- """)
- class bcolors:
- HEADER = '\033[95m'
- OKBLUE = '\033[94m'
- OKGREEN = '\033[92m'
- WARNING = '\033[93m'
- FAIL = '\033[91m'
- ENDC = '\033[0m'
- BOLD = '\033[1m'
- UNDERLINE = '\033[4m'
- if __name__ == "__main__":
- main(sys.argv[1:])
|