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 2 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
12 changes: 12 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,18 @@ 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=CLIArgumentType(options_list=['--file', '-f'], completer=FilesCompleter(),
type=file_type, help="The path to the Bicep file to generate the parameters file from 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, generates the parameters file without restoring external modules."))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CLIArgumentType is used to reuse the same attribute definitions of parameters in multiple places. If you do not have a reuse scenario here, please directly declare the attributes of these parameters

Suggested change
c.argument('file', arg_type=CLIArgumentType(options_list=['--file', '-f'], completer=FilesCompleter(),
type=file_type, help="The path to the Bicep file to generate the parameters file from 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, generates the parameters file without restoring external modules."))
c.argument('file', options_list=['--file', '-f'], completer=FilesCompleter(),
type=file_type, help="The path to the Bicep file to generate the parameters file from in the file system.")
c.argument('outdir', options_list=['--outdir'], completer=DirectoriesCompleter(),
help="When set, saves the output at the specified directory.")
c.argument('outfile', options_list=['--outfile'], completer=FilesCompleter(),
help="When set, saves the output as the specified file path.")
c.argument('stdout', options_list=['--stdout'], action='store_true',
help="When set, prints all output to stdout instead of corresponding files.")
c.argument('no_restore', options_list=['--no-restore'], action='store_true',
help="When set, generates the parameters file without restoring external modules.")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhoxing-ms Done! I've also fixed the same for other bicep related commands (which I referred to myself). Hope that is OK.


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