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
1 change: 1 addition & 0 deletions src/azure-cli-core/azure/cli/core/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
CONFIG_FILE_NAME = 'config'
GLOBAL_CONFIG_PATH = os.path.join(GLOBAL_CONFIG_DIR, CONFIG_FILE_NAME)
ENV_VAR_PREFIX = 'AZURE_'
DEFAULTS_SECTION = 'defaults'

_UNSET = object()
_ENV_VAR_FORMAT = ENV_VAR_PREFIX + '{section}_{option}'
Expand Down
17 changes: 16 additions & 1 deletion src/azure-cli-core/azure/cli/core/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from azure.cli.core._util import CLIError
from azure.cli.core.application import APPLICATION
from azure.cli.core.prompting import prompt_y_n, NoTTYException
from azure.cli.core._config import az_config
from azure.cli.core._config import az_config, DEFAULTS_SECTION

from ._introspection import (extract_args_from_signature,
extract_full_summary_from_signature)
Expand Down Expand Up @@ -204,8 +204,23 @@ def add_argument(self, param_name, *option_strings, **kwargs):

def update_argument(self, param_name, argtype):
arg = self.arguments[param_name]
self._resolve_default_value_from_cfg_file(arg, argtype)
arg.type.update(other=argtype)

def _resolve_default_value_from_cfg_file(self, arg, overrides):
if 'configured_default' in overrides.settings:
def_config = overrides.settings.pop('configured_default', None)
# same blunt mechanism like we handled id-parts, for create command, no name default
if (self.name.split()[-1] == 'create' and
overrides.settings.get('metavar', None) == 'NAME'):
return
if arg.type.settings.get('required', False):
setattr(arg.type, 'configured_default_applied', True)
config_value = az_config.get(DEFAULTS_SECTION, def_config, None)
if config_value:
overrides.settings['default'] = config_value
overrides.settings['required'] = False

def execute(self, **kwargs):
return self.handler(**kwargs)

Expand Down
9 changes: 5 additions & 4 deletions src/azure-cli-core/azure/cli/core/commands/arm.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,11 @@ def __call__(self, parser, namespace, values, option_string=None):
existing_values.append(parts[arg.id_part])
else:
if isinstance(existing_values, str):
logger.warning(
"Property '%s=%s' being overriden by value '%s' from IDs parameter.", # pylint: disable=line-too-long
arg.name, existing_values, parts[arg.id_part]
)
if not getattr(arg.type, 'configured_default_applied', None):
logger.warning(
"Property '%s=%s' being overriden by value '%s' from IDs parameter.", # pylint: disable=line-too-long
arg.name, existing_values, parts[arg.id_part]
)
existing_values = IterateValue()
existing_values.append(parts[arg.id_part])
setattr(namespace, arg.name, existing_values)
Expand Down
7 changes: 5 additions & 2 deletions src/azure-cli-core/azure/cli/core/commands/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,18 @@ def __call__(self, parser, namespace, values, option_string=None):
options_list=('--resource-group', '-g'),
completer=get_resource_group_completion_list,
id_part='resource_group',
help='Name of resource group')
help="Name of resource group. You can configure the default group using 'az configure --defaults group=<name>'",
configured_default='group')

name_type = CliArgumentType(options_list=('--name', '-n'), help='the primary resource name')

location_type = CliArgumentType(
options_list=('--location', '-l'),
completer=get_location_completion_list,
type=location_name_type,
help='Location.', metavar='LOCATION')
help="Location. You can configure the default location using 'az configure --defaults location=<location>'",
metavar='LOCATION',
configured_default='location')

