Skip to content

{Core} aaz: Add AAZFileUploadArg for file upload#31573

Open
AllyW wants to merge 6 commits intoAzure:devfrom
AllyW:add-file-as-binary
Open

{Core} aaz: Add AAZFileUploadArg for file upload#31573
AllyW wants to merge 6 commits intoAzure:devfrom
AllyW:add-file-as-binary

Conversation

@AllyW
Copy link
Copy Markdown
Contributor

@AllyW AllyW commented May 29, 2025

Related command

Description

Testing Guide

History Notes

[Component Name 1] BREAKING CHANGE: az command a: Make some customer-facing breaking change
[Component Name 2] az command b: Add some customer-facing feature


This checklist is used to make sure that common guidelines for a pull request are followed.

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd bot commented May 29, 2025

️✔️AzureCLI-FullTest
️✔️acr
️✔️latest
️✔️3.12
️✔️3.9
️✔️acs
️✔️latest
️✔️3.12
️✔️3.9
️✔️advisor
️✔️latest
️✔️3.12
️✔️3.9
️✔️ams
️✔️latest
️✔️3.12
️✔️3.9
️✔️apim
️✔️latest
️✔️3.12
️✔️3.9
️✔️appconfig
️✔️latest
️✔️3.12
️✔️3.9
️✔️appservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️aro
️✔️latest
️✔️3.12
️✔️3.9
️✔️backup
️✔️latest
️✔️3.12
️✔️3.9
️✔️batch
️✔️latest
️✔️3.12
️✔️3.9
️✔️batchai
️✔️latest
️✔️3.12
️✔️3.9
️✔️billing
️✔️latest
️✔️3.12
️✔️3.9
️✔️botservice
️✔️latest
️✔️3.12
️✔️3.9
️✔️cdn
️✔️latest
️✔️3.12
️✔️3.9
️✔️cloud
️✔️latest
️✔️3.12
️✔️3.9
️✔️cognitiveservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️compute_recommender
️✔️latest
️✔️3.12
️✔️3.9
️✔️computefleet
️✔️latest
️✔️3.12
️✔️3.9
️✔️config
️✔️latest
️✔️3.12
️✔️3.9
️✔️configure
️✔️latest
️✔️3.12
️✔️3.9
️✔️consumption
️✔️latest
️✔️3.12
️✔️3.9
️✔️container
️✔️latest
️✔️3.12
️✔️3.9
️✔️containerapp
️✔️latest
️✔️3.12
️✔️3.9
️✔️core
️✔️latest
️✔️3.12
️✔️3.9
️✔️cosmosdb
️✔️latest
️✔️3.12
️✔️3.9
️✔️databoxedge
️✔️latest
️✔️3.12
️✔️3.9
️✔️dls
️✔️latest
️✔️3.12
️✔️3.9
️✔️dms
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventgrid
️✔️latest
️✔️3.12
️✔️3.9
️✔️eventhubs
️✔️latest
️✔️3.12
️✔️3.9
️✔️feedback
️✔️latest
️✔️3.12
️✔️3.9
️✔️find
️✔️latest
️✔️3.12
️✔️3.9
️✔️hdinsight
️✔️latest
️✔️3.12
️✔️3.9
️✔️identity
️✔️latest
️✔️3.12
️✔️3.9
️✔️iot
️✔️latest
️✔️3.12
️✔️3.9
️✔️keyvault
️✔️latest
️✔️3.12
️✔️3.9
️✔️lab
️✔️latest
️✔️3.12
️✔️3.9
️✔️managedservices
️✔️latest
️✔️3.12
️✔️3.9
️✔️maps
️✔️latest
️✔️3.12
️✔️3.9
️✔️marketplaceordering
️✔️latest
️✔️3.12
️✔️3.9
️✔️monitor
️✔️latest
️✔️3.12
️✔️3.9
️✔️mysql
️✔️latest
️✔️3.12
️✔️3.9
️✔️netappfiles
️✔️latest
️✔️3.12
️✔️3.9
️✔️network
️✔️latest
️✔️3.12
️✔️3.9
️✔️policyinsights
️✔️latest
️✔️3.12
️✔️3.9
️✔️privatedns
️✔️latest
️✔️3.12
️✔️3.9
️✔️profile
️✔️latest
️✔️3.12
️✔️3.9
️✔️rdbms
️✔️latest
️✔️3.12
️✔️3.9
️✔️redis
️✔️latest
️✔️3.12
️✔️3.9
️✔️relay
️✔️latest
️✔️3.12
️✔️3.9
️✔️resource
️✔️latest
️✔️3.12
️✔️3.9
️✔️role
️✔️latest
️✔️3.12
️✔️3.9
️✔️search
️✔️latest
️✔️3.12
️✔️3.9
️✔️security
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicebus
️✔️latest
️✔️3.12
️✔️3.9
️✔️serviceconnector
️✔️latest
️✔️3.12
️✔️3.9
️✔️servicefabric
️✔️latest
️✔️3.12
️✔️3.9
️✔️signalr
️✔️latest
️✔️3.12
️✔️3.9
️✔️sql
️✔️latest
️✔️3.12
️✔️3.9
️✔️sqlvm
️✔️latest
️✔️3.12
️✔️3.9
️✔️storage
️✔️latest
️✔️3.12
️✔️3.9
️✔️synapse
️✔️latest
️✔️3.12
️✔️3.9
️✔️telemetry
️✔️latest
️✔️3.12
️✔️3.9
️✔️util
️✔️latest
️✔️3.12
️✔️3.9
️✔️vm
️✔️latest
️✔️3.12
️✔️3.9

@azure-client-tools-bot-prd
Copy link
Copy Markdown

