From 3c431a54d96ebc9bdb911161468d0f6bedbbcbfe Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 27 Dec 2020 12:47:02 +0800 Subject: [PATCH] mgr/dashboard: use param annotation for args desc use parameter annotation for CLICommand arg desc less repeating this way Signed-off-by: Kefu Chai --- src/mypy.ini | 1 + src/pybind/mgr/dashboard/module.py | 15 ++- .../mgr/dashboard/plugins/feature_toggles.py | 12 +- .../mgr/dashboard/services/access_control.py | 122 +++++++----------- .../mgr/dashboard/services/iscsi_cli.py | 11 +- src/pybind/mgr/test_orchestrator/module.py | 2 +- 6 files changed, 70 insertions(+), 93 deletions(-) diff --git a/src/mypy.ini b/src/mypy.ini index 3371557f10d..bae404dafdf 100755 --- a/src/mypy.ini +++ b/src/mypy.ini @@ -4,6 +4,7 @@ no_implicit_optional = True warn_incomplete_stub = True check_untyped_defs = True show_error_context = True +allow_redefinition = True [mypy-rados] # This would require a rados.pyi file diff --git a/src/pybind/mgr/dashboard/module.py b/src/pybind/mgr/dashboard/module.py index 0c902d909f1..087eda57380 100644 --- a/src/pybind/mgr/dashboard/module.py +++ b/src/pybind/mgr/dashboard/module.py @@ -14,6 +14,7 @@ import sys import tempfile import threading import time +from typing import Optional from mgr_module import CLIWriteCommand, MgrModule, MgrStandbyModule, Option from mgr_util import ServerConfigException, create_self_signed_cert, \ @@ -358,9 +359,10 @@ class Module(MgrModule, CherryPyConfig): logger.info('Stopping engine...') self.shutdown_event.set() - @CLIWriteCommand("dashboard set-ssl-certificate", - "name=mgr_id,type=CephString,req=false") - def set_ssl_certificate(self, mgr_id=None, inbuf=None): + @CLIWriteCommand("dashboard set-ssl-certificate") + def set_ssl_certificate(self, + mgr_id: Optional[str] = None, + inbuf: Optional[bytes] = None): if inbuf is None: return -errno.EINVAL, '',\ 'Please specify the certificate file with "-i" option' @@ -370,9 +372,10 @@ class Module(MgrModule, CherryPyConfig): self.set_store('crt', inbuf) return 0, 'SSL certificate updated', '' - @CLIWriteCommand("dashboard set-ssl-certificate-key", - "name=mgr_id,type=CephString,req=false") - def set_ssl_certificate_key(self, mgr_id=None, inbuf=None): + @CLIWriteCommand("dashboard set-ssl-certificate-key") + def set_ssl_certificate_key(self, + mgr_id: Optional[str] = None, + inbuf: Optional[bytes] = None): if inbuf is None: return -errno.EINVAL, '',\ 'Please specify the certificate key file with "-i" option' diff --git a/src/pybind/mgr/dashboard/plugins/feature_toggles.py b/src/pybind/mgr/dashboard/plugins/feature_toggles.py index 76d18818340..3085d1977e5 100644 --- a/src/pybind/mgr/dashboard/plugins/feature_toggles.py +++ b/src/pybind/mgr/dashboard/plugins/feature_toggles.py @@ -2,6 +2,7 @@ from __future__ import absolute_import from enum import Enum +from typing import List, Optional import cherrypy from mgr_module import CLICommand, Option @@ -77,13 +78,10 @@ class FeatureToggles(I.CanMgr, I.Setupable, I.HasOptions, @PM.add_hook def register_commands(self): - @CLICommand( - "dashboard feature", - "name=action,type=CephChoices,strings={} ".format( - "|".join(a.value for a in Actions)) - + "name=features,type=CephChoices,strings={},req=false,n=N".format( - "|".join(f.value for f in Features))) - def cmd(mgr, action, features=None): + @CLICommand("dashboard feature") + def cmd(mgr, + action: Actions, + features: Optional[List[Features]] = None): ''' Enable or disable features in Ceph-Mgr Dashboard ''' diff --git a/src/pybind/mgr/dashboard/services/access_control.py b/src/pybind/mgr/dashboard/services/access_control.py index 433779f7f63..bb44bad9e9a 100644 --- a/src/pybind/mgr/dashboard/services/access_control.py +++ b/src/pybind/mgr/dashboard/services/access_control.py @@ -11,6 +11,7 @@ import threading import time from datetime import datetime, timedelta from string import ascii_lowercase, ascii_uppercase, digits, punctuation +from typing import List, Optional, Sequence import bcrypt from mgr_module import CLICheckNonemptyFileInput, CLIReadCommand, CLIWriteCommand @@ -579,10 +580,9 @@ def load_access_control_db(): # CLI dashboard access control scope commands -@CLIWriteCommand('dashboard set-login-credentials', - 'name=username,type=CephString') +@CLIWriteCommand('dashboard set-login-credentials') @CLICheckNonemptyFileInput -def set_login_credentials_cmd(_, username, inbuf): +def set_login_credentials_cmd(_, username: str, inbuf: str): ''' Set the login credentials. Password read from -i ''' @@ -604,9 +604,8 @@ def set_login_credentials_cmd(_, username, inbuf): Username and password updated''', '' -@CLIReadCommand('dashboard ac-role-show', - 'name=rolename,type=CephString,req=false') -def ac_role_show_cmd(_, rolename=None): +@CLIReadCommand('dashboard ac-role-show') +def ac_role_show_cmd(_, rolename: Optional[str] = None): ''' Show role info ''' @@ -624,10 +623,8 @@ def ac_role_show_cmd(_, rolename=None): return 0, json.dumps(role.to_dict()), '' -@CLIWriteCommand('dashboard ac-role-create', - 'name=rolename,type=CephString ' - 'name=description,type=CephString,req=false') -def ac_role_create_cmd(_, rolename, description=None): +@CLIWriteCommand('dashboard ac-role-create') +def ac_role_create_cmd(_, rolename: str, description: Optional[str] = None): ''' Create a new access control role ''' @@ -639,9 +636,8 @@ def ac_role_create_cmd(_, rolename, description=None): return -errno.EEXIST, '', str(ex) -@CLIWriteCommand('dashboard ac-role-delete', - 'name=rolename,type=CephString') -def ac_role_delete_cmd(_, rolename): +@CLIWriteCommand('dashboard ac-role-delete') +def ac_role_delete_cmd(_, rolename: str): ''' Delete an access control role ''' @@ -658,11 +654,11 @@ def ac_role_delete_cmd(_, rolename): return -errno.EPERM, '', str(ex) -@CLIWriteCommand('dashboard ac-role-add-scope-perms', - 'name=rolename,type=CephString ' - 'name=scopename,type=CephString ' - 'name=permissions,type=CephString,n=N') -def ac_role_add_scope_perms_cmd(_, rolename, scopename, permissions): +@CLIWriteCommand('dashboard ac-role-add-scope-perms') +def ac_role_add_scope_perms_cmd(_, + rolename: str, + scopename: str, + permissions: Sequence[str]): ''' Add the scope permissions for a role ''' @@ -687,10 +683,8 @@ def ac_role_add_scope_perms_cmd(_, rolename, scopename, permissions): .format(Permission.all_permissions()) -@CLIWriteCommand('dashboard ac-role-del-scope-perms', - 'name=rolename,type=CephString ' - 'name=scopename,type=CephString') -def ac_role_del_scope_perms_cmd(_, rolename, scopename): +@CLIWriteCommand('dashboard ac-role-del-scope-perms') +def ac_role_del_scope_perms_cmd(_, rolename: str, scopename: str): ''' Delete the scope permissions for a role ''' @@ -709,9 +703,8 @@ def ac_role_del_scope_perms_cmd(_, rolename, scopename): return -errno.ENOENT, '', str(ex) -@CLIReadCommand('dashboard ac-user-show', - 'name=username,type=CephString,req=false') -def ac_user_show_cmd(_, username=None): +@CLIReadCommand('dashboard ac-user-show') +def ac_user_show_cmd(_, username: Optional[str] = None): ''' Show user info ''' @@ -726,19 +719,16 @@ def ac_user_show_cmd(_, username=None): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-create', - 'name=username,type=CephString ' - 'name=rolename,type=CephString,req=false ' - 'name=name,type=CephString,req=false ' - 'name=email,type=CephString,req=false ' - 'name=enabled,type=CephBool,req=false ' - 'name=force_password,type=CephBool,req=false ' - 'name=pwd_expiration_date,type=CephInt,req=false ' - 'name=pwd_update_required,type=CephBool,req=false') +@CLIWriteCommand('dashboard ac-user-create') @CLICheckNonemptyFileInput -def ac_user_create_cmd(_, username, inbuf, rolename=None, name=None, - email=None, enabled=True, force_password=False, - pwd_expiration_date=None, pwd_update_required=False): +def ac_user_create_cmd(_, username: str, inbuf: str, + rolename: Optional[str] = None, + name: Optional[str] = None, + email: Optional[str] = None, + enabled: bool = True, + force_password: bool = False, + pwd_expiration_date: Optional[int] = None, + pwd_update_required: bool = False): ''' Create a user. Password read from -i ''' @@ -768,9 +758,8 @@ def ac_user_create_cmd(_, username, inbuf, rolename=None, name=None, return 0, json.dumps(user.to_dict()), '' -@CLIWriteCommand('dashboard ac-user-enable', - 'name=username,type=CephString') -def ac_user_enable(_, username): +@CLIWriteCommand('dashboard ac-user-enable') +def ac_user_enable(_, username: str): ''' Enable a user ''' @@ -785,9 +774,8 @@ def ac_user_enable(_, username): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-disable', - 'name=username,type=CephString') -def ac_user_disable(_, username): +@CLIWriteCommand('dashboard ac-user-disable') +def ac_user_disable(_, username: str): ''' Disable a user ''' @@ -801,9 +789,8 @@ def ac_user_disable(_, username): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-delete', - 'name=username,type=CephString') -def ac_user_delete_cmd(_, username): +@CLIWriteCommand('dashboard ac-user-delete') +def ac_user_delete_cmd(_, username: str): ''' Delete user ''' @@ -815,15 +802,13 @@ def ac_user_delete_cmd(_, username): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-set-roles', - 'name=username,type=CephString ' - 'name=roles,type=CephString,n=N') -def ac_user_set_roles_cmd(_, username, roles): +@CLIWriteCommand('dashboard ac-user-set-roles') +def ac_user_set_roles_cmd(_, username: str, roles: Sequence[str]): ''' Set user roles ''' rolesname = roles - roles = [] + roles: List[Role] = [] for rolename in rolesname: try: roles.append(mgr.ACCESS_CTRL_DB.get_role(rolename)) @@ -840,15 +825,13 @@ def ac_user_set_roles_cmd(_, username, roles): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-add-roles', - 'name=username,type=CephString ' - 'name=roles,type=CephString,n=N') -def ac_user_add_roles_cmd(_, username, roles): +@CLIWriteCommand('dashboard ac-user-add-roles') +def ac_user_add_roles_cmd(_, username: str, roles: Sequence[str]): ''' Add roles to user ''' rolesname = roles - roles = [] + roles: List[Role] = [] for rolename in rolesname: try: roles.append(mgr.ACCESS_CTRL_DB.get_role(rolename)) @@ -865,15 +848,13 @@ def ac_user_add_roles_cmd(_, username, roles): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-del-roles', - 'name=username,type=CephString ' - 'name=roles,type=CephString,n=N') -def ac_user_del_roles_cmd(_, username, roles): +@CLIWriteCommand('dashboard ac-user-del-roles') +def ac_user_del_roles_cmd(_, username: str, roles: Sequence[str]): ''' Delete roles from user ''' rolesname = roles - roles = [] + roles: List[Role] = [] for rolename in rolesname: try: roles.append(mgr.ACCESS_CTRL_DB.get_role(rolename)) @@ -892,11 +873,10 @@ def ac_user_del_roles_cmd(_, username, roles): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-set-password', - 'name=username,type=CephString ' - 'name=force_password,type=CephBool,req=false') +@CLIWriteCommand('dashboard ac-user-set-password') @CLICheckNonemptyFileInput -def ac_user_set_password(_, username, inbuf, force_password=False): +def ac_user_set_password(_, username: str, inbuf: str, + force_password: bool = False): ''' Set user password from -i ''' @@ -915,10 +895,9 @@ def ac_user_set_password(_, username, inbuf, force_password=False): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-set-password-hash', - 'name=username,type=CephString') +@CLIWriteCommand('dashboard ac-user-set-password-hash') @CLICheckNonemptyFileInput -def ac_user_set_password_hash(_, username, inbuf): +def ac_user_set_password_hash(_, username: str, inbuf: str): ''' Set user password bcrypt hash from -i ''' @@ -937,11 +916,8 @@ def ac_user_set_password_hash(_, username, inbuf): return -errno.ENOENT, '', str(ex) -@CLIWriteCommand('dashboard ac-user-set-info', - 'name=username,type=CephString ' - 'name=name,type=CephString ' - 'name=email,type=CephString') -def ac_user_set_info(_, username, name, email): +@CLIWriteCommand('dashboard ac-user-set-info') +def ac_user_set_info(_, username: str, name: str, email: str): ''' Set user info ''' diff --git a/src/pybind/mgr/dashboard/services/iscsi_cli.py b/src/pybind/mgr/dashboard/services/iscsi_cli.py index 7f72495d932..93abbcd190a 100644 --- a/src/pybind/mgr/dashboard/services/iscsi_cli.py +++ b/src/pybind/mgr/dashboard/services/iscsi_cli.py @@ -3,6 +3,7 @@ from __future__ import absolute_import import errno import json +from typing import Optional from mgr_module import CLICheckNonemptyFileInput, CLIReadCommand, CLIWriteCommand @@ -21,10 +22,9 @@ def list_iscsi_gateways(_): return 0, json.dumps(IscsiGatewaysConfig.get_gateways_config()), '' -@CLIWriteCommand('dashboard iscsi-gateway-add', - 'name=name,type=CephString,req=false') +@CLIWriteCommand('dashboard iscsi-gateway-add') @CLICheckNonemptyFileInput -def add_iscsi_gateway(_, inbuf, name=None): +def add_iscsi_gateway(_, inbuf, name: Optional[str] = None): ''' Add iSCSI gateway configuration. Gateway URL read from -i ''' @@ -45,9 +45,8 @@ def add_iscsi_gateway(_, inbuf, name=None): return -errno.EINVAL, '', str(ex) -@CLIWriteCommand('dashboard iscsi-gateway-rm', - 'name=name,type=CephString') -def remove_iscsi_gateway(_, name): +@CLIWriteCommand('dashboard iscsi-gateway-rm') +def remove_iscsi_gateway(_, name: str): ''' Remove iSCSI gateway configuration ''' diff --git a/src/pybind/mgr/test_orchestrator/module.py b/src/pybind/mgr/test_orchestrator/module.py index 6a8318beda4..c8a422d6c57 100644 --- a/src/pybind/mgr/test_orchestrator/module.py +++ b/src/pybind/mgr/test_orchestrator/module.py @@ -75,7 +75,7 @@ class TestOrchestrator(MgrModule, orchestrator.Orchestrator): for p in completions: p.evaluate() - @CLICommand('test_orchestrator load_data', args='', perm='w') + @CLICommand('test_orchestrator load_data', perm='w') def _load_data(self, inbuf): """ load dummy data into test orchestrator -- 2.39.5