deployment_name_type = CliArgumentType(
help=argparse.SUPPRESS,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def get_hostname_completion_list(prefix, action, parsed_args, **kwargs): # pylin
register_cli_argument('appservice plan', 'admin_site_name', help='The name of the admin web app.')

register_cli_argument('appservice web', 'slot', options_list=('--slot', '-s'), help="the name of the slot. Default to the productions slot if not specified")
register_cli_argument('appservice web', 'name', arg_type=name_arg_type, completer=get_resource_name_completion_list('Microsoft.Web/sites'), id_part='name', help='name of the web')
register_cli_argument('appservice web', 'name', configured_default='web',
arg_type=name_arg_type, completer=get_resource_name_completion_list('Microsoft.Web/sites'), id_part='name',
help="name of the web. You can configure the default using 'az configure --defaults web=<name>'")
register_cli_argument('appservice web create', 'name', options_list=('--name', '-n'), help='name of the new webapp')
register_cli_argument('appservice web create', 'plan', options_list=('--plan', '-p'), completer=get_resource_name_completion_list('Microsoft.Web/serverFarms'),
help="name or resource id of the app service plan. Use 'appservice plan create' to get one")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import azure.cli.command_modules.configure._help # pylint: disable=unused-import

def load_params(_):
pass
import azure.cli.command_modules.configure._params #pylint: disable=redefined-outer-name

def load_commands():
import azure.cli.command_modules.configure.commands #pylint: disable=redefined-outer-name
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import register_cli_argument

# pylint: disable=line-too-long
register_cli_argument('configure', 'defaults', nargs='+',
help="space separated 'name=value' pairs for common arguments defaults, e.g. '--defaults group=myRG web=myweb vm=myvm'. Use '' to clear the defaults, e.g. --defaults vm='' web=''")
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from adal.adal_error import AdalError

import azure.cli.core.azlogging as azlogging
from azure.cli.core._config import (GLOBAL_CONFIG_PATH, ENV_VAR_PREFIX, set_global_config)
from azure.cli.core._config import (GLOBAL_CONFIG_PATH, ENV_VAR_PREFIX, set_global_config,
set_global_config_value, DEFAULTS_SECTION)
from azure.cli.core._util import CLIError
from azure.cli.core.prompting import (prompt,
prompt_y_n,
Expand Down Expand Up @@ -129,7 +130,16 @@ def _handle_global_configuration():
global_config.set('logging', 'enable_log_file', 'yes' if enable_file_logging else 'no')
set_global_config(global_config)

def handle_configure():
def handle_configure(defaults=None):
if defaults:
for default in defaults:
parts = default.split('=', 1)
if len(parts) == 1:
raise CLIError('usage error: --defaults STRING=STRING STRING=STRING ...')
set_global_config_value(DEFAULTS_SECTION, parts[0], _normalize_config_value(parts[1]))
return

# if nothing supplied, we go interactively
try:
print(MSG_INTRO)
_handle_global_configuration()
Expand All @@ -139,3 +149,10 @@ def handle_configure():
raise CLIError('This command is interactive and no tty available.')
except (EOFError, KeyboardInterrupt):
print()


def _normalize_config_value(value):
if value:
value = '' if value in ["''", '""'] else value
Copy link
Member

Choose a reason for hiding this comment

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

Is this to remove the default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

correct

return value

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
register_cli_argument('group deployment operation show', 'operation_ids', nargs='+', help='A list of operation ids to show')
register_cli_argument('group export', 'include_comments', action='store_true')
register_cli_argument('group export', 'include_parameter_default_value', action='store_true')
register_cli_argument('group create', 'resource_group_name', completer=None)
register_cli_argument('group create', 'rg_name', options_list=('--name', '-n'), help='name of the new resource group', completer=None)
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't use resource_group_name_type in parameters.py.
Does that mean the default RG won't apply here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, for create, we should not use the default RG

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FYI, for other renaming on vm/vmss/vmss, my recent push has got rid of it. I decided to handle them in one place, that for create command, the name part will not load the default configured value. This is not a new thing, we did the same on splitting id-part


register_cli_argument('tag', 'tag_name', options_list=('--name', '-n'))
register_cli_argument('tag', 'tag_value', options_list=('--value',))
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def list_resource_groups(tag=None): # pylint: disable=no-self-use
groups = rcf.resource_groups.list(filter=filter_text)
return list(groups)

def create_resource_group(resource_group_name, location, tags=None):
def create_resource_group(rg_name, location, tags=None):
''' Create a new resource group.
:param str resource_group_name:the desired resource group name
:param str location:the resource group location
Expand All @@ -60,7 +60,7 @@ def create_resource_group(resource_group_name, location, tags=None):
location=location,
tags=tags
)
return rcf.resource_groups.create_or_update(resource_group_name, parameters)
return rcf.resource_groups.create_or_update(rg_name, parameters)

def export_group_as_template(
resource_group_name, include_comments=False, include_parameter_default_value=False):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ def get_vm_size_completion_list(prefix, action, parsed_args, **kwargs): # pylin

name_arg_type = CliArgumentType(options_list=('--name', '-n'), metavar='NAME')
multi_ids_type = CliArgumentType(nargs='+')
existing_vm_name = CliArgumentType(overrides=name_arg_type, help='The name of the Virtual Machine', completer=get_resource_name_completion_list('Microsoft.Compute/virtualMachines'), id_part='name')
vmss_name_type = CliArgumentType(name_arg_type, completer=get_resource_name_completion_list('Microsoft.Compute/virtualMachineScaleSets'), help='Scale set name.', id_part='name')
existing_vm_name = CliArgumentType(overrides=name_arg_type,
configured_default='vm',
help="The name of the Virtual Machine. You can configure the default using 'az configure --defaults vm=<name>'",
completer=get_resource_name_completion_list('Microsoft.Compute/virtualMachines'), id_part='name')
vmss_name_type = CliArgumentType(name_arg_type,
configured_default='vmss',
completer=get_resource_name_completion_list('Microsoft.Compute/virtualMachineScaleSets'),
help="Scale set name. You can configure the default using 'az configure --defaults vmss=<name>'",
id_part='name')
disk_sku = CliArgumentType(required=False, help='underlying storage sku', **enum_choice_list(['Premium_LRS', 'Standard_LRS']))

# ARGUMENT REGISTRATION
Expand Down