Advertisement
Guest User

Untitled

a guest
May 2nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3.  
  4. from .api import Api
  5. from .gui import Gui
  6. from .check import Check
  7. from .maintenance import Maintenance
  8.  
  9.  
  10. class Client(object):
  11. """Interact with API and GUI."""
  12.  
  13. def __init__(self, username, password, apikey, email, api_version='2.1'):
  14. """
  15. Initializer.
  16.  
  17. :param username: account main email
  18. :param password: account password
  19. :param apikey: Pingdom api key
  20. :param email: required for `Multi-User Authentication
  21. <https://www.pingdom.com/resources/api#multi-user+authentication>`_.
  22. """
  23. self.username = username
  24. self.password = password
  25. self.apikey = apikey
  26. self.email = email
  27. self.api = Api(username, password, apikey, email, api_version)
  28. self.gui = Gui(username, password)
  29. # cache checks
  30. self.checks = {}
  31. for item in self.api.send('get', "checks", params={"include_tags": True})['checks']:
  32. self.checks[item["name"]] = Check(self.api, json=item)
  33.  
  34. def get_check(self, name=None, _id=None):
  35. if name:
  36. return self.checks.get(name, None)
  37. elif _id:
  38. for _name, check in self.checks.items():
  39. if check._id == _id:
  40. return check
  41. else:
  42. raise Exception("Missing name or _id")
  43.  
  44. def get_checks(self, filters=None):
  45. if filters is None:
  46. return [c for c in self.checks.values()]
  47. return [c for c in self.checks.values() if not len(set(filters.get("tags", [])).intersection(set([x['name']
  48. for x in c.tags]))) == 0]
  49.  
  50. def create_check(self, obj):
  51. c = Check(self.api, obj=obj)
  52. data = c.to_json()
  53. response = self.api.send(method='post', resource='checks', data=data)
  54. c._id = int(response["check"]["id"])
  55. c.from_json(self.api.send('get', "checks", response["check"]["id"])['check'])
  56. self.checks[c.name] = c
  57. return c
  58.  
  59. def delete_check(self, check):
  60. if not check._id:
  61. raise Exception("CheckNotFound %s" % check.name)
  62. self.api.send(method='delete', resource='checks', resource_id=check._id)
  63. self.checks.pop(check.name, None)
  64.  
  65. def update_check(self, check, changes):
  66. # ensure definition is updated
  67. check.fetch()
  68. # cache current definition to detect idempotence when modify is called
  69. cached_definition = check.to_json()
  70. check.from_obj(changes)
  71. data = check.to_json()
  72. if data == cached_definition:
  73. return False
  74. del data["type"] # type can't be changed
  75. self.api.send(method='put', resource='checks', resource_id=check._id, data=data)
  76. check.from_json(self.api.send('get', "checks", check._id)['check'])
  77. return check
  78.  
  79. def get_maintenances(self, filters=None):
  80. if filters is None:
  81. filters = {}
  82.  
  83. response = self.api.send(method='get', resource='maintenance')
  84. res = []
  85. for obj in response['maintenance']:
  86. if "checks" in filters:
  87. wanted_ids = [check._id for check in filters['checks']]
  88. got_ids = [int(x['compound_id']) for x in obj['checks']]
  89. if len(set(wanted_ids).intersection(set(got_ids))) == 0:
  90. continue
  91. window = Maintenance(self, json=obj)
  92. if "names" in filters and window.name not in filters['names']:
  93. continue
  94. if "after" in filters and filters["after"] >= window.start:
  95. continue
  96. if "before" in filters and filters["before"] <= window.stop:
  97. continue
  98. res.append(window)
  99. return res
  100.  
  101. def create_maintenance(self, obj):
  102. window = Maintenance(self, obj=obj)
  103. params = window.to_json()
  104. # parameters = {
  105. # 'description': description,
  106. # 'from': from_time,
  107. # 'to': to_time,
  108. # 'uptimeids': checks
  109. # }
  110. response = self.api.send(methon='post', resource='maintenance', params=params)
  111. window._id = response.json()["checks_maintenance"]["id"]
  112. return window
  113.  
  114. def delete_maintenance(self, window):
  115. response = self.api.send(methon='post', resource='maintenance/'+str(window))
  116. return response
  117.  
  118. def servertime(self):
  119. return self.api.send(method='get', resource='servertime')['servertime']
  120.  
  121. def get_summary_outage(self, checkid, start=None, end=None, order="asc"):
  122. params = {}
  123. if start is not None:
  124. params['from'] = start
  125. if end is not None:
  126. params['to'] = end
  127. if order is not None:
  128. params['order'] = order
  129. return self.api.send('get', resource="summary.outage", resource_id=checkid, params=params)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement