Skip to content

Commit

Permalink
{Storage} Migrate azure-storage-queue to track2 sdk (Azure#21912)
Browse files Browse the repository at this point in the history
* migrate storage queue to track2 sdk

* fix checks

* fix checks
  • Loading branch information
jonie001 authored Apr 8, 2022
1 parent 86263ed commit b0738f3
Show file tree
Hide file tree
Showing 14 changed files with 3,067 additions and 1,083 deletions.
114 changes: 114 additions & 0 deletions src/azure-cli/azure/cli/command_modules/storage/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -2401,6 +2401,120 @@
short-summary: Manage storage queues.
"""

helps['storage queue stats'] = """
type: command
short-summary: >
Retrieve statistics related to replication for the Queue service.
It is only available when read-access geo-redundant replication is enabled for the storage account.
examples:
- name: Show statistics related to replication for the Queue service.
text: az storage queue stats --account-name mystorageaccount
"""

helps['storage queue exists'] = """
type: command
short-summary: Return a boolean indicating whether the queue exists.
examples:
- name: Check whether the queue exists.
text: az storage queue exists -n myqueue --account-name mystorageaccount
"""

helps['storage queue generate-sas'] = """
type: command
short-summary: Generate a shared access signature for the queue.Use the returned signature with the sas_token parameter of QueueService.
examples:
- name: Generate a sas token for the queue with read-only permissions.
text: |
end=`date -u -d "30 minutes" '+%Y-%m-%dT%H:%MZ'`
az storage queue generate-sas -n myqueue --account-name mystorageaccount --permissions r --expiry $end --https-only
- name: Generate a sas token for the queue with ip range specified.
text: |
end=`date -u -d "30 minutes" '+%Y-%m-%dT%H:%MZ'`
az storage queue generate-sas -n myqueue --account-name mystorageaccount --ip "176.134.171.0-176.134.171.255" --permissions r --expiry $end --https-only
"""

helps['storage queue create'] = """
type: command
short-summary: Create a queue under the given account.
examples:
- name: Create a queue under the given account with metadata.
text: az storage queue create -n myqueue --metadata key1=value1 key2=value2 --account-name mystorageaccount
"""

helps['storage queue delete'] = """
type: command
short-summary: Delete the specified queue and any messages it contains.
examples:
- name: Delete the specified queue, throw an exception if the queue doesn't exist.
text: az storage queue delete -n myqueue --fail-not-exist --account-name mystorageaccount
"""

helps['storage queue metadata show'] = """
type: command
short-summary: Return all user-defined metadata for the specified queue.
examples:
- name: Return all user-defined metadata for the specified queue.
text: az storage queue metadata show -n myqueue --account-name mystorageaccount
"""

helps['storage queue metadata update'] = """
type: command
short-summary: Set user-defined metadata on the specified queue.
examples:
- name: Set user-defined metadata on the specified queue.
text: az storage queue metadata update -n myqueue --metadata a=b c=d --account-name mystorageaccount
"""

helps['storage message put'] = """
type: command
short-summary: Add a new message to the back of the message queue.
examples:
- name: Add a new message which will live one day.
text: az storage message put -q myqueue --content mymessagecontent --time-to-live 86400 --account-name mystorageaccount
"""

helps['storage message peek'] = """
type: command
short-summary: Retrieve one or more messages from the front of the queue, but do not alter the visibility of the message.
examples:
- name: Retrieve 5 messages from the front of the queue (do not alter the visibility of the message).
text: az storage message peek -q myqueue --num-messages 5 --account-name mystorageaccount
"""

helps['storage message get'] = """
type: command
short-summary: Retrieve one or more messages from the front of the queue.
examples:
- name: Retrieve one message from the front of the queue and reset the visibility timeout to 5 minutes later.
text: az storage message get -q myqueue --visibility-timeout 300 --account-name mystorageaccount
"""

helps['storage message update'] = """
type: command
short-summary: Update the visibility timeout of a message.
examples:
- name: Update the visibility timeout and content of a message.
text: |
az storage message update --id messageid --pop-receipt popreceiptreturned -q myqueue
--visibility-timeout 3600 --content newmessagecontent --account-name mystorageaccount
"""

helps['storage message delete'] = """
type: command
short-summary: Delete the specified message.
examples:
- name: Delete the specified message.
text: az storage message delete --id messageid --pop-receipt popreceiptreturned -q myqueue --account-name mystorageaccount
"""

helps['storage message clear'] = """
type: command
short-summary: Delete all messages from the specified queue.
examples:
- name: Delete all messages from the specified queue.
text: az storage message clear -q myqueue --account-name mystorageaccount
"""

helps['storage queue list'] = """
type: command
short-summary: List queues in a storage account.
Expand Down
89 changes: 74 additions & 15 deletions src/azure-cli/azure/cli/command_modules/storage/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem

t_base_blob_service = self.get_sdk('blob.baseblobservice#BaseBlobService')
t_file_service = self.get_sdk('file#FileService')
t_queue_service = self.get_sdk('queue#QueueService')
t_queue_service = self.get_sdk('_queue_service_client#QueueServiceClient',
resource_type=ResourceType.DATA_STORAGE_QUEUE)
t_table_service = get_table_data_type(self.cli_ctx, 'table', 'TableService')

storage_account_type = CLIArgumentType(options_list='--storage-account',
Expand Down Expand Up @@ -1765,24 +1766,29 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
c.extra('services', validator=get_char_options_validator('bfqt', 'services'), required=True,
options_list='--services')

for item in ['stats', 'exists', 'metadata show', 'metadata update']:
with self.argument_context('storage queue {}'.format(item)) as c:
c.extra('timeout', help='Request timeout in seconds. Applies to each call to the service.', type=int)

for item in ['exists', 'generate-sas', 'create', 'delete', 'metadata show', 'metadata update']:
with self.argument_context('storage queue {}'.format(item)) as c:
c.extra('queue_name', queue_name_type, options_list=('--name', '-n'), required=True)

with self.argument_context('storage queue generate-sas') as c:
from .completers import get_storage_acl_name_completion_list

t_queue_permissions = self.get_sdk('queue.models#QueuePermissions')
t_queue_permissions = self.get_sdk('_models#QueueSasPermissions', resource_type=ResourceType.DATA_STORAGE_QUEUE)

c.register_sas_arguments()

c.argument('id', options_list='--policy-name',
c.argument('policy_id', options_list='--policy-name',
help='The name of a stored access policy within the share\'s ACL.',
completer=get_storage_acl_name_completion_list(t_queue_permissions, 'queue_name', 'get_queue_acl'))
completer=get_storage_acl_name_completion_list(t_queue_service, 'container_name',
'get_queue_access_policy'))
c.argument('permission', options_list='--permissions',
help=sas_help.format(get_permission_help_string(t_queue_permissions)),
validator=get_permission_validator(t_queue_permissions))
c.ignore('sas_token')
c.ignore('auth_mode')

with self.argument_context('storage queue') as c:
c.argument('queue_name', queue_name_type, options_list=('--name', '-n'))

with self.argument_context('storage queue list') as c:
c.argument('include_metadata', help='Specify that queue metadata be returned in the response.')
Expand All @@ -1795,16 +1801,24 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
c.extra('timeout', help='Request timeout in seconds. Apply to each call to the service.', type=int)

with self.argument_context('storage queue create') as c:
c.argument('queue_name', queue_name_type, options_list=('--name', '-n'), completer=None)
c.argument('fail_on_exist', help='Specify whether to throw an exception if the queue already exists.')

with self.argument_context('storage queue delete') as c:
c.argument('fail_not_exist', help='Specify whether to throw an exception if the queue doesn\'t exist.')

for item in ['create', 'delete', 'show', 'list', 'update']:
with self.argument_context('storage queue policy {}'.format(item)) as c:
c.extra('queue_name', queue_name_type, required=True)

with self.argument_context('storage queue policy') as c:
from .completers import get_storage_acl_name_completion_list

t_queue_permissions = self.get_sdk('queue.models#QueuePermissions')
t_queue_permissions = self.get_sdk('_models#QueueSasPermissions', resource_type=ResourceType.DATA_STORAGE_QUEUE)

c.argument('container_name', queue_name_type)
c.argument('policy_name', options_list=('--name', '-n'), help='The stored access policy name.',
completer=get_storage_acl_name_completion_list(t_queue_service, 'container_name', 'get_queue_acl'))
completer=get_storage_acl_name_completion_list(t_queue_service, 'container_name',
'get_queue_access_policy'))

help_str = 'Allowed values: {}. Can be combined'.format(get_permission_help_string(t_queue_permissions))
c.argument('permission', options_list='--permissions', help=help_str,
Expand All @@ -1815,10 +1829,55 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
c.argument('expiry', type=get_datetime_type(True), help='expiration UTC datetime in (Y-m-d\'T\'H:M:S\'Z\')')
c.ignore('auth_mode')

with self.argument_context('storage message') as c:
c.argument('queue_name', queue_name_type)
c.argument('message_id', options_list='--id')
c.argument('content', type=str, help='Message content, up to 64KB in size.')
from six import u as unicode_string
for item in ['get', 'peek', 'put', 'update', 'delete', 'clear']:
with self.argument_context('storage message {}'.format(item)) as c:
c.extra('queue_name', queue_name_type, required=True)
c.extra('timeout', help='Request timeout in seconds. Applies to each call to the service.', type=int)

for item in ['update', 'delete']:
with self.argument_context('storage message {}'.format(item)) as c:
c.argument('message', options_list='--id', required=True,
help='The message id identifying the message to delete.')
c.argument('pop_receipt', required=True,
help='A valid pop receipt value returned from an earlier call to '
'the :func:`~get_messages` or :func:`~update_message` operation.')

with self.argument_context('storage message put') as c:
c.argument('content', type=unicode_string, help='Message content, up to 64KB in size.')
c.extra('time_to_live', type=int,
help='Specify the time-to-live interval for the message, in seconds. '
'The time-to-live may be any positive number or -1 for infinity. '
'If this parameter is omitted, the default time-to-live is 7 days.')
c.extra('visibility_timeout', type=int,
help='If not specified, the default value is 0. Specify the new visibility timeout value, '
'in seconds, relative to server time. The value must be larger than or equal to 0, '
'and cannot be larger than 7 days. The visibility timeout of a message cannot be set '
'to a value later than the expiry time. visibility_timeout should be set to a value '
'smaller than the time_to_live value.')

with self.argument_context('storage message get') as c:
c.extra('messages_per_page', options_list='--num-messages', type=int, default=1,
help='A nonzero integer value that specifies the number of messages to retrieve from the queue, '
'up to a maximum of 32. If fewer are visible, the visible messages are returned. '
'By default, a single message is retrieved from the queue with this operation.')
c.extra('visibility_timeout', type=int,
help='Specify the new visibility timeout value, in seconds, relative to server time. '
'The new value must be larger than or equal to 1 second, and cannot be larger than 7 days. '
'The visibility timeout of a message can be set to a value later than the expiry time.')

with self.argument_context('storage message peek') as c:
c.extra('max_messages', options_list='--num-messages', type=int,
help='A nonzero integer value that specifies the number of messages to peek from the queue, up to '
'a maximum of 32. By default, a single message is peeked from the queue with this operation.')

with self.argument_context('storage message update') as c:
c.argument('content', type=unicode_string, help='Message content, up to 64KB in size.')
c.extra('visibility_timeout', type=int,
help='If not specified, the default value is 0. Specify the new visibility timeout value, in seconds, '
'relative to server time. The new value must be larger than or equal to 0, and cannot be larger '
'than 7 days. The visibility timeout of a message cannot be set to a value later than the expiry '
'time. A message can be updated until it has been deleted or has expired.')

with self.argument_context('storage remove') as c:
from .completers import file_path_completer
Expand Down
46 changes: 46 additions & 0 deletions src/azure-cli/azure/cli/command_modules/storage/_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# --------------------------------------------------------------------------------------------

import base64

from dateutil import parser
from knack.log import get_logger
from knack.util import todict, to_camel_case
from .track2_util import _encode_bytes
Expand Down Expand Up @@ -320,3 +322,47 @@ def transform_blob_upload_output(result):
result["lastModified"] = result["last_modified"]
del result["last_modified"]
return result


def transform_queue_stats_output(result):
if isinstance(result, dict):
new_result = {}
from azure.cli.core.commands.arm import make_camel_case
for key in result.keys():
new_key = make_camel_case(key)
new_result[new_key] = transform_queue_stats_output(result[key])
return new_result
return result


def transform_message_list_output(result):
for i, item in enumerate(result):
result[i] = transform_message_output(item)
return list(result)


def transform_message_output(result):
result = todict(result)
new_result = {'expirationTime': result.pop('expiresOn', None),
'insertionTime': result.pop('insertedOn', None),
'timeNextVisible': result.pop('nextVisibleOn', None)}
from azure.cli.core.commands.arm import make_camel_case
for key in sorted(result.keys()):
new_result[make_camel_case(key)] = result[key]
return new_result


def transform_queue_policy_json_output(result):
new_result = {}
for policy in sorted(result.keys()):
new_result[policy] = transform_queue_policy_output(result[policy])
return new_result


def transform_queue_policy_output(result):
result = todict(result)
if result['start']:
result['start'] = parser.parse(result['start'])
if result['expiry']:
result['expiry'] = parser.parse(result['expiry'])
return result
Loading

0 comments on commit b0738f3

Please sign in to comment.