-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[ARM] az deployment group/sub/mg/tenant validate/create/what-if: Support deployment with Bicep files #16857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ARM] az deployment group/sub/mg/tenant validate/create/what-if: Support deployment with Bicep files #16857
Changes from 1 commit
4b2ff90
e89fc1e
1d436dd
4538abc
266f5ec
4c1c67e
69d68a9
52b4de9
38f85cb
decec74
95c039b
cff5e93
a66c212
895b289
277a4f8
3208ddd
1e6d746
bb0e6c9
19b6806
e579a7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,36 +17,43 @@ | |||||||||||||||||||||||||
| _logger = get_logger(__name__) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def run_bicep_command(*args): | ||||||||||||||||||||||||||
| bicep_executable_path = ensure_bicep_installation() | ||||||||||||||||||||||||||
| return _run_command(bicep_executable_path, *args) | ||||||||||||||||||||||||||
| def run_bicep_command(args, auto_install=True, check_upgrade=True): | ||||||||||||||||||||||||||
| installation_path = _get_bicep_installation_path(platform.system()) | ||||||||||||||||||||||||||
| installed = os.path.isfile(installation_path) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if not installed and not auto_install: | ||||||||||||||||||||||||||
| raise CLIError('Bicep CLI not found. Install it now by running "az bicep install".') | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def ensure_bicep_installation(release_tag=None, check_upgrade=True): | ||||||||||||||||||||||||||
| if installed and check_upgrade: | ||||||||||||||||||||||||||
| with suppress(CLIError): | ||||||||||||||||||||||||||
| # Checking upgrade should not raise CLIError. | ||||||||||||||||||||||||||
| # Users may continue using the current installed version. | ||||||||||||||||||||||||||
| installed_version = _get_bicep_installed_version(installation_path) | ||||||||||||||||||||||||||
| latest_release_tag = get_bicep_latest_release_tag() | ||||||||||||||||||||||||||
| latest_version = _extract_semver(get_bicep_latest_release_tag()) | ||||||||||||||||||||||||||
| if installed_version and latest_version and semver.compare(installed_version, latest_version) < 0: | ||||||||||||||||||||||||||
| _logger.warning( | ||||||||||||||||||||||||||
| 'A new Bicep release is available: %s. Upgrade now by running "az bicep upgrade".', | ||||||||||||||||||||||||||
| latest_release_tag, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| ensure_bicep_installation() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| return _run_command(installation_path, args) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def ensure_bicep_installation(release_tag=None): | ||||||||||||||||||||||||||
| system = platform.system() | ||||||||||||||||||||||||||
| installation_path = _get_bicep_installation_path(system) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if os.path.isfile(installation_path): | ||||||||||||||||||||||||||
| if release_tag: | ||||||||||||||||||||||||||
| installed_version = _get_bicep_installed_version(installation_path) | ||||||||||||||||||||||||||
| target_version = _extract_semver(release_tag) | ||||||||||||||||||||||||||
| if installed_version and target_version and semver.compare(installed_version, target_version) == 0: | ||||||||||||||||||||||||||
| # The requested version is already installed. | ||||||||||||||||||||||||||
| return installation_path | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| if check_upgrade: | ||||||||||||||||||||||||||
| with suppress(CLIError): | ||||||||||||||||||||||||||
| # Checking upgrade should not raise CLIError. | ||||||||||||||||||||||||||
| # Users may continue using the current installed version. | ||||||||||||||||||||||||||
| installed_version = _get_bicep_installed_version(installation_path) | ||||||||||||||||||||||||||
| latest_release_tag = get_bicep_latest_release_tag() | ||||||||||||||||||||||||||
| latest_version = _extract_semver(get_bicep_latest_release_tag()) | ||||||||||||||||||||||||||
| if installed_version and latest_version and semver.compare(installed_version, latest_version) < 0: | ||||||||||||||||||||||||||
| _logger.warning( | ||||||||||||||||||||||||||
| 'A new Bicep release is available: %s. Upgrade now by running "az bicep upgrade".', | ||||||||||||||||||||||||||
| latest_release_tag, | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
| return installation_path | ||||||||||||||||||||||||||
| if not release_tag: | ||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| installed_version = _get_bicep_installed_version(installation_path) | ||||||||||||||||||||||||||
| target_version = _extract_semver(release_tag) | ||||||||||||||||||||||||||
| if installed_version and target_version and semver.compare(installed_version, target_version) == 0: | ||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @shenglol, if azure-cli/src/azure-cli-core/azure/cli/core/extension/__init__.py Lines 287 to 298 in 22725b3
This will help us reduce Azure CLI’s packaging maintenance cost (#26523). |
||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| installation_dir = os.path.dirname(installation_path) | ||||||||||||||||||||||||||
| if not os.path.exists(installation_dir): | ||||||||||||||||||||||||||
|
|
@@ -64,7 +71,6 @@ def ensure_bicep_installation(release_tag=None, check_upgrade=True): | |||||||||||||||||||||||||
| f.write(request.read()) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| print(f'Successfully installed Bicep CLI to "{installation_path}".') | ||||||||||||||||||||||||||
| return installation_path | ||||||||||||||||||||||||||
| except IOError as err: | ||||||||||||||||||||||||||
| raise CLIError(f"Error while attempting to download Bicep CLI: {err}") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -90,7 +96,7 @@ def get_bicep_latest_release_tag(): | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _get_bicep_installed_version(bicep_executable_path): | ||||||||||||||||||||||||||
| installed_version_output = _run_command(bicep_executable_path, "--version") | ||||||||||||||||||||||||||
| installed_version_output = _run_command(bicep_executable_path, ["--version"]) | ||||||||||||||||||||||||||
| return _extract_semver(installed_version_output) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
@@ -123,8 +129,8 @@ def _extract_semver(text): | |||||||||||||||||||||||||
| return semver_match.group(0) if semver_match else None | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| def _run_command(executable_path, *args): | ||||||||||||||||||||||||||
| process = subprocess.run([rf"{executable_path}"] + list(args), capture_output=True) | ||||||||||||||||||||||||||
| def _run_command(bicep_installation_path, args): | ||||||||||||||||||||||||||
| process = subprocess.run([rf"{bicep_installation_path}"] + args, capture_output=True) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| process.check_returncode() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,53 @@ | ||
| import unittest | ||
| import mock | ||
|
|
||
| from azure.cli.command_modules.resource._bicep import ensure_bicep_installation | ||
| from knack.util import CLIError | ||
| from azure.cli.command_modules.resource._bicep import ensure_bicep_installation, run_bicep_command | ||
|
|
||
|
|
||
| class TestBicep(unittest.TestCase): | ||
| @mock.patch("os.path.isfile") | ||
| def test_run_bicep_command_raise_error_if_not_installed_and_not_auto_install(self, isfile_stub): | ||
| isfile_stub.return_value = False | ||
|
|
||
| with self.assertRaisesRegex(CLIError, 'Bicep CLI not found. Install it now by running "az bicep install".'): | ||
| run_bicep_command(["--version"], auto_install=False) | ||
|
|
||
| @mock.patch("azure.cli.command_modules.resource._bicep._logger.warning") | ||
| @mock.patch("azure.cli.command_modules.resource._bicep._run_command") | ||
| @mock.patch("azure.cli.command_modules.resource._bicep.ensure_bicep_installation") | ||
| @mock.patch("azure.cli.command_modules.resource._bicep.get_bicep_latest_release_tag") | ||
| @mock.patch("azure.cli.command_modules.resource._bicep._get_bicep_installed_version") | ||
| @mock.patch("azure.cli.command_modules.resource._bicep._logger.warning") | ||
| def test_ensure_bicep_installation_check_upgrade( | ||
| self, warning_mock, _get_bicep_installed_version_stub, get_bicep_latest_release_tag_stub, isfile_stub | ||
| @mock.patch("os.path.isfile") | ||
| def test_run_bicep_command_check_upgrade( | ||
| self, | ||
| isfile_stub, | ||
| _get_bicep_installed_version_stub, | ||
| get_bicep_latest_release_tag_stub, | ||
| ensure_bicep_installation_mock, | ||
| _run_command_mock, | ||
| warning_mock, | ||
| ): | ||
| # Arrange. | ||
| isfile_stub.return_value = True | ||
| _get_bicep_installed_version_stub.return_value = "1.0.0" | ||
| get_bicep_latest_release_tag_stub.return_value = "v2.0.0" | ||
| isfile_stub.return_value = True | ||
|
|
||
| # Act. | ||
| installation_path = ensure_bicep_installation(check_upgrade=True) | ||
| run_bicep_command(["--version"], check_upgrade=True) | ||
|
|
||
| # Assert. | ||
| warning_mock.assert_called_once_with( | ||
| 'A new Bicep release is available: %s. Upgrade now by running "az bicep upgrade".', | ||
| "v2.0.0", | ||
| ) | ||
| self.assertIsNotNone(installation_path) | ||
|
|
||
| @mock.patch("os.path.isfile") | ||
| @mock.patch("azure.cli.command_modules.resource._bicep._get_bicep_installed_version") | ||
| @mock.patch("os.path.dirname") | ||
| def test_ensure_bicep_installation_skip_download_if_installed_version_matches_release_tag( | ||
| self, dirname_mock, _get_bicep_installed_version_stub, isfile_stub | ||
| ): | ||
| # Arrange. | ||
| _get_bicep_installed_version_stub.return_value = "0.1.0" | ||
| isfile_stub.return_value = True | ||
|
|
||
| # Act. | ||
| installation_path = ensure_bicep_installation(release_tag='v0.1.0') | ||
| ensure_bicep_installation(release_tag="v0.1.0") | ||
|
|
||
| # Assert | ||
| dirname_mock.assert_not_called() | ||
| self.assertIsNotNone(installation_path) | ||
| dirname_mock.assert_not_called() |
Uh oh!
There was an error while loading. Please reload this page.