azure-client-tools-bot-prd bot commented May 29, 2025

️✔️AzureCLI-BreakingChangeTest
️✔️Non Breaking Changes

@yonzhan
Copy link
Copy Markdown
Collaborator

yonzhan commented May 29, 2025

Thank you for your contribution! We will review the pull request and get back to you soon.

@github-actions
Copy link
Copy Markdown

The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR.

Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions).
After that please run the following commands to enable git hooks:

pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>

@microsoft-github-policy-service microsoft-github-policy-service bot added Auto-Assign Auto assign by bot Core CLI core infrastructure labels May 29, 2025
@AllyW AllyW force-pushed the add-file-as-binary branch 2 times, most recently from 86a1740 to 1639a38 Compare May 29, 2025 02:52
@AllyW AllyW force-pushed the add-file-as-binary branch from e7e23bc to defac1c Compare May 30, 2025 05:49
@AllyW AllyW force-pushed the add-file-as-binary branch from defac1c to 92c3b81 Compare June 3, 2025 00:47
@AllyW AllyW changed the title {Core} aaz: Add file as bytes arg {Core} aaz: Add AAZFileUploadArg arg Jun 4, 2025
@AllyW AllyW changed the title {Core} aaz: Add AAZFileUploadArg arg {Core} aaz: Add AAZFileUploadArg for file upload Jun 4, 2025
@AllyW AllyW marked this pull request as ready for review June 4, 2025 02:34
Copilot AI review requested due to automatic review settings June 4, 2025 02:34
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds a structured file-upload mechanism to the AAZ framework, enabling CLI args to accept a file path and produce either in-memory content or a file handle for larger files.

  • Introduce AAZFileUploadValue for serializing file data (in-memory if <5 MB, stream otherwise).
  • Define AAZFileUploadType and AAZFileUploadTypeArgAction to validate file paths and integrate with the type system.
  • Add AAZFileUploadArg and update module exports to expose the new argument.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/azure-cli-core/azure/cli/core/aaz/_field_value.py Added AAZFileUploadValue to read or stream file content.
src/azure-cli-core/azure/cli/core/aaz/_field_type.py Added AAZFileUploadType binding to the new value class.
src/azure-cli-core/azure/cli/core/aaz/_arg_action.py Implemented AAZFileUploadTypeArgAction to check file existence.
src/azure-cli-core/azure/cli/core/aaz/_arg.py Introduced AAZFileUploadArg for CLI commands.
src/azure-cli-core/azure/cli/core/aaz/init.py Exported the new AAZFileUploadArg and AAZFileUploadType.
Comments suppressed due to low confidence (4)

src/azure-cli-core/azure/cli/core/aaz/_field_type.py:114

  • [nitpick] Add a docstring explaining the purpose and usage of AAZFileUploadType to clarify how it integrates with AAZFileUploadValue.
class AAZFileUploadType(AAZStrType):

src/azure-cli-core/azure/cli/core/aaz/_field_value.py:63

  • Introduce unit tests for AAZFileUploadValue.to_serialized_data covering both small (<5MB) and large (>=5MB) file scenarios to verify return values and proper resource cleanup.
class AAZFileUploadValue(AAZSimpleValue):

src/azure-cli-core/azure/cli/core/aaz/_arg_action.py:252

  • Missing imports for 'os' and 'copy' at the top of the file. Without these, calls to os.path.exists and copy.deepcopy will raise NameError.
class AAZFileUploadTypeArgAction(AAZArgAction):

src/azure-cli-core/azure/cli/core/aaz/_field_value.py:63

  • [nitpick] Use a context manager (the with statement) when opening files to guarantee that file handles are closed even if an exception occurs, avoiding resource leaks.
class AAZFileUploadValue(AAZSimpleValue):

Comment on lines +603 to +614
class AAZFileUploadArg(AAZStrArg, AAZFileUploadType):
"""Argument that accepts a file path and returns file content, file hander and file size"""

@property
def _type_in_help(self):
return "File Content"

def _build_cmd_action(self):
class Action(AAZFileUploadTypeArgAction):
_schema = self # bind action class with current schema

return Action
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can directly use the AAZFileArg with a new binary fmt to verify the file existance

Comment on lines +252 to +276
class AAZFileUploadTypeArgAction(AAZArgAction):

@classmethod
def setup_operations(cls, dest_ops, values, prefix_keys=None):
if values is None:
data = AAZBlankArgValue # use blank data when values string is None
else:
data = values
data = cls.format_data(data)
dest_ops.add(data)

@classmethod
def format_data(cls, data):
if data == AAZBlankArgValue:
if cls._schema._blank == AAZUndefined:
raise AAZInvalidValueError("argument value cannot be blank")
data = copy.deepcopy(cls._schema._blank)

if data is None:
if cls._schema._nullable:
return data
raise AAZInvalidValueError("field is not nullable")
if not os.path.exists(data):
raise AAZInvalidValueError(f"File '{data}' doesn't exist")
return data
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The exist check can be in the new binary fmt

Comment on lines +63 to +75
class AAZFileUploadValue(AAZSimpleValue): # pylint: disable=too-few-public-methods

def to_serialized_data(self, processor=None, **kwargs):
_file_size = os.path.getsize(self._data)
_content = None
_file_handle = open(self._data, "rb")
if _file_size < 5 * 1024 * 1024: # 5MB
_content = _file_handle.read()
_file_handle.close()
_file_handle = None
return _content, _file_handle, _file_size

return _content, _file_handle, _file_size
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

we can directly use the simple value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

act-platform-engineering-squad Auto-Assign Auto assign by bot Core CLI core infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants