Skip to content

Commit 7888181

Browse files
authored
[App Service] az webapp create: Add error message that clearly lists all valid options and tells them how to discover available runtimes (#33252)
1 parent 5aa864e commit 7888181

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

src/azure-cli/azure/cli/command_modules/appservice/custom.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,17 +269,25 @@ def create_webapp(cmd, resource_group_name, name, plan, runtime=None, startup_fi
269269
if not validate_container_app_create_options(runtime, container_image_name,
270270
multicontainer_config_type, multicontainer_config_file,
271271
sitecontainers_app):
272+
if not any([runtime, container_image_name, multicontainer_config_type,
273+
multicontainer_config_file, deployment_container_image_name, sitecontainers_app]):
274+
raise ArgumentUsageError('Creating a Linux webapp requires one of the following: '
275+
'--runtime, --container-image-name, '
276+
'or --sitecontainers-app. '
277+
"Run 'az webapp list-runtimes --os-type linux' for supported runtimes. "
278+
"For custom containers, see 'az webapp sitecontainers create --help': "
279+
"https://learn.microsoft.com/cli/azure/webapp/sitecontainers")
272280
if deployment_container_image_name:
273281
raise ArgumentUsageError('Please specify both --multicontainer-config-type TYPE '
274282
'and --multicontainer-config-file FILE, '
275283
'and only specify one out of --runtime, '
276284
'--deployment-container-image-name, --multicontainer-config-type '
277-
'or --sitecontainers_app')
285+
'or --sitecontainers-app')
278286
raise ArgumentUsageError('Please specify both --multicontainer-config-type TYPE '
279287
'and --multicontainer-config-file FILE, '
280288
'and only specify one out of --runtime, '
281289
'--container-image-name, --multicontainer-config-type '
282-
'or --sitecontainers_app')
290+
'or --sitecontainers-app')
283291
if startup_file:
284292
site_config.app_command_line = startup_file
285293
if sitecontainers_app:

src/azure-cli/azure/cli/command_modules/appservice/tests/latest/test_webapp_commands_thru_mock.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from knack.util import CLIError
1313
from azure.cli.core.azclierror import (InvalidArgumentValueError,
1414
MutuallyExclusiveArgumentError,
15-
AzureResponseError)
15+
AzureResponseError,
16+
ArgumentUsageError)
1617
from azure.cli.command_modules.appservice.custom import (set_deployment_user,
1718
update_git_token, add_hostname,
1819
update_site_configs,
@@ -33,7 +34,8 @@
3334
add_github_actions,
3435
update_app_settings,
3536
update_application_settings_polling,
36-
update_webapp)
37+
update_webapp,
38+
create_webapp)
3739

3840
# pylint: disable=line-too-long
3941
from azure.cli.core.profiles import ResourceType
@@ -436,6 +438,36 @@ def test_valid_linux_create_options(self):
436438
self.assertFalse(validate_container_app_create_options(None, None, test_multi_container_config, None))
437439
self.assertFalse(validate_container_app_create_options(None, None, None, None))
438440

441+
@mock.patch('azure.cli.command_modules.appservice.custom.web_client_factory', autospec=True)
442+
@mock.patch('azure.cli.command_modules.appservice.custom._StackRuntimeHelper', autospec=True)
443+
@mock.patch('azure.cli.command_modules.appservice.custom.get_site_availability', autospec=True)
444+
def test_linux_webapp_create_no_runtime_raises_error(self, get_site_avail_mock,
445+
stack_helper_mock, web_client_mock):
446+
cmd_mock = _get_test_cmd()
447+
SiteConfig, SkuDescription, NameValuePair = cmd_mock.get_models(
448+
'SiteConfig', 'SkuDescription', 'NameValuePair')
449+
cmd_mock.get_models = mock.MagicMock(return_value=(SiteConfig, SkuDescription, NameValuePair))
450+
451+
# Mock a Linux plan (reserved=True)
452+
plan_info = mock.MagicMock()
453+
plan_info.reserved = True
454+
plan_info.location = 'eastus'
455+
plan_info.id = '/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Web/serverfarms/plan'
456+
plan_info.sku = SkuDescription(name='F1')
457+
web_client_mock.return_value.app_service_plans.get.return_value = plan_info
458+
459+
# Mock site availability (new app name)
460+
name_validation = mock.MagicMock()
461+
name_validation.name_available = True
462+
get_site_avail_mock.return_value = name_validation
463+
464+
with self.assertRaises(ArgumentUsageError) as context:
465+
create_webapp(cmd_mock, 'test-rg', 'test-app', 'test-plan')
466+
467+
self.assertIn('Creating a Linux webapp requires one of the following', str(context.exception))
468+
self.assertIn('--runtime', str(context.exception))
469+
self.assertIn('--os-type linux', str(context.exception))
470+
439471
@mock.patch('azure.cli.command_modules.appservice.custom._verify_hostname_binding', autospec=True)
440472
@mock.patch('azure.cli.command_modules.appservice.custom.web_client_factory', autospec=True)
441473
@mock.patch('azure.cli.command_modules.appservice.custom._generic_site_operation', autospec=True)

0 commit comments

Comments
 (0)