{Core} aaz: Add AAZFileUploadArg for file upload#31573
{Core} aaz: Add AAZFileUploadArg for file upload#31573
aaz: Add AAZFileUploadArg for file upload#31573Conversation
️✔️AzureCLI-FullTest
|
️✔️AzureCLI-BreakingChangeTest
|
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
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). pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>
|
86a1740 to
1639a38
Compare
aaz: Add file as bytes argaaz: Add AAZFileUploadArg arg
aaz: Add AAZFileUploadArg argaaz: Add AAZFileUploadArg for file upload
There was a problem hiding this comment.
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
AAZFileUploadValuefor serializing file data (in-memory if <5 MB, stream otherwise). - Define
AAZFileUploadTypeandAAZFileUploadTypeArgActionto validate file paths and integrate with the type system. - Add
AAZFileUploadArgand 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):
| 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 |
There was a problem hiding this comment.
You can directly use the AAZFileArg with a new binary fmt to verify the file existance
| 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 |
There was a problem hiding this comment.
The exist check can be in the new binary fmt
| 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 |
There was a problem hiding this comment.
we can directly use the simple value
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 featureThis checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.