Skip to content

Commit

Permalink
Fix storage seconds (Azure#1888)
Browse files Browse the repository at this point in the history
* Fix storage bugs Azure#1560 Azure#1563

* 2 space inline comment

* removed else

* abstracted date_string

* abstraction through nested functions

* removed f

* cleared up parameters and error message

* change function calls

* change function calls
  • Loading branch information
Courtney (CJ) Oka authored Jan 27, 2017
1 parent d811566 commit a6bb10b
Show file tree
Hide file tree
Showing 14 changed files with 2,427 additions and 2,419 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ src/build
/.vs/config/applicationhost.config
.vscode/settings.json
.vscode/.ropeproject/
.project
.pydevproject

# Azure deployment credentials
*.pubxml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ def register_attributes_argument(scope, name, attr_class, create=False):
register_extra_cli_argument(scope, 'disabled', action='store_true', help='Create {} in disabled state.'.format(name))
else:
register_extra_cli_argument(scope, 'enabled', default=None, choices=['true', 'false'], help='Enable the {}.'.format(name))
register_extra_cli_argument(scope, 'expires', default=None, help='Expiration UTC datetime (Y-m-d\'T\'H:M\'Z\').', type=datetime_type)
register_extra_cli_argument(scope, 'not_before', default=None, help='Key not usable before the provided UTC datetime (Y-m-d\'T\'H:M\'Z\').', type=datetime_type)
register_extra_cli_argument(scope, 'expires', default=None, help='Expiration UTC datetime (Y-m-d\'T\'H:M:S\'Z\').', type=datetime_type)
register_extra_cli_argument(scope, 'not_before', default=None, help='Key not usable before the provided UTC datetime (Y-m-d\'T\'H:M:S\'Z\').', type=datetime_type)

# ARGUMENT DEFINITIONS

Expand Down Expand Up @@ -122,8 +122,8 @@ def register_attributes_argument(scope, name, attr_class, create=False):
register_cli_argument('keyvault key {}'.format(item), 'destination', options_list=('--protection', '-p'), choices=['software', 'hsm'], help='Specifies the type of key protection.', validator=validate_key_type, type=str.lower)
register_cli_argument('keyvault key {}'.format(item), 'disabled', action='store_true', help='Create key in disabled state.')
register_cli_argument('keyvault key {}'.format(item), 'key_size', options_list=('--size',), type=int)
register_cli_argument('keyvault key {}'.format(item), 'expires', default=None, help='Expiration UTC datetime (Y-m-d\'T\'H:M\'Z\').', type=datetime_type)
register_cli_argument('keyvault key {}'.format(item), 'not_before', default=None, help='Key not usable before the provided UTC datetime (Y-m-d\'T\'H:M\'Z\').', type=datetime_type)
register_cli_argument('keyvault key {}'.format(item), 'expires', default=None, help='Expiration UTC datetime (Y-m-d\'T\'H:M:S\'Z\').', type=datetime_type)
register_cli_argument('keyvault key {}'.format(item), 'not_before', default=None, help='Key not usable before the provided UTC datetime (Y-m-d\'T\'H:M:S\'Z\').', type=datetime_type)

register_cli_argument('keyvault key import', 'pem_file', type=file_type, help='PEM file containing the key to be imported.', arg_group='Key Source', completer=FilesCompleter(), validator=validate_key_import_source)
register_cli_argument('keyvault key import', 'pem_password', help='Password of PEM file.', arg_group='Key Source')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,16 @@ def base64_encoded_certificate_type(string):
return cert_data

def datetime_type(string):
""" Validates UTC datettime in format '%Y-%m-%d\'T\'%H:%M\'Z\''. """
date_format = '%Y-%m-%dT%H:%MZ'
return datetime.strptime(string, date_format)
""" Validates UTC datettime in accepted format. Examples: 2017-12-31T01:11:59Z,
2017-12-31T01:11Z or 2017-12-31T01Z or 2017-12-31 """
accepted_date_formats = ['%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%dT%H:%MZ',
'%Y-%m-%dT%HZ', '%Y-%m-%d']
for form in accepted_date_formats:
try:
return datetime.strptime(string, form)
except ValueError: # checks next format
pass
raise ValueError("Input '{}' not valid. Valid example: 2000-12-31T12:59:59Z".format(string))

def vault_base_url_type(name):
from azure.cli.core._profile import CLOUD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from ._factory import get_storage_data_service_client
from ._validators import \
(datetime_type, datetime_string_type, get_file_path_validator, validate_metadata,
(get_datetime_type, get_file_path_validator, validate_metadata,
get_permission_validator, table_permission_validator, get_permission_help_string,
resource_type_type, services_type, ipv4_range_type, validate_entity,
validate_select, validate_source_uri, validate_blob_type, validate_included_datasets,
Expand Down Expand Up @@ -221,8 +221,8 @@ def register_source_uri_arguments(scope):
register_cli_argument('storage', 'metadata', nargs='+', help='Metadata in space-separated key=value pairs. This overwrites any existing metadata.', validator=validate_metadata)
register_cli_argument('storage', 'timeout', help='Request timeout in seconds. Applies to each call to the service.', type=int)

register_cli_argument('storage', 'if_modified_since', help='Alter only if modified since supplied UTC datetime (Y-m-d\'T\'H:M\'Z\')', type=datetime_type, arg_group='Pre-condition')
register_cli_argument('storage', 'if_unmodified_since', help='Alter only if unmodified since supplied UTC datetime (Y-m-d\'T\'H:M\'Z\')', type=datetime_type, arg_group='Pre-condition')
register_cli_argument('storage', 'if_modified_since', help='Alter only if modified since supplied UTC datetime (Y-m-d\'T\'H:M\'Z\')', type=get_datetime_type(False), arg_group='Pre-condition')
register_cli_argument('storage', 'if_unmodified_since', help='Alter only if unmodified since supplied UTC datetime (Y-m-d\'T\'H:M\'Z\')', type=get_datetime_type(False), arg_group='Pre-condition')
register_cli_argument('storage', 'if_match', arg_group='Pre-condition')
register_cli_argument('storage', 'if_none_match', arg_group='Pre-condition')

Expand Down Expand Up @@ -450,8 +450,8 @@ def register_source_uri_arguments(scope):
register_path_argument('storage file url')

for item in ['container', 'share', 'table', 'queue']:
register_cli_argument('storage {} policy'.format(item), 'start', type=datetime_string_type, help='start UTC datetime (Y-m-d\'T\'H:M\'Z\'). Defaults to time of request.')
register_cli_argument('storage {} policy'.format(item), 'expiry', type=datetime_string_type, help='expiration UTC datetime in (Y-m-d\'T\'H:M\'Z\')')
register_cli_argument('storage {} policy'.format(item), 'start', type=get_datetime_type(True), help='start UTC datetime (Y-m-d\'T\'H:M:S\'Z\'). Defaults to time of request.')
register_cli_argument('storage {} policy'.format(item), 'expiry', type=get_datetime_type(True), help='expiration UTC datetime in (Y-m-d\'T\'H:M:S\'Z\')')

register_cli_argument('storage table', 'table_name', table_name_type, options_list=('--name', '-n'))

Expand Down Expand Up @@ -484,8 +484,8 @@ def register_source_uri_arguments(scope):

for item in ['account', 'blob', 'container', 'file', 'share', 'table', 'queue']:
register_cli_argument('storage {} generate-sas'.format(item), 'ip', help='Specifies the IP address or range of IP addresses from which to accept requests. Supports only IPv4 style addresses.', type=ipv4_range_type)
register_cli_argument('storage {} generate-sas'.format(item), 'expiry', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes invalid. Do not use if a stored access policy is referenced with --id that specifies this value.', type=datetime_string_type)
register_cli_argument('storage {} generate-sas'.format(item), 'start', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes valid. Do not use if a stored access policy is referenced with --id that specifies this value. Defaults to the time of the request.', type=datetime_string_type)
register_cli_argument('storage {} generate-sas'.format(item), 'expiry', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes invalid. Do not use if a stored access policy is referenced with --id that specifies this value.', type=get_datetime_type(True))
register_cli_argument('storage {} generate-sas'.format(item), 'start', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes valid. Do not use if a stored access policy is referenced with --id that specifies this value. Defaults to the time of the request.', type=get_datetime_type(True))
register_cli_argument('storage {} generate-sas'.format(item), 'protocol', options_list=('--https-only',), help='Only permit requests made with the HTTPS protocol. If omitted, requests from both the HTTP and HTTPS protocol are permitted.', action='store_const', const='https')

help_format = 'The permissions the SAS grants. Allowed values: {}. Do not use if a stored access policy is referenced with --id that specifies this value. Can be combined.'
Expand All @@ -505,8 +505,8 @@ def register_source_uri_arguments(scope):

register_cli_argument('storage account generate-sas', 'services', help='The storage services the SAS is applicable for. Allowed values: (b)lob (f)ile (q)ueue (t)able. Can be combined.', type=services_type)
register_cli_argument('storage account generate-sas', 'resource_types', help='The resource types the SAS is applicable for. Allowed values: (s)ervice (c)ontainer (o)bject. Can be combined.', type=resource_type_type)
register_cli_argument('storage account generate-sas', 'expiry', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes invalid.', type=datetime_string_type)
register_cli_argument('storage account generate-sas', 'start', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes valid. Defaults to the time of the request.', type=datetime_string_type)
register_cli_argument('storage account generate-sas', 'expiry', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes invalid.', type=get_datetime_type(True))
register_cli_argument('storage account generate-sas', 'start', help='Specifies the UTC datetime (Y-m-d\'T\'H:M\'Z\') at which the SAS becomes valid. Defaults to the time of the request.', type=get_datetime_type(True))
register_cli_argument('storage account generate-sas', 'account_name', account_name_type, options_list=('--account-name',), help='Storage account name. Must be used in conjunction with either storage account key or a SAS token. Environment Variable: AZURE_STORAGE_ACCOUNT')
register_cli_argument('storage account generate-sas', 'sas_token', ignore_type)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -574,16 +574,24 @@ def process_metric_update_namespace(namespace):
# region TYPES


def datetime_string_type(string):
''' Validates UTC datettime in format '%Y-%m-%d\'T\'%H:%M\'Z\''. '''
date_format = '%Y-%m-%dT%H:%MZ'
return datetime.strptime(string, date_format).strftime(date_format)


def datetime_type(string):
''' Validates UTC datettime in format '%Y-%m-%d\'T\'%H:%M\'Z\''. '''
date_format = '%Y-%m-%dT%H:%MZ'
return datetime.strptime(string, date_format)
def get_datetime_type(to_string):
""" Validates UTC datetime. Examples of accepted forms:
2017-12-31T01:11:59Z,2017-12-31T01:11Z or 2017-12-31T01Z or 2017-12-31 """
def datetime_type(string):
""" Validates UTC datetime. Examples of accepted forms:
2017-12-31T01:11:59Z,2017-12-31T01:11Z or 2017-12-31T01Z or 2017-12-31 """
accepted_date_formats = ['%Y-%m-%dT%H:%M:%SZ', '%Y-%m-%dT%H:%MZ',
'%Y-%m-%dT%HZ', '%Y-%m-%d']
for form in accepted_date_formats:
try:
if to_string:
return datetime.strptime(string, form).strftime(form)
else:
return datetime.strptime(string, form)
except ValueError:
continue
raise ValueError("Input '{}' not valid. Valid example: 2000-12-31T12:59:59Z".format(string))
return datetime_type


def ipv4_range_type(string):
Expand Down
Loading

0 comments on commit a6bb10b

Please sign in to comment.