Skip to content

Commit

Permalink
{Storage} az storage container lease: Track2 migration (#22150)
Browse files Browse the repository at this point in the history
* storage container lease track2 migration

* linter

* add type

* add type

* indent
  • Loading branch information
calvinhzy authored Apr 26, 2022
1 parent 1d065f0 commit 2b6056d
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ def cf_container_client(cli_ctx, kwargs):
return cf_blob_service(cli_ctx, kwargs).get_container_client(container=kwargs.pop('container_name', None))


def cf_container_lease_client(cli_ctx, kwargs):
t_lease_service = get_sdk(cli_ctx, ResourceType.DATA_STORAGE_BLOB, '_lease#BlobLeaseClient')
container_client = cf_container_client(cli_ctx, kwargs)
return t_lease_service(client=container_client, lease_id=kwargs.pop('lease_id', None))


def cf_adls_service(cli_ctx, kwargs):
client_kwargs = prepare_client_kwargs_track2(cli_ctx)
client_kwargs = _config_location_mode(kwargs, client_kwargs)
Expand Down
55 changes: 55 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 @@ -1382,6 +1382,61 @@
short-summary: Manage blob storage container leases.
"""

helps['storage container lease acquire'] = """
type: command
short-summary: Request a new lease.
long-summary: If the container does not have an active lease, the Blob service creates a lease on the container and returns a new lease ID.
examples:
- name: Request a new lease.
text: az storage container lease acquire --container-name mycontainer --account-name mystorageaccount --account-key 0000-0000
"""

helps['storage container lease renew'] = """
type: command
short-summary: Renew the lease.
long-summary: The lease can be renewed if the lease ID specified matches that associated with the
container. Note that the lease may be renewed even if it has expired as long as the
container has not been leased again since the expiration of that lease. When you renew a
lease, the lease duration clock resets.
examples:
- name: Renew the lease.
text: az storage container lease renew -c mycontainer --lease-id "32fe23cd-4779-4919-adb3-357e76c9b1bb" --account-name mystorageaccount --account-key 0000-0000
"""

helps['storage container lease break'] = """
type: command
short-summary: Break the lease, if the container has an active lease.
long-summary: Once a lease is broken, it cannot be renewed. Any authorized request can break the lease;
the request is not required to specify a matching lease ID. When a lease is broken, the
lease break period is allowed to elapse, during which time no lease operation except break
and release can be performed on the container. When a lease is successfully broken, the
response indicates the interval in seconds until a new lease can be acquired.
examples:
- name: Break the lease.
text: az storage container lease break -c mycontainer --lease-break-period 10 --account-name mystorageaccount --account-key 0000-0000
"""

helps['storage container lease change'] = """
type: command
short-summary: Change the lease ID of an active lease.
long-summary: A change must include the current lease ID and a new lease ID.
examples:
- name: Change the lease.
text: az storage container lease change -c mycontainer --lease-id "32fe23cd-4779-4919-adb3-357e76c9b1bb" --proposed-lease-id "sef2ef2d-4779-4919-adb3-357e76c9b1bb" --account-name mystorageaccount --account-key 0000-0000
"""

helps['storage container lease release'] = """
type: command
short-summary: Release the lease.
long-summary: The lease may be released if the lease_id specified matches that associated with the
container. Releasing the lease allows another client to immediately acquire the lease for
the container as soon as the release is complete.
examples:
- name: Release the lease.
text: az storage container lease release -c mycontainer --lease-id "32fe23cd-4779-4919-adb3-357e76c9b1bb" --account-name mystorageaccount --account-key 0000-0000
"""


helps['storage container legal-hold'] = """
type: group
short-summary: Manage container legal holds.
Expand Down
41 changes: 35 additions & 6 deletions src/azure-cli/azure/cli/command_modules/storage/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,9 +1369,6 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
with self.argument_context('storage container set-permission') as c:
c.ignore('signed_identifiers')

with self.argument_context('storage container lease') as c:
c.argument('container_name', container_name_type)

with self.argument_context('storage container') as c:
c.argument('account_name', completer=get_resource_name_completion_list('Microsoft.Storage/storageAccounts'))
c.argument('resource_group_name', required=False, validator=process_resource_group)
Expand Down Expand Up @@ -1445,9 +1442,41 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
c.extra('encryption_scope', help='A predefined encryption scope used to encrypt the data on the service.')
c.ignore('sas_token')

with self.argument_context('storage container lease') as c:
c.argument('lease_duration', type=int)
c.argument('lease_break_period', type=int)
for cmd in ['acquire', 'renew', 'break', 'change', 'release']:
with self.argument_context(f'storage container lease {cmd}') as c:
c.register_precondition_options()
c.register_container_arguments()
c.argument('container_name', required=True, options_list=['--container-name', '-c'])

with self.argument_context('storage container lease acquire') as c:
c.argument('lease_duration',
help='Specify the duration of the lease, in seconds, or negative one (-1) for a lease that never'
' expires. A non-infinite lease can be between 15 and 60 seconds. A lease duration cannot '
'be changed using renew or change. Default is -1 (infinite lease)', type=int)
c.extra('lease_id', options_list='--proposed-lease-id',
help='Proposed lease ID, in a GUID string format. The Blob service returns 400 (Invalid request) '
'if the proposed lease ID is not in the correct format.')

for cmd in ['renew', 'change', 'release']:
with self.argument_context(f'storage container lease {cmd}') as c:
c.extra('lease_id', help='Lease ID for active lease.', required=True)

with self.argument_context('storage container lease break') as c:
c.extra('lease_break_period', type=int, help='This is the proposed duration of seconds that the lease should '
'continue before it is broken, between 0 and 60 seconds. '
'This break period is only used if it is shorter than the time '
'remaining on the lease. If longer, the time remaining on the '
'lease is used. A new lease will not be available before the '
'break period has expired, but the lease may be held for longer '
'than the break period. If this header does not appear with a '
'break operation, a fixed-duration lease breaks after the '
'remaining lease period elapses, and an infinite lease breaks '
'immediately.')

with self.argument_context('storage container lease change') as c:
c.extra('proposed_lease_id', help='Proposed lease ID, in a GUID string format. The Blob service returns 400'
' (Invalid request) if the proposed lease ID is not in the correct format.',
required=True)

with self.argument_context('storage container list', resource_type=ResourceType.DATA_STORAGE_BLOB) as c:
c.extra('timeout', timeout_type)
Expand Down
23 changes: 17 additions & 6 deletions src/azure-cli/azure/cli/command_modules/storage/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
cf_blob_client, cf_blob_lease_client,
cf_or_policy, cf_container_client,
cf_queue_service, cf_table_service, cf_table_client,
cf_sa_blob_inventory, cf_blob_service, cf_queue_client)
cf_sa_blob_inventory, cf_blob_service, cf_queue_client,
cf_container_lease_client)

