From 1a66a437cfd7e4f89f0db5689ce0ced9b6924cb9 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 17 Feb 2022 09:43:45 +0100 Subject: [PATCH 1/2] chore: improve error message when the client cannot find org by name --- influxdb_client/client/exceptions.py | 4 ++-- influxdb_client/client/util/helpers.py | 8 +++++++- tests/test_Helpers.py | 23 ++++++++++++++++++++++- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/influxdb_client/client/exceptions.py b/influxdb_client/client/exceptions.py index 5d77b541..5278d8d8 100644 --- a/influxdb_client/client/exceptions.py +++ b/influxdb_client/client/exceptions.py @@ -10,7 +10,7 @@ class InfluxDBError(Exception): """Raised when a server error occurs.""" - def __init__(self, response: HTTPResponse): + def __init__(self, response: HTTPResponse = None, message: str = None): """Initialize the InfluxDBError handler.""" if response is not None: self.response = response @@ -18,7 +18,7 @@ def __init__(self, response: HTTPResponse): self.retry_after = response.getheader('Retry-After') else: self.response = None - self.message = 'no response' + self.message = message or 'no response' self.retry_after = None super().__init__(self.message) diff --git a/influxdb_client/client/util/helpers.py b/influxdb_client/client/util/helpers.py index 96539bd1..548486c0 100644 --- a/influxdb_client/client/util/helpers.py +++ b/influxdb_client/client/util/helpers.py @@ -32,7 +32,13 @@ def get_org_query_param(org, client, required_id=False): _org = _org.id if required_id and _org and not _is_id(_org): try: - return client.organizations_api().find_organizations(org=_org)[0].id + organizations = client.organizations_api().find_organizations(org=_org) + if len(organizations) < 1: + from influxdb_client.client.exceptions import InfluxDBError + message = f"The client cannot find organization with name: '{_org}' " \ + "to determine their ID. Are you using token with sufficient permission?" + raise InfluxDBError(response=None, message=message) + return organizations[0].id except ApiException: return None diff --git a/tests/test_Helpers.py b/tests/test_Helpers.py index ebf3773e..8cec768a 100644 --- a/tests/test_Helpers.py +++ b/tests/test_Helpers.py @@ -1,5 +1,8 @@ -from influxdb_client import InfluxDBClient, Organization +import pytest + +from influxdb_client import InfluxDBClient, Organization, PermissionResource, Permission # noinspection PyProtectedMember +from influxdb_client.client.exceptions import InfluxDBError from influxdb_client.client.util.helpers import get_org_query_param, _is_id from tests.base_test import BaseTest @@ -36,3 +39,21 @@ def test_both_none(self): self.client = InfluxDBClient(url=self.client.url, token="my-token") org = get_org_query_param(None, self.client) self.assertIsNone(org) + + def test_not_permission_to_read_org(self): + # Create Token without permission to read Organizations + resource = PermissionResource(type="buckets", org_id=self.find_my_org().id) + authorization = self.client \ + .authorizations_api() \ + .create_authorization(org_id=self.find_my_org().id, + permissions=[Permission(resource=resource, action="read"), + Permission(resource=resource, action="write")]) + self.client.close() + + # Initialize client without permission to read Organizations + self.client = InfluxDBClient(url=self.client.url, token=authorization.token) + + with pytest.raises(InfluxDBError) as e: + get_org_query_param("my-org", self.client, required_id=True) + assert "The client cannot find organization with name: 'my-org' to determine their ID. Are you using token " \ + "with sufficient permission?" in f"{e.value} " From 39101ea1581844c92c7d52e372bf68a8bb98cee3 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 17 Feb 2022 09:46:22 +0100 Subject: [PATCH 2/2] docs: update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3ce4c7b..4a9ebc95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ This release introduces a support for new version of InfluxDB OSS API definition ### API 1. [#399](https://github.com/influxdata/influxdb-client-python/pull/399): Use the latest InfluxDB OSS API definitions to generated APIs +### Bug Fixes +1. [#408](https://github.com/influxdata/influxdb-client-python/pull/408): Improve error message when the client cannot find organization by name + ## 1.25.0 [2022-01-20] ### Features