Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import requests
  5. class PdnsApi:
  6. api_key = None
  7. base_url = None
  8. http_auth = None # Standard-value of requests-library will be used
  9. verify_cert = None # Standard-value of requests-library will be used
  10. def set_api_key(self, api_key):
  11. self.api_key = api_key
  12. def set_base_url(self, base_url):
  13. self.base_url = base_url
  14. def set_verify_cert(self, verify_cert):
  15. if verify_cert in ("True", "true", True): # convert from string to real bool if needed
  16. self.verify_cert = True
  17. elif verify_cert in ("False", "false", False): # convert from string to real bool if needed
  18. self.verify_cert = False
  19. elif isinstance(verify_cert, str): # alternative: path to local cert is given as string
  20. self.verify_cert = verify_cert # see requests-documentation for more info
  21. def set_http_auth(self, http_auth): # credentials should be given as list containing two string-elements
  22. if len(http_auth) == 2: # first: username, second: password for http-basic auth
  23. self.http_auth = (str(http_auth[0]), str(http_auth[1])) # ensure right format of credentials
  24. def _query(self, uri, method, kwargs=None):
  25. headers = {
  26. 'X-API-Key': self.api_key,
  27. 'Accept': 'application/json',
  28. 'Content-Type': 'application/json'
  29. }
  30. data = json.dumps(kwargs)
  31. if method == "GET":
  32. request = requests.get(self.base_url + uri, headers=headers,
  33. auth=self.http_auth, verify=self.verify_cert)
  34. elif method == "POST":
  35. request = requests.post(self.base_url + uri, headers=headers, data=data,
  36. auth=self.http_auth, verify=self.verify_cert)
  37. elif method == "PUT":
  38. request = requests.put(self.base_url + uri, headers=headers, data=data,
  39. auth=self.http_auth, verify=self.verify_cert)
  40. elif method == "PATCH":
  41. request = requests.patch(self.base_url + uri, headers=headers, data=data,
  42. auth=self.http_auth, verify=self.verify_cert)
  43. elif method == "DELETE":
  44. request = requests.delete(self.base_url + uri, headers=headers,
  45. auth=self.http_auth, verify=self.verify_cert)
  46. else:
  47. raise ValueError("Invalid method '%s'" % method)
  48. return None if request.status_code == 204 else request.json()
  49. def list_zones(self):
  50. return self._query("/servers/localhost/zones", "GET")
  51. def get_zone(self, zone_name):
  52. return self._query("/servers/localhost/zones/%s" % zone_name, "GET")
  53. def update_zone(self, zone_name, data):
  54. return self._query("/servers/localhost/zones/%s" % zone_name, "PUT", data)
  55. def replace_record(self, zone_name, name, type, ttl, content, disabled, set_ptr):
  56. return self._query("/servers/localhost/zones/%s" % zone_name, "PATCH", {"rrsets": [
  57. {
  58. "name": name,
  59. "type": type,
  60. "ttl": ttl,
  61. "changetype": "REPLACE",
  62. "records": [
  63. {
  64. "content": content,
  65. "disabled": disabled,
  66. "set-prt": set_ptr
  67. }
  68. ]
  69. }
  70. ]})
  71. def delete_record(self, zone_name, name, type, ttl, content, disabled, set_ptr):
  72. return self._query("/servers/localhost/zones/%s" % zone_name, "PATCH", {"rrsets": [
  73. {
  74. "name": name,
  75. "type": type,
  76. "ttl": ttl,
  77. "changetype": "DELETE",
  78. "records": [
  79. {
  80. "content": content,
  81. "disabled": disabled,
  82. "set-prt": set_ptr
  83. }
  84. ]
  85. }
  86. ]})
  87. def notify_zone(self, zone_name):
  88. return self._query("/servers/localhost/zones/%s/notify" % zone_name, "PUT")
  89. def flush_zone_cache(self, zone_name):
  90. return self._query("/servers/localhost/cache/flush?domain=%s" % zone_name, "PUT")