def integrations_command_handler(topic, message): from pyhermes.publishing import publish from pyhermes.settings import HERMES_SETTINGS if not HERMES_SETTINGS.ENABLED: sys.stderr.write( 'Hermes integration is disabled. ' 'Check HERMES.ENABLED variable ' 'in your settings or environment.' ) return if topic == TOPICS_ALL: topics = HERMES_SETTINGS.PUBLISHING_TOPICS.keys() else: topics = [topic] if not topics: sys.stderr.write('Topics list is empty') for topic in topics: try: sys.stdout.write('Sending message to {}'.format(topic)) publish(topic, {'result': message}) except HermesPublishException as e: sys.stderr.write(str(e)) else: sys.stdout.write( 'Message was sent successfully to {}!'.format(topic) )
def integrations_command_handler(topic, message): from pyhermes.publishing import publish from pyhermes.settings import HERMES_SETTINGS if not HERMES_SETTINGS.ENABLED: sys.stderr.write('Hermes integration is disabled. ' 'Check HERMES.ENABLED variable ' 'in your settings or environment.') return if topic == TOPICS_ALL: topics = HERMES_SETTINGS.PUBLISHING_TOPICS.keys() else: topics = [topic] if not topics: sys.stderr.write('Topics list is empty') for topic in topics: try: sys.stdout.write('Sending message to {}'.format(topic)) publish(topic, {'result': message}) except HermesPublishException as e: sys.stderr.write(str(e)) else: sys.stdout.write( 'Message was sent successfully to {}!'.format(topic))
def test_publish_request_error(self, fake_handler, msg): data = {'test': 'data'} responses.add_callback( method=responses.POST, url="{}/topics/{}.{}".format(HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC), match_querystring=True, content_type='application/json', callback=fake_handler, ) with self.assertRaises(HermesPublishException) as cm: publish(TEST_TOPIC, data) self.assertEqual(str(cm.exception), 'Error pushing event to Hermes: {}.'.format(msg))
def test_publish_request_error_with_retry(self): data = {'test': 'data'} hermes_event_id = 'hermes_ok' tries = [0] def callback(request): tries[0] += 1 print(tries) if tries[0] <= 2: raise ConnectionError('connection error') print('Returning normal') return (201, {'Hermes-Message-Id': hermes_event_id}, "") responses.add_callback( method=responses.POST, url="{}/topics/{}.{}".format( HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC ), match_querystring=True, content_type='application/json', callback=callback, ) response = publish('{}.{}'.format(TEST_GROUP_NAME, TEST_TOPIC), data) self.assertEqual(response, hermes_event_id) self.assertEqual(tries[0], 3)
def test_publish_bad_status_code(self, status_code): data = {'test': 'data'} responses.add( method=responses.POST, url="{}/topics/{}.{}".format(HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC), match_querystring=True, body=None, status=status_code, content_type='application/json', ) with self.assertRaises(HermesPublishException) as cm: publish(TEST_TOPIC, data) self.assertEqual( str(cm.exception), 'Bad response code during Hermes push: {}.'.format(status_code))
def test_publish_request_error(self, fake_handler, msg): data = {'test': 'data'} responses.add_callback( method=responses.POST, url="{}/topics/{}.{}".format( HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC ), match_querystring=True, content_type='application/json', callback=fake_handler, ) with self.assertRaises(HermesPublishException) as cm: publish(TEST_TOPIC, data) self.assertEqual( str(cm.exception), 'Error pushing event to Hermes: {}.'.format(msg) )
def test_publish_bad_status_code(self, status_code): data = {'test': 'data'} responses.add( method=responses.POST, url="{}/topics/{}.{}".format( HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC ), match_querystring=True, body=None, status=status_code, content_type='application/json', ) with self.assertRaises(HermesPublishException) as cm: publish(TEST_TOPIC, data) self.assertEqual( str(cm.exception), 'Bad response code during Hermes push: {}.'.format(status_code) )
def publish_host_update(instance): """ Publish information about DC Host updates using DCHost API serializer. """ if settings.HERMES_HOST_UPDATE_TOPIC_NAME: logger.info('Publishing host update for {}'.format(instance), extra={ 'type': 'PUBLISH_HOST_UPDATE', 'instance_id': instance.id, 'content_type': instance.content_type.name, }) host_data = _get_host_data(instance) # call publish directly to make testing easier logger.info('Publishing DCHost update', extra={ 'publish_data': host_data, }) publish(settings.HERMES_HOST_UPDATE_TOPIC_NAME, host_data)
def test_publish_full_topic_name(self): hermes_event_id = 'hermes_ok' data = {'test': 'data'} responses.add(method=responses.POST, url="{}/topics/{}.{}".format(HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC), match_querystring=True, body=None, status=201, content_type='application/json', adding_headers={'Hermes-Message-Id': hermes_event_id}) # TODO: check data response = publish('{}.{}'.format(TEST_GROUP_NAME, TEST_TOPIC), data) self.assertEqual(response, hermes_event_id)
def test_publish_full_topic_name(self): hermes_event_id = 'hermes_ok' data = {'test': 'data'} responses.add( method=responses.POST, url="{}/topics/{}.{}".format( HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC ), match_querystring=True, body=None, status=201, content_type='application/json', adding_headers={ 'Hermes-Message-Id': hermes_event_id } ) # TODO: check data response = publish('{}.{}'.format(TEST_GROUP_NAME, TEST_TOPIC), data) self.assertEqual(response, hermes_event_id)
def test_publish_request_wrong_response_code_with_retry(self): data = {'test': 'data'} hermes_event_id = 'hermes_ok' tries = [0] def callback(request): tries[0] += 1 if tries[0] <= 2: return (408, {}, "") return (202, {'Hermes-Message-Id': hermes_event_id}, "") responses.add_callback( method=responses.POST, url="{}/topics/{}.{}".format(HERMES_SETTINGS.BASE_URL, TEST_GROUP_NAME, TEST_TOPIC), match_querystring=True, content_type='application/json', callback=callback, ) response = publish('{}.{}'.format(TEST_GROUP_NAME, TEST_TOPIC), data) self.assertEqual(response, hermes_event_id) self.assertEqual(tries[0], 3)
def wrapper(*args, **kwargs): result = func(*args, **kwargs) if self.auto_publish_result: publish(self.topic, result) return result