Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/azure-cli-core/azure/cli/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,8 @@ def command_group(self, group_name, command_type=None, **kwargs):
def argument_context(self, scope, **kwargs):
return self._argument_context_cls(self, scope, **kwargs)

# Please use add_cli_command instead of _cli_command.
# Currently "keyvault" and "batch" modules are still rely on this function, so it cannot be removed now.
def _cli_command(self, name, operation=None, handler=None, argument_loader=None, description_loader=None, **kwargs):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internal functions are replaced by CommandOperation


from knack.deprecation import Deprecated
Expand Down Expand Up @@ -829,6 +831,31 @@ def default_description_loader():
handler or default_command_handler,
**kwargs)

def add_cli_command(self, name, command_operation, **kwargs):
"""Register a command in command_table with command operation provided"""
from knack.deprecation import Deprecated
from .commands.command_operation import BaseCommandOperation
if not issubclass(type(command_operation), BaseCommandOperation):
raise TypeError("CommandOperation must be an instance of subclass of BaseCommandOperation."
" Got instance of '{}'".format(type(command_operation)))

kwargs['deprecate_info'] = Deprecated.ensure_new_style_deprecation(self.cli_ctx, kwargs, 'command')

name = ' '.join(name.split())

if self.supported_api_version(resource_type=kwargs.get('resource_type'),
min_api=kwargs.get('min_api'),
max_api=kwargs.get('max_api'),
operation_group=kwargs.get('operation_group')):
self._populate_command_group_table_with_subgroups(' '.join(name.split()[:-1]))
self.command_table[name] = self.command_cls(loader=self,
name=name,
handler=command_operation.handler,
arguments_loader=command_operation.arguments_loader,
description_loader=command_operation.description_loader,
command_operation=command_operation,
**kwargs)

def get_op_handler(self, operation, operation_group=None):
""" Import and load the operation handler """
# Patch the unversioned sdk path to include the appropriate API version for the
Expand Down
82 changes: 58 additions & 24 deletions src/azure-cli-core/azure/cli/core/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,12 @@ def update(self, other=None, **kwargs):
class AzCommandGroup(CommandGroup):

