Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

pdnsapi.py 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import json
  2. import requests
  3. class PdnsApi:
  4. http_auth_user = None
  5. http_auth_pass = None
  6. api_key = None
  7. api_pass = None
  8. base_url = None
  9. def set_api_key(self, api_key):
  10. self.api_key = api_key
  11. def set_base_url(self, base_url):
  12. self.base_url = base_url
  13. def set_api_pass(self, api_pass):
  14. self.api_pass = api_pass
  15. def set_http_auth_user(self, http_auth_user):
  16. self.http_auth_user = http_auth_user
  17. def set_http_auth_pass(self, http_auth_pass):
  18. self.http_auth_pass = http_auth_pass
  19. def _query(self, uri, method, kwargs=None):
  20. headers = {
  21. 'X-API-Key': self.api_key,
  22. 'Accept': 'application/json',
  23. 'Content-Type': 'application/json'
  24. }
  25. data = json.dumps(kwargs)
  26. if method == "GET":
  27. request = requests.get(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass))
  28. elif method == "POST":
  29. request = requests.post(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass), data=data)
  30. elif method == "PUT":
  31. request = requests.put(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass), data=data)
  32. elif method == "PATCH":
  33. request = requests.patch(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass), data=data)
  34. elif method == "DELETE":
  35. request = requests.delete(self.base_url + uri, headers=headers, auth=(self.http_auth_user, self.http_auth_pass))
  36. else:
  37. raise ValueError("Invalid method '%s'" % method)
  38. return None if request.status_code == 204 else request.json()
  39. def list_zones(self):
  40. return self._query("/servers/localhost/zones", "GET")
  41. def get_zone(self, zone_name):
  42. return self._query("/servers/localhost/zones/%s" % zone_name, "GET")
  43. def update_zone(self, zone_name, data):
  44. return self._query("/servers/localhost/zones/%s" % zone_name, "PUT", data)
  45. def replace_record(self, zone_name, name, type, ttl, content, disabled, set_ptr):
  46. return self._query("/servers/localhost/zones/%s" % zone_name, "PATCH", {"rrsets": [
  47. {
  48. "name": name,
  49. "type": type,
  50. "ttl": ttl,
  51. "changetype": "REPLACE",
  52. "records": [
  53. {
  54. "content": content,
  55. "disabled": disabled,
  56. "set-prt": set_ptr
  57. }
  58. ]
  59. }
  60. ]})
  61. def delete_record(self, zone_name, name, type, ttl, content, disabled, set_ptr):
  62. return self._query("/servers/localhost/zones/%s" % zone_name, "PATCH", {"rrsets": [
  63. {
  64. "name": name,
  65. "type": type,
  66. "ttl": ttl,
  67. "changetype": "DELETE",
  68. "records": [
  69. {
  70. "content": content,
  71. "disabled": disabled,
  72. "set-prt": set_ptr
  73. }
  74. ]
  75. }
  76. ]})
  77. def notify_zone(self, zone_name):
  78. return self._query("/servers/localhost/zones/%s/notify" % zone_name, "PUT")
  79. def flush_zone_cache(self, zone_name):
  80. return self._query("/servers/localhost/cache/flush?domain=%s" % zone_name, "PUT")