def login(username=None, password=None, service_principal=None, tenant=None, allow_no_subscriptions=False, msi=False, msi_port=DefaultStr(50342)): """Log in to access Azure subscriptions""" import os import re from adal.adal_error import AdalError import requests # quick argument usage check if (any([username, password, service_principal, tenant, allow_no_subscriptions]) and any([msi, not getattr(msi_port, 'is_default', None)])): raise CLIError("usage error: '--msi/--msi-port' are not applicable with other arguments") interactive = False profile = Profile() if _in_cloud_console(): console_tokens = os.environ.get('AZURE_CONSOLE_TOKENS', None) if console_tokens: return profile.find_subscriptions_in_cloud_console(re.split(';|,', console_tokens)) else: raise CLIError(_CLOUD_CONSOLE_ERR_TEMPLATE.format('login')) if msi: return profile.find_subscriptions_in_vm_with_msi(msi_port) if username: if not password: try: password = prompt_pass('Password: '******'Please specify both username and password in non-interactive mode.') else: interactive = True try: subscriptions = profile.find_subscriptions_on_login( interactive, username, password, service_principal, tenant, allow_no_subscriptions=allow_no_subscriptions) except AdalError as err: # try polish unfriendly server errors if username: msg = str(err) suggestion = "For cross-check, try 'az login' to authenticate through browser." if ('ID3242:' in msg) or ('Server returned an unknown AccountType' in msg): raise CLIError("The user name might be invalid. " + suggestion) if 'Server returned error in RSTR - ErrorCode' in msg: raise CLIError("Logging in through command line is not supported. " + suggestion) raise CLIError(err) except requests.exceptions.ConnectionError as err: raise CLIError('Please ensure you have network connection. Error detail: ' + str(err)) all_subscriptions = list(subscriptions) for sub in all_subscriptions: sub['cloudName'] = sub.pop('environmentName', None) return all_subscriptions
def update_argument(self, param_name, argtype): from azure.cli.core.commands.validators import DefaultStr, DefaultInt arg = self.arguments[param_name] self._resolve_default_value_from_cfg_file(arg, argtype) arg.type.update(other=argtype) arg_default = arg.type.settings.get('default', None) if isinstance(arg_default, str): arg_default = DefaultStr(arg_default) elif isinstance(arg_default, int): arg_default = DefaultInt(arg_default) if arg_default: arg.type.settings['default'] = arg_default
def _resolve_default_value_from_cfg_file(self, arg, overrides): from azure.cli.core._config import DEFAULTS_SECTION from azure.cli.core.commands.validators import DefaultStr if not hasattr(arg.type, 'required_tooling'): required = arg.type.settings.get('required', False) setattr(arg.type, 'required_tooling', required) if 'configured_default' in overrides.settings: def_config = overrides.settings.pop('configured_default', None) setattr(arg.type, 'default_name_tooling', def_config) # 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 config_value = self.cli_ctx.config.get(DEFAULTS_SECTION, def_config, None) if config_value: logger.info("Configured default '%s' for arg %s", config_value, arg.name) overrides.settings['default'] = DefaultStr(config_value) overrides.settings['required'] = False