Skip to content
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 bicep restore: Add new command to restore external modules #22423

Merged
merged 3 commits into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_bicep.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ def get_bicep_latest_release_tag():
raise ClientRequestError(f"Error while attempting to retrieve the latest Bicep version: {err}.")


def bicep_version_greater_than_or_equal_to(version):
system = platform.system()
installation_path = _get_bicep_installation_path(system)
installed_version = _get_bicep_installed_version(installation_path)
return semver.compare(installed_version, version) >= 0


def supports_bicep_publish():
system = platform.system()
installation_path = _get_bicep_installation_path(system)
Expand Down
14 changes: 14 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2731,6 +2731,8 @@
text: az bicep build --file {bicep_file} --outdir {out_dir}
- name: Build a Bicep file and save the result to the specified file.
text: az bicep build --file {bicep_file} --outfile {out_file}
- name: Build a Bicep file without restoring external modules.
text: az bicep build --file {bicep_file} --no-restore
"""

helps['bicep decompile'] = """
Expand All @@ -2739,6 +2741,8 @@
examples:
- name: Decompile an ARM template file.
text: az bicep decompile --file {json_template_file}
- name: Decompile an ARM template file and overwrite existing Bicep file.
text: az bicep decompile --file {json_template_file} --force
"""

helps['bicep publish'] = """
Expand All @@ -2749,6 +2753,16 @@
text: az bicep publish --file {bicep_file} --target "br:{registry}/{module_path}:{tag}"
"""

helps['bicep restore'] = """
type: command
short-summary: Restore external modules for a bicep file.
examples:
- name: Retore external modules.
text: az bicep restore --file {bicep_file}
- name: Retore external modules and overwrite cached external modules.
text: az bicep restore --file {bicep_file} --force
"""

helps['bicep version'] = """
type: command
short-summary: Show the installed version of Bicep CLI.
Expand Down
35 changes: 19 additions & 16 deletions src/azure-cli/azure/cli/command_modules/resource/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,20 @@ def load_arguments(self, _):
help="When set, saves the output as the specified file path."))
c.argument('stdout', arg_type=CLIArgumentType(options_list=['--stdout'], action='store_true',
help="When set, prints all output to stdout instead of corresponding files."))
c.argument('no_restore', arg_type=CLIArgumentType(options_list=['--no-restore'], action='store_true',
help="When set, builds the bicep file without restoring external modules."))

with self.argument_context('bicep decompile') as c:
c.argument('file', arg_type=CLIArgumentType(options_list=['--file', '-f'], completer=FilesCompleter(),
type=file_type, help="The path to the ARM template to decompile in the file system."))
c.argument('force', arg_type=CLIArgumentType(options_list=['--force'], action='store_true',
help="Allows overwriting the output file if it exists."))

with self.argument_context('bicep restore') as c:
c.argument('file', arg_type=CLIArgumentType(options_list=['--file', '-f'], completer=FilesCompleter(),
type=file_type, help="The path to the Bicep file to restore external modules for."))
c.argument('force', arg_type=CLIArgumentType(options_list=['--force'], action='store_true',
help="Allows overwriting the cached external modules."))

