-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[Draft] Support --what-if argument
#32131
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
wangzelin007
wants to merge
31
commits into
Azure:dev
Choose a base branch
from
wangzelin007:wzl/add-what-if
base: dev
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 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
abb2f17
az what-if (save)
MoChilia 46cf88d
az what-if
MoChilia 7bc0289
Add what if feature
wangzelin007 df108e2
Update src/azure-cli-core/azure/cli/core/commands/__init__.py
wangzelin007 f126b42
update
MoChilia 08cc6c1
add progress bar and support pretty output
MoChilia 6a1b839
fix style issue
MoChilia 6a52aac
add a mock test
MoChilia b4aae27
use get_raw_token
MoChilia b764741
def PropertyChange
MoChilia c3caf10
update mock test
MoChilia 2b61732
minor fix
wangzelin007 3f86e44
Update what_if.py
wangzelin007 499e477
move call what-if to core
MoChilia e11f6aa
Merge remote-tracking branch 'upstream/dev' into whatif
MoChilia 8d394ba
Merge branch 'whatif' into wzl/add-what-if
MoChilia 6393d35
remove what if command
wangzelin007 2d75315
minor fix
wangzelin007 31dab53
minor fix
wangzelin007 4207bca
add --export-bicep
wangzelin007 8402c51
minor fix
wangzelin007 6c3d3bc
Merge branch 'dev' into wzl/add-what-if
wangzelin007 9c44270
add whitelist
wangzelin007 7d89ee3
minor fix
wangzelin007 6bdefb9
minor fix
wangzelin007 6fd0a35
resource: update What-If noise notice link to GitHub issues
wangzelin007 45f3dc9
resource: update What-If issue link to the new GitHub issue template
wangzelin007 d36baf6
minor fix
wangzelin007 252813c
Add telemetry
wangzelin007 819cea0
minor fix
wangzelin007 9f6ab14
minor fix
wangzelin007 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
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
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,109 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| """ | ||
| Module for handling what-if functionality in Azure CLI. | ||
| This module provides the core logic for preview mode execution without actually running commands. | ||
|
|
||
| IMPORTANT: The what-if service requires client-side authentication to operate under the | ||
| caller's subscription and permissions. Server-side authentication is not supported for | ||
| what-if operations as it would not provide access to the caller's subscription. | ||
|
|
||
| This client now uses AzureCliCredential to obtain an access token for the caller's subscription. | ||
|
|
||
| The what-if service will use your configured credentials to access your subscription | ||
| and preview deployment changes under your permissions. | ||
| """ | ||
|
|
||
| import requests | ||
| from typing import Dict, Any, Optional | ||
| from azure.identity import AzureCliCredential | ||
| from datetime import datetime, timezone | ||
| from knack.log import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
| # Configuration | ||
| FUNCTION_APP_URL = "https://azcli-script-insight.azurewebsites.net" | ||
|
|
||
|
|
||
| def get_azure_cli_access_token() -> Optional[str]: | ||
| """ | ||
| Get access token for the caller's subscription using AzureCliCredential | ||
|
|
||
| Returns: | ||
| Access token string if successful, None if failed | ||
| """ | ||
| token_info = get_azure_cli_token_info() | ||
| return token_info.get("accessToken") if token_info else None | ||
|
|
||
|
|
||
| def get_azure_cli_token_info() -> Optional[Dict[str, Any]]: | ||
| """ | ||
| Get complete token information using AzureCliCredential including expiration | ||
|
|
||
| Returns: | ||
| Dictionary with token info including accessToken, expiresOn, etc., or None if failed | ||
| """ | ||
| try: | ||
| # Use AzureCliCredential for Azure CLI authentication | ||
| cli_credential = AzureCliCredential(process_timeout=30) | ||
|
|
||
| # Get access token for Azure Resource Manager | ||
| token = cli_credential.get_token("https://management.azure.com/.default") | ||
|
|
||
| token_info = { | ||
| "accessToken": token.token, | ||
| "expiresOn": datetime.fromtimestamp(token.expires_on, tz=timezone.utc).isoformat(), | ||
| "tokenType": "Bearer" | ||
| } | ||
|
|
||
| return token_info | ||
|
|
||
| except Exception as e: | ||
| logger.warning(f"Error getting access token with AzureCliCredential: {str(e)}") | ||
| return None | ||
|
|
||
|
|
||
| def what_if_preview(azcli_script: str, subscription_id: Optional[str] = None) -> Dict[str, Any]: | ||
| """ | ||
| Preview deployment changes using Azure what-if functionality | ||
|
|
||
| Args: | ||
| function_app_url: Base URL of your Azure Function App | ||
| azcli_script: Azure CLI script to analyze | ||
| subscription_id: Optional fallback subscription ID if not in script | ||
|
|
||
| Returns: | ||
| Dictionary with what-if preview result | ||
| """ | ||
| url = f"{FUNCTION_APP_URL.rstrip('/')}/api/what_if_preview" | ||
|
|
||
| headers = { | ||
| 'Content-Type': 'application/json', | ||
| 'Accept': 'application/json' | ||
| } | ||
|
|
||
| # Get access token from Azure CLI | ||
| access_token = get_azure_cli_access_token() | ||
| if not access_token: | ||
| return { | ||
| "error": "Failed to get access token from Azure CLI. Please ensure you are logged in with 'az login'", | ||
| "details": "The what-if service requires client credentials to access your subscription. Please provide an access token.", | ||
| "success": False | ||
| } | ||
|
|
||
| # Use Authorization header for access token | ||
| headers['Authorization'] = f'Bearer {access_token}' | ||
|
|
||
| payload = {"azcli_script": azcli_script} | ||
| if subscription_id: | ||
| payload["subscription_id"] = subscription_id | ||
|
|
||
| try: | ||
| response = requests.post(url, json=payload, headers=headers, timeout=300) | ||
| return response.json() | ||
| except requests.RequestException as e: | ||
| raise e |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.