Skip to content

Commit

Permalink
[CosmosDB] Add table and gremlin APIs for point-in-time restore (Azur…
Browse files Browse the repository at this point in the history
…e#24530)

* Added cli changes for table/gremlin APIs

* Updated params and commands

* updated version

* fixed linter and style errors

* session records for cosmosdb

* removed preview tag from some restore commands

* Added session records from security and network group related to cosmosdb

* fixed linter and style issues
  • Loading branch information
kshittiz7 authored Mar 2, 2023
1 parent ddbce4a commit 7d708dd
Show file tree
Hide file tree
Showing 82 changed files with 28,187 additions and 11,968 deletions.
8 changes: 8 additions & 0 deletions linter_exclusions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,14 @@ cosmosdb create:
virtual_network_rules:
rule_exclusions:
- option_length_too_long
gremlin_databases_to_restore:
rule_exclusions:
- option_length_too_long
cosmosdb restore:
parameters:
gremlin_databases_to_restore:
rule_exclusions:
- option_length_too_long
cosmosdb mongodb collection create:
parameters:
analytical_storage_ttl:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,25 @@ def cf_cassandra_data_center(cli_ctx, _):

def cf_service(cli_ctx, _):
return cf_cosmosdb(cli_ctx).service


# restorable gremlin databases/graphs
def cf_restorable_gremlin_databases(cli_ctx, _):
return cf_cosmosdb(cli_ctx).restorable_gremlin_databases


def cf_restorable_gremlin_graphs(cli_ctx, _):
return cf_cosmosdb(cli_ctx).restorable_gremlin_graphs


def cf_restorable_gremlin_resources(cli_ctx, _):
return cf_cosmosdb(cli_ctx).restorable_gremlin_resources


# restorable tables
def cf_restorable_tables(cli_ctx, _):
return cf_cosmosdb(cli_ctx).restorable_tables


def cf_restorable_table_resources(cli_ctx, _):
return cf_cosmosdb(cli_ctx).restorable_table_resources
86 changes: 85 additions & 1 deletion src/azure-cli/azure/cli/command_modules/cosmosdb/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@
short-summary: Add a database and its collection names to restore
long-summary: |
Usage: --databases-to-restore name=DatabaseName collections=collection1 [collection2 ...]
- name: --gremlin-databases-to-restore
short-summary: Add a gremlin database and its graph names to restore
long-summary: |
Usage: --gremlin-databases-to-restore name=DatabaseName graphs=graph1 [graph2 ...]
- name: --tables-to-restore
short-summary: Add table names to restore
long-summary: |
Usage: --tables-to-restore tables=table1 [table2 ...]
examples:
- name: Creates a new Azure Cosmos DB database account. (autogenerated)
text: az cosmosdb create --name MyCosmosDBDatabaseAccount --resource-group MyResourceGroup --subscription MySubscription
Expand All @@ -191,11 +199,23 @@
long-summary: |
Usage: --databases-to-restore name=DatabaseName collections=collection1 [collection2 ...]
Multiple databases can be specified by using more than one `--databases-to-restore` argument.
- name: --gremlin-databases-to-restore
short-summary: Add a gremlin database and its graph names to restore
long-summary: |
Usage: --gremlin-databases-to-restore name=DatabaseName graphs=graph1 [graph2 ...]
- name: --tables-to-restore
short-summary: Add table names to restore
long-summary: |
Usage: --tables-to-restore table1 [table2 ...]
examples:
- name: Create a new Azure Cosmos DB database account by restoring from an existing database account.
text: az cosmosdb restore --target-database-account-name MyRestoredCosmosDBDatabaseAccount --account-name MySourceAccount --restore-timestamp 2020-07-13T16:03:41+0000 -g MyResourceGroup --location westus
- name: Create a new Azure Cosmos DB database account by restoring only the selected databases and collections from an existing database account.
- name: Create a new Azure Cosmos DB Sql or MongoDB database account by restoring only the selected databases and collections from an existing database account.
text: az cosmosdb restore -g MyResourceGroup --target-database-account-name MyRestoredCosmosDBDatabaseAccount --account-name MySourceAccount --restore-timestamp 2020-07-13T16:03:41+0000 --location westus --databases-to-restore name=MyDB1 collections=collection1 collection2 --databases-to-restore name=MyDB2 collections=collection3 collection4
- name: Create a new Azure Cosmos DB Gremlin database account by restoring only the selected databases or graphs from an existing database account.
text: az cosmosdb restore -g MyResourceGroup --target-database-account-name MyRestoredCosmosDBDatabaseAccount --account-name MySourceAccount --restore-timestamp 2020-07-13T16:03:41+0000 --location westus --gremlin-databases-to-restore name=graphdb1 graphs=graph1 graph2
- name: Create a new Azure Cosmos DB Table database account by restoring only the selected tables from an existing database account.
text: az cosmosdb restore -g MyResourceGroup --target-database-account-name MyRestoredCosmosDBDatabaseAccount --account-name MySourceAccount --restore-timestamp 2020-07-13T16:03:41+0000 --location westus --tables-to-restore table1,table2
"""

helps['cosmosdb database'] = """
Expand Down Expand Up @@ -1309,6 +1329,70 @@
short-summary: Retrieves latest restorable timestamp for the given sql container in given region.
"""

# gremlin restorable resources
helps['cosmosdb gremlin restorable-database'] = """
type: group
short-summary: Manage different versions of gremlin databases that are restorable in a Azure Cosmos DB account.
"""

helps['cosmosdb gremlin restorable-database list'] = """
type: command
short-summary: List all the versions of all the gremlin databases that were created / modified / deleted in the given restorable account.
"""

helps['cosmosdb gremlin restorable-graph'] = """
type: group
short-summary: Manage different versions of gremlin graphs that are restorable in a database of a Azure Cosmos DB account.
"""

helps['cosmosdb gremlin restorable-graph list'] = """
type: command
short-summary: List all the versions of all the gremlin graphs that were created / modified / deleted in the given database and restorable account.
"""

helps['cosmosdb gremlin restorable-resource'] = """
type: group
short-summary: Manage the databases and its graphs that can be restored in the given account at the given timestamp and region.
"""

helps['cosmosdb gremlin restorable-resource list'] = """
type: command
short-summary: List all the databases and its graphs that can be restored in the given account at the given timestamp and region.
"""

# table restorable resources
helps['cosmosdb table restorable-table'] = """
type: group
short-summary: Manage different versions of tables that are restorable in Azure Cosmos DB account.
"""

helps['cosmosdb table restorable-table list'] = """
type: command
short-summary: List all the versions of all the tables that were created / modified / deleted in the given restorable account.
"""

helps['cosmosdb table restorable-resource'] = """
type: group
short-summary: Manage the tables that can be restored in the given account at the given timestamp and region.
"""

helps['cosmosdb table restorable-resource list'] = """
type: command
short-summary: List all the tables that can be restored in the given account at the given timestamp and region.
"""

# gremlin graph latest backup time.
helps['cosmosdb gremlin retrieve-latest-backup-time'] = """
type: command
short-summary: Retrieves latest restorable timestamp for the given gremlin graph in given region.
"""

# table latest backup time.
helps['cosmosdb table retrieve-latest-backup-time'] = """
type: command
short-summary: Retrieves latest restorable timestamp for the given table in given region.
"""

helps['cosmosdb mongodb retrieve-latest-backup-time'] = """
type: command
short-summary: Retrieves latest restorable timestamp for the given mongodb collection in given region.
Expand Down
69 changes: 64 additions & 5 deletions src/azure-cli/azure/cli/command_modules/cosmosdb/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
validate_mongo_user_definition_id)

from azure.cli.command_modules.cosmosdb.actions import (
CreateLocation, CreateDatabaseRestoreResource, UtcDatetimeAction, InvokeCommandArgumentsAddAction)
CreateLocation, CreateDatabaseRestoreResource, CreateGremlinDatabaseRestoreResource, CreateTableRestoreResource, UtcDatetimeAction, InvokeCommandArgumentsAddAction)
from azure.cli.command_modules.cosmosdb.custom import (
CosmosKeyTypes)

Expand Down Expand Up @@ -81,10 +81,12 @@ def load_arguments(self, _):
c.argument('account_name', completer=None)
c.argument('enable_free_tier', arg_type=get_three_state_flag(), help="If enabled the account is free-tier.", is_preview=True)
c.argument('assign_identity', nargs='*', help="Assign system or user assigned identities separated by spaces. Use '[system]' to refer system assigned identity.", 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', 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('is_restore_request', options_list=['--is-restore-request', '-r'], arg_type=get_three_state_flag(), help="Restore from an existing/deleted account.", 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.", 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.", arg_group='Restore')
c.argument('databases_to_restore', nargs='+', action=CreateDatabaseRestoreResource, arg_group='Restore')
c.argument('gremlin_databases_to_restore', nargs='+', action=CreateGremlinDatabaseRestoreResource, arg_group='Restore')
c.argument('tables_to_restore', nargs='+', action=CreateTableRestoreResource, arg_group='Restore')

for scope in ['cosmosdb create', 'cosmosdb update']:
with self.argument_context(scope) as c:
Expand Down Expand Up @@ -380,6 +382,8 @@ def load_arguments(self, _):
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)
c.argument('gremlin_databases_to_restore', nargs='+', action=CreateGremlinDatabaseRestoreResource)
c.argument('tables_to_restore', nargs='+', action=CreateTableRestoreResource)

# Mongo role definition
with self.argument_context('cosmosdb mongodb role definition') as c:
Expand Down Expand Up @@ -426,13 +430,17 @@ def load_arguments(self, _):
c.argument('location', options_list=['--location', '-l'], help="Location", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('restorable_sql_database_rid', options_list=['--database-rid', '-d'], help="Rid of the database", required=True)
c.argument('start_time', options_list=['--start-time', '-s'], help="Start time of restorable Sql container event feed", required=False)
c.argument('end_time', options_list=['--end-time', '-e'], help="End time of restorable Sql container event feed", required=False)

# Restorable Sql Resources
with self.argument_context('cosmosdb sql restorable-resource') as c:
c.argument('location', options_list=['--location', '-l'], help="Azure Location of the account", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('restore_location', options_list=['--restore-location', '-r'], help="The region of the restore.", required=True)
c.argument('restore_timestamp_in_utc', options_list=['--restore-timestamp', '-t'], help="The timestamp of the restore", required=True)
c.argument('start_time', options_list=['--start-time', '-s'], help="Start time of restorable MongoDB collections event feed", required=False)
c.argument('end_time', options_list=['--end-time', '-e'], help="End time of restorable MongoDB collections event feed", required=False)

# Restorable Mongodb Databases
with self.argument_context('cosmosdb mongodb restorable-database') as c:
Expand All @@ -444,6 +452,8 @@ def load_arguments(self, _):
c.argument('location', options_list=['--location', '-l'], help="Location", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('restorable_mongodb_database_rid', options_list=['--database-rid', '-d'], help="Rid of the database", required=True)
c.argument('start_time', options_list=['--start-time', '-s'], help="Start time of restorable MongoDB collections event feed", required=False)
c.argument('end_time', options_list=['--end-time', '-e'], help="End time of restorable MongoDB collections event feed", required=False)

# Restorable mongodb Resources
with self.argument_context('cosmosdb mongodb restorable-resource') as c:
Expand All @@ -452,6 +462,54 @@ def load_arguments(self, _):
c.argument('restore_location', options_list=['--restore-location', '-r'], help="The region of the restore.", required=True)
c.argument('restore_timestamp_in_utc', options_list=['--restore-timestamp', '-t'], help="The timestamp of the restore", required=True)

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

# Restorable Gremlin Graphs
with self.argument_context('cosmosdb gremlin restorable-graph') as c:
c.argument('location', options_list=['--location', '-l'], help="Location", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('restorable_gremlin_database_rid', options_list=['--database-rid', '-d'], help="Rid of the gremlin database", required=True)
c.argument('start_time', options_list=['--start-time', '-s'], help="Start time of restorable Gremlin graph event feed", required=False)
c.argument('end_time', options_list=['--end-time', '-e'], help="End time of restorable Gremlin graph event feed", required=False)

# Restorable Gremlin Resources
with self.argument_context('cosmosdb gremlin restorable-resource') as c:
c.argument('location', options_list=['--location', '-l'], help="Azure Location of the account", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('restore_location', options_list=['--restore-location', '-r'], help="The region of the restore.", required=True)
c.argument('restore_timestamp_in_utc', options_list=['--restore-timestamp', '-t'], help="The timestamp of the restore", required=True)

# Restorable Tables
with self.argument_context('cosmosdb table restorable-table') as c:
c.argument('location', options_list=['--location', '-l'], help="Location", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('start_time', options_list=['--start-time', '-s'], help="Start time of restorable tables event feed", required=False)
c.argument('end_time', options_list=['--end-time', '-e'], help="End time of restorable tables event feed", required=False)

# Restorable table Resources
with self.argument_context('cosmosdb table restorable-resource') as c:
c.argument('location', options_list=['--location', '-l'], help="Azure Location of the account", required=True)
c.argument('instance_id', options_list=['--instance-id', '-i'], help="InstanceId of the Account", required=True)
c.argument('restore_location', options_list=['--restore-location', '-r'], help="The region of the restore.", required=True)
c.argument('restore_timestamp_in_utc', options_list=['--restore-timestamp', '-t'], help="The timestamp of the restore", required=True)

# Retrive Gremlin Graph Backup Info
database_name_type = CLIArgumentType(options_list=['--database-name', '-d'], help='Database name.')
with self.argument_context('cosmosdb gremlin retrieve-latest-backup-time') as c:
c.argument('account_name', account_name_type, id_part=None, required=True, help='Name of the CosmosDB database account')
c.argument('database_name', database_name_type, required=True, help='Name of the CosmosDB Gremlin database name')
c.argument('graph_name', options_list=['--graph-name', '-n'], required=True, help='Name of the CosmosDB Gremlin graph name')
c.argument('location', options_list=['--location', '-l'], help="Location of the account", required=True)

# Retrive Table Backup Info
with self.argument_context('cosmosdb table retrieve-latest-backup-time') as c:
c.argument('account_name', account_name_type, id_part=None, required=True, help='Name of the CosmosDB database account')
c.argument('table_name', options_list=['--table-name', '-n'], required=True, help='Name of the CosmosDB Table name')
c.argument('location', options_list=['--location', '-l'], help="Location of the account", required=True)

# Account locations
with self.argument_context('cosmosdb locations show') as c:
c.argument('location', options_list=['--location', '-l'], help="Name of the location", required=True)
Expand Down Expand Up @@ -542,6 +600,7 @@ def load_arguments(self, _):
with self.argument_context('cosmosdb service') as c:
c.argument('account_name', completer=None, options_list=['--account-name', '-a'], help='Name of the Cosmos DB database account.', id_part=None)
c.argument('resource_group_name', completer=None, options_list=['--resource-group-name', '-g'], help='Name of the resource group of the database account.', id_part=None)
c.argument('service_kind', options_list=['--kind', '-k'], help="Service kind")
c.argument('service_name', options_list=['--name', '-n'], help="Service Name.")
c.argument('instance_count', options_list=['--count', '-c'], help="Instance Count.")
c.argument('instance_size', options_list=['--size'], help="Instance Size. Possible values are: Cosmos.D4s, Cosmos.D8s, Cosmos.D16s etc")
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
# pylint: disable=line-too-long, too-many-statements, no-else-raise, unused-argument, too-many-branches

import ipaddress
from azure.cli.core.azclierror import InvalidArgumentValueError
Expand Down
Loading

0 comments on commit 7d708dd

Please sign in to comment.