Skip to content

Commit

Permalink
added worm feature
Browse files Browse the repository at this point in the history
enable listing usage by region

ci fixes

fixes

fixes

flake8 fixes

removed prints
  • Loading branch information
williexu committed May 15, 2018
1 parent aa958b3 commit 0981e24
Show file tree
Hide file tree
Showing 16 changed files with 1,596 additions and 88 deletions.
4 changes: 4 additions & 0 deletions src/storage-preview/azext_storage_preview/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, cli_ctx=None):
storage_custom = CliCommandType(operations_tmpl='azext_storage_preview.custom#{}')

super(StorageCommandsLoader, self).__init__(cli_ctx=cli_ctx,
min_profile='2017-03-10-profile',
resource_type=CUSTOM_DATA_STORAGE,
custom_command_type=storage_custom,
command_group_cls=StorageCommandGroup,
Expand Down Expand Up @@ -197,14 +198,17 @@ def handler(ex):

def _register_data_plane_account_arguments(self, command_name):
""" Add parameters required to create a storage client """
from azure.cli.core.commands.parameters import get_resource_name_completion_list
from ._validators import validate_client_parameters
command = self.command_loader.command_table.get(command_name, None)
if not command:
return

group_name = 'Storage Account'

command.add_argument('account_name', '--account-name', required=False, default=None,
arg_group=group_name,
completer=get_resource_name_completion_list('Microsoft.Storage/storageAccounts'),
help='Storage account name. Related environment variable: AZURE_STORAGE_ACCOUNT. Must be '
'used in conjunction with either storage account key or a SAS token. If neither are '
'present, the command will try to query the storage account key using the '
Expand Down
4 changes: 4 additions & 0 deletions src/storage-preview/azext_storage_preview/_client_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,7 @@ def get_creator(name, service_type):

def cf_sa(cli_ctx, _):
return storage_client_factory(cli_ctx).storage_accounts


def cf_blob_container_mgmt(cli_ctx, _):
return storage_client_factory(cli_ctx).blob_containers
8 changes: 8 additions & 0 deletions src/storage-preview/azext_storage_preview/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,11 @@ def transformer(result):

return return_list
return transformer


# pylint: disable=inconsistent-return-statements
def transform_immutability_policy(result):
# service returns policy with period value of "0" after it has been deleted
# this only shows the policy if the property value is greater than 0
if result.immutability_period_since_creation_in_days:
return result
15 changes: 15 additions & 0 deletions src/storage-preview/azext_storage_preview/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,21 @@
short-summary: Manage container stored access policies.
"""

helps['storage container immutability-policy'] = """
type: group
short-summary: Manage container immutability policies.
"""

helps['storage container legal-hold'] = """
type: group
short-summary: Manage container legal holds.
"""

helps['storage container legal-hold show'] = """
type: command
short-summary: Get the legal hold properties of a container.
"""

helps['storage cors'] = """
type: group
short-summary: Manage storage service Cross-Origin Resource Sharing (CORS).
Expand Down
19 changes: 15 additions & 4 deletions src/storage-preview/azext_storage_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
get_char_options_validator, validate_bypass, validate_encryption_source)
from .profiles import CUSTOM_MGMT_STORAGE


def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statements
from argcomplete.completers import FilesCompleter
from six import u as unicode_string
Expand All @@ -23,7 +24,7 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
from azure.cli.core.commands.parameters import get_resource_name_completion_list

from .sdkutil import get_table_data_type
from .completers import get_storage_name_completion_list
from .completers import get_storage_name_completion_list, get_container_name_completions

t_base_blob_service = self.get_sdk('blob.baseblobservice#BaseBlobService')
t_file_service = self.get_sdk('file#FileService')
Expand All @@ -38,8 +39,7 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
parent='container_name'))