from azure.cli.core.commands import CliCommandType
from azure.cli.core.commands.arm import show_exception_handler
Expand Down Expand Up @@ -470,11 +471,21 @@ def get_custom_sdk(custom_module, client_factory, resource_type=ResourceType.DAT
g.storage_command_oauth(
'metadata show', 'get_container_metadata', exception_handler=show_exception_handler)

g.storage_command_oauth('lease acquire', 'acquire_container_lease')
g.storage_command_oauth('lease renew', 'renew_container_lease')
g.storage_command_oauth('lease release', 'release_container_lease')
g.storage_command_oauth('lease change', 'change_container_lease')
g.storage_command_oauth('lease break', 'break_container_lease')
container_lease_client_sdk = CliCommandType(
operations_tmpl='azure.multiapi.storagev2.blob._lease#BlobLeaseClient.{}',
client_factory=cf_container_lease_client,
resource_type=ResourceType.DATA_STORAGE_BLOB
)

with self.command_group('storage container lease', container_lease_client_sdk,
resource_type=ResourceType.DATA_STORAGE_BLOB,
custom_command_type=get_custom_sdk('blob', client_factory=cf_container_lease_client,
resource_type=ResourceType.DATA_STORAGE_BLOB)) as g:
g.storage_custom_command_oauth('acquire', 'acquire_blob_lease')
g.storage_custom_command_oauth('renew', 'renew_blob_lease')
g.storage_command_oauth('break', 'break_lease')
g.storage_command_oauth('change', 'change')
g.storage_command_oauth('release', 'release')

with self.command_group('storage container', custom_command_type=blob_service_custom_sdk,
resource_type=ResourceType.DATA_STORAGE_BLOB,
Expand Down
Loading

0 comments on commit 2b6056d

Please sign in to comment.