You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pdnsapi.py 2.8KB

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