container_name_type = CLIArgumentType(options_list=['--container-name', '-c'], help='The container name.',
completer=get_storage_name_completion_list(t_base_blob_service,
'list_containers'))
completer=get_container_name_completions)
directory_type = CLIArgumentType(options_list=['--directory-name', '-d'], help='The directory name.',
completer=get_storage_name_completion_list(t_file_service,
'list_directories_and_files',
Expand Down Expand Up @@ -83,7 +83,7 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
c.argument('if_match')
c.argument('if_none_match')

for item in ['delete', 'list', 'show', 'show-usage', 'update', 'keys']:
for item in ['delete', 'list', 'show', 'update', 'keys']:
with self.argument_context('storage account {}'.format(item)) as c:
c.argument('account_name', acct_name_type, options_list=['--name', '-n'])

Expand Down Expand Up @@ -416,6 +416,17 @@ def load_arguments(self, _): # pylint: disable=too-many-locals, too-many-statem
c.argument('lease_duration', type=int)
c.argument('lease_break_period', type=int)

with self.argument_context('storage container immutability-policy') as c:
c.argument('immutability_period_since_creation_in_days', options_list='--period')
c.argument('container_name', container_name_type)
c.argument('account_name', completer=get_resource_name_completion_list('Microsoft.Storage/storageAccounts'))

with self.argument_context('storage container legal-hold') as c:
c.argument('container_name', container_name_type)
c.argument('account_name', completer=get_resource_name_completion_list('Microsoft.Storage/storageAccounts'))
c.argument('tags', nargs='+', help='Each tag should be 3 to 23 alphanumeric characters and is '
'normalized to lower case')

with self.argument_context('storage share') as c:
c.argument('share_name', share_name_type, options_list=('--name', '-n'))

Expand Down
22 changes: 21 additions & 1 deletion src/storage-preview/azext_storage_preview/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

from azure.cli.core.commands import CliCommandType
from azure.cli.core.profiles import ResourceType
from ._client_factory import (cf_sa, blob_data_service_factory,
from ._client_factory import (cf_sa, cf_blob_container_mgmt, blob_data_service_factory,
page_blob_service_factory, file_data_service_factory,
queue_data_service_factory, table_data_service_factory,
cloud_storage_account_service_factory,
multi_service_properties_factory)
from .sdkutil import cosmosdb_table_exists
from .profiles import CUSTOM_DATA_STORAGE, CUSTOM_MGMT_STORAGE
from ._format import transform_immutability_policy


def load_command_table(self, _): # pylint: disable=too-many-locals, too-many-statements
Expand Down Expand Up @@ -190,6 +191,25 @@ def get_custom_sdk(custom_module, client_factory, resource_type=CUSTOM_DATA_STOR
g.storage_custom_command_oauth('policy show', 'get_acl_policy', exception_handler=g.get_handler_suppress_404())
g.storage_custom_command_oauth('policy list', 'list_acl_policies', table_transformer=transform_acl_list_output)

blob_container_mgmt_sdk = CliCommandType(
operations_tmpl='azext_storage_preview.vendored_sdks.azure_mgmt_storage.operations.blob_containers_operations'
'#BlobContainersOperations.{}',
client_factory=cf_blob_container_mgmt,
resource_type=CUSTOM_MGMT_STORAGE
)

with self.command_group('storage container immutability-policy', command_type=blob_container_mgmt_sdk) as g:
g.command('show', 'get_immutability_policy', transform=transform_immutability_policy)
g.command('create', 'create_or_update_immutability_policy')
g.command('delete', 'delete_immutability_policy', transform=lambda x: None)
g.command('lock', 'lock_immutability_policy')
g.command('extend', 'extend_immutability_policy')

with self.command_group('storage container legal-hold', command_type=blob_container_mgmt_sdk) as g:
g.command('set', 'set_legal_hold')
g.command('clear', 'clear_legal_hold')
g.command('show', 'get', transform=lambda x: getattr(x, 'legal_hold', x))

file_sdk = CliCommandType(
operations_tmpl='azure.multiapi.storage.file.fileservice#FileService.{}',
client_factory=file_data_service_factory,
Expand Down
14 changes: 14 additions & 0 deletions src/storage-preview/azext_storage_preview/completers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from .util import get_storage_client
from ._validators import validate_client_parameters
from ._client_factory import cf_sa, cf_blob_container_mgmt


@Completer
Expand Down Expand Up @@ -97,3 +98,16 @@ def completer(cmd, _, namespace):
return list(getattr(client, func)(container_name))

return completer


@Completer
def get_container_name_completions(cmd, _, namespace):
if namespace.account_name:
account_client = cf_sa(cmd.cli_ctx, None)
account = next((x for x in account_client.list() if x.name == namespace.account_name), None)
if account:
from msrestazure.tools import parse_resource_id
rg = parse_resource_id(account.id)['resource_group']
container_client = cf_blob_container_mgmt(cmd.cli_ctx, None)
return [container.name for container in container_client.list(rg, account.name).value]
return []
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ def show_storage_account_connection_string(cmd, resource_group_name, account_nam
return {'connectionString': connection_string}


def show_storage_account_usage(cmd):
def show_storage_account_usage(cmd, location=None):
scf = storage_client_factory(cmd.cli_ctx)
return next((x for x in scf.usage.list() if x.name.value == 'StorageAccounts'), None) # pylint: disable=no-member
if not location:
return next((x for x in scf.usage.list() if x.name.value == 'StorageAccounts'), None) # pylint: disable=no-member
return next((x for x in scf.usage.list_by_location(location) if x.name.value == 'StorageAccounts'), None) # pylint: disable=no-member


# pylint: disable=too-many-locals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interactions:
cache-control: [no-cache]
content-length: ['217']
content-type: [application/json]
date: ['Mon, 14 May 2018 23:39:28 GMT']
date: ['Tue, 15 May 2018 02:32:42 GMT']
expires: ['-1']
pragma: [no-cache]
server: [Microsoft-Azure-Storage-Resource-Provider/1.0, Microsoft-HTTPAPI/2.0]
Expand All @@ -30,4 +30,34 @@ interactions:
vary: [Accept-Encoding]
x-content-type-options: [nosniff]
status: {code: 200, message: OK}
- request:
body: null
headers:
Accept: [application/json]
Accept-Encoding: ['gzip, deflate']
CommandName: [storage account show-usage]
Connection: [keep-alive]
Content-Type: [application/json; charset=utf-8]
User-Agent: [python/3.6.2 (Windows-10-10.0.16299-SP0) requests/2.18.4 msrest/0.4.29
msrest_azure/0.4.29 azure-mgmt-storage/2.0.0rc1 Azure-SDK-For-Python AZURECLI/2.0.33]
accept-language: [en-US]
method: GET
uri: https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Storage/locations/centraluseuap/usages?api-version=2018-02-01
response:
body: {string: '{"value":[{"unit":"Count","currentValue":0,"limit":250,"name":{"value":"StorageAccounts","localizedValue":"Storage
Accounts"}}]}'}
headers:
cache-control: [no-cache]
content-length: ['128']
content-type: [application/json]
date: ['Tue, 15 May 2018 02:32:43 GMT']
expires: ['-1']
pragma: [no-cache]
server: ['Microsoft-Azure-Storage-Resource-Provider/1.0,Microsoft-HTTPAPI/2.0
Microsoft-HTTPAPI/2.0']
strict-transport-security: [max-age=31536000; includeSubDomains]
transfer-encoding: [chunked]
vary: [Accept-Encoding]
x-content-type-options: [nosniff]
status: {code: 200, message: OK}
version: 1
Loading

0 comments on commit 0981e24

Please sign in to comment.