Skip to content

Commit

Permalink
[CosmosDB-preview] Adding capability to convert database account back…
Browse files Browse the repository at this point in the history
…up policy to continuous and migrating to track 2 sdk (#3514)
  • Loading branch information
kavskalyan authored Jun 21, 2021
1 parent 097827a commit bd16344
Show file tree
Hide file tree
Showing 95 changed files with 44,069 additions and 18,755 deletions.
3 changes: 3 additions & 0 deletions src/cosmosdb-preview/HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Release History
===============
0.8.0
++++++
* Adding the support for conversion of accounts from periodic to continuous backup policy and updating to python sdk 4.0.

0.7.0
++++++
Expand Down
9 changes: 9 additions & 0 deletions src/cosmosdb-preview/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ az cosmosdb create \
--backup-policy-type "Continuous"
```

#### Convert a periodic backup policy CosmosDB account to continuous backup Account ####

```sh
az cosmosdb update \
--resource-group "my-rg" \
--name "my-continuous-backup-account" \
--backup-policy-type "Continuous"
```

#### List all the CosmosDB accounts that can be restored (live and deleted) ####

This command returns all the continuous mode backup accounts that can be restored.
Expand Down
10 changes: 2 additions & 8 deletions src/cosmosdb-preview/azext_cosmosdb_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# --------------------------------------------------------------------------------------------
# pylint: disable=line-too-long, too-many-statements

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_location_type)
Expand All @@ -19,12 +18,7 @@
from azext_cosmosdb_preview.actions import (
CreateLocation, CreateDatabaseRestoreResource, UtcDatetimeAction)

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


class BackupPolicyTypes(str, Enum):
periodic = "Periodic"
continuous = "Continuous"
from azext_cosmosdb_preview.vendored_sdks.azure_mgmt_cosmosdb.models import DefaultConsistencyLevel, DatabaseAccountKind, ServerVersion, BackupPolicyType


def load_arguments(self, _):
Expand All @@ -43,7 +37,6 @@ def load_arguments(self, _):
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', 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')

for scope in ['cosmosdb create', 'cosmosdb update']:
with self.argument_context(scope) as c:
Expand All @@ -65,6 +58,7 @@ def load_arguments(self, _):
c.argument('enable_analytical_storage', arg_type=get_three_state_flag(), help="Flag to enable log storage on the account.", is_preview=True)
c.argument('backup_interval', type=int, help="the frequency(in minutes) with which backups are taken (only for accounts with periodic mode backups)", arg_group='Backup Policy')
c.argument('backup_retention', type=int, help="the time(in hours) for which each backup is retained (only for accounts with periodic mode backups)", arg_group='Backup Policy')
c.argument('backup_policy_type', arg_type=get_enum_type(BackupPolicyType), help="The type of backup policy of the account to create", arg_group='Backup Policy')

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')
Expand Down
4 changes: 2 additions & 2 deletions src/cosmosdb-preview/azext_cosmosdb_preview/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ def load_command_table(self, _):
g.custom_command('node-status', 'cli_cosmosdb_managed_cassandra_fetch_node_status', table_transformer=amc_node_status_table_format, supports_no_wait=True)
g.custom_command('list', 'cli_cosmosdb_managed_cassandra_cluster_list')
g.show_command('show', 'get')
g.command('delete', 'delete', confirmation=True, supports_no_wait=True)
g.command('delete', 'begin_delete', confirmation=True, supports_no_wait=True)

with self.command_group('managed-cassandra datacenter', cosmosdb_managed_cassandra_datacenter_sdk, client_factory=cf_cassandra_data_center, is_preview=True) as g:
g.custom_command('create', 'cli_cosmosdb_managed_cassandra_datacenter_create', supports_no_wait=True)
g.custom_command('update', 'cli_cosmosdb_managed_cassandra_datacenter_update', supports_no_wait=True)
g.command('list', 'list')
g.show_command('show', 'get')
g.command('delete', 'delete', confirmation=True, supports_no_wait=True)
g.command('delete', 'begin_delete', confirmation=True, supports_no_wait=True)
20 changes: 13 additions & 7 deletions src/cosmosdb-preview/azext_cosmosdb_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def _create_database_account(client,
tags=tags,
kind=kind)

async_docdb_create = client.create_or_update(resource_group_name, account_name, params)
async_docdb_create = client.begin_create_or_update(resource_group_name, account_name, params)
docdb_account = async_docdb_create.result()
docdb_account = client.get(resource_group_name, account_name) # Workaround
return docdb_account
Expand All @@ -259,7 +259,8 @@ def cli_cosmosdb_update(client,
enable_public_network=None,
enable_analytical_storage=None,
backup_interval=None,
backup_retention=None):
backup_retention=None,
backup_policy_type=None):
"""Update an existing Azure Cosmos DB database account. """
existing = client.get(resource_group_name, account_name)

Expand Down Expand Up @@ -291,6 +292,8 @@ def cli_cosmosdb_update(client,
backup_policy = None
if backup_interval is not None or backup_retention is not None:
if isinstance(existing.backup_policy, PeriodicModeBackupPolicy):
if backup_policy_type is not None and backup_policy_type.lower() == 'continuous':
raise CLIError('backup-interval and backup-retention can only be set with periodic backup policy.')
periodic_mode_properties = PeriodicModeProperties(
backup_interval_in_minutes=backup_interval,
backup_retention_interval_in_hours=backup_retention
Expand All @@ -300,6 +303,9 @@ def cli_cosmosdb_update(client,
else:
raise CLIError(
'backup-interval and backup-retention can only be set for accounts with periodic backup policy.')
elif backup_policy_type is not None and backup_policy_type.lower() == 'continuous':
if isinstance(existing.backup_policy, PeriodicModeBackupPolicy):
backup_policy = ContinuousModeBackupPolicy()

params = DatabaseAccountUpdateParameters(
locations=locations,
Expand All @@ -315,7 +321,7 @@ def cli_cosmosdb_update(client,
public_network_access=public_network_access,
enable_analytical_storage=enable_analytical_storage,
backup_policy=backup_policy)
async_docdb_update = client.update(resource_group_name, account_name, params)
async_docdb_update = client.begin_update(resource_group_name, account_name, params)
docdb_account = async_docdb_update.result()
docdb_account = client.get(resource_group_name, account_name) # Workaround
return docdb_account
Expand Down Expand Up @@ -449,7 +455,7 @@ def cli_cosmosdb_managed_cassandra_cluster_create(client,
identity=identity,
properties=cluster_properties)

return client.create_update(resource_group_name, cluster_name, cluster_resource_create_update_parameters)
return client.begin_create_update(resource_group_name, cluster_name, cluster_resource_create_update_parameters)


def cli_cosmosdb_managed_cassandra_cluster_update(client,
Expand Down Expand Up @@ -519,7 +525,7 @@ def cli_cosmosdb_managed_cassandra_cluster_update(client,
identity=identity,
properties=cluster_properties)

return client.create_update(resource_group_name, cluster_name, cluster_resource_create_update_parameters)
return client.begin_create_update(resource_group_name, cluster_name, cluster_resource_create_update_parameters)


def cli_cosmosdb_managed_cassandra_cluster_list(client,
Expand Down Expand Up @@ -560,7 +566,7 @@ def cli_cosmosdb_managed_cassandra_datacenter_create(client,
base64_encoded_cassandra_yaml_fragment=base64_encoded_cassandra_yaml_fragment
)

return client.create_update(resource_group_name, cluster_name, data_center_name, data_center_properties)
return client.begin_create_update(resource_group_name, cluster_name, data_center_name, data_center_properties)


def cli_cosmosdb_managed_cassandra_datacenter_update(client, resource_group_name,
Expand All @@ -586,7 +592,7 @@ def cli_cosmosdb_managed_cassandra_datacenter_update(client, resource_group_name
seed_nodes=data_center_resource.properties.seed_nodes,
base64_encoded_cassandra_yaml_fragment=base64_encoded_cassandra_yaml_fragment)

return client.create_update(resource_group_name, cluster_name, data_center_name, data_center_properties)
return client.begin_create_update(resource_group_name, cluster_name, data_center_name, data_center_properties)


def _gen_guid():
Expand Down
Loading

0 comments on commit bd16344

Please sign in to comment.