def create_client(self, version=None, service_type=None): """Return Zaqar client.""" from zaqarclient.queues import client as zaqar kc = self.keystone() messaging_api_url = kc.service_catalog.url_for( service_type=self.choose_service_type(service_type), endpoint_type=self.credential.endpoint_type, region_name=self.credential.region_name) conf = { "auth_opts": { "backend": "keystone", "options": { "os_username": self.credential.username, "os_password": self.credential.password, "os_project_name": self.credential.tenant_name, "os_project_id": kc.auth_ref.get("token").get("tenant").get("id"), "os_auth_url": self.credential.auth_url, "insecure": self.credential.insecure, } } } client = zaqar.Client(url=messaging_api_url, version=self.choose_version(version), conf=conf) return client
def zaqar(self, version=1.1): """Return Zaqar client.""" from zaqarclient.queues import client as zaqar kc = self.keystone() messaging_api_url = kc.service_catalog.url_for( service_type="messaging", endpoint_type=self.endpoint.endpoint_type, region_name=self.endpoint.region_name) conf = { "auth_opts": { "backend": "keystone", "options": { "os_username": self.endpoint.username, "os_password": self.endpoint.password, "os_project_name": self.endpoint.tenant_name, "os_project_id": kc.auth_tenant_id, "os_auth_url": self.endpoint.auth_url, "insecure": self.endpoint.insecure, } } } client = zaqar.Client(url=messaging_api_url, version=version, conf=conf) return client
def healthy(): # Note: credential information should be provided # using `conf` keyword argument if authentication # is enabled at server side. Please refer to # keystone_auth.py for more information. cli = client.Client(url=URL, version=2) return True if cli.health() else False
def create_client(self, version=None, service_type=None): """Return Zaqar client.""" from zaqarclient.queues import client as zaqar client = zaqar.Client(url=self._get_endpoint(), version=self.choose_version(version), session=self.keystone.get_session()[0]) return client
def check_and_trigger(context, **kwargs): file_name = 'count.txt' r = requests.get('http://httpbin.org/status/500') if r.status_code != requests.codes.ok: if not os.path.isfile(file_name): count = 1 with open(file_name, 'w') as f: f.write(str(count)) else: with open(file_name, 'r+') as f: count = int(f.readline()) count += 1 if count == 3: # Send message and stop trigger after 3 checks z_client = client.Client( session=context['os_session'], version=2, ) _send_message(z_client, kwargs.get('queue'), r.status_code, 'api1.production.catalyst.co.nz') f.seek(0) f.write(str(count)) f.truncate() print('new count: %s' % count) else: try: os.remove(file_name) except OSError: pass
def get_zaqar_client(self, conf): try: from zaqarclient.queues import client as zaqar_client return zaqar_client.Client(self._get_endpoint(), version=2, conf=conf) except Exception: LOG.error("Failed to connect to Zaqar service ", exc_info=True)
def test_health_ok(self, version): cli = client.Client('http://example.com', version, {"auth_opts": { 'backend': 'noauth' }}) with mock.patch.object(core, 'health', autospec=True) as core_health: core_health.return_value = None self.assertTrue(cli.health())
def _reset_queues(): cli = client.Client(CONF.server_url) for i in range(CONF.num_queues): # TODO(kgriffs): DRY up name generation so it is done # in a helper, vs. being copy-pasted everywhere. queue = cli.queue(CONF.queue_prefix + '-' + str(i)) queue.delete()
def setUp(self): super(QueuesTestBase, self).setUp() self.transport = self.transport_cls(self.conf) self.client = client.Client(self.url, self.version, self.conf) mocked_transport = mock.Mock(return_value=self.transport) self.client._get_transport = mocked_transport self.queue = self.client.queue(1, auto_create=False)
def load_generator(stats, num_workers, num_queues, test_duration, limit): cli = client.Client(CONF.server_url) queues = [ cli.queue(CONF.queue_prefix + '-' + str(i)) for i in range(num_queues) ] gevent.joinall([ gevent.spawn(observer, queues, stats, test_duration, limit) for _ in range(num_workers) ])
def test_health_bad(self, version): cli = client.Client('http://example.com', version, {"auth_opts": { 'backend': 'noauth' }}) def raise_error(*args, **kwargs): raise errors.ServiceUnavailableError() with mock.patch.object(core, 'health', autospec=True) as core_health: core_health.side_effect = raise_error self.assertFalse(cli.health())
def load_generator(stats, num_workers, num_queues, test_duration): cli = client.Client(CONF.server_url) queues = [ cli.queue(CONF.queue_prefix + '-' + str(i)) for i in range(num_queues) ] message_pool = load_messages() gevent.joinall([ gevent.spawn(producer, queues, message_pool, stats, test_duration) for _ in range(num_workers) ])
def create_client(self, version=None, service_type=None): """Return Zaqar client.""" from zaqarclient.queues import client as zaqar tenant_id = self.keystone.auth_ref.project_id conf = {"auth_opts": {"backend": "keystone", "options": { "os_username": self.credential.username, "os_password": self.credential.password, "os_project_name": self.credential.tenant_name, "os_project_id": tenant_id, "os_auth_url": self.credential.auth_url, "insecure": self.credential.insecure, }}} client = zaqar.Client(url=self._get_endpoint(), version=self.choose_version(version), conf=conf) return client
def test_signed_url(self): queue = self.client.queue('test_queue') messages = [{'ttl': 300, 'body': 'Post It!'}] queue.post(messages) self.addCleanup(queue.delete) signature = queue.signed_url() opts = { 'paths': signature['paths'], 'expires': signature['expires'], 'methods': signature['methods'], 'signature': signature['signature'], 'os_project_id': signature['project'], } auth_opts = {'backend': 'signed-url', 'options': opts} conf = {'auth_opts': auth_opts} signed_client = client.Client(self.url, self.version, conf) queue = signed_client.queue('test_queue') [message] = list(queue.messages()) self.assertEqual('Post It!', message.body)
def get_zaqar_client(self): conf = self.conf.service_credentials params = { 'auth_opts': { 'backend': 'keystone', 'options': { 'os_username': conf.os_username, 'os_password': conf.os_password, 'os_project_name': conf.os_tenant_name, 'os_auth_url': conf.os_auth_url, 'insecure': '' } } } try: from zaqarclient.queues import client as zaqar_client return zaqar_client.Client(self._get_endpoint(), version=1.1, conf=params) except Exception: LOG.error(_LE("Failed to connect to Zaqar service "), exc_info=True)
def zaqar(self, version=1.1): """Return Zaqar client.""" kc = self.keystone() messaging_api_url = kc.service_catalog.url_for( service_type='messaging', endpoint_type=self.endpoint.endpoint_type, region_name=self.endpoint.region_name) conf = { 'auth_opts': { 'backend': 'keystone', 'options': { 'os_username': self.endpoint.username, 'os_password': self.endpoint.password, 'os_project_name': self.endpoint.tenant_name, 'os_auth_url': self.endpoint.auth_url, 'insecure': CONF.https_insecure, } } } client = zaqar.Client(url=messaging_api_url, version=version, conf=conf) return client
def zaqarclient(request): zaqar_url = "" service_type = 'messaging' try: zaqar_url = base.url_for(request, service_type) except exceptions.ServiceCatalogException: LOG.debug('No messaging service is configured.') return None LOG.debug('zaqarclient connection created using the token "%s" and url' '"%s"' % (request.user.token.id, zaqar_url)) opts = { 'os_auth_token': request.user.token.id, 'os_auth_url': base.url_for(request, 'identity'), 'os_project_id': request.user.tenant_id, 'os_service_type': service_type } auth_opts = {'backend': 'keystone', 'options': opts} conf = {'auth_opts': auth_opts} return zaqar_client.Client(url=zaqar_url, version=2, conf=conf)
def get_new_client(): return client.Client(CONF.server_url, 1.1, conf=client_conf)
def test_health(self, version): cli = client.Client('http://example.com', version, {}) with mock.patch.object(core, 'health', autospec=True) as core_health: resp = response.Response(None, None) core_health.return_value = resp self.assertIsNotNone(cli.health())
def test_transport(self, version): cli = client.Client('http://example.com', version, {"auth_opts": { 'backend': 'noauth' }}) self.assertIsNotNone(cli.transport())
def test_transport(self, version): cli = client.Client('http://example.com', version, {}) self.assertIsNotNone(cli.transport())
def get_client(): return zaqarclient.Client(ZAQAR_URL, ZAQAR_VERSION, conf=conf)
def test_get_instance(self): version = list(client._CLIENTS.keys())[0] cli = client.Client('http://example.com', version, {}) self.assertTrue(isinstance(cli, client._CLIENTS[version]))
def healthy(): cli = client.Client(url=URL, version=1) return True if cli.health() else False
def get_new_client(): return client.Client(CONF.server_url, client_api.get, conf=client_conf)