Skip to content

Commit 81e7d21

Browse files
authored
feat: update management API - BucketsApi, OrganizationsApi, UsersApi (influxdata#358)
1 parent bb378af commit 81e7d21

File tree

9 files changed

+163
-8
lines changed

9 files changed

+163
-8
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 1.24.0 [unreleased]
22

3+
### Features
4+
1. [#358](https://github.com/influxdata/influxdb-client-python/pull/358): Update management API:
5+
- `BucketsApi` - add possibility to: `update`
6+
- `OrganizationsApi` - add possibility to: `update`
7+
- `UsersApi` - add possibility to: `update`, `delete`, `find`
8+
39
## 1.23.0 [2021-10-26]
410

511
### Deprecates

examples/buckets_management.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
org=org)
2525
print(created_bucket)
2626

27+
"""
28+
Update Bucket
29+
"""
30+
print(f"------- Update -------\n")
31+
created_bucket.description = "Update description"
32+
created_bucket = buckets_api.update_bucket(bucket=created_bucket)
33+
print(created_bucket)
34+
2735
"""
2836
List all Buckets
2937
"""
@@ -39,4 +47,3 @@
3947
print(f"------- Delete -------\n")
4048
buckets_api.delete_bucket(created_bucket)
4149
print(f" successfully deleted bucket: {created_bucket.name}")
42-

influxdb_client/client/bucket_api.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77
import warnings
88

9-
from influxdb_client import BucketsService, Bucket, PostBucketRequest
9+
from influxdb_client import BucketsService, Bucket, PostBucketRequest, PatchBucketRequest
1010
from influxdb_client.client.util.helpers import get_org_query_param
1111

1212

@@ -58,13 +58,23 @@ def create_bucket(self, bucket=None, bucket_name=None, org_id=None, retention_ru
5858

5959
return self._buckets_service.post_buckets(post_bucket_request=bucket)
6060

61+
def update_bucket(self, bucket: Bucket) -> Bucket:
62+
"""Update a bucket.
63+
64+
:param bucket: Bucket update to apply (required)
65+
:return: Bucket
66+
"""
67+
request = PatchBucketRequest(name=bucket.name,
68+
description=bucket.description,
69+
retention_rules=bucket.retention_rules)
70+
71+
return self._buckets_service.patch_buckets_id(bucket_id=bucket.id, patch_bucket_request=request)
72+
6173
def delete_bucket(self, bucket):
6274
"""Delete a bucket.
6375
6476
:param bucket: bucket id or Bucket
6577
:return: Bucket
66-
If the method is called asynchronously,
67-
returns the request thread.
6878
"""
6979
if isinstance(bucket, Bucket):
7080
bucket_id = bucket.id

influxdb_client/client/organizations_api.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
All dashboards, tasks, buckets, members, etc., belong to an organization.
55
"""
66

7-
8-
from influxdb_client import OrganizationsService, UsersService, Organization
7+
from influxdb_client import OrganizationsService, UsersService, Organization, PatchOrganizationRequest
98

109

1110
class OrganizationsApi(object):
@@ -45,6 +44,17 @@ def create_organization(self, name: str = None, organization: Organization = Non
4544
organization = Organization(name=name)
4645
return self._organizations_service.post_orgs(post_organization_request=organization)
4746

47+
def update_organization(self, organization: Organization) -> Organization:
48+
"""Update an organization.
49+
50+
:param organization: Organization update to apply (required)
51+
:return: Organization
52+
"""
53+
request = PatchOrganizationRequest(name=organization.name,
54+
description=organization.description)
55+
56+
return self._organizations_service.patch_orgs_id(org_id=organization.id, patch_organization_request=request)
57+
4858
def delete_organization(self, org_id: str):
4959
"""Delete an organization."""
5060
return self._organizations_service.delete_orgs_id(org_id=org_id)

influxdb_client/client/users_api.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
and provide them with an authentication token.
66
"""
77

8-
from influxdb_client import UsersService, User
8+
from typing import Union
9+
from influxdb_client import UsersService, User, Users, UserResponse
910

1011

1112
class UsersApi(object):
@@ -26,3 +27,39 @@ def create_user(self, name: str) -> User:
2627
user = User(name=name)
2728

2829
return self._service.post_users(user=user)
30+
31+
def update_user(self, user: User) -> UserResponse:
32+
"""Update a user.
33+
34+
:param user: User update to apply (required)
35+
:return: User
36+
"""
37+
return self._service.patch_users_id(user_id=user.id, user=user)
38+
39+
def delete_user(self, user: Union[str, User, UserResponse]) -> None:
40+
"""Delete a user.
41+
42+
:param user: user id or User
43+
:return: User
44+
"""
45+
if isinstance(user, User):
46+
user_id = user.id
47+
elif isinstance(user, UserResponse):
48+
user_id = user.id
49+
else:
50+
user_id = user
51+
52+
return self._service.delete_users_id(user_id=user_id)
53+
54+
def find_users(self, **kwargs) -> Users:
55+
"""List all users.
56+
57+
:key int offset: Offset for pagination
58+
:key int limit: Limit for pagination
59+
:key str after: The last resource ID from which to seek from (but not including).
60+
This is to be used instead of `offset`.
61+
:key str name: The user name.
62+
:key str id: The user ID.
63+
:return: Buckets
64+
"""
65+
return self._service.get_users(**kwargs)

tests/base_test.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212

1313
def generate_bucket_name():
14-
return "test_bucket_" + str(datetime.datetime.now().timestamp()) + "_IT"
14+
return generate_name(key="bucket")
15+
16+
17+
def generate_name(key: str):
18+
return f"test_{key}_" + str(datetime.datetime.now().timestamp()) + "_IT"
1519

1620

1721
class BaseTest(unittest.TestCase):

tests/test_BucketsApi.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ def test_pagination(self):
103103
buckets = self.buckets_api.find_buckets(limit=1).buckets
104104
self.assertEqual(1, len(buckets))
105105

106+
def test_update_bucket(self):
107+
my_org = self.find_my_org()
108+
109+
bucket = self.buckets_api.create_bucket(bucket_name=generate_bucket_name(),
110+
org=my_org,
111+
description="my description")
112+
self.assertEqual("my description", bucket.description)
113+
114+
bucket.description = "updated description"
115+
self.buckets_api.update_bucket(bucket=bucket)
116+
self.assertEqual("updated description", bucket.description)
117+
106118

107119
if __name__ == '__main__':
108120
unittest.main()

tests/test_OrganizationsApi.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from tests.base_test import BaseTest, generate_name
2+
3+
4+
class OrganizationsApiTests(BaseTest):
5+
6+
def setUp(self) -> None:
7+
super(OrganizationsApiTests, self).setUp()
8+
organizations_api = self.client.organizations_api()
9+
organizations = organizations_api.find_organizations()
10+
11+
for organization in organizations:
12+
if organization.name.endswith("_IT"):
13+
print("Delete organization: ", organization.name)
14+
organizations_api.delete_organization(org_id=organization.id)
15+
16+
def test_update_organization(self):
17+
organizations_api = self.client.organizations_api()
18+
19+
organization = organizations_api.create_organization(name=generate_name(key='org'))
20+
self.assertEqual("", organization.description)
21+
22+
organization.description = "updated description"
23+
organization = organizations_api.update_organization(organization=organization)
24+
self.assertEqual("updated description", organization.description)

tests/test_UsersApi.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
from influxdb_client import UserResponse
4+
from influxdb_client.rest import ApiException
5+
from tests.base_test import BaseTest, generate_name
6+
7+
8+
class UsersApiTests(BaseTest):
9+
10+
def setUp(self) -> None:
11+
super(UsersApiTests, self).setUp()
12+
users_api = self.client.users_api()
13+
users = users_api.find_users()
14+
15+
for user in users.users:
16+
if user.name.endswith("_IT"):
17+
print("Delete user: ", user.name)
18+
users_api.delete_user(user=user)
19+
20+
def test_delete_user(self):
21+
users_api = self.client.users_api()
22+
23+
user = users_api.create_user(name=generate_name(key='user'))
24+
users = users_api.find_users(id=user.id)
25+
self.assertEqual(1, len(users.users))
26+
self.assertEqual(user, users.users[0])
27+
28+
users_api.delete_user(user)
29+
30+
with pytest.raises(ApiException) as e:
31+
assert users_api.find_users(id=user.id)
32+
assert "user not found" in e.value.body
33+
34+
def test_update_user(self):
35+
users_api = self.client.users_api()
36+
37+
name = generate_name(key='user')
38+
user = users_api.create_user(name=name)
39+
self.assertEqual(name, user.name)
40+
41+
user.name = "updated_" + name
42+
user = users_api.update_user(user=user)
43+
self.assertIsInstance(user, UserResponse)
44+
user = users_api.find_users(id=user.id).users[0]
45+
self.assertEqual("updated_" + name, user.name)

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