-
Notifications
You must be signed in to change notification settings - Fork 1.5k
{Site} Add quickstart command and ARM template for Site + Config depl… #9594
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
Open
akanksha020901
wants to merge
14
commits into
Azure:main
Choose a base branch
from
akanksha020901:developer/akanksha/site-config-cli
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8e36b7a
{Site} Add quickstart command and ARM template for Site + Config depl…
akanksha020901 8c5a364
{Site} Update help text for config-name argument in quickstart command
akanksha020901 03b58cd
{Site} Add location argument to quickstart command for deployment
akanksha020901 ca47836
{Site} Add AAZResourceLocationArg import for quickstart command
akanksha020901 5fa677c
{Site} Fix formatting in quickstart command argument definitions
akanksha020901 b906a47
{Site} Add argument formatting for site name in quickstart command
akanksha020901 1f3ca3e
Apply suggestion from @Copilot
akanksha020901 aac1e8b
Apply suggestion from @Copilot
akanksha020901 819a00a
Apply suggestion from @Copilot
akanksha020901 eb08a54
Merge branch 'developer/akanksha/site-config-cli' of https://github.c…
akanksha020901 1f3e672
{Site} Add resourceGroupName parameter with default value in main.json
akanksha020901 63d2ba2
{Site} Make resource group argument required in Quickstart command
akanksha020901 e199fc3
{Site} Enhance quickstart deployment with error suppression and succe…
akanksha020901 c76d5fe
{Site} Enhance error handling and messaging in quickstart deployment
akanksha020901 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,3 +14,4 @@ | |
| from ._list import * | ||
| from ._show import * | ||
| from ._update import * | ||
| from ._quickstart import * | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
| from azure.cli.core.aaz import ( # type: ignore[import-unresolved] | ||
| AAZCommand, | ||
| AAZStrArg, | ||
| AAZStrArgFormat, | ||
| AAZBoolArg, | ||
| AAZResourceGroupNameArg, | ||
| has_value, | ||
| register_command, | ||
| AAZResourceLocationArg | ||
| ) | ||
| from azure.cli.core.azclierror import ( # type: ignore[import-unresolved] | ||
| InvalidArgumentValueError, | ||
| FileOperationError, | ||
| CLIInternalError, | ||
| ) | ||
| from azure.cli.core import get_default_cli # type: ignore[import-unresolved] | ||
| from knack.log import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| def _resolve_template_path() -> Path: | ||
| # ...\azext_site\aaz\latest\site\_quickstart.py -> ...\azext_site\templates\infra\main.json | ||
| azext_root = Path(__file__).resolve().parents[3] # ...\azext_site | ||
| return azext_root / "templates" / "infra" / "main.json" | ||
|
|
||
|
|
||
| @register_command("site quickstart") | ||
| class Quickstart(AAZCommand): | ||
| """Quickstart: deploy internal ARM template to create Site + Config + ConfigRef.""" | ||
|
|
||
| _args_schema = None | ||
|
|
||
| @classmethod | ||
| def _build_arguments_schema(cls, *args, **kwargs): | ||
| if cls._args_schema is not None: | ||
| return cls._args_schema | ||
|
|
||
| cls._args_schema = super()._build_arguments_schema(*args, **kwargs) | ||
| _args_schema = cls._args_schema | ||
|
|
||
| _args_schema.name = AAZStrArg( | ||
| options=["-n", "--name"], | ||
| required=True, | ||
| help="Site name (siteName).", | ||
| fmt=AAZStrArgFormat( | ||
| pattern=r"^[a-zA-Z0-9][a-zA-Z0-9-_.]{0,62}[a-zA-Z0-9]$", | ||
| min_length=2, | ||
| max_length=64, | ||
| ), | ||
| ) | ||
|
|
||
| _args_schema.defaultconfiguration = AAZBoolArg( | ||
| options=["--defaultconfiguration", "--default-configuration"], | ||
| help="Trigger the internal ARM template flow (Site + Config + ConfigRef).", | ||
| ) | ||
|
|
||
| _args_schema.resource_group = AAZResourceGroupNameArg( | ||
| options=["-g", "--resource-group"], | ||
| required=True, | ||
| help="Resource group for deployment.", | ||
| ) | ||
|
|
||
| _args_schema.location = AAZResourceLocationArg( | ||
| options=["-l", "--location"], | ||
| help="Location for the deployment. Default: resource group location.", | ||
| ) | ||
|
|
||
| _args_schema.config_name = AAZStrArg( | ||
| options=["--config-name"], | ||
| help="Optional configName override. Default in template: 'siteName-configuration'.", | ||
| ) | ||
|
|
||
| return cls._args_schema | ||
|
|
||
| def _handler(self, command_args): | ||
| super()._handler(command_args) | ||
|
|
||
| if not self.ctx.args.defaultconfiguration: | ||
| raise InvalidArgumentValueError("Specify --defaultconfiguration to run quickstart.") | ||
|
|
||
| return self.handle() | ||
|
|
||
| def handle(self): | ||
| template = _resolve_template_path() | ||
| if not template.exists(): | ||
| raise FileOperationError(f"Internal ARM template not found: {template}") | ||
|
|
||
| site_name = self.ctx.args.name.to_serialized_data() | ||
| rg = self.ctx.args.resource_group.to_serialized_data() | ||
| deployment_name = f"site-quickstart-{site_name}" | ||
|
|
||
| invoke_args = [ | ||
| "deployment", "group", "create", | ||
| "--name", deployment_name, | ||
| "--resource-group", rg, | ||
| "--template-file", str(template), | ||
| "--parameters", f"siteName={site_name}", | ||
| "--only-show-errors", | ||
| "--output", "none", | ||
| ] | ||
|
|
||
| if has_value(self.ctx.args.location): | ||
| loc = self.ctx.args.location.to_serialized_data() | ||
| invoke_args.extend(["--parameters", f"location={loc}"]) | ||
|
|
||
| if has_value(self.ctx.args.config_name): | ||
| cfg = self.ctx.args.config_name.to_serialized_data() | ||
| invoke_args.extend(["--parameters", f"configName={cfg}"]) | ||
|
|
||
| cli = get_default_cli() | ||
| rc = cli.invoke(invoke_args) | ||
| if rc != 0: | ||
| # Capture the original error first (before more invokes overwrite cli.result) | ||
| underlying_error = None | ||
| if getattr(cli, "result", None) is not None: | ||
| underlying_error = getattr(cli.result, "error", None) | ||
|
|
||
| deployment_error = None | ||
| failed_ops = None | ||
|
|
||
| # Try to fetch ARM deployment error object (code/message/details) | ||
| try: | ||
| show_args = [ | ||
| "deployment", "group", "show", | ||
| "--name", deployment_name, | ||
| "--resource-group", rg, | ||
| "--only-show-errors", | ||
| "--query", "properties.error", | ||
| "--output", "json", | ||
| ] | ||
| cli.invoke(show_args) | ||
| if getattr(cli, "result", None) is not None: | ||
| deployment_error = cli.result.result | ||
| except Exception: | ||
| deployment_error = None | ||
|
|
||
| # Try to fetch failed operations (often contains the most actionable message) | ||
| try: | ||
| ops_args = [ | ||
| "deployment", "operation", "group", "list", | ||
| "--name", deployment_name, | ||
| "--resource-group", rg, | ||
| "--only-show-errors", | ||
| "--query", | ||
| "[?properties.provisioningState=='Failed']." | ||
| "{type:properties.targetResource.resourceType," | ||
| " name:properties.targetResource.resourceName," | ||
| " statusMessage:properties.statusMessage}", | ||
| "--output", "json", | ||
| ] | ||
| cli.invoke(ops_args) | ||
| if getattr(cli, "result", None) is not None: | ||
| failed_ops = cli.result.result | ||
| except Exception: | ||
| failed_ops = None | ||
|
|
||
| msg = ( | ||
| "ARM deployment failed for site quickstart. " | ||
| f"Deployment name: {deployment_name}, resource group: {rg}." | ||
| ) | ||
| if underlying_error: | ||
| msg = f"{msg}\nUnderlying error: {underlying_error}" | ||
|
|
||
| if deployment_error: | ||
| msg = f"{msg}\nDeployment error:\n{json.dumps(deployment_error, indent=2)}" | ||
|
|
||
| if failed_ops: | ||
| msg = f"{msg}\nFailed operations:\n{json.dumps(failed_ops, indent=2)}" | ||
|
|
||
| raise CLIInternalError(msg) | ||
|
|
||
| # 2) Query deployment operations and print friendly success messages | ||
| ops_args = [ | ||
| "deployment", "operation", "group", "list", | ||
| "--name", deployment_name, | ||
| "--resource-group", rg, | ||
| "--only-show-errors", | ||
| "--output", "none", | ||
| ] | ||
| cli.invoke(ops_args) | ||
| ops = [] | ||
| if getattr(cli, "result", None) is not None: | ||
| ops = cli.result.result or [] | ||
|
|
||
| succeeded_types = set() | ||
| if isinstance(ops, list): | ||
| for op in ops: | ||
| if not isinstance(op, dict): | ||
| continue | ||
| props = op.get("properties") or {} | ||
| if not isinstance(props, dict): | ||
| continue | ||
| if props.get("provisioningState") != "Succeeded": | ||
| continue | ||
| tr = props.get("targetResource") or {} | ||
| if isinstance(tr, dict): | ||
| rtype = tr.get("resourceType") | ||
| if rtype: | ||
| succeeded_types.add(rtype) | ||
|
|
||
| if "Microsoft.Edge/sites" in succeeded_types: | ||
| print("Site created successfully.") | ||
| if "Microsoft.Edge/Configurations" in succeeded_types: | ||
| print("Config created successfully.") | ||
| if "Microsoft.Edge/configurationReferences" in succeeded_types: | ||
| print("Config reference created successfully.") | ||
|
|
||
| if not ({"Microsoft.Edge/sites", "Microsoft.Edge/Configurations", "Microsoft.Edge/configurationReferences"} & succeeded_types): | ||
| print("Deployment completed successfully.") | ||
|
|
||
| return None | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A new user-facing command (
site quickstart) and an embedded deployment template are introduced, but there is no scenario test covering it. Since this extension already hasScenarioTestcoverage (e.g.,test_site.py), add a test that runsaz site quickstart ...and asserts the deployment succeeded / expected outputs are returned.