def create_heartbeat(): try: heartbeat = Heartbeat.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) heartbeat.customer = assign_customer(wanted=heartbeat.customer, permission=Scope.admin_heartbeats) try: heartbeat = heartbeat.create() except Exception as e: raise ApiError(str(e), 500) write_audit_trail.send(current_app._get_current_object(), event='heartbeat-created', message='', user=g.login, customers=g.customers, scopes=g.scopes, resource_id=heartbeat.id, type='heartbeat', request=request) if heartbeat: return jsonify(status='ok', id=heartbeat.id, heartbeat=heartbeat.serialize), 201 else: raise ApiError('insert or update of received heartbeat failed', 500)
def create_heartbeat(): try: heartbeat = Heartbeat.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) heartbeat.customer = assign_customer(wanted=heartbeat.customer, permission='admin:heartbeats') try: heartbeat = heartbeat.create() except Exception as e: raise ApiError(str(e), 500) if heartbeat: return jsonify(status="ok", id=heartbeat.id, heartbeat=heartbeat.serialize), 201 else: raise ApiError("insert or update of received heartbeat failed", 500)
def create_heartbeat(): try: heartbeat = Heartbeat.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) if g.get('customer', None): heartbeat.customer = g.get('customer') try: heartbeat = heartbeat.create() except Exception as e: raise ApiError(str(e), 500) if heartbeat: return jsonify(status="ok", id=heartbeat.id, heartbeat=heartbeat.serialize), 201 else: raise ApiError("insert or update of received heartbeat failed", 500)
def create_heartbeat(): try: heartbeat = Heartbeat.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) if g.get('customer', None): heartbeat.customer = g.get('customer') try: heartbeat = heartbeat.create() except Exception as e: raise ApiError(str(e), 500) if heartbeat: return jsonify(status="ok", id=heartbeat.id, heartbeat=heartbeat.serialize), 201 else: raise ApiError("insert or update of received heartbeat failed", 500)
def create_heartbeat(): try: heartbeat = Heartbeat.parse(request.json) except ValueError as e: raise ApiError(str(e), 400) heartbeat.customer = assign_customer(wanted=heartbeat.customer, permission=Scope.admin_heartbeats) try: heartbeat = heartbeat.create() except Exception as e: raise ApiError(str(e), 500) write_audit_trail.send(current_app._get_current_object(), event='heartbeat-created', message='', user=g.login, customers=g.customers, scopes=g.scopes, resource_id=heartbeat.id, type='heartbeat', request=request) if heartbeat: return jsonify(status='ok', id=heartbeat.id, heartbeat=heartbeat.serialize), 201 else: raise ApiError('insert or update of received heartbeat failed', 500)
def test_timeout(self): # create heartbeat with default timeout response = self.client.post('/heartbeat', data=json.dumps(self.heartbeat), headers=self.headers) self.assertEqual(response.status_code, 201) data = json.loads(response.data.decode('utf-8')) self.assertEqual(data['heartbeat']['timeout'], 4) # resend heartbeat with different timeout self.heartbeat['timeout'] = 20 response = self.client.post('/heartbeat', data=json.dumps(self.heartbeat), headers=self.headers) self.assertEqual(response.status_code, 201) data = json.loads(response.data.decode('utf-8')) self.assertEqual(data['heartbeat']['timeout'], 20) # resend heartbeat with timeout disabled (ie. 0) self.heartbeat['timeout'] = 0 response = self.client.post('/heartbeat', data=json.dumps(self.heartbeat), headers=self.headers) self.assertEqual(response.status_code, 201) data = json.loads(response.data.decode('utf-8')) self.assertEqual(data['heartbeat']['timeout'], 0) # send heartbeat with different timeout self.heartbeat['timeout'] = 10 response = self.client.post('/heartbeat', data=json.dumps(self.heartbeat), headers=self.headers) self.assertEqual(response.status_code, 201) data = json.loads(response.data.decode('utf-8')) self.assertEqual(data['heartbeat']['timeout'], 10) # send heartbeat with very short timeout self.heartbeat['timeout'] = 2 response = self.client.post('/heartbeat', data=json.dumps(self.heartbeat), headers=self.headers) self.assertEqual(response.status_code, 201) data = json.loads(response.data.decode('utf-8')) self.assertEqual(data['heartbeat']['timeout'], 2) heartbeat_id = data['id'] time.sleep(4) # get heartbeat response = self.client.get('/heartbeat/' + heartbeat_id) self.assertEqual(response.status_code, 200) data = json.loads(response.data.decode('utf-8')) self.assertEqual(heartbeat_id, data['heartbeat']['id']) self.assertEqual(data['heartbeat']['timeout'], 2) self.assertEqual(data['heartbeat']['status'], 'expired') with self.app.app_context(): hb = Heartbeat.parse(data['heartbeat']) payload = { 'resource': hb.origin, 'event': 'HeartbeatFail', 'environment': 'Production', 'severity': 'major', 'correlate': ['HeartbeatFail', 'HeartbeatSlow', 'HeartbeatOK'], 'service': ['Test'], 'group': 'Alerta', 'value': f'{hb.since}', 'text': f'Heartbeat not received in {hb.timeout} seconds', 'tags': hb.tags, 'attributes': hb.attributes, # 'timeout': None } # create heartbeat alert response = self.client.post('/alert', json=payload) self.assertEqual(response.status_code, 201, response.data) data = json.loads(response.data.decode('utf-8')) self.assertEqual(data['alert']['resource'], hb.origin) self.assertEqual(data['alert']['status'], 'open') self.assertEqual(data['alert']['service'], ['Test']) self.assertEqual(data['alert']['timeout'], 2) self.assertEqual(data['alert']['duplicateCount'], 0) self.assertEqual(data['alert']['trendIndication'], 'moreSevere') self.assertEqual(data['alert']['history'][0]['user'], None)