Skip to content

Commit

Permalink
feat: update management API - BucketsApi, OrganizationsApi, `User…
Browse files Browse the repository at this point in the history
…sApi` (#358)
  • Loading branch information
bednar authored Nov 11, 2021
1 parent bb378af commit 81e7d21
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 1.24.0 [unreleased]

### Features
1. [#358](https://github.com/influxdata/influxdb-client-python/pull/358): Update management API:
- `BucketsApi` - add possibility to: `update`
- `OrganizationsApi` - add possibility to: `update`
- `UsersApi` - add possibility to: `update`, `delete`, `find`

## 1.23.0 [2021-10-26]

### Deprecates
Expand Down
9 changes: 8 additions & 1 deletion examples/buckets_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
org=org)
print(created_bucket)

"""
Update Bucket
"""
print(f"------- Update -------\n")
created_bucket.description = "Update description"
created_bucket = buckets_api.update_bucket(bucket=created_bucket)
print(created_bucket)

"""
List all Buckets
"""
Expand All @@ -39,4 +47,3 @@
print(f"------- Delete -------\n")
buckets_api.delete_bucket(created_bucket)
print(f" successfully deleted bucket: {created_bucket.name}")

16 changes: 13 additions & 3 deletions influxdb_client/client/bucket_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import warnings

from influxdb_client import BucketsService, Bucket, PostBucketRequest
from influxdb_client import BucketsService, Bucket, PostBucketRequest, PatchBucketRequest
from influxdb_client.client.util.helpers import get_org_query_param


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

return self._buckets_service.post_buckets(post_bucket_request=bucket)

def update_bucket(self, bucket: Bucket) -> Bucket:
"""Update a bucket.
:param bucket: Bucket update to apply (required)
:return: Bucket
"""
request = PatchBucketRequest(name=bucket.name,
description=bucket.description,
retention_rules=bucket.retention_rules)

return self._buckets_service.patch_buckets_id(bucket_id=bucket.id, patch_bucket_request=request)

def delete_bucket(self, bucket):
"""Delete a bucket.
:param bucket: bucket id or Bucket
:return: Bucket
If the method is called asynchronously,
returns the request thread.
"""
if isinstance(bucket, Bucket):
bucket_id = bucket.id
Expand Down
14 changes: 12 additions & 2 deletions influxdb_client/client/organizations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
All dashboards, tasks, buckets, members, etc., belong to an organization.
"""


from influxdb_client import OrganizationsService, UsersService, Organization
from influxdb_client import OrganizationsService, UsersService, Organization, PatchOrganizationRequest


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

def update_organization(self, organization: Organization) -> Organization:
"""Update an organization.
:param organization: Organization update to apply (required)
:return: Organization
"""
request = PatchOrganizationRequest(name=organization.name,
description=organization.description)

return self._organizations_service.patch_orgs_id(org_id=organization.id, patch_organization_request=request)

def delete_organization(self, org_id: str):
"""Delete an organization."""
return self._organizations_service.delete_orgs_id(org_id=org_id)
39 changes: 38 additions & 1 deletion influxdb_client/client/users_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
and provide them with an authentication token.
"""

from influxdb_client import UsersService, User
from typing import Union
from influxdb_client import UsersService, User, Users, UserResponse


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

return self._service.post_users(user=user)

def update_user(self, user: User) -> UserResponse:
"""Update a user.
:param user: User update to apply (required)
:return: User
"""
return self._service.patch_users_id(user_id=user.id, user=user)

def delete_user(self, user: Union[str, User, UserResponse]) -> None:
"""Delete a user.
:param user: user id or User
:return: User
"""
if isinstance(user, User):
user_id = user.id
elif isinstance(user, UserResponse):
user_id = user.id
else:
user_id = user

return self._service.delete_users_id(user_id=user_id)

def find_users(self, **kwargs) -> Users:
"""List all users.
:key int offset: Offset for pagination
:key int limit: Limit for pagination
:key str after: The last resource ID from which to seek from (but not including).
This is to be used instead of `offset`.
:key str name: The user name.
:key str id: The user ID.
:return: Buckets
"""
return self._service.get_users(**kwargs)
6 changes: 5 additions & 1 deletion tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@


def generate_bucket_name():
return "test_bucket_" + str(datetime.datetime.now().timestamp()) + "_IT"
return generate_name(key="bucket")


def generate_name(key: str):
return f"test_{key}_" + str(datetime.datetime.now().timestamp()) + "_IT"


class BaseTest(unittest.TestCase):
Expand Down
12 changes: 12 additions & 0 deletions tests/test_BucketsApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@ def test_pagination(self):
buckets = self.buckets_api.find_buckets(limit=1).buckets
self.assertEqual(1, len(buckets))

def test_update_bucket(self):
my_org = self.find_my_org()

bucket = self.buckets_api.create_bucket(bucket_name=generate_bucket_name(),
org=my_org,
description="my description")
self.assertEqual("my description", bucket.description)

bucket.description = "updated description"
self.buckets_api.update_bucket(bucket=bucket)
self.assertEqual("updated description", bucket.description)


if __name__ == '__main__':
unittest.main()
24 changes: 24 additions & 0 deletions tests/test_OrganizationsApi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from tests.base_test import BaseTest, generate_name


class OrganizationsApiTests(BaseTest):

def setUp(self) -> None:
super(OrganizationsApiTests, self).setUp()
organizations_api = self.client.organizations_api()
organizations = organizations_api.find_organizations()

for organization in organizations:
if organization.name.endswith("_IT"):
print("Delete organization: ", organization.name)
organizations_api.delete_organization(org_id=organization.id)

def test_update_organization(self):
organizations_api = self.client.organizations_api()

organization = organizations_api.create_organization(name=generate_name(key='org'))
self.assertEqual("", organization.description)

organization.description = "updated description"
organization = organizations_api.update_organization(organization=organization)
self.assertEqual("updated description", organization.description)
45 changes: 45 additions & 0 deletions tests/test_UsersApi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import pytest

from influxdb_client import UserResponse
from influxdb_client.rest import ApiException
from tests.base_test import BaseTest, generate_name


class UsersApiTests(BaseTest):

def setUp(self) -> None:
super(UsersApiTests, self).setUp()
users_api = self.client.users_api()
users = users_api.find_users()

for user in users.users:
if user.name.endswith("_IT"):
print("Delete user: ", user.name)
users_api.delete_user(user=user)

def test_delete_user(self):
users_api = self.client.users_api()

user = users_api.create_user(name=generate_name(key='user'))
users = users_api.find_users(id=user.id)
self.assertEqual(1, len(users.users))
self.assertEqual(user, users.users[0])

users_api.delete_user(user)

with pytest.raises(ApiException) as e:
assert users_api.find_users(id=user.id)
assert "user not found" in e.value.body

def test_update_user(self):
users_api = self.client.users_api()

name = generate_name(key='user')
user = users_api.create_user(name=name)
self.assertEqual(name, user.name)

user.name = "updated_" + name
user = users_api.update_user(user=user)
self.assertIsInstance(user, UserResponse)
user = users_api.find_users(id=user.id).users[0]
self.assertEqual("updated_" + name, user.name)

0 comments on commit 81e7d21

Please sign in to comment.