Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/azure-cli-core/azure/cli/core/aaz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
AAZFreeFormDictArg, AAZFloatArg, AAZBaseArg, AAZBoolArg, AAZListArg, AAZResourceGroupNameArg, \
AAZResourceLocationArg, AAZResourceIdArg, AAZSubscriptionIdArg, AAZUuidArg, AAZDateArg, AAZTimeArg, \
AAZDateTimeArg, AAZDurationArg, AAZFileArg, AAZPasswordArg, AAZPaginationTokenArg, AAZPaginationLimitArg, \
AAZAnyTypeArg
AAZAnyTypeArg, AAZFileUploadArg
from ._arg_fmt import AAZStrArgFormat, AAZIntArgFormat, AAZFloatArgFormat, AAZBoolArgFormat, AAZObjectArgFormat, \
AAZDictArgFormat, AAZFreeFormDictArgFormat, AAZListArgFormat, AAZResourceLocationArgFormat, \
AAZResourceIdArgFormat, AAZSubscriptionIdArgFormat, AAZUuidFormat, AAZDateFormat, AAZTimeFormat, \
Expand All @@ -23,7 +23,7 @@
from ._command import AAZCommand, AAZWaitCommand, AAZCommandGroup, \
register_callback, register_command, register_command_group, load_aaz_command_table, link_helper
from ._field_type import AAZIntType, AAZFloatType, AAZStrType, AAZBoolType, AAZDictType, AAZFreeFormDictType, \
AAZListType, AAZObjectType, AAZIdentityObjectType, AAZAnyType
AAZListType, AAZObjectType, AAZIdentityObjectType, AAZAnyType, AAZFileUploadType
from ._operation import AAZHttpOperation, AAZJsonInstanceUpdateOperation, AAZGenericInstanceUpdateOperation, \
AAZJsonInstanceDeleteOperation, AAZJsonInstanceCreateOperation
from ._prompt import AAZPromptInput, AAZPromptPasswordInput
Expand Down
19 changes: 17 additions & 2 deletions src/azure-cli-core/azure/cli/core/aaz/_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
from knack.log import get_logger

from ._arg_action import AAZSimpleTypeArgAction, AAZObjectArgAction, AAZDictArgAction, \
AAZListArgAction, AAZGenericUpdateAction, AAZGenericUpdateForceStringAction, AAZAnyTypeArgAction
AAZListArgAction, AAZGenericUpdateAction, AAZGenericUpdateForceStringAction, AAZAnyTypeArgAction, \
AAZFileUploadTypeArgAction
from ._base import AAZBaseType, AAZUndefined
from ._field_type import AAZObjectType, AAZStrType, AAZIntType, AAZBoolType, AAZFloatType, AAZListType, AAZDictType, \
AAZSimpleType, AAZFreeFormDictType, AAZAnyType
AAZSimpleType, AAZFreeFormDictType, AAZAnyType, AAZFileUploadType
from ._field_value import AAZObject
from ._arg_fmt import AAZObjectArgFormat, AAZListArgFormat, AAZDictArgFormat, AAZFreeFormDictArgFormat, \
AAZSubscriptionIdArgFormat, AAZResourceLocationArgFormat, AAZResourceIdArgFormat, AAZUuidFormat, AAZDateFormat, \
Expand Down Expand Up @@ -599,6 +600,20 @@ def __init__(self, fmt=None, **kwargs):
super().__init__(fmt=fmt, **kwargs)


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
Comment on lines +603 to +614
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



# Generic Update arguments
class AAZGenericUpdateForceStringArg(AAZBoolArg):

Expand Down
27 changes: 27 additions & 0 deletions src/azure-cli-core/azure/cli/core/aaz/_arg_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,33 @@ def format_data(cls, data):
return data


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
Comment on lines +252 to +276
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



class AAZCompoundTypeArgAction(AAZArgAction): # pylint: disable=abstract-method

@classmethod
Expand Down
6 changes: 5 additions & 1 deletion src/azure-cli-core/azure/cli/core/aaz/_field_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from azure.cli.core.util import shell_safe_json_parse
from ._base import AAZBaseType, AAZValuePatch, AAZUndefined
from ._field_value import AAZObject, AAZDict, AAZFreeFormDict, AAZList, AAZSimpleValue, \
AAZIdentityObject, AAZAnyValue
AAZIdentityObject, AAZAnyValue, AAZFileUploadValue
from ._utils import to_snack_case
from .exceptions import AAZUnknownFieldError, AAZConflictFieldDefinitionError, AAZValuePrecisionLossError, \
AAZInvalidFieldError, AAZInvalidValueError
Expand Down Expand Up @@ -111,6 +111,10 @@ def process_data(self, data, **kwargs):
return data


class AAZFileUploadType(AAZStrType):
_ValueCls = AAZFileUploadValue


class AAZAnyType(AAZSimpleType):
"""Any type"""

Expand Down
16 changes: 16 additions & 0 deletions src/azure-cli-core/azure/cli/core/aaz/_field_value.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# pylint: disable=protected-access
from ._base import AAZBaseValue, AAZValuePatch, AAZUndefined
import abc
import os


class AAZSimpleValue(AAZBaseValue):
Expand Down Expand Up @@ -59,6 +60,21 @@ class AAZAnyValue(AAZSimpleValue): # pylint: disable=too-few-public-methods
pass


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
Comment on lines +63 to +75
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



class AAZObject(AAZBaseValue):

def __init__(self, schema, data):
Expand Down
Loading