def __init__(self, command_loader, group_name, **kwargs):
"""
:param command_loader: The command loader that commands will be registered into
:type command_loader: azure.cli.core.AzCommandsLoader
:param group_name: The name of the group of commands in the command hierarchy
:type group_name: str
"""
merged_kwargs = self._merge_kwargs(kwargs, base_kwargs=command_loader.module_kwargs)
operations_tmpl = merged_kwargs.pop('operations_tmpl', None)
super(AzCommandGroup, self).__init__(command_loader, group_name,
Expand Down Expand Up @@ -1244,16 +1250,24 @@ def custom_command(self, name, method_name=None, **kwargs):
return self._command(name, method_name=method_name, custom_command=True, **kwargs)

def _command(self, name, method_name, custom_command=False, **kwargs):
from .command_operation import CommandOperation

self._check_stale()
merged_kwargs = self._flatten_kwargs(kwargs, get_command_type_kwarg(custom_command))
self._apply_tags(merged_kwargs, kwargs, name)

operations_tmpl = merged_kwargs['operations_tmpl']
command_name = '{} {}'.format(self.group_name, name) if self.group_name else name
self.command_loader._cli_command(command_name, # pylint: disable=protected-access
operation=operations_tmpl.format(method_name),
**merged_kwargs)
op_path = operations_tmpl.format(method_name)

command_name = '{} {}'.format(self.group_name, name) if self.group_name else name
command_operation = CommandOperation(
command_loader=self.command_loader,
op_path=op_path,
**merged_kwargs
)
self.command_loader.add_cli_command(command_name,
command_operation=command_operation,
**merged_kwargs)
return command_name

# pylint: disable=no-self-use
Expand Down Expand Up @@ -1283,27 +1297,31 @@ def generic_update_command(self, name, getter_name='get', getter_type=None,
setter_name='create_or_update', setter_type=None, setter_arg_name='parameters',
child_collection_prop_name=None, child_collection_key='name', child_arg_name='item_name',
custom_func_name=None, custom_func_type=None, **kwargs):
from azure.cli.core.commands.arm import _cli_generic_update_command
from azure.cli.core.commands.command_operation import GenericUpdateCommandOperation
self._check_stale()
merged_kwargs = self._flatten_kwargs(kwargs, get_command_type_kwarg())
merged_kwargs_custom = self._flatten_kwargs(kwargs, get_command_type_kwarg(custom_command=True))
self._apply_tags(merged_kwargs, kwargs, name)

getter_op = self._resolve_operation(merged_kwargs, getter_name, getter_type)
setter_op = self._resolve_operation(merged_kwargs, setter_name, setter_type)
custom_func_op = self._resolve_operation(merged_kwargs_custom, custom_func_name, custom_func_type,
custom_command=True) if custom_func_name else None
_cli_generic_update_command(
self.command_loader,
'{} {}'.format(self.group_name, name),
getter_op=getter_op,
setter_op=setter_op,
getter_op_path = self._resolve_operation(merged_kwargs, getter_name, getter_type)
setter_op_path = self._resolve_operation(merged_kwargs, setter_name, setter_type)
custom_function_op_path = self._resolve_operation(merged_kwargs_custom, custom_func_name, custom_func_type,
custom_command=True) if custom_func_name else None
command_name = '{} {}'.format(self.group_name, name) if self.group_name else name
command_operation = GenericUpdateCommandOperation(
command_loader=self.command_loader,
getter_op_path=getter_op_path,
setter_op_path=setter_op_path,
setter_arg_name=setter_arg_name,
custom_function_op=custom_func_op,
custom_function_op_path=custom_function_op_path,
child_collection_prop_name=child_collection_prop_name,
child_collection_key=child_collection_key,
child_arg_name=child_arg_name,
**merged_kwargs)
**merged_kwargs
)
self.command_loader.add_cli_command(command_name,
command_operation=command_operation,
**merged_kwargs)

def wait_command(self, name, getter_name='get', **kwargs):
self._wait_command(name, getter_name=getter_name, custom_command=False, **kwargs)
Expand All @@ -1315,16 +1333,24 @@ def generic_wait_command(self, name, getter_name='get', getter_type=None, **kwar
self._wait_command(name, getter_name=getter_name, getter_type=getter_type, **kwargs)

def _wait_command(self, name, getter_name='get', getter_type=None, custom_command=False, **kwargs):
from azure.cli.core.commands.arm import _cli_wait_command
from azure.cli.core.commands.command_operation import WaitCommandOperation
self._check_stale()
merged_kwargs = self._flatten_kwargs(kwargs, get_command_type_kwarg(custom_command))
self._apply_tags(merged_kwargs, kwargs, name)

if getter_type:
merged_kwargs = _merge_kwargs(getter_type.settings, merged_kwargs, CLI_COMMAND_KWARGS)
getter_op = self._resolve_operation(merged_kwargs, getter_name, getter_type, custom_command=custom_command)
_cli_wait_command(self.command_loader, '{} {}'.format(self.group_name, name), getter_op=getter_op,
custom_command=custom_command, **merged_kwargs)
getter_op_path = self._resolve_operation(merged_kwargs, getter_name, getter_type, custom_command=custom_command)

command_name = '{} {}'.format(self.group_name, name) if self.group_name else name
command_operation = WaitCommandOperation(
command_loader=self.command_loader,
op_path=getter_op_path,
**merged_kwargs
)
self.command_loader.add_cli_command(command_name,
command_operation=command_operation,
**merged_kwargs)

def show_command(self, name, getter_name='get', **kwargs):
self._show_command(name, getter_name=getter_name, custom_command=False, **kwargs)
Expand All @@ -1333,16 +1359,24 @@ def custom_show_command(self, name, getter_name='get', **kwargs):
self._show_command(name, getter_name=getter_name, custom_command=True, **kwargs)

def _show_command(self, name, getter_name='get', getter_type=None, custom_command=False, **kwargs):
from azure.cli.core.commands.arm import _cli_show_command
from azure.cli.core.commands.command_operation import ShowCommandOperation
self._check_stale()
merged_kwargs = self._flatten_kwargs(kwargs, get_command_type_kwarg(custom_command))
self._apply_tags(merged_kwargs, kwargs, name)

if getter_type:
merged_kwargs = _merge_kwargs(getter_type.settings, merged_kwargs, CLI_COMMAND_KWARGS)
getter_op = self._resolve_operation(merged_kwargs, getter_name, getter_type, custom_command=custom_command)
_cli_show_command(self.command_loader, '{} {}'.format(self.group_name, name), getter_op=getter_op,
custom_command=custom_command, **merged_kwargs)
op_path = self._resolve_operation(merged_kwargs, getter_name, getter_type, custom_command=custom_command)

command_name = '{} {}'.format(self.group_name, name) if self.group_name else name
command_operation = ShowCommandOperation(
command_loader=self.command_loader,
op_path=op_path,
**merged_kwargs
)
self.command_loader.add_cli_command(command_name,
command_operation=command_operation,
**merged_kwargs)

def _apply_tags(self, merged_kwargs, kwargs, command_name):
# don't inherit deprecation or preview info from command group
Expand Down
Loading