mirror of https://github.com/ansible/ansible.git
Update pagerduty modules to rest v2 (#42618)
* refactored from procedural to OOP * updated ongoing maintenance windows to PagerDuty REST API v2 * update create maintenance windows to PagerDuty REST API v2 * update absent maintenance windows to PagerDuty REST API v2 * update pager alert module to PagerDuty REST API v2 * removed basic HTTP authorization updated parameter description and examples * fix failed sanity checks * revised documentation according to review * make obsolete service key parameter an alias to a new integration key parameterpull/43494/head
parent
122780abce
commit
dd28be3aab
@ -0,0 +1,123 @@
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.modules.monitoring import pagerduty
|
||||
|
||||
import json
|
||||
|
||||
|
||||
class PagerDutyTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.pd = pagerduty.PagerDutyRequest(module=pagerduty, name='name', user='user', token='token')
|
||||
|
||||
def _assert_ongoing_maintenance_windows(self, module, url, headers):
|
||||
self.assertEquals('https://api.pagerduty.com/maintenance_windows?filter=ongoing', url)
|
||||
return object(), {'status': 200}
|
||||
|
||||
def _assert_ongoing_window_with_v1_compatible_header(self, module, url, headers, data=None, method=None):
|
||||
self.assertDictContainsSubset(
|
||||
{'Accept': 'application/vnd.pagerduty+json;version=2'},
|
||||
headers,
|
||||
'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found'
|
||||
)
|
||||
return object(), {'status': 200}
|
||||
|
||||
def _assert_create_a_maintenance_window_url(self, module, url, headers, data=None, method=None):
|
||||
self.assertEquals('https://api.pagerduty.com/maintenance_windows', url)
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_create_a_maintenance_window_http_method(self, module, url, headers, data=None, method=None):
|
||||
self.assertEquals('POST', method)
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_create_a_maintenance_window_from_header(self, module, url, headers, data=None, method=None):
|
||||
self.assertDictContainsSubset(
|
||||
{'From': 'requester_id'},
|
||||
headers,
|
||||
'From:requester_id HTTP header not found'
|
||||
)
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_create_window_with_v1_compatible_header(self, module, url, headers, data=None, method=None):
|
||||
self.assertDictContainsSubset(
|
||||
{'Accept': 'application/vnd.pagerduty+json;version=2'},
|
||||
headers,
|
||||
'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found'
|
||||
)
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_create_window_payload(self, module, url, headers, data=None, method=None):
|
||||
payload = json.loads(data)
|
||||
window_data = payload['maintenance_window']
|
||||
self.assertTrue('start_time' in window_data, '"start_time" is requiered attribute')
|
||||
self.assertTrue('end_time' in window_data, '"end_time" is requiered attribute')
|
||||
self.assertTrue('services' in window_data, '"services" is requiered attribute')
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_create_window_single_service(self, module, url, headers, data=None, method=None):
|
||||
payload = json.loads(data)
|
||||
window_data = payload['maintenance_window']
|
||||
services = window_data['services']
|
||||
self.assertEquals(
|
||||
[{'id': 'service_id', 'type': 'service_reference'}],
|
||||
services
|
||||
)
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_create_window_multiple_service(self, module, url, headers, data=None, method=None):
|
||||
payload = json.loads(data)
|
||||
window_data = payload['maintenance_window']
|
||||
services = window_data['services']
|
||||
print(services)
|
||||
self.assertEquals(
|
||||
[
|
||||
{'id': 'service_id_1', 'type': 'service_reference'},
|
||||
{'id': 'service_id_2', 'type': 'service_reference'},
|
||||
{'id': 'service_id_3', 'type': 'service_reference'},
|
||||
],
|
||||
services
|
||||
)
|
||||
return object(), {'status': 201}
|
||||
|
||||
def _assert_absent_maintenance_window_url(self, module, url, headers, method=None):
|
||||
self.assertEquals('https://api.pagerduty.com/maintenance_windows/window_id', url)
|
||||
return object(), {'status': 204}
|
||||
|
||||
def _assert_absent_window_with_v1_compatible_header(self, module, url, headers, method=None):
|
||||
self.assertDictContainsSubset(
|
||||
{'Accept': 'application/vnd.pagerduty+json;version=2'},
|
||||
headers,
|
||||
'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found'
|
||||
)
|
||||
return object(), {'status': 204}
|
||||
|
||||
def test_ongoing_maintenance_windos_url(self):
|
||||
self.pd.ongoing(http_call=self._assert_ongoing_maintenance_windows)
|
||||
|
||||
def test_ongoing_maintenance_windos_compatibility_header(self):
|
||||
self.pd.ongoing(http_call=self._assert_ongoing_window_with_v1_compatible_header)
|
||||
|
||||
def test_create_maintenance_window_url(self):
|
||||
self.pd.create('requester_id', 'service', 1, 0, 'desc', http_call=self._assert_create_a_maintenance_window_url)
|
||||
|
||||
def test_create_maintenance_window_http_method(self):
|
||||
self.pd.create('requester_id', 'service', 1, 0, 'desc', http_call=self._assert_create_a_maintenance_window_http_method)
|
||||
|
||||
def test_create_maintenance_from_header(self):
|
||||
self.pd.create('requester_id', 'service', 1, 0, 'desc', http_call=self._assert_create_a_maintenance_window_from_header)
|
||||
|
||||
def test_create_maintenance_compatibility_header(self):
|
||||
self.pd.create('requester_id', 'service', 1, 0, 'desc', http_call=self._assert_create_window_with_v1_compatible_header)
|
||||
|
||||
def test_create_maintenance_request_payload(self):
|
||||
self.pd.create('requester_id', 'service', 1, 0, 'desc', http_call=self._assert_create_window_payload)
|
||||
|
||||
def test_create_maintenance_for_single_service(self):
|
||||
self.pd.create('requester_id', 'service_id', 1, 0, 'desc', http_call=self._assert_create_window_single_service)
|
||||
|
||||
def test_create_maintenance_for_multiple_services(self):
|
||||
self.pd.create('requester_id', ['service_id_1', 'service_id_2', 'service_id_3'], 1, 0, 'desc', http_call=self._assert_create_window_multiple_service)
|
||||
|
||||
def test_absent_maintenance_window_url(self):
|
||||
self.pd.absent('window_id', http_call=self._assert_absent_maintenance_window_url)
|
||||
|
||||
def test_absent_maintenance_compatibility_header(self):
|
||||
self.pd.absent('window_id', http_call=self._assert_absent_window_with_v1_compatible_header)
|
@ -0,0 +1,41 @@
|
||||
from ansible.compat.tests import unittest
|
||||
from ansible.modules.monitoring import pagerduty_alert
|
||||
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlparse, urlencode, urlunparse
|
||||
|
||||
|
||||
class PagerDutyAlertsTest(unittest.TestCase):
|
||||
def _assert_incident_api(self, module, url, method, headers):
|
||||
self.assertTrue('https://api.pagerduty.com/incidents' in url, 'url must contain REST API v2 network path')
|
||||
self.assertTrue('service_ids%5B%5D=service_id' in url, 'url must contain service id to filter incidents')
|
||||
self.assertTrue('sort_by=incident_number%3Adesc' in url, 'url should contain sorting parameter')
|
||||
self.assertTrue('time_zone=UTC' in url, 'url should contain time zone parameter')
|
||||
return Response(), {'status': 200}
|
||||
|
||||
def _assert_compatibility_header(self, module, url, method, headers):
|
||||
self.assertDictContainsSubset(
|
||||
{'Accept': 'application/vnd.pagerduty+json;version=2'},
|
||||
headers,
|
||||
'Accept:application/vnd.pagerduty+json;version=2 HTTP header not found'
|
||||
)
|
||||
return Response(), {'status': 200}
|
||||
|
||||
def _assert_incident_key(self, module, url, method, headers):
|
||||
self.assertTrue('incident_key=incident_key_value' in url, 'url must contain incident key')
|
||||
return Response(), {'status': 200}
|
||||
|
||||
def test_incident_url(self):
|
||||
pagerduty_alert.check(None, 'name', 'state', 'service_id', 'integration_key', 'api_key', http_call=self._assert_incident_api)
|
||||
|
||||
def test_compatibility_header(self):
|
||||
pagerduty_alert.check(None, 'name', 'state', 'service_id', 'integration_key', 'api_key', http_call=self._assert_compatibility_header)
|
||||
|
||||
def test_incident_key_in_url_when_it_is_given(self):
|
||||
pagerduty_alert.check(
|
||||
None, 'name', 'state', 'service_id', 'integration_key', 'api_key', incident_key='incident_key_value', http_call=self._assert_incident_key
|
||||
)
|
||||
|
||||
|
||||
class Response(object):
|
||||
def read(self):
|
||||
return '{"incidents":[{"id": "incident_id", "status": "triggered"}]}'
|
Loading…
Reference in New Issue