diff --git a/azure-cli.pyproj b/azure-cli.pyproj index 8bf10d973..f01790c96 100644 --- a/azure-cli.pyproj +++ b/azure-cli.pyproj @@ -29,7 +29,7 @@ - + diff --git a/src/azure/cli/_logging.py b/src/azure/cli/_logging.py index c0dc58797..47b45839b 100644 --- a/src/azure/cli/_logging.py +++ b/src/azure/cli/_logging.py @@ -48,6 +48,9 @@ def configure_logging(argv, config): logger.level = stderr_handler.level = level logger.handlers.append(stderr_handler) + # Set logging level for all loggers + _logging.basicConfig(level=level) + if logfile: # Configure the handler that logs code to a text file log_handler = _logging.StreamHandler(open(logfile, 'w', encoding='utf-8')) diff --git a/src/azure/cli/commands/__init__.py b/src/azure/cli/commands/__init__.py index fbb05927c..0f4b0942a 100644 --- a/src/azure/cli/commands/__init__.py +++ b/src/azure/cli/commands/__init__.py @@ -7,7 +7,7 @@ COMMAND_MODULES = [ 'login', 'logout', 'network', - 'resourcegroup', + 'resource', 'storage', 'vm', ] diff --git a/src/azure/cli/commands/_command_creation.py b/src/azure/cli/commands/_command_creation.py index d891ae542..1d3784fa2 100644 --- a/src/azure/cli/commands/_command_creation.py +++ b/src/azure/cli/commands/_command_creation.py @@ -1,13 +1,10 @@ from .._profile import Profile import azure.cli._debug as _debug import azure.cli as cli -from .._logging import logger def get_mgmt_service_client(client_type, config_type): profile = Profile() config = config_type(*profile.get_login_credentials()) - config.log_name = 'az' - config.log_level = logger.level client = client_type(config) _debug.allow_debug_connection(client) diff --git a/src/azure/cli/commands/resource.py b/src/azure/cli/commands/resource.py new file mode 100644 index 000000000..5ce01c2df --- /dev/null +++ b/src/azure/cli/commands/resource.py @@ -0,0 +1,99 @@ +from .._argparse import IncorrectUsageError +from ..commands import command, description, option +from ._command_creation import get_mgmt_service_client +from .._locale import L + +from azure.mgmt.resource.resources import (ResourceManagementClient, + ResourceManagementClientConfiguration) + +@command('resource group list') +@description('List resource groups') +@option('--tag-name -tn ', L("the resource group's tag name")) +@option('--tag-value -tv ', L("the resource group's tag value")) +def list_groups(args, unexpected): #pylint: disable=unused-argument + from azure.mgmt.resource.resources.models import ResourceGroup, ResourceGroupFilter + + rmc = get_mgmt_service_client(ResourceManagementClient, ResourceManagementClientConfiguration) + + filters = [] + if args.get('tag-name'): + filters.append("tagname eq '{}'".format(args.get('tag-name'))) + if args.get('tag-value'): + filters.append("tagvalue eq '{}'".format(args.get('tag-value'))) + + filter_text = ' and '.join(filters) if len(filters) > 0 else None + + groups = rmc.resource_groups.list(filter=filter_text) + return list(groups) + +@command('resource show') +@description(L('Show details of a specific resource in a resource group or subscription')) +@option('--resource-group -g ', L('the resource group name'), required=True) +@option('--name -n ', L('the resource name'), required=True) +@option('--resource-type -r ', + L('the resource type in format: /'), required=True) +@option('--api-version -o ', L('the API version of the resource provider')) +@option('--parent ', + L('the name of the parent resource (if needed), in / format')) +def show_resource(args, unexpected): #pylint: disable=unused-argument + rmc = get_mgmt_service_client(ResourceManagementClient, ResourceManagementClientConfiguration) + + full_type = args.get('resource-type').split('/') + try: + provider_namespace = full_type[0] + resource_type = full_type[1] + except IndexError: + raise IncorrectUsageError('Parameter --resource-type must be in / format.') + + api_version = _resolve_api_version(args, rmc) + if not api_version: + raise IncorrectUsageError( + L('API version is required and could not be resolved for resource {}' + .format(full_type))) + + results = rmc.resources.get( + resource_group_name=args.get('resource-group'), + resource_name=args.get('name'), + resource_provider_namespace=provider_namespace, + resource_type=resource_type, + api_version=api_version, + parent_resource_path=args.get('parent', '') + ) + return results + +def _resolve_api_version(args, rmc): + api_version = args.get('api-version') + if api_version: + return api_version + + # if api-version not supplied, attempt to resolve using provider namespace + parent = args.get('parent') + full_type = args.get('resource-type').split('/') + try: + provider_namespace = full_type[0] + resource_type = full_type[1] + except IndexError: + raise IncorrectUsageError('Parameter --resource-type must be in / format.') + + if parent: + try: + parent_type = parent.split('/')[0] + except IndexError: + raise IncorrectUsageError('Parameter --parent must be in / format.') + + resource_type = "{}/{}".format(parent_type, resource_type) + else: + resource_type = resource_type + provider = rmc.providers.get(provider_namespace) + for t in provider.resource_types: + if t.resource_type == resource_type: + # Return first non-preview version + for version in t.api_versions: + if not version.find('preview'): + return version + # No non-preview version found. Take first preview version + try: + return t.api_versions[0] + except IndexError: + return None + return None diff --git a/src/azure/cli/commands/resourcegroup.py b/src/azure/cli/commands/resourcegroup.py deleted file mode 100644 index e192cf85c..000000000 --- a/src/azure/cli/commands/resourcegroup.py +++ /dev/null @@ -1,27 +0,0 @@ -from ..commands import command, description, option -from ._command_creation import get_mgmt_service_client -from .._locale import L - -@command('resource group list') -@description('List resource groups') -@option('--tag-name -tn ', L("the resource group's tag name")) -@option('--tag-value -tv ', L("the resource group's tag value")) -@option('--top -t ', L('Top N resource groups to retrieve')) -def list_groups(args, unexpected): #pylint: disable=unused-argument - from azure.mgmt.resource.resources import ResourceManagementClient, \ - ResourceManagementClientConfiguration - from azure.mgmt.resource.resources.models import ResourceGroup, ResourceGroupFilter - - rmc = get_mgmt_service_client(ResourceManagementClient, ResourceManagementClientConfiguration) - - filters = [] - if args.get('tag-name'): - filters.append("tagname eq '{}'".format(args.get('tag-name'))) - if args.get('tag-value'): - filters.append("tagvalue eq '{}'".format(args.get('tag-value'))) - - filter_text = ' and '.join(filters) if len(filters) > 0 else None - - # TODO: top param doesn't work in SDK [bug #115521665] - groups = rmc.resource_groups.list(filter=filter_text, top=args.get('top')) - return list(groups)