Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_run_maintenance_error(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test",
'time_type': 0,
'maintenance_window_name': "test",
'maintenance_type': 0,
'start_date': "2017-11-14 10:40",
'end_date': "2017-11-14 10:45"}
host_dict = {'name': "test", 'hostid': '1'}
maintenance_dict = {'maintenanceids': ['1']}
action.connect = mock_connect
action.find_host = mock.MagicMock(return_value=host_dict['hostid'])
action.maintenance_create_or_update = mock.MagicMock(return_value=maintenance_dict,
side_effect=ZabbixAPIException('maintenance error'))
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
def test_run_host_error(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test"}
host_dict = {'name': "test", 'hostid': '1'}
action.find_hosts = mock.MagicMock(return_value=host_dict['hostid'],
side_effect=ZabbixAPIException('host error'))
action.connect = mock_connect
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
def test_run_delete_error(self, mock_connect, mock_client):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'maintenance_window_name': None, 'maintenance_id': '1'}
action.connect = mock_connect
mock_client.maintenance.delete.side_effect = ZabbixAPIException('maintenance error')
mock_client.maintenance.delete.return_value = "delete return"
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
def test_run_delete_error(self, mock_connect, mock_client):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test"}
host_dict = {'name': "test", 'hostid': '1'}
action.connect = mock_connect
action.find_host = mock.MagicMock(return_value=host_dict['hostid'])
mock_client.host.delete.side_effect = ZabbixAPIException('host error')
mock_client.host.delete.return_value = "delete return"
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
def test_find_host_fail(self, mock_client):
action = self.get_action_instance(self.full_config)
test_dict = {'host_name': "test", 'hostid': "1"}
mock_client.host.get.side_effect = ZabbixAPIException('host error')
mock_client.host.get.return_value = [test_dict]
action.client = mock_client
with self.assertRaises(ZabbixAPIException):
action.find_host(test_dict['host_name'])
def test_run_host_error(self, mock_connect):
action = self.get_action_instance(self.full_config)
mock_connect.return_vaue = "connect return"
test_dict = {'host': "test"}
host_dict = {'name': "test", 'hostid': '1'}
action.find_host = mock.MagicMock(return_value=host_dict['hostid'],
side_effect=ZabbixAPIException('host error'))
action.connect = mock_connect
with self.assertRaises(ZabbixAPIException):
action.run(**test_dict)
req.add_header('Content-Type', 'application/json-rpc')
try:
res = urlopen(req)
res_str = res.read().decode('utf-8')
res_json = json.loads(res_str)
except ValueError as e:
raise ZabbixAPIException("Unable to parse json: %s" % e.message)
res_str = json.dumps(res_json, indent=4, separators=(',', ': '))
logger.debug("Response Body: %s", res_str)
if 'error' in res_json:
err = res_json['error'].copy()
err.update({'json': str(request_json)})
raise ZabbixAPIException(err)
return res_json
json.dumps(request_json)))
data = json.dumps(request_json)
if not isinstance(data, bytes):
data = data.encode("utf-8")
req = urllib2.Request(self.url, data)
req.get_method = lambda: 'POST'
req.add_header('Content-Type', 'application/json-rpc')
try:
res = urlopen(req)
res_str = res.read().decode('utf-8')
res_json = json.loads(res_str)
except ValueError as e:
raise ZabbixAPIException("Unable to parse json: %s" % e.message)
res_str = json.dumps(res_json, indent=4, separators=(',', ': '))
logger.debug("Response Body: %s", res_str)
if 'error' in res_json:
err = res_json['error'].copy()
err.update({'json': str(request_json)})
raise ZabbixAPIException(err)
return res_json
def find_hosts(self, host_name):
""" Queries the zabbix api for a host and returns just the
ids of the hosts as a list. If a host could not be found it
returns an empty list.
"""
try:
zabbix_hosts = self.client.host.get(filter={"host": host_name})
except ZabbixAPIException as e:
raise ZabbixAPIException(("There was a problem searching for the host: "
"{0}".format(e)))
zabbix_hosts_return = []
if len(zabbix_hosts) > 0:
for host in zabbix_hosts:
zabbix_hosts_return.append(host['hostid'])
return zabbix_hosts_return
if len(maintenance_result) == 0:
raise ValueError(("Could not find maintenance windows with name: "
"{0}").format(maintenance_window_name))
elif len(maintenance_result) == 1:
maintenance_id = maintenance_result[0]['maintenanceid']
elif len(maintenance_result) >= 2:
raise ValueError(("There are multiple maintenance windows with the "
"name: {0}").format(maintenance_window_name))
elif maintenance_window_name is None and maintenance_id is None:
raise ValueError("Must provide either a maintenance_window_name or a maintenance_id")
try:
self.client.maintenance.delete(maintenance_id)
except ZabbixAPIException as e:
raise ZabbixAPIException(("There was a problem deleting the "
"maintenance window: {0}").format(e))
return True