Skip to content

Commit

Permalink
[CosmosDB] Add Sql stored procedure, udf and trigger cmdlets (#11999)
Browse files Browse the repository at this point in the history
* Added cosmosDB sql stored procedure, udf and trigger cmdlets

* added help, fixed indentation, fixed wrong code

* fixed a typo

* indentation fix

* flake8 issues fixed

* PR comments

* changed help accordingly

* style fix

* change in help msg
MehaKaushik authored Feb 24, 2020
1 parent af4fca8 commit 1644eec
Showing 8 changed files with 7,168 additions and 50 deletions.
92 changes: 92 additions & 0 deletions src/azure-cli/azure/cli/command_modules/cosmosdb/_help.py
Original file line number Diff line number Diff line change
@@ -509,6 +509,98 @@
short-summary: Manage SQL resources of Azure Cosmos DB account.
"""

helps['cosmosdb sql stored-procedure'] = """
type: group
short-summary: Manage Azure Cosmos DB SQL stored procedures.
"""

helps['cosmosdb sql stored-procedure create'] = """
type: command
short-summary: Create an SQL stored procedure under an Azure Cosmos DB SQL container.
examples:
- name: Create an Azure Cosmos DB SQL stored procedure.
text: az cosmosdb sql stored-procedure create -g MyResourceGroup -a MyAccount -d MyDatabase -c MyContainer -n MyStoredProcedure -b StoredProcedureBody
crafted: true
"""

helps['cosmosdb sql stored-procedure delete'] = """
type: command
short-summary: Delete the SQL stored procedure under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql stored-procedure list'] = """
type: command
short-summary: List the SQL stored procedures under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql stored-procedure show'] = """
type: command
short-summary: Show the details of a SQL stored procedure under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql'] = """
type: group
short-summary: Manage SQL resources of Azure Cosmos DB account.
"""

helps['cosmosdb sql trigger'] = """
type: group
short-summary: Manage Azure Cosmos DB SQL triggers.
"""

helps['cosmosdb sql trigger create'] = """
type: command
short-summary: Create an SQL trigger under an Azure Cosmos DB SQL container.
examples:
- name: Create an Azure Cosmos DB SQL trigger.
text: az cosmosdb sql trigger create -g MyResourceGroup -a MyAccount -d MyDatabase -c MyContainer -n MyTrigger -b TriggerBody
crafted: true
"""

helps['cosmosdb sql trigger delete'] = """
type: command
short-summary: Delete the SQL trigger under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql trigger list'] = """
type: command
short-summary: List the SQL triggers under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql trigger show'] = """
type: command
short-summary: Show the details of a SQL trigger under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql user-defined-function'] = """
type: group
short-summary: Manage Azure Cosmos DB SQL user defined functions.
"""

helps['cosmosdb sql user-defined-function create'] = """
type: command
short-summary: Create an SQL user defined function under an Azure Cosmos DB SQL container.
examples:
- name: Create an Azure Cosmos DB SQL user defined function.
text: az cosmosdb sql user-defined-function create -g MyResourceGroup -a MyAccount -d MyDatabase -c MyContainer -n MyUserDefinedFunction -b UserDefinedFunctionBody
crafted: true
"""

helps['cosmosdb sql user-defined-function delete'] = """
type: command
short-summary: Delete the SQL user defined function under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql user-defined-function list'] = """
type: command
short-summary: List the SQL user defined functions under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql user-defined-function show'] = """
type: command
short-summary: Show the details of a SQL user defined function under an Azure Cosmos DB SQL container.
"""

helps['cosmosdb sql container'] = """
type: group
short-summary: Manage Azure Cosmos DB SQL containers.
82 changes: 56 additions & 26 deletions src/azure-cli/azure/cli/command_modules/cosmosdb/_params.py
Original file line number Diff line number Diff line change
@@ -37,8 +37,8 @@


def load_arguments(self, _):

from azure.mgmt.cosmosdb.models import KeyKind, DefaultConsistencyLevel, DatabaseAccountKind
from knack.arguments import CLIArgumentType
from azure.mgmt.cosmosdb.models import KeyKind, DefaultConsistencyLevel, DatabaseAccountKind, TriggerType, TriggerOperation

with self.argument_context('cosmosdb') as c:
c.argument('account_name', arg_type=name_type, help='Name of the Cosmos DB database account', completer=get_resource_name_completion_list('Microsoft.DocumentDb/databaseAccounts'), id_part='name')
@@ -97,16 +97,20 @@ def load_arguments(self, _):
with self.argument_context('cosmosdb database') as c:
c.argument('throughput', type=int, help='Offer Throughput (RU/s)')

account_name_type = CLIArgumentType(options_list=['--account-name', '-a'], help="Cosmosdb account name.")
database_name_type = CLIArgumentType(options_list=['--database-name', '-d'], help='Database name.')
container_name_type = CLIArgumentType(options_list=['--container-name', '-c'], help='Container name.')

# SQL database
with self.argument_context('cosmosdb sql database') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', options_list=['--name', '-n'], help="Database name")
c.argument('throughput', help='The throughput of SQL database (RU/s). Default value is 400')

# SQL container
with self.argument_context('cosmosdb sql container') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('database_name', options_list=['--database-name', '-d'], help="Database name")
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('container_name', options_list=['--name', '-n'], help="Container name")
c.argument('partition_key_path', options_list=['--partition-key-path', '-p'], help='Partition Key Path, e.g., \'/address/zipcode\'')
c.argument('default_ttl', options_list=['--ttl'], type=int, help='Default TTL. If the value is missing or set to "-1", items don’t expire. If the value is set to "n", items will expire "n" seconds after last modified time.')
@@ -115,28 +119,54 @@ def load_arguments(self, _):
c.argument('conflict_resolution_policy', options_list=['--conflict-resolution-policy', '-c'], type=shell_safe_json_parse, completer=FilesCompleter(), help='Conflict Resolution Policy, you can enter it as a string or as a file, e.g., --conflict-resolution-policy @policy-file.json or ' + SQL_GREMLIN_CONFLICT_RESOLUTION_POLICY_EXAMPLE)
c.argument('throughput', help='The throughput of SQL container (RU/s). Default value is 400')

# SQL stored procedure
with self.argument_context('cosmosdb sql stored-procedure') as c:
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('container_name', container_name_type)
c.argument('stored_procedure_name', options_list=['--name', '-n'], help="StoredProcedure name")
c.argument('stored_procedure_body', options_list=['--body', '-b'], completer=FilesCompleter(), help="StoredProcedure body, you can enter it as a string or as a file, e.g., --body @sprocbody-file.json")

# SQL trigger
with self.argument_context('cosmosdb sql trigger') as c:
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('container_name', container_name_type)
c.argument('trigger_name', options_list=['--name', '-n'], help="Trigger name")
c.argument('trigger_body', options_list=['--body', '-b'], completer=FilesCompleter(), help="Trigger body, you can enter it as a string or as a file, e.g., --body @triggerbody-file.json")
c.argument('trigger_type', options_list=['--type', '-t'], arg_type=get_enum_type(TriggerType), help="Trigger type")
c.argument('trigger_operation', options_list=['--operation'], arg_type=get_enum_type(TriggerOperation), help="The operation of the trigger.")

# SQL user defined function
with self.argument_context('cosmosdb sql user-defined-function') as c:
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('container_name', container_name_type)
c.argument('user_defined_function_name', options_list=['--name', '-n'], help="UserDefinedFunction name")
c.argument('user_defined_function_body', options_list=['--body', '-b'], completer=FilesCompleter(), help="UserDefinedFunction body, you can enter it as a string or as a file, e.g., --body @udfbody-file.json")

# MongoDB
with self.argument_context('cosmosdb mongodb database') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', options_list=['--name', '-n'], help="Database name")
c.argument('throughput', help='The throughput of MongoDB database (RU/s). Default value is 400')

with self.argument_context('cosmosdb mongodb collection') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('database_name', options_list=['--database-name', '-d'], help="Database name")
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('collection_name', options_list=['--name', '-n'], help="Collection name")
c.argument('shard_key_path', options_list=['--shard'], help="Sharding key path.")
c.argument('indexes', options_list=['--idx'], type=shell_safe_json_parse, completer=FilesCompleter(), help='Indexes, you can enter it as a string or as a file, e.g., --idx @indexes-file.json or ' + MONGODB_INDEXES_EXAMPLE)
c.argument('throughput', help='The throughput of MongoDB collection (RU/s). Default value is 400')

# Cassandra
with self.argument_context('cosmosdb cassandra keyspace') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('keyspace_name', options_list=['--name', '-n'], help="Keyspace name")
c.argument('throughput', help='The throughput of Cassandra keyspace (RU/s). Default value is 400')

with self.argument_context('cosmosdb cassandra table') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('keyspace_name', options_list=['--keyspace-name', '-k'], help="Keyspace name")
c.argument('table_name', options_list=['--name', '-n'], help="Table name")
c.argument('default_ttl', options_list=['--ttl'], type=int, help='Default TTL. If the value is missing or set to "-1", items don’t expire. If the value is set to "n", items will expire "n" seconds after last modified time.')
@@ -145,13 +175,13 @@ def load_arguments(self, _):

# Gremlin
with self.argument_context('cosmosdb gremlin database') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', options_list=['--name', '-n'], help="Database name")
c.argument('throughput', help='The throughput Gremlin database (RU/s). Default value is 400')

with self.argument_context('cosmosdb gremlin graph') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('database_name', options_list=['--database-name', '-d'], help="Database name")
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('graph_name', options_list=['--name', '-n'], help="Graph name")
c.argument('partition_key_path', options_list=['--partition-key-path', '-p'], help='Partition Key Path, e.g., \'/address/zipcode\'')
c.argument('default_ttl', options_list=['--ttl'], type=int, help='Default TTL. If the value is missing or set to "-1", items don’t expire. If the value is set to "n", items will expire "n" seconds after last modified time.')
@@ -161,56 +191,56 @@ def load_arguments(self, _):

# Table
with self.argument_context('cosmosdb table') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('table_name', options_list=['--name', '-n'], help="Table name")
c.argument('throughput', help='The throughput of Table (RU/s). Default value is 400')

# Throughput
with self.argument_context('cosmosdb sql database throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', options_list=['--name', '-n'], help="Database name")
c.argument('throughput', type=int, help='The throughput of SQL database (RU/s).')

with self.argument_context('cosmosdb sql container throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('database_name', options_list=['--database-name', '-d'], help="Database name")
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('container_name', options_list=['--name', '-n'], help="Container name")
c.argument('throughput', type=int, help='The throughput of SQL container (RU/s).')

with self.argument_context('cosmosdb mongodb database throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', options_list=['--name', '-n'], help="Database name")
c.argument('throughput', type=int, help='The throughput of MongoDB database (RU/s).')

with self.argument_context('cosmosdb mongodb collection throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('database_name', options_list=['--database-name', '-d'], help="Database name")
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('collection_name', options_list=['--name', '-n'], help="Collection name")
c.argument('throughput', type=int, help='The throughput of MongoDB collection (RU/s).')

with self.argument_context('cosmosdb cassandra keyspace throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('keyspace_name', options_list=['--name', '-n'], help="Keyspace name")
c.argument('throughput', type=int, help='The throughput of Cassandra keyspace (RU/s).')

with self.argument_context('cosmosdb cassandra table throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('keyspace_name', options_list=['--keyspace-name', '-k'], help="Keyspace name")
c.argument('table_name', options_list=['--name', '-n'], help="Table name")
c.argument('throughput', type=int, help='The throughput of Cassandra table (RU/s).')

with self.argument_context('cosmosdb gremlin database throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', options_list=['--name', '-n'], help="Database name")
c.argument('throughput', type=int, help='The throughput of Gremlin database (RU/s).')

with self.argument_context('cosmosdb gremlin graph throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('database_name', options_list=['--database-name', '-d'], help="Database name")
c.argument('account_name', account_name_type, id_part=None)
c.argument('database_name', database_name_type)
c.argument('graph_name', options_list=['--name', '-n'], help="Grapth name")
c.argument('throughput', type=int, help='The throughput Gremlin graph (RU/s).')

with self.argument_context('cosmosdb table throughput') as c:
c.argument('account_name', options_list=['--account-name', '-a'], help="Cosmosdb account name", id_part=None)
c.argument('account_name', account_name_type, id_part=None)
c.argument('table_name', options_list=['--name', '-n'], help="Table name")
c.argument('throughput', type=int, help='The throughput of Table (RU/s).')
45 changes: 33 additions & 12 deletions src/azure-cli/azure/cli/command_modules/cosmosdb/commands.py
Original file line number Diff line number Diff line change
@@ -56,7 +56,7 @@ def load_command_table(self, _):
g.command('list-connection-strings', 'list_connection_strings', table_transformer=list_connection_strings_output, deprecate_info=g.deprecate(redirect='cosmosdb keys list', hide=True))
g.command('regenerate-key', 'regenerate_key', deprecate_info=g.deprecate(redirect='cosmosdb keys regenerate', hide=True))
g.command('check-name-exists', 'check_name_exists')
g.command('delete', 'delete')
g.command('delete', 'delete', confirmation=True)
g.command('failover-priority-change', 'failover_priority_change')
g.custom_command('create', 'cli_cosmosdb_create')
g.custom_command('update', 'cli_cosmosdb_update')
@@ -69,14 +69,35 @@ def load_command_table(self, _):
g.custom_command('create', 'cli_cosmosdb_sql_database_create')
g.command('list', 'list_sql_databases')
g.command('show', 'get_sql_database')
g.command('delete', 'delete_sql_database')
g.command('delete', 'delete_sql_database', confirmation=True)

with self.command_group('cosmosdb sql container', cosmosdb_sql_sdk, client_factory=cf_sql_resources) as g:
g.custom_command('create', 'cli_cosmosdb_sql_container_create')
g.custom_command('update', 'cli_cosmosdb_sql_container_update')
g.command('list', 'list_sql_containers')
g.command('show', 'get_sql_container')
g.command('delete', 'delete_sql_container')
g.command('delete', 'delete_sql_container', confirmation=True)

with self.command_group('cosmosdb sql stored-procedure', cosmosdb_sql_sdk, client_factory=cf_sql_resources) as g:
g.custom_command('create', 'cli_cosmosdb_sql_stored_procedure_create_update')
g.custom_command('update', 'cli_cosmosdb_sql_stored_procedure_create_update')
g.command('list', 'list_sql_stored_procedures')
g.command('show', 'get_sql_stored_procedure')
g.command('delete', 'delete_sql_stored_procedure', confirmation=True)

with self.command_group('cosmosdb sql trigger', cosmosdb_sql_sdk, client_factory=cf_sql_resources) as g:
g.custom_command('create', 'cli_cosmosdb_sql_trigger_create')
g.custom_command('update', 'cli_cosmosdb_sql_trigger_update')
g.command('list', 'list_sql_triggers')
g.command('show', 'get_sql_trigger')
g.command('delete', 'delete_sql_trigger', confirmation=True)

with self.command_group('cosmosdb sql user-defined-function', cosmosdb_sql_sdk, client_factory=cf_sql_resources) as g:
g.custom_command('create', 'cli_cosmosdb_sql_user_defined_function_create_update')
g.custom_command('update', 'cli_cosmosdb_sql_user_defined_function_create_update')
g.command('list', 'list_sql_user_defined_functions')
g.command('show', 'get_sql_user_defined_function')
g.command('delete', 'delete_sql_user_defined_function', confirmation=True)

# MongoDB api
with self.command_group('cosmosdb mongodb', is_preview=True):
@@ -85,14 +106,14 @@ def load_command_table(self, _):
g.custom_command('create', 'cli_cosmosdb_mongodb_database_create')
g.command('list', 'list_mongo_db_databases')
g.command('show', 'get_mongo_db_database')
g.command('delete', 'delete_mongo_db_database')
g.command('delete', 'delete_mongo_db_database', confirmation=True)

with self.command_group('cosmosdb mongodb collection', cosmosdb_mongo_sdk, client_factory=cf_mongo_db_resources) as g:
g.custom_command('create', 'cli_cosmosdb_mongodb_collection_create')
g.custom_command('update', 'cli_cosmosdb_mongodb_collection_update')
g.command('list', 'list_mongo_db_collections')
g.command('show', 'get_mongo_db_collection')
g.command('delete', 'delete_mongo_db_collection')
g.command('delete', 'delete_mongo_db_collection', confirmation=True)

# Cassandra api
with self.command_group('cosmosdb cassandra', is_preview=True):
@@ -101,14 +122,14 @@ def load_command_table(self, _):
g.custom_command('create', 'cli_cosmosdb_cassandra_keyspace_create')
g.command('list', 'list_cassandra_keyspaces')
g.command('show', 'get_cassandra_keyspace')
g.command('delete', 'delete_cassandra_keyspace')
g.command('delete', 'delete_cassandra_keyspace', confirmation=True)

with self.command_group('cosmosdb cassandra table', cosmosdb_cassandra_sdk, client_factory=cf_cassandra_resources) as g:
g.custom_command('create', 'cli_cosmosdb_cassandra_table_create')
g.custom_command('update', 'cli_cosmosdb_cassandra_table_update')
g.command('list', 'list_cassandra_tables')
g.command('show', 'get_cassandra_table')
g.command('delete', 'delete_cassandra_table')
g.command('delete', 'delete_cassandra_table', confirmation=True)

# Gremlin api
with self.command_group('cosmosdb gremlin', is_preview=True):
@@ -117,21 +138,21 @@ def load_command_table(self, _):
g.custom_command('create', 'cli_cosmosdb_gremlin_database_create')
g.command('list', 'list_gremlin_databases')
g.command('show', 'get_gremlin_database')
g.command('delete', 'delete_gremlin_database')
g.command('delete', 'delete_gremlin_database', confirmation=True)

with self.command_group('cosmosdb gremlin graph', cosmosdb_gremlin_sdk, client_factory=cf_gremlin_resources) as g:
g.custom_command('create', 'cli_cosmosdb_gremlin_graph_create')
g.custom_command('update', 'cli_cosmosdb_gremlin_graph_update')
g.command('list', 'list_gremlin_graphs')
g.command('show', 'get_gremlin_graph')
g.command('delete', 'delete_gremlin_graph')
g.command('delete', 'delete_gremlin_graph', confirmation=True)

# Table api
with self.command_group('cosmosdb table', cosmosdb_table_sdk, client_factory=cf_table_resources, is_preview=True) as g:
g.custom_command('create', 'cli_cosmosdb_table_create')
g.command('list', 'list_tables')
g.command('show', 'get_table')
g.command('delete', 'delete_table')
g.command('delete', 'delete_table', confirmation=True)

# Offer throughput
with self.command_group('cosmosdb sql database throughput', cosmosdb_sql_sdk, client_factory=cf_sql_resources) as g:
@@ -187,13 +208,13 @@ def load_command_table(self, _):
g.cosmosdb_custom('list', 'cli_cosmosdb_database_list', table_transformer=list_database_output)
g.cosmosdb_custom('exists', 'cli_cosmosdb_database_exists')
g.cosmosdb_custom('create', 'cli_cosmosdb_database_create', table_transformer=database_output)
g.cosmosdb_custom('delete', 'cli_cosmosdb_database_delete')
g.cosmosdb_custom('delete', 'cli_cosmosdb_database_delete', confirmation=True)

# collection operations
with self.command_group('cosmosdb collection', deprecate_info=self.deprecate(redirect=COLLECTION_DEPRECATON_INFO, hide=True)) as g:
g.cosmosdb_custom('show', 'cli_cosmosdb_collection_show', table_transformer=collection_output)
g.cosmosdb_custom('list', 'cli_cosmosdb_collection_list', table_transformer=list_collection_output)
g.cosmosdb_custom('exists', 'cli_cosmosdb_collection_exists')
g.cosmosdb_custom('create', 'cli_cosmosdb_collection_create', table_transformer=collection_output)
g.cosmosdb_custom('delete', 'cli_cosmosdb_collection_delete')
g.cosmosdb_custom('delete', 'cli_cosmosdb_collection_delete', confirmation=True)
g.cosmosdb_custom('update', 'cli_cosmosdb_collection_update')
145 changes: 145 additions & 0 deletions src/azure-cli/azure/cli/command_modules/cosmosdb/custom.py
Original file line number Diff line number Diff line change
@@ -21,6 +21,12 @@
SqlContainerResource,
SqlContainerCreateUpdateParameters,
ContainerPartitionKey,
SqlStoredProcedureResource,
SqlStoredProcedureCreateUpdateParameters,
SqlTriggerResource,
SqlTriggerCreateUpdateParameters,
SqlUserDefinedFunctionResource,
SqlUserDefinedFunctionCreateUpdateParameters,
TableResource,
TableCreateUpdateParameters,
MongoDBDatabaseResource,
@@ -320,6 +326,145 @@ def cli_cosmosdb_sql_container_update(client,
sql_container_create_update_resource)


def cli_cosmosdb_sql_stored_procedure_create_update(client,
resource_group_name,
account_name,
database_name,
container_name,
stored_procedure_name,
stored_procedure_body):

"""Creates or Updates an Azure Cosmos DB SQL stored procedure """
sql_stored_procedure_resource = SqlStoredProcedureResource(id=stored_procedure_name)
sql_stored_procedure_resource.body = stored_procedure_body

sql_stored_procedure_create_update_resource = SqlStoredProcedureCreateUpdateParameters(
resource=sql_stored_procedure_resource,
options={})

return client.create_update_sql_stored_procedure(resource_group_name,
account_name,
database_name,
container_name,
stored_procedure_name,
sql_stored_procedure_create_update_resource)


def cli_cosmosdb_sql_user_defined_function_create_update(client,
resource_group_name,
account_name,
database_name,
container_name,
user_defined_function_name,
user_defined_function_body):

"""Creates or Updates an Azure Cosmos DB SQL user defined function """
sql_user_defined_function_resource = SqlUserDefinedFunctionResource(id=user_defined_function_name)
sql_user_defined_function_resource.body = user_defined_function_body

sql_user_defined_function_create_update_resource = SqlUserDefinedFunctionCreateUpdateParameters(
resource=sql_user_defined_function_resource,
options={})

return client.create_update_sql_user_defined_function(resource_group_name,
account_name,
database_name,
container_name,
user_defined_function_name,
sql_user_defined_function_create_update_resource)


def _populate_sql_trigger_definition(sql_trigger_resource,
trigger_body,
trigger_operation,
trigger_type):
if all(arg is None for arg in
[trigger_body, trigger_operation, trigger_type]):
return False

if trigger_body is not None:
sql_trigger_resource.body = trigger_body

if trigger_operation is not None:
sql_trigger_resource.trigger_operation = trigger_operation

if trigger_type is not None:
sql_trigger_resource.trigger_type = trigger_type

return True


def cli_cosmosdb_sql_trigger_create(client,
resource_group_name,
account_name,
database_name,
container_name,
trigger_name,
trigger_body,
trigger_type=None,
trigger_operation=None):

"""Creates an Azure Cosmos DB SQL trigger """
if trigger_operation is None:
trigger_operation = "All"

if trigger_type is None:
trigger_type = "Pre"

sql_trigger_resource = SqlTriggerResource(id=trigger_name)
sql_trigger_resource.body = trigger_body
sql_trigger_resource.trigger_type = trigger_type
sql_trigger_resource.trigger_operation = trigger_operation

sql_trigger_create_update_resource = SqlTriggerCreateUpdateParameters(
resource=sql_trigger_resource,
options={})

return client.create_update_sql_trigger(resource_group_name,
account_name,
database_name,
container_name,
trigger_name,
sql_trigger_create_update_resource)


def cli_cosmosdb_sql_trigger_update(client,
resource_group_name,
account_name,
database_name,
container_name,
trigger_name,
trigger_body=None,
trigger_type=None,
trigger_operation=None):

"""Updates an Azure Cosmos DB SQL trigger """
logger.debug('reading SQL trigger')
sql_trigger = client.get_sql_trigger(resource_group_name, account_name, database_name, container_name, trigger_name)

sql_trigger_resource = SqlTriggerResource(id=trigger_name)
sql_trigger_resource.body = sql_trigger.resource.body
sql_trigger_resource.trigger_operation = sql_trigger.resource.trigger_operation
sql_trigger_resource.trigger_type = sql_trigger.resource.trigger_type

if _populate_sql_trigger_definition(sql_trigger_resource,
trigger_body,
trigger_operation,
trigger_type):
logger.debug('replacing SQL trigger')

sql_trigger_create_update_resource = SqlTriggerCreateUpdateParameters(
resource=sql_trigger_resource,
options={})

return client.create_update_sql_trigger(resource_group_name,
account_name,
database_name,
container_name,
trigger_name,
sql_trigger_create_update_resource)


def cli_cosmosdb_gremlin_database_create(client,
resource_group_name,
account_name,

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -70,7 +70,7 @@ def test_delete_database_account(self, resource_group):
})

self.cmd('az cosmosdb create -n {acc} -g {rg}')
self.cmd('az cosmosdb delete -n {acc} -g {rg}')
self.cmd('az cosmosdb delete -n {acc} -g {rg} --yes')
self.cmd('az cosmosdb show -n {acc} -g {rg}', expect_failure=True)

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_account')
@@ -325,7 +325,7 @@ def test_cosmosdb_database(self, resource_group):
database_list = self.cmd('az cosmosdb database list -g {rg} -n {acc}').get_output_in_json()
assert len(database_list) == 1

self.cmd('az cosmosdb database delete -g {rg} -n {acc} -d {db_name}')
self.cmd('az cosmosdb database delete -g {rg} -n {acc} -d {db_name} --yes')
assert not self.cmd('az cosmosdb database exists -g {rg} -n {acc} -d {db_name}').get_output_in_json()

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_collection')
@@ -372,7 +372,7 @@ def test_cosmosdb_collection(self, resource_group):
disable_ttl = self.cmd('az cosmosdb collection update --default-ttl 0 -g {rg} -n {acc} -d {db_name} -c {col}').get_output_in_json()
assert "defaultTtl" not in disable_ttl["collection"]

self.cmd('az cosmosdb collection delete -g {rg} -n {acc} -d {db_name} -c {col}')
self.cmd('az cosmosdb collection delete -g {rg} -n {acc} -d {db_name} -c {col} --yes')
assert not self.cmd('az cosmosdb collection exists -g {rg} -n {acc} -d {db_name} -c {col}').get_output_in_json()

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_sql_database')
@@ -395,7 +395,7 @@ def test_cosmosdb_sql_database(self, resource_group):
database_list = self.cmd('az cosmosdb sql database list -g {rg} -a {acc}').get_output_in_json()
assert len(database_list) == 1

self.cmd('az cosmosdb sql database delete -g {rg} -a {acc} -n {db_name}')
self.cmd('az cosmosdb sql database delete -g {rg} -a {acc} -n {db_name} --yes')
database_list = self.cmd('az cosmosdb sql database list -g {rg} -a {acc}').get_output_in_json()
assert len(database_list) == 0

@@ -443,10 +443,136 @@ def test_cosmosdb_sql_container(self, resource_group):
container_list = self.cmd('az cosmosdb sql container list -g {rg} -a {acc} -d {db_name}').get_output_in_json()
assert len(container_list) == 1

self.cmd('az cosmosdb sql container delete -g {rg} -a {acc} -d {db_name} -n {ctn_name}')
self.cmd('az cosmosdb sql container delete -g {rg} -a {acc} -d {db_name} -n {ctn_name} --yes')
container_list = self.cmd('az cosmosdb sql container list -g {rg} -a {acc} -d {db_name}').get_output_in_json()
assert len(container_list) == 0

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_sql_stored_procedure')
def test_cosmosdb_sql_stored_procedure(self, resource_group):
db_name = self.create_random_name(prefix='cli', length=15)
ctn_name = self.create_random_name(prefix='cli', length=15)
partition_key = "/thePartitionKey"
sproc_name = self.create_random_name(prefix='cli', length=15)
body = "sampleBody"
nbody = "sampleBody2"

self.kwargs.update({
'acc': self.create_random_name(prefix='cli', length=15),
'db_name': db_name,
'ctn_name': ctn_name,
'part': partition_key,
'sproc_name': sproc_name,
'body': body,
'nbody': nbody
})

self.cmd('az cosmosdb create -n {acc} -g {rg}')
self.cmd('az cosmosdb sql database create -g {rg} -a {acc} -n {db_name}')
self.cmd('az cosmosdb sql container create -g {rg} -a {acc} -d {db_name} -n {ctn_name} -p {part} ').get_output_in_json()
sproc_create = self.cmd('az cosmosdb sql stored-procedure create --resource-group {rg} -a {acc} -d {db_name} -c {ctn_name} -n {sproc_name} -b {body}').get_output_in_json()

assert sproc_create["name"] == sproc_name
assert sproc_create["resource"]["body"] == body

sproc_update = self.cmd('az cosmosdb sql stored-procedure update -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {sproc_name} -b {nbody}').get_output_in_json()
assert sproc_update["resource"]["body"] == nbody

sproc_show = self.cmd('az cosmosdb sql stored-procedure show -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {sproc_name}').get_output_in_json()
assert sproc_show["name"] == sproc_name

sproc_list = self.cmd('az cosmosdb sql stored-procedure list -g {rg} -a {acc} -d {db_name} -c {ctn_name}').get_output_in_json()
assert len(sproc_list) == 1

self.cmd('az cosmosdb sql stored-procedure delete -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {sproc_name} --yes')
sproc_list = self.cmd('az cosmosdb sql stored-procedure list -g {rg} -a {acc} -d {db_name} -c {ctn_name}').get_output_in_json()
assert len(sproc_list) == 0

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_sql_user_defined_function')
def test_cosmosdb_sql_user_defined_function(self, resource_group):
db_name = self.create_random_name(prefix='cli', length=15)
ctn_name = self.create_random_name(prefix='cli', length=15)
partition_key = "/thePartitionKey"
udf_name = self.create_random_name(prefix='cli', length=15)
body = "sampleBody"
nbody = "sampleBody2"

self.kwargs.update({
'acc': self.create_random_name(prefix='cli', length=15),
'db_name': db_name,
'ctn_name': ctn_name,
'part': partition_key,
'udf_name': udf_name,
'body': body,
'nbody': nbody
})

self.cmd('az cosmosdb create -n {acc} -g {rg}')
self.cmd('az cosmosdb sql database create -g {rg} -a {acc} -n {db_name}')
self.cmd('az cosmosdb sql container create -g {rg} -a {acc} -d {db_name} -n {ctn_name} -p {part} ').get_output_in_json()
udf_create = self.cmd('az cosmosdb sql user-defined-function create --resource-group {rg} -a {acc} -d {db_name} -c {ctn_name} -n {udf_name} -b {body}').get_output_in_json()

assert udf_create["name"] == udf_name
assert udf_create["resource"]["body"] == body

udf_update = self.cmd('az cosmosdb sql user-defined-function update -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {udf_name} -b {nbody}').get_output_in_json()
assert udf_update["resource"]["body"] == nbody

udf_show = self.cmd('az cosmosdb sql user-defined-function show -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {udf_name}').get_output_in_json()
assert udf_show["name"] == udf_name

udf_list = self.cmd('az cosmosdb sql user-defined-function list -g {rg} -a {acc} -d {db_name} -c {ctn_name}').get_output_in_json()
assert len(udf_list) == 1

self.cmd('az cosmosdb sql user-defined-function delete -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {udf_name} --yes')
udf_list = self.cmd('az cosmosdb sql user-defined-function list -g {rg} -a {acc} -d {db_name} -c {ctn_name}').get_output_in_json()
assert len(udf_list) == 0

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_sql_trigger')
def test_cosmosdb_sql_trigger(self, resource_group):
db_name = self.create_random_name(prefix='cli', length=15)
ctn_name = self.create_random_name(prefix='cli', length=15)
partition_key = "/thePartitionKey"
trigger_name = self.create_random_name(prefix='cli', length=15)
body = "sampleBody"
trigger_type = "Pre"
trigger_operation = "Delete"
nbody = "sampleBody2"

self.kwargs.update({
'acc': self.create_random_name(prefix='cli', length=15),
'db_name': db_name,
'ctn_name': ctn_name,
'part': partition_key,
'trigger_name': trigger_name,
'body': body,
'type': trigger_type,
'op': trigger_operation,
'nbody': nbody
})

self.cmd('az cosmosdb create -n {acc} -g {rg}')
self.cmd('az cosmosdb sql database create -g {rg} -a {acc} -n {db_name}')
self.cmd('az cosmosdb sql container create -g {rg} -a {acc} -d {db_name} -n {ctn_name} -p {part} ').get_output_in_json()
trigger_create = self.cmd('az cosmosdb sql trigger create --resource-group {rg} -a {acc} -d {db_name} -c {ctn_name} -n {trigger_name} -b {body}').get_output_in_json()

assert trigger_create["name"] == trigger_name
assert trigger_create["resource"]["body"] == body

trigger_update = self.cmd('az cosmosdb sql trigger update -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {trigger_name} -b {nbody} --operation {op} -t {type}').get_output_in_json()
assert trigger_update["resource"]["body"] == nbody
assert trigger_update["resource"]["triggerOperation"] == trigger_operation
assert trigger_update["resource"]["triggerType"] == trigger_type

trigger_show = self.cmd('az cosmosdb sql trigger show -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {trigger_name}').get_output_in_json()
assert trigger_show["name"] == trigger_name

trigger_list = self.cmd('az cosmosdb sql trigger list -g {rg} -a {acc} -d {db_name} -c {ctn_name}').get_output_in_json()
assert len(trigger_list) == 1

self.cmd('az cosmosdb sql trigger delete -g {rg} -a {acc} -d {db_name} -c {ctn_name} -n {trigger_name} --yes')
trigger_list = self.cmd('az cosmosdb sql trigger list -g {rg} -a {acc} -d {db_name} -c {ctn_name}').get_output_in_json()
assert len(trigger_list) == 0

@ResourceGroupPreparer(name_prefix='cli_test_cosmosdb_mongodb_database')
def test_cosmosdb_mongodb_database(self, resource_group):
db_name = self.create_random_name(prefix='cli', length=15)
@@ -467,7 +593,7 @@ def test_cosmosdb_mongodb_database(self, resource_group):
database_list = self.cmd('az cosmosdb mongodb database list -g {rg} -a {acc}').get_output_in_json()
assert len(database_list) == 1

self.cmd('az cosmosdb mongodb database delete -g {rg} -a {acc} -n {db_name}')
self.cmd('az cosmosdb mongodb database delete -g {rg} -a {acc} -n {db_name} --yes')
database_list = self.cmd('az cosmosdb mongodb database list -g {rg} -a {acc}').get_output_in_json()
assert len(database_list) == 0

@@ -503,7 +629,7 @@ def test_cosmosdb_mongodb_collection(self, resource_group):
'az cosmosdb mongodb collection list -g {rg} -a {acc} -d {db_name}').get_output_in_json()
assert len(collection_list) == 1

self.cmd('az cosmosdb mongodb collection delete -g {rg} -a {acc} -d {db_name} -n {col_name}')
self.cmd('az cosmosdb mongodb collection delete -g {rg} -a {acc} -d {db_name} -n {col_name} --yes')
collection_list = self.cmd(
'az cosmosdb mongodb collection list -g {rg} -a {acc} -d {db_name}').get_output_in_json()
assert len(collection_list) == 0
@@ -528,7 +654,7 @@ def test_cosmosdb_cassandra_keyspace(self, resource_group):
keyspace_list = self.cmd('az cosmosdb cassandra keyspace list -g {rg} -a {acc}').get_output_in_json()
assert len(keyspace_list) == 1

self.cmd('az cosmosdb cassandra keyspace delete -g {rg} -a {acc} -n {ks_name}')
self.cmd('az cosmosdb cassandra keyspace delete -g {rg} -a {acc} -n {ks_name} --yes')
keyspace_list = self.cmd('az cosmosdb cassandra keyspace list -g {rg} -a {acc}').get_output_in_json()
assert len(keyspace_list) == 0

@@ -560,7 +686,7 @@ def test_cosmosdb_cassandra_table(self, resource_group):
table_list = self.cmd('az cosmosdb cassandra table list -g {rg} -a {acc} -k {ks_name}').get_output_in_json()
assert len(table_list) == 1

self.cmd('az cosmosdb cassandra table delete -g {rg} -a {acc} -k {ks_name} -n {table_name}')
self.cmd('az cosmosdb cassandra table delete -g {rg} -a {acc} -k {ks_name} -n {table_name} --yes')
table_list = self.cmd('az cosmosdb cassandra table list -g {rg} -a {acc} -k {ks_name}').get_output_in_json()
assert len(table_list) == 0

@@ -584,7 +710,7 @@ def test_cosmosdb_gremlin_database(self, resource_group):
database_list = self.cmd('az cosmosdb gremlin database list -g {rg} -a {acc}').get_output_in_json()
assert len(database_list) == 1

self.cmd('az cosmosdb gremlin database delete -g {rg} -a {acc} -n {db_name}')
self.cmd('az cosmosdb gremlin database delete -g {rg} -a {acc} -n {db_name} --yes')
database_list = self.cmd('az cosmosdb gremlin database list -g {rg} -a {acc}').get_output_in_json()
assert len(database_list) == 0

@@ -628,7 +754,7 @@ def test_cosmosdb_gremlin_graph(self, resource_group):
graph_list = self.cmd('az cosmosdb gremlin graph list -g {rg} -a {acc} -d {db_name}').get_output_in_json()
assert len(graph_list) == 1

self.cmd('az cosmosdb gremlin graph delete -g {rg} -a {acc} -d {db_name} -n {gp_name}')
self.cmd('az cosmosdb gremlin graph delete -g {rg} -a {acc} -d {db_name} -n {gp_name} --yes')
graph_list = self.cmd('az cosmosdb gremlin graph list -g {rg} -a {acc} -d {db_name}').get_output_in_json()
assert len(graph_list) == 0

@@ -652,7 +778,7 @@ def test_cosmosdb_table(self, resource_group):
table_list = self.cmd('az cosmosdb table list -g {rg} -a {acc}').get_output_in_json()
assert len(table_list) == 1

self.cmd('az cosmosdb table delete -g {rg} -a {acc} -n {table_name}')
self.cmd('az cosmosdb table delete -g {rg} -a {acc} -n {table_name} --yes')
table_list = self.cmd('az cosmosdb table list -g {rg} -a {acc}').get_output_in_json()
assert len(table_list) == 0

0 comments on commit 1644eec

Please sign in to comment.