diff --git a/src/azure-cli/azure/cli/command_modules/resource/commands.py b/src/azure-cli/azure/cli/command_modules/resource/commands.py index 598e893af0f..c697c3bd0b3 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/commands.py +++ b/src/azure-cli/azure/cli/command_modules/resource/commands.py @@ -228,7 +228,7 @@ def load_command_table(self, _): with self.command_group('group', resource_group_sdk, resource_type=ResourceType.MGMT_RESOURCE_RESOURCES) as g: g.command('delete', 'begin_delete', supports_no_wait=True, confirmation=True) - g.show_command('show', 'get') + g.custom_show_command('show', 'get_resource_group') g.command('exists', 'check_existence') g.custom_command('list', 'list_resource_groups', table_transformer=transform_resource_group_list) g.custom_command('create', 'create_resource_group') diff --git a/src/azure-cli/azure/cli/command_modules/resource/custom.py b/src/azure-cli/azure/cli/command_modules/resource/custom.py index 381654efca7..e9bc759e8c1 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/custom.py +++ b/src/azure-cli/azure/cli/command_modules/resource/custom.py @@ -1317,6 +1317,17 @@ def update_resource_group(instance, tags=None): return instance +def get_resource_group(cmd, resource_group_name): + from azure.core.exceptions import ResourceNotFoundError + try: + rcf = _resource_client_factory(cmd.cli_ctx) + return rcf.resource_groups.get(resource_group_name=resource_group_name) + except ResourceNotFoundError: + subscription_id = get_subscription_id(cmd.cli_ctx) + raise ResourceNotFoundError("Resource group '{}' could not be found in subscription '{}'." + .format(resource_group_name, subscription_id)) + + def export_group_as_template( cmd, resource_group_name, include_comments=False, include_parameter_default_value=False, resource_ids=None, skip_resource_name_params=False, skip_all_params=False): """Captures a resource group as a template. diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/recordings/test_show_not_existing_resource_group.yaml b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/recordings/test_show_not_existing_resource_group.yaml new file mode 100644 index 00000000000..25266cadafd --- /dev/null +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/recordings/test_show_not_existing_resource_group.yaml @@ -0,0 +1,45 @@ +interactions: +- request: + body: null + headers: + Accept: + - application/json + Accept-Encoding: + - gzip, deflate + CommandName: + - group show + Connection: + - keep-alive + ParameterSetName: + - -n + User-Agent: + - AZURECLI/2.39.0 azsdk-python-azure-mgmt-resource/21.1.0b1 Python/3.8.0 (Windows-10-10.0.19041-SP0) + method: GET + uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/not_exist_group_000001?api-version=2021-04-01 + response: + body: + string: '{"error":{"code":"ResourceGroupNotFound","message":"Resource group + ''not_exist_group_000001'' could not be found."}}' + headers: + cache-control: + - no-cache + content-length: + - '114' + content-type: + - application/json; charset=utf-8 + date: + - Thu, 11 Aug 2022 09:48:53 GMT + expires: + - '-1' + pragma: + - no-cache + strict-transport-security: + - max-age=31536000; includeSubDomains + x-content-type-options: + - nosniff + x-ms-failure-cause: + - gateway + status: + code: 404 + message: Not Found +version: 1 diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py index 24a259f84f7..c90eb980d28 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource.py @@ -100,7 +100,18 @@ def test_resource_group_force_deletion_type(self, resource_group): self.cmd('group delete -n testrg -f Microsoft.Compute/virtualMachines --yes') self.cmd('group exists -n testrg', - checks=self.check('@', False)) + checks=self.check('@', False)) + + def test_show_not_existing_resource_group(self): + self.kwargs.update({ + 'group': self.create_random_name('not_exist_group_', 30) + }) + subscriptionId = self.get_subscription_id() + from azure.core.exceptions import ResourceNotFoundError + message = "Resource group '{}' could not be found in subscription '{}'." \ + .format(self.kwargs['group'], subscriptionId) + with self.assertRaisesRegex(ResourceNotFoundError, message): + self.cmd('group show -n {group}') class ResourceGroupNoWaitScenarioTest(ScenarioTest):