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] Fix #22930: az bicep generate-params: Add support for bicep generate-params command #22951

Merged
merged 3 commits into from
Jun 29, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 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 @@ -2776,6 +2776,22 @@
short-summary: List out all available versions of Bicep CLI.
"""

helps['bicep generate-params'] = """
type: command
short-summary: Generate parameters file for a Bicep file.
examples:
- name: Generate parameters file for a Bicep file.
text: az bicep generate-params --file {bicep_file}
- name: Generate parameters file for a Bicep file and print all output to stdout.
text: az bicep generate-params --file {bicep_file} --stdout
- name: Generate parameters file for a Bicep file and save the result to the specified directory.
text: az bicep generate-params --file {bicep_file} --outdir {out_dir}
- name: Generate parameters file for a Bicep file and save the result to the specified file.
text: az bicep generate-params --file {bicep_file} --outfile {out_file}
- name: Generate parameters file for a Bicep file without restoring external modules.
text: az bicep generate-params --file {bicep_file} --no-restore
"""

helps['resourcemanagement'] = """
type: group
short-summary: resourcemanagement CLI command group.
Expand Down
43 changes: 23 additions & 20 deletions src/azure-cli/azure/cli/command_modules/resource/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ def load_arguments(self, _):
ui_form_definition_file_type = CLIArgumentType(options_list=['--ui-form-definition'], completer=FilesCompleter(), type=file_type,
help="A path to a uiFormDefinition file in the file system")

bicep_file_type = CLIArgumentType(options_list=['--file', '-f'], completer=FilesCompleter(), type=file_type)
bicep_force_type = CLIArgumentType(options_list=['--force'], action='store_true')
bicep_no_restore_type = CLIArgumentType(options_list=['--no-restore'], action='store_true')
bicep_outdir_type = CLIArgumentType(options_list=['--outdir'], completer=DirectoriesCompleter(), help="When set, saves the output at the specified directory.")
bicep_outfile_type = CLIArgumentType(options_list=['--outfile'], completer=FilesCompleter(), help="When set, saves the output as the specified file path.")
bicep_stdout_type = CLIArgumentType(options_list=['--stdout'], action='store_true', help="When set, prints all output to stdout instead of corresponding files.")
bicep_target_platform_type = CLIArgumentType(options_list=['--target-platform', '-t'],
arg_type=get_enum_type(
["win-x64", "linux-musl-x64", "linux-x64", "osx-x64"]),
Expand Down Expand Up @@ -641,32 +647,22 @@ def load_arguments(self, _):
c.argument('resource_group', arg_type=resource_group_name_type)

with self.argument_context('bicep build') 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 build in the file system."))
c.argument('outdir', arg_type=CLIArgumentType(options_list=['--outdir'], completer=DirectoriesCompleter(),
help="When set, saves the output at the specified directory."))
c.argument('outfile', arg_type=CLIArgumentType(options_list=['--outfile'], completer=FilesCompleter(),
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."))
c.argument('file', arg_type=bicep_file_type, help="The path to the Bicep file to build in the file system.")
c.argument('outdir', arg_type=bicep_outdir_type)
c.argument('outfile', arg_type=bicep_outfile_type)
c.argument('stdout', arg_type=bicep_stdout_type)
c.argument('no_restore', arg_type=bicep_no_restore_type, 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."))
c.argument('file', arg_type=bicep_file_type, help="The path to the ARM template to decompile in the file system.")
c.argument('force', arg_type=bicep_force_type, 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."))
c.argument('file', arg_type=bicep_file_type, help="The path to the Bicep file to restore external modules for.")
c.argument('force', arg_type=bicep_force_type, 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(),
type=file_type, help="The path to the Bicep module file to publish in the file system."))
c.argument('file', arg_type=bicep_file_type, help="The path to the Bicep module file to publish in the file system.")
c.argument('target', arg_type=CLIArgumentType(options_list=['--target', '-t'],
help="The target location where the Bicep module will be published."))

Expand All @@ -677,6 +673,13 @@ def load_arguments(self, _):
with self.argument_context('bicep upgrade') as c:
c.argument('target_platform', arg_type=bicep_target_platform_type)

with self.argument_context('bicep generate-params') as c:
c.argument('file', arg_type=bicep_file_type, help="The path to the Bicep file to generate the parameters file from in the file system.")
c.argument('outdir', arg_type=bicep_outdir_type)
c.argument('outfile', arg_type=bicep_outfile_type)
c.argument('stdout', arg_type=bicep_stdout_type)
c.argument('no_restore', arg_type=bicep_no_restore_type, help="When set, generates the parameters file without restoring external modules.")

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.')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,7 @@ def load_command_table(self, _):
g.custom_command('publish', 'publish_bicep_file')
g.custom_command('version', 'show_bicep_cli_version')
g.custom_command('list-versions', 'list_bicep_cli_versions')
g.custom_command('generate-params', 'generate_params_file')

with self.command_group('resourcemanagement private-link', resource_resourcemanagementprivatelink_sdk, resource_type=ResourceType.MGMT_RESOURCE_PRIVATELINKS) as g:
g.custom_command('create', 'create_resourcemanager_privatelink')
Expand Down
23 changes: 23 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3727,6 +3727,29 @@ def list_bicep_cli_versions(cmd):
return get_bicep_available_release_tags()


def generate_params_file(cmd, file, no_restore=None, outdir=None, outfile=None, stdout=None):
ensure_bicep_installation()

minimum_supported_version = "0.7.4"
if bicep_version_greater_than_or_equal_to(minimum_supported_version):
args = ["generate-params", file]
if no_restore:
args += ["--no-restore"]
if outdir:
args += ["--outdir", outdir]
if outfile:
args += ["--outfile", outfile]
if stdout:
args += ["--stdout"]

output = run_bicep_command(args)

if stdout:
print(output)
else:
logger.error("az bicep generate-params could not be executed with the current version of Bicep CLI. Please upgrade Bicep CLI to v%s or later.", minimum_supported_version)


def create_resourcemanager_privatelink(
cmd, resource_group, name, location):
rcf = _resource_privatelinks_client_factory(cmd.cli_ctx)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
param demoString string
param demoInt int
param demoBool bool
param demoObject object
param demoArray array

param location string = resourceGroup().location
Original file line number Diff line number Diff line change
Expand Up @@ -3981,6 +3981,21 @@ def test_bicep_build_decompile(self):
if os.path.exists(decompile_path):
os.remove(decompile_path)

class BicepGenerateParamsTest(LiveScenarioTest):

def test_bicep_generate_params(self):
curr_dir = os.path.dirname(os.path.realpath(__file__))
tf = os.path.join(curr_dir, 'sample_params.bicep').replace('\\', '\\\\')
params_path = os.path.join(curr_dir, 'sample_params.parameters.json').replace('\\', '\\\\')
self.kwargs.update({
'tf': tf,
'params_path': params_path,
})

self.cmd('az bicep generate-params -f {tf} --outfile {params_path}')

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

class BicepInstallationTest(LiveScenarioTest):
def setup(self):
Expand Down