Skip to content

Commit 5ceb049

Browse files
committed
Finish drop / alter / show role and list roles
Signed-off-by: Jin Hai <haijin.chn@gmail.com>
1 parent 05b1401 commit 5ceb049

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

admin/client/admin_client.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -712,21 +712,28 @@ def _create_role(self, command):
712712
response = requests.post(
713713
url,
714714
auth=HTTPBasicAuth(self.admin_account, self.admin_password),
715-
json={'role_name': role_name}
715+
json={'role_name': role_name, 'description': desc_str}
716716
)
717717
res_json = response.json()
718718
if response.status_code == 200:
719719
self._print_table_simple(res_json['data'])
720720
else:
721721
print(f"Fail to create role {role_name}, code: {res_json['code']}, message: {res_json['message']}")
722722

723-
pass
724-
725723
def _drop_role(self, command):
726724
role_name_tree: Tree = command['role_name']
727725
role_name: str = role_name_tree.children[0].strip("'\"")
728726
print(f"drop role name: {role_name}")
729-
pass
727+
url = f'http://{self.host}:{self.port}/api/v1/admin/roles/{role_name}'
728+
response = requests.delete(
729+
url,
730+
auth=HTTPBasicAuth(self.admin_account, self.admin_password),
731+
)
732+
res_json = response.json()
733+
if response.status_code == 200:
734+
self._print_table_simple(res_json['data'])
735+
else:
736+
print(f"Fail to drop role {role_name}, code: {res_json['code']}, message: {res_json['message']}")
730737

731738
def _alter_role(self, command):
732739
role_name_tree: Tree = command['role_name']
@@ -735,17 +742,46 @@ def _alter_role(self, command):
735742
desc_str: str = desc_tree.children[0].strip("'\"")
736743

737744
print(f"alter role name: {role_name}, description: {desc_str}")
738-
pass
745+
url = f'http://{self.host}:{self.port}/api/v1/admin/roles/{role_name}'
746+
response = requests.put(
747+
url,
748+
auth=HTTPBasicAuth(self.admin_account, self.admin_password),
749+
json={'description': desc_str}
750+
)
751+
res_json = response.json()
752+
if response.status_code == 200:
753+
self._print_table_simple(res_json['data'])
754+
else:
755+
print(
756+
f"Fail to update role {role_name} with description: {desc_str}, code: {res_json['code']}, message: {res_json['message']}")
739757

740758
def _list_roles(self, command):
741759
print("Listing all roles")
742-
pass
760+
url = f'http://{self.host}:{self.port}/api/v1/admin/roles'
761+
response = requests.get(
762+
url,
763+
auth=HTTPBasicAuth(self.admin_account, self.admin_password)
764+
)
765+
res_json = response.json()
766+
if response.status_code == 200:
767+
self._print_table_simple(res_json['data'])
768+
else:
769+
print(f"Fail to list roles, code: {res_json['code']}, message: {res_json['message']}")
743770

744771
def _show_role(self, command):
745772
role_name_tree: Tree = command['role_name']
746773
role_name: str = role_name_tree.children[0].strip("'\"")
747774
print(f"show role: {role_name}")
748-
pass
775+
url = f'http://{self.host}:{self.port}/api/v1/admin/roles/{role_name}/permissions'
776+
response = requests.get(
777+
url,
778+
auth=HTTPBasicAuth(self.admin_account, self.admin_password)
779+
)
780+
res_json = response.json()
781+
if response.status_code == 200:
782+
self._print_table_simple(res_json['data'])
783+
else:
784+
print(f"Fail to list roles, code: {res_json['code']}, message: {res_json['message']}")
749785

750786
def _grant_permission(self, command):
751787
role_name_tree: Tree = command['role_name']

admin/server/roles.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,34 @@
2323

2424
class RoleMgr:
2525
@staticmethod
26-
def create_role(role_name: str):
27-
error_msg = f"not implement: create role: {role_name}"
26+
def create_role(role_name: str, description: str):
27+
error_msg = f"not implement: create role: {role_name}, description: {description}"
2828
logging.error(error_msg)
2929
raise AdminException(error_msg)
3030

3131
@staticmethod
3232
def update_role_description(role_name: str, description: str) -> Dict[str, Any]:
33-
raise AdminException(f"Not implement {inspect.currentframe().f_code.co_name}")
33+
error_msg = f"not implement: update role: {role_name} with description: {description}"
34+
logging.error(error_msg)
35+
raise AdminException(error_msg)
3436

3537
@staticmethod
3638
def delete_role(role_name: str) -> Dict[str, Any]:
37-
raise AdminException(f"Not implement {inspect.currentframe().f_code.co_name}")
39+
error_msg = f"not implement: drop role: {role_name}"
40+
logging.error(error_msg)
41+
raise AdminException(error_msg)
3842

3943
@staticmethod
4044
def list_roles() -> Dict[str, Any]:
41-
raise AdminException(f"Not implement {inspect.currentframe().f_code.co_name}")
45+
error_msg = f"not implement: list roles"
46+
logging.error(error_msg)
47+
raise AdminException(error_msg)
4248

4349
@staticmethod
4450
def get_role_permissions(role_name: str) -> Dict[str, Any]:
45-
raise AdminException(f"Not implement {inspect.currentframe().f_code.co_name}")
51+
error_msg = f"not implement: show role {role_name}"
52+
logging.error(error_msg)
53+
raise AdminException(error_msg)
4654

4755
@staticmethod
4856
def grant_role_permissions(role_name: str, permissions: str) -> Dict[str, Any]:

admin/server/routes.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ def create_role():
218218
if not data or 'role_name' not in data:
219219
return error_response("Role name is required", 400)
220220
role_name: str = data['role_name']
221-
res = RoleMgr.create_role(role_name)
221+
description: str = data['description']
222+
res = RoleMgr.create_role(role_name, description)
222223
return success_response(res)
223224
except Exception as e:
224225
return error_response(str(e), 500)
@@ -229,10 +230,10 @@ def create_role():
229230
def update_role(role_name: str):
230231
try:
231232
data = request.get_json()
232-
if not data or 'role_description' not in data:
233+
if not data or 'description' not in data:
233234
return error_response("Role description is required", 400)
234-
role_description: str = data['role_description']
235-
res = RoleMgr.update_role_description(role_name, role_description)
235+
description: str = data['description']
236+
res = RoleMgr.update_role_description(role_name, description)
236237
return success_response(res)
237238
except Exception as e:
238239
return error_response(str(e), 500)

0 commit comments

Comments
 (0)