googledns.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # This script requires:
  2. # pip install --upgrade google google-cloud-dns
  3. # Thanks for using this software! DL.
  4. import sys, getopt, time
  5. try:
  6. import google
  7. from google.cloud import dns
  8. except ImportError:
  9. print('\nYou must install google and google-cloud-dns modules to run this script. You may run:\n')
  10. print('pip install --upgrade google google-cloud-dns\n')
  11. sys.exit(2)
  12. # Globals
  13. google_client = None
  14. app_subdomains = ['base', 'radius', 'cablemodem', 'ftth', 'dhcp', 'mapas', 'stats', 'grafana', 'geoserver', 'api', 'swagger', 'pma']
  15. def main(argv):
  16. global google_client
  17. json_path = action = client = ip_address = domain = opts = args = None
  18. try:
  19. opts, args = getopt.getopt(argv, None, ["key=", "action=", "client=", "ip_address=", "domain="])
  20. except getopt.GetoptError:
  21. print_usage()
  22. sys.exit(2)
  23. for opt, arg in opts:
  24. if opt in ('--key'):
  25. json_path = arg
  26. if opt in ('--action'):
  27. action = arg
  28. if opt in ('--client'):
  29. client = arg
  30. if opt in ('--ip_address'):
  31. ip_address = arg
  32. if opt in ('--domain'):
  33. domain = arg
  34. if not json_path:
  35. print(bcolors.FAIL + bcolors.BOLD +
  36. '\nERROR: You must provide a path to the JSON Key File! Check documentation for further assistance.'+
  37. bcolors.ENDC + bcolors.ENDC)
  38. print_usage()
  39. sys.exit(2)
  40. google_client = dns.Client.from_service_account_json(json_path)
  41. if not google_client:
  42. print(bcolors.FAIL + bcolors.BOLD +
  43. '\nERROR: Failed to create Google Client object.\n'+
  44. bcolors.ENDC + bcolors.ENDC)
  45. sys.exit(2)
  46. if action == 'create':
  47. if not client or not ip_address or not domain:
  48. print(bcolors.FAIL + bcolors.BOLD +
  49. '\nERROR: create requires client, ip_address and domain!\n'+
  50. bcolors.ENDC + bcolors.ENDC)
  51. print_usage()
  52. sys.exit(2)
  53. if create(client=client, ip_address=ip_address, domain=domain):
  54. print(bcolors.OKGREEN + bcolors.BOLD +
  55. '\nEntities have been successfully created!\n' +
  56. bcolors.ENDC + bcolors.ENDC)
  57. elif action == 'delete':
  58. if not client or not ip_address or not domain:
  59. print(bcolors.FAIL + bcolors.BOLD +
  60. '\nERROR: delete requires client and domain!\n'+
  61. bcolors.ENDC + bcolors.ENDC)
  62. print_usage()
  63. sys.exit(2)
  64. if delete(client=client, domain=domain):
  65. print(bcolors.OKGREEN + bcolors.BOLD +
  66. '\nEntities have been successfully deleted!\n' +
  67. bcolors.ENDC + bcolors.ENDC)
  68. elif action == 'list':
  69. list()
  70. def list():
  71. zone = google_client.zone('flowdatnet')
  72. records = zone.list_resource_record_sets() # API request
  73. for record in records:
  74. print(record.name, record.record_type, record.ttl, record.rrdatas)
  75. return True
  76. def create(client=None, ip_address=None, domain='flowdat.net'):
  77. zone = google_client.zone('flowdatnet')
  78. changes = zone.changes()
  79. # Add "A" records for domain
  80. record_set = zone.resource_record_set('{}.{}.'.format(client, domain), 'A', '3600', ip_address)
  81. changes.add_record_set(record_set)
  82. # Add "A" records for subdomains
  83. for subdomain in app_subdomains:
  84. record_set = zone.resource_record_set('{}.{}.{}.'.format(subdomain, client, domain), 'A', '3600', ip_address)
  85. changes.add_record_set(record_set)
  86. try:
  87. changes.create()
  88. except google.api_core.exceptions.Conflict as e:
  89. print(bcolors.FAIL + bcolors.BOLD +
  90. "\nError: Entity already exist. Please use the delete action to make sure there are no existing records and try again.\n" +
  91. bcolors.ENDC + bcolors.ENDC)
  92. print("{}\n".format(e))
  93. sys.exit(2)
  94. return True
  95. def delete(client=None, domain='flowdat.net'):
  96. zone = google_client.zone('flowdatnet')
  97. changes = zone.changes()
  98. records = []
  99. all_records = zone.list_resource_record_sets()
  100. for record in all_records:
  101. # Match "A" record for main domain
  102. if record.name == '{}.{}.'.format(client, domain):
  103. records.append(record)
  104. continue
  105. # Match "A" records for subdomains
  106. for subdomain in app_subdomains:
  107. if record.name == '{}.{}.{}.'.format(subdomain, client, domain):
  108. records.append(record)
  109. break
  110. for record in records:
  111. changes.delete_record_set(record)
  112. try:
  113. changes.create()
  114. except ValueError as e:
  115. print(bcolors.OKBLUE + bcolors.BOLD +
  116. '\nNothing to change.\n' +
  117. bcolors.ENDC + bcolors.ENDC)
  118. return False
  119. return True
  120. def print_usage():
  121. print("\n")
  122. print(bcolors.HEADER + "-- Usage --" + bcolors.ENDC)
  123. print(
  124. """
  125. python googledns.py --key=<Path to JSON Key File> --action=create --client=<Client Name> --ip_address=<IP Address> --domain=<Domain>
  126. python googledns.py --key=<Path to JSON Key File> --action=delete --client=<Client Name> --domain=<Domain>
  127. python googledns.py --key=<Path to JSON Key File> --action=list
  128. """)
  129. class bcolors:
  130. HEADER = '\033[95m'
  131. OKBLUE = '\033[94m'
  132. OKGREEN = '\033[92m'
  133. WARNING = '\033[93m'
  134. FAIL = '\033[91m'
  135. ENDC = '\033[0m'
  136. BOLD = '\033[1m'
  137. UNDERLINE = '\033[4m'
  138. if __name__ == "__main__":
  139. main(sys.argv[1:])