Skip to content

Commit 52a9d02

Browse files
authored
chore: improve error message when the client cannot find org by name (influxdata#408)
1 parent 64a401e commit 52a9d02

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ This release introduces a support for new version of InfluxDB OSS API definition
3333
### API
3434
1. [#399](https://github.com/influxdata/influxdb-client-python/pull/399): Use the latest InfluxDB OSS API definitions to generated APIs
3535

36+
### Bug Fixes
37+
1. [#408](https://github.com/influxdata/influxdb-client-python/pull/408): Improve error message when the client cannot find organization by name
38+
3639
## 1.25.0 [2022-01-20]
3740

3841
### Features

influxdb_client/client/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
class InfluxDBError(Exception):
1111
"""Raised when a server error occurs."""
1212

13-
def __init__(self, response: HTTPResponse):
13+
def __init__(self, response: HTTPResponse = None, message: str = None):
1414
"""Initialize the InfluxDBError handler."""
1515
if response is not None:
1616
self.response = response
1717
self.message = self._get_message(response)
1818
self.retry_after = response.getheader('Retry-After')
1919
else:
2020
self.response = None
21-
self.message = 'no response'
21+
self.message = message or 'no response'
2222
self.retry_after = None
2323
super().__init__(self.message)
2424

influxdb_client/client/util/helpers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ def get_org_query_param(org, client, required_id=False):
3232
_org = _org.id
3333
if required_id and _org and not _is_id(_org):
3434
try:
35-
return client.organizations_api().find_organizations(org=_org)[0].id
35+
organizations = client.organizations_api().find_organizations(org=_org)
36+
if len(organizations) < 1:
37+
from influxdb_client.client.exceptions import InfluxDBError
38+
message = f"The client cannot find organization with name: '{_org}' " \
39+
"to determine their ID. Are you using token with sufficient permission?"
40+
raise InfluxDBError(response=None, message=message)
41+
return organizations[0].id
3642
except ApiException:
3743
return None
3844

tests/test_Helpers.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
from influxdb_client import InfluxDBClient, Organization
1+
import pytest
2+
3+
from influxdb_client import InfluxDBClient, Organization, PermissionResource, Permission
24
# noinspection PyProtectedMember
5+
from influxdb_client.client.exceptions import InfluxDBError
36
from influxdb_client.client.util.helpers import get_org_query_param, _is_id
47
from tests.base_test import BaseTest
58

@@ -36,3 +39,21 @@ def test_both_none(self):
3639
self.client = InfluxDBClient(url=self.client.url, token="my-token")
3740
org = get_org_query_param(None, self.client)
3841
self.assertIsNone(org)
42+
43+
def test_not_permission_to_read_org(self):
44+
# Create Token without permission to read Organizations
45+
resource = PermissionResource(type="buckets", org_id=self.find_my_org().id)
46+
authorization = self.client \
47+
.authorizations_api() \
48+
.create_authorization(org_id=self.find_my_org().id,
49+
permissions=[Permission(resource=resource, action="read"),
50+
Permission(resource=resource, action="write")])
51+
self.client.close()
52+
53+
# Initialize client without permission to read Organizations
54+
self.client = InfluxDBClient(url=self.client.url, token=authorization.token)
55+
56+
with pytest.raises(InfluxDBError) as e:
57+
get_org_query_param("my-org", self.client, required_id=True)
58+
assert "The client cannot find organization with name: 'my-org' to determine their ID. Are you using token " \
59+
"with sufficient permission?" in f"{e.value} "

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy