Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cosmosdb pitr timestamp fixes #2914

Merged
merged 5 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Making utc the default timezone and adding name argument to restorabl…
…e command
  • Loading branch information
kavskalyan committed Jan 16, 2021
commit bf57be2f88c7cf60ef551a523382133d296e88d7
14 changes: 9 additions & 5 deletions src/cosmosdb-preview/azext_cosmosdb_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from enum import Enum

from azure.cli.core.commands.parameters import (
get_resource_name_completion_list, name_type, get_enum_type, get_three_state_flag, get_datetime_type, get_location_type)
get_resource_name_completion_list, name_type, get_enum_type, get_three_state_flag, get_location_type)

from azext_cosmosdb_preview._validators import (
validate_capabilities, validate_virtual_network_rules, validate_ip_range_filter)

from azext_cosmosdb_preview.actions import (
CreateLocation, CreateDatabaseRestoreResource)
CreateLocation, CreateDatabaseRestoreResource, UtcDatetimeAction)

from azext_cosmosdb_preview.vendored_sdks.azure_mgmt_cosmosdb.models import DefaultConsistencyLevel, DatabaseAccountKind, ServerVersion

Expand All @@ -38,7 +38,7 @@ def load_arguments(self, _):
c.argument('server_version', arg_type=get_enum_type(ServerVersion), help="Valid only for MongoDB accounts.", is_preview=True)
c.argument('is_restore_request', options_list=['--is-restore-request', '-r'], arg_type=get_three_state_flag(), help="Restore from an existing/deleted account.", is_preview=True, arg_group='Restore')
c.argument('restore_source', help="The restorable-database-account Id of the source account from which the account has to be restored. Required if --is-restore-request is set to true.", is_preview=True, arg_group='Restore')
c.argument('restore_timestamp', arg_type=get_datetime_type(help="The timestamp to which the account has to be restored to. Required if --is-restore-request is set to true."), is_preview=True, arg_group='Restore')
c.argument('restore_timestamp', action=UtcDatetimeAction, help="The timestamp to which the account has to be restored to. Required if --is-restore-request is set to true.", is_preview=True, arg_group='Restore')
c.argument('databases_to_restore', nargs='+', action=CreateDatabaseRestoreResource, is_preview=True, arg_group='Restore')
c.argument('backup_policy_type', arg_type=get_enum_type(BackupPolicyTypes), help="The type of backup policy of the account to create", arg_group='Backup Policy')

Expand Down Expand Up @@ -66,15 +66,19 @@ def load_arguments(self, _):
with self.argument_context('cosmosdb restore') as c:
c.argument('target_database_account_name', options_list=['--target-database-account-name', '-n'], help='Name of the new target Cosmos DB database account after the restore')
c.argument('account_name', completer=None, options_list=['--account-name', '-a'], help='Name of the source Cosmos DB database account for the restore', id_part=None)
c.argument('restore_timestamp', options_list=['--restore-timestamp', '-t'], arg_type=get_datetime_type(help="The timestamp to which the account has to be restored to."))
c.argument('restore_timestamp', options_list=['--restore-timestamp', '-t'], action=UtcDatetimeAction, help="The timestamp to which the account has to be restored to.")
c.argument('location', arg_type=get_location_type(self.cli_ctx), help="The location of the source account from which restore is triggered. This will also be the write region of the restored account")
c.argument('databases_to_restore', nargs='+', action=CreateDatabaseRestoreResource)

# Restorable Database Accounts
with self.argument_context('cosmosdb restorable-database-account') as c:
with self.argument_context('cosmosdb restorable-database-account show') as c:
c.argument('location', options_list=['--location', '-l'], help="Location", required=False)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=False)

with self.argument_context('cosmosdb restorable-database-account list') as c:
c.argument('location', options_list=['--location', '-l'], help="Location", required=False)
c.argument('account_name', options_list=['--accout-name', '-n'], help="Name of the Account", required=False, id_part=None)

# Restorable Sql Databases
with self.argument_context('cosmosdb sql restorable-database') as c:
c.argument('location', options_list=['--location', '-l'], help="Location", required=True)
Expand Down
30 changes: 30 additions & 0 deletions src/cosmosdb-preview/azext_cosmosdb_preview/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,33 @@ def __call__(self, parser, namespace, values, option_string=None):
database_restore_resource.collection_names.append(item)
i += 1
namespace.databases_to_restore.append(database_restore_resource)


class UtcDatetimeAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
""" Parse a date value and return the ISO8601 string. """
import dateutil.parser
import dateutil.tz

accepted_formats = []
accepted_formats.append('date (yyyy-mm-dd)')
accepted_formats.append('time (hh:mm:ss.xxxxx)')
accepted_formats.append('timezone (+/-hh:mm)')
help_string = 'Format: ' + ' '.join(accepted_formats)
value_string = ''.join(values)
dt_val = None
print(values)
kavskalyan marked this conversation as resolved.
Show resolved Hide resolved
try:
# attempt to parse ISO 8601
dt_val = dateutil.parser.parse(value_string)
except ValueError:
pass

if not dt_val:
raise CLIError("Unable to parse: '{}'. Expected format: {}".format(value_string, help_string))

if not dt_val.tzinfo:
dt_val = dt_val.replace(tzinfo=dateutil.tz.tzutc())

iso_string = dt_val.isoformat()
setattr(namespace, self.dest, iso_string)
2 changes: 1 addition & 1 deletion src/cosmosdb-preview/azext_cosmosdb_preview/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def load_command_table(self, _):

with self.command_group('cosmosdb restorable-database-account', cosmosdb_restorable_database_accounts_sdk, client_factory=cf_restorable_database_accounts, is_preview=True) as g:
g.show_command('show', 'get_by_location')
g.command('list', 'list')
g.custom_command('list', 'cli_cosmosdb_restorable_database_account_list')

with self.command_group('cosmosdb', cosmosdb_sdk, client_factory=cf_db_accounts) as g:
g.show_command('show', 'get')
Expand Down
20 changes: 20 additions & 0 deletions src/cosmosdb-preview/azext_cosmosdb_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,23 @@ def cli_cosmosdb_list(client, resource_group_name=None):
return client.list_by_resource_group(resource_group_name)

return client.list()


def cli_cosmosdb_restorable_database_account_list(client,
location=None,
account_name=None):
restorable_database_accounts = None
if location is not None:
restorable_database_accounts = client.list_by_location(location)
else:
restorable_database_accounts = client.list()

if account_name is None:
return restorable_database_accounts

matching_restorable_accounts = []
restorable_database_accounts_list = list(restorable_database_accounts)
for account in restorable_database_accounts_list:
if account.account_name == account_name:
matching_restorable_accounts.append(account)
return matching_restorable_accounts