def get_tenant(self, tenant_id): if self.cache.cache_exists(tenant_id, CACHE_TENANT): tenant_dict = jsonutils.loads( self.cache.cache_get(tenant_id, CACHE_TENANT)) tenant = load_tenant_from_dict(tenant_dict) return tenant return None
def setUp(self): self.tenant_id = "12673247623548752387452378" self.tenant_dict = { "tenant_id": self.tenant_id, "tenant_name": "TenantName", "_id": "507f1f77bcf86cd799439011", "event_producers": [{ "id": 123, "name": "apache", "pattern": "apache2.cee", "durable": False, "encrypted": False, "sinks": ["elasticsearch"] }, { "id": 124, "name": "system.auth", "pattern": "auth_log.cee", "durable": False, "encrypted": False, "sinks": ["elasticsearch", "hdfs"] }], "token": { "valid": "c8a4db32-635a-46b6-94ed-04b1bd533f41", "previous": None, "last_changed": "2013-03-19T18:16:48.411029Z" } } self.producer_id = "234" self.event_producer = EventProducer(_id=self.producer_id, name="nginx", pattern="nginx") self.ds_handler = MagicMock() self.ds_handler.find_one.return_value = self.tenant_dict self.tenant_obj = tenant_util.load_tenant_from_dict(self.tenant_dict) self.tenant_cache = MagicMock() self.tenant_cache.cache_get.return_value = jsonutils.dumps( self.tenant_dict) self.tenant_cache.cache_exists.return_value = True self.tenant_cache.cache_update = MagicMock() self.token_cache = MagicMock() self.token_cache.cache_get.return_value = jsonutils.dumps( self.tenant_dict['token']) self.token_cache.cache_exists.return_value = True self.cache_empty = MagicMock() self.cache_empty.cache_exists.return_value = False self.cache_empty.cache_set = MagicMock()
def setUp(self): self.tenant_id = "12673247623548752387452378" self.tenant_dict = { "tenant_id": self.tenant_id, "tenant_name": "TenantName", "_id": "507f1f77bcf86cd799439011", "event_producers": [ { "id": 123, "name": "apache", "pattern": "apache2.cee", "durable": False, "encrypted": False, "sinks": ["elasticsearch"] }, { "id": 124, "name": "system.auth", "pattern": "auth_log.cee", "durable": False, "encrypted": False, "sinks": ["elasticsearch", "hdfs"] } ], "token": { "valid": "c8a4db32-635a-46b6-94ed-04b1bd533f41", "previous": None, "last_changed": "2013-03-19T18:16:48.411029Z" } } self.producer_id = "234" self.event_producer = EventProducer( _id=self.producer_id, name="nginx", pattern="nginx") self.ds_handler = MagicMock() self.ds_handler.find_one.return_value = self.tenant_dict self.tenant_obj = tenant_util.load_tenant_from_dict(self.tenant_dict) self.tenant_cache = MagicMock() self.tenant_cache.cache_get.return_value = jsonutils.dumps( self.tenant_dict) self.tenant_cache.cache_exists.return_value = True self.tenant_cache.cache_update = MagicMock() self.token_cache = MagicMock() self.token_cache.cache_get.return_value = jsonutils.dumps( self.tenant_dict['token']) self.token_cache.cache_exists.return_value = True self.cache_empty = MagicMock() self.cache_empty.cache_exists.return_value = False self.cache_empty.cache_set = MagicMock()
def _get_tenant_from_coordinator(tenant_id, message_token, message): """ This method retrieves tenant data from the coordinator, and persists the tenant data in the local cache for future lookups. The message is then handed off to be packed with correlation data. """ config = _get_config_from_cache() try: resp = http_request('{0}/tenant/{1}'.format(config.coordinator_uri, tenant_id), { MESSAGE_TOKEN: message_token, 'hostname': config.hostname }, http_verb='GET') except requests.RequestException as ex: _LOG.exception(ex.message) raise errors.CoordinatorCommunicationError if resp.status_code == httplib.OK: response_body = resp.json() #load new tenant data from response body tenant = tenant_util.load_tenant_from_dict(response_body['tenant']) # update the cache with new tenant info _save_tenant_to_cache(tenant_id, tenant) # add correlation to message _add_correlation_info_to_message(tenant, message) elif resp.status_code == httplib.NOT_FOUND: error_message = 'unable to locate tenant.' _LOG.debug(error_message) raise errors.ResourceNotFoundError(error_message) else: #coordinator responds, but coordinator datasink could be unreachable raise errors.CoordinatorCommunicationError
def _get_tenant_from_coordinator(tenant_id, message_token, message): """ This method retrieves tenant data from the coordinator, and persists the tenant data in the local cache for future lookups. The message is then handed off to be packed with correlation data. """ config = _get_config_from_cache() try: resp = http_request( '{0}/tenant/{1}'.format(config.coordinator_uri, tenant_id), {MESSAGE_TOKEN: message_token, 'hostname': config.hostname}, http_verb='GET') except requests.RequestException as ex: _LOG.exception(ex.message) raise errors.CoordinatorCommunicationError if resp.status_code == httplib.OK: response_body = resp.json() #load new tenant data from response body tenant = tenant_util.load_tenant_from_dict(response_body['tenant']) # update the cache with new tenant info _save_tenant_to_cache(tenant_id, tenant) # add correlation to message _add_correlation_info_to_message(tenant, message) elif resp.status_code == httplib.NOT_FOUND: error_message = 'unable to locate tenant.' _LOG.debug(error_message) raise errors.ResourceNotFoundError(error_message) else: #coordinator responds, but coordinator datasink could be unreachable raise errors.CoordinatorCommunicationError
def _get_tenant_from_coordinator(self): """ This method calls to the coordinator to retrieve tenant """ config_cache = ConfigCache() config = config_cache.get_config() token_header = { MESSAGE_TOKEN: self.message_token, "WORKER-ID": config.worker_id, "WORKER-TOKEN": config.worker_token } request_uri = "{0}/tenant/{1}".format( config.coordinator_uri, self.tenant_id) try: resp = http_request(request_uri, token_header, http_verb='GET') except requests.RequestException as ex: _LOG.exception(ex.message) raise errors.CoordinatorCommunicationError if resp.status_code == httplib.OK: response_body = resp.json() tenant = load_tenant_from_dict(response_body['tenant']) return tenant elif resp.status_code == httplib.NOT_FOUND: message = 'unable to locate tenant.' _LOG.debug(message) raise errors.ResourceNotFoundError(message) else: raise errors.CoordinatorCommunicationError
def test_load_tenant_from_dict(self): tenant = tenant_util.load_tenant_from_dict( self.tenant_obj.format_for_save()) self.assertIsInstance(tenant, Tenant) self.assertEqual(tenant.format_for_save(), self.tenant_obj.format_for_save())