with self.argument_context('bicep publish') as c:
c.argument('file', arg_type=CLIArgumentType(options_list=['--file', '-f'], completer=FilesCompleter(),
Expand All @@ -669,16 +679,14 @@ def load_arguments(self, _):
with self.argument_context('resourcemanagement private-link create') as c:
c.argument('resource_group', arg_type=resource_group_name_type,
help='The name of the resource group.')
c.argument('name', options_list=[
'--name', '-n'], help='The name of the resource management private link.')
c.argument('name', options_list=['--name', '-n'], help='The name of the resource management private link.')
c.argument('location', arg_type=get_location_type(self.cli_ctx), validator=get_default_location_from_resource_group,
help='the region to create the resource management private link')

with self.argument_context('resourcemanagement private-link show') as c:
c.argument('resource_group', arg_type=resource_group_name_type,
help='The name of the resource group.')
c.argument('name', options_list=[
'--name', '-n'], help='The name of the resource management private link.')
c.argument('name', options_list=['--name', '-n'], help='The name of the resource management private link.')

with self.argument_context('resourcemanagement private-link list') as c:
c.argument('resource_group', arg_type=resource_group_name_type,
Expand All @@ -687,27 +695,22 @@ def load_arguments(self, _):
with self.argument_context('resourcemanagement private-link delete') as c:
c.argument('resource_group', arg_type=resource_group_name_type,
help='The name of the resource group.')
c.argument('name', options_list=[
'--name', '-n'], help='The name of the resource management private link.')
c.argument('name', options_list=['--name', '-n'], help='The name of the resource management private link.')

with self.argument_context('private-link association create') as c:
c.argument('management_group_id', arg_type=management_group_id_type)
c.argument('name', options_list=[
'--name', '-n'], help='The name of the private link association')
c.argument('privatelink', options_list=[
'--privatelink', '-p'], help='The name of the private link')
c.argument('public_network_access', options_list=['--public-network-access', '-a'], arg_type=get_enum_type(
['enabled', 'disabled']), help='restrict traffic to private link')
c.argument('name', options_list=['--name', '-n'], help='The name of the private link association')
c.argument('privatelink', options_list=['--privatelink', '-p'], help='The name of the private link')
c.argument('public_network_access', options_list=['--public-network-access', '-a'],
arg_type=get_enum_type(['enabled', 'disabled']), help='restrict traffic to private link')

with self.argument_context('private-link association show') as c:
c.argument('management_group_id', arg_type=management_group_id_type)
c.argument('name', options_list=[
'--name', '-n'], help='The name of the private link association')
c.argument('name', options_list=['--name', '-n'], help='The name of the private link association')

with self.argument_context('private-link association list') as c:
c.argument('management_group_id', arg_type=management_group_id_type)

with self.argument_context('private-link association delete') as c:
c.argument('management_group_id', arg_type=management_group_id_type)
c.argument('name', options_list=[
'--name', '-n'], help='The name of the private link association')
c.argument('name', options_list=['--name', '-n'], help='The name of the private link association')
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ def load_command_table(self, _):
g.custom_command('upgrade', 'upgrade_bicep_cli')
g.custom_command('build', 'build_bicep_file')
g.custom_command('decompile', 'decompile_bicep_file')
g.custom_command('restore', 'restore_bicep_file')
g.custom_command('publish', 'publish_bicep_file')
g.custom_command('version', 'show_bicep_cli_version')
g.custom_command('list-versions', 'list_bicep_cli_versions')
Expand Down
35 changes: 28 additions & 7 deletions src/azure-cli/azure/cli/command_modules/resource/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
get_bicep_latest_release_tag,
get_bicep_available_release_tags,
validate_bicep_target_scope,
supports_bicep_publish
bicep_version_greater_than_or_equal_to
)

from ._utils import _build_preflight_error_message, _build_http_response_error_message
Expand Down Expand Up @@ -2310,7 +2310,7 @@ def _build_identities_info(cmd, identities, resourceGroupName):
return ResourceIdentity(type=ResourceIdentityType.system_assigned)

user_assigned_identities = [x for x in identities if x != MSI_LOCAL_ID]
if user_assigned_identities and len(user_assigned_identities) > 0:
if user_assigned_identities:
msiId = _get_resource_id(cmd.cli_ctx, user_assigned_identities[0], resourceGroupName,
'userAssignedIdentities', 'Microsoft.ManagedIdentity')

Expand Down Expand Up @@ -3672,28 +3672,49 @@ def upgrade_bicep_cli(cmd, target_platform=None):
ensure_bicep_installation(release_tag=latest_release_tag, target_platform=target_platform)


def build_bicep_file(cmd, file, stdout=None, outdir=None, outfile=None):
def build_bicep_file(cmd, file, stdout=None, outdir=None, outfile=None, no_restore=None):
args = ["build", file]
if outdir:
args += ["--outdir", outdir]
if outfile:
args += ["--outfile", outfile]
if stdout:
args += ["--stdout"]
if no_restore:
args += ["--no-restore"]
print(run_bicep_command(args))
return
run_bicep_command(args)


def publish_bicep_file(cmd, file, target):
if supports_bicep_publish():
ensure_bicep_installation()

minimum_supported_version = "0.4.1008"
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
run_bicep_command(["publish", file, "--target", target])
else:
logger.error("az bicep publish could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v0.4.1008 or later.")
logger.error("az bicep publish could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version)


def restore_bicep_file(cmd, file, force=None):
ensure_bicep_installation()

minimum_supported_version = "0.4.1008"
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
args = ["restore", file]
if force:
args += ["--force"]
run_bicep_command(args)
else:
logger.error("az bicep restore could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version)


def decompile_bicep_file(cmd, file):
run_bicep_command(["decompile", file])
def decompile_bicep_file(cmd, file, force=None):
args = ["decompile", file]
if force:
args += ["--force"]
run_bicep_command(args)


def show_bicep_cli_version(cmd):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module helloWorld 'br/public:samples/hello-world:1.0.1' = {
name: 'helloWorld'
params: {
name: 'Bicep developers'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,7 @@ def test_bicep_build_decompile(self):

self.cmd('az bicep build -f {tf} --outfile {build_path}')
self.cmd('az bicep decompile -f {build_path}')
self.cmd('az bicep decompile -f {build_path} --force')

if os.path.exists(build_path):
os.remove(build_path)
Expand Down Expand Up @@ -3994,6 +3995,24 @@ def test_install_and_upgrade(self):
self.cmd('az bicep version')


class BicepRestoreTest(LiveScenarioTest):
def test_restore(self):
curr_dir = os.path.dirname(os.path.realpath(__file__))
bf = os.path.join(curr_dir, 'data', 'external_modules.bicep').replace('\\', '\\\\')
out_path = os.path.join(curr_dir, 'data', 'external_modules.json').replace('\\', '\\\\')
self.kwargs.update({
'bf': bf,
'out_path': out_path,
})

self.cmd('az bicep restore -f {bf}')
self.cmd('az bicep restore -f {bf} --force')
self.cmd('az bicep build -f {bf} --no-restore')

if os.path.exists(out_path):
os.remove(out_path)


class DeploymentWithBicepScenarioTest(LiveScenarioTest):
def setup(self):
super.setup()
Expand Down