Skip to content

Commit bc0f92d

Browse files
authored
Convert CosmosDB to Knack (Azure#5118)
1 parent 883e96d commit bc0f92d

File tree

18 files changed

+1354
-380
lines changed

18 files changed

+1354
-380
lines changed

.flake8

-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ exclude =
1414
scripts
1515
doc
1616
build_scripts
17-
src/command_modules/azure-cli-cosmosdb
1817
src/command_modules/azure-cli-monitor

azure-cli.pyproj

+14-16
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@
104104
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\webhook.py" />
105105
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_constants.py" />
106106
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_docker_utils.py" />
107-
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_factory.py" />
108107
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_format.py" />
109108
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_help.py" />
110109
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_params.py" />
@@ -266,6 +265,9 @@
266265
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_exception_handler.py" />
267266
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_help.py" />
268267
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_params.py" />
268+
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_validators.py">
269+
<SubType>Code</SubType>
270+
</Compile>
269271
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\__init__.py" />
270272
<Compile Include="command_modules\azure-cli-cosmosdb\setup.py" />
271273
<Compile Include="command_modules\azure-cli-consumption\azure\cli\command_modules\consumption\commands.py" />
@@ -397,7 +399,6 @@
397399
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\tests\test_sas_token_auth.py" />
398400
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\tests\_test_utils.py" />
399401
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_constants.py" />
400-
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_factory.py" />
401402
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_help.py" />
402403
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_params.py" />
403404
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_utils.py" />
@@ -438,13 +439,7 @@
438439
<Compile Include="command_modules\azure-cli-lab\azure\cli\command_modules\lab\__init__.py" />
439440
<Compile Include="command_modules\azure-cli-lab\azure_bdist_wheel.py" />
440441
<Compile Include="command_modules\azure-cli-lab\setup.py" />
441-
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\actions.py">
442-
<SubType>Code</SubType>
443-
</Compile>
444-
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\action_groups.py" />
445-
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\activity_log_alerts.py" />
446442
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\commands.py" />
447-
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\custom.py" />
448443
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\test_custom.py" />
449444
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\test_monitor.py" />
450445
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\test_monitor_action_groups.py" />
@@ -550,7 +545,6 @@
550545
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_client_factory.py" />
551546
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_help.py" />
552547
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_params.py" />
553-
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_validators.py" />
554548
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\__init__.py" />
555549
<Compile Include="command_modules\azure-cli-servicefabric\setup.py" />
556550
<Compile Include="command_modules\azure-cli-sql\azure\cli\command_modules\sql\commands.py" />
@@ -572,8 +566,16 @@
572566
<Compile Include="command_modules\azure-cli-rdbms\azure\cli\command_modules\rdbms\__init__.py" />
573567
<Compile Include="command_modules\azure-cli-rdbms\azure_bdist_wheel.py" />
574568
<Compile Include="command_modules\azure-cli-rdbms\setup.py" />
575-
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\blob.py" />
576-
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\file.py" />
569+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\account.py" />
570+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\acl.py" />
571+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\blob.py" />
572+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\cors.py" />
573+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\directory.py" />
574+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\file.py" />
575+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\logging.py" />
576+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\metrics.py" />
577+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\table.py" />
578+
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\__init__.py" />
577579
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\sdkutil.py" />
578580
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\services_wrapper.py" />
579581
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\storage_url_helpers.py" />
@@ -593,7 +595,6 @@
593595
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\tests\test_storage_validators.py" />
594596
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\url_quote_util.py" />
595597
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\util.py" />
596-
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_command_type.py" />
597598
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_client_factory.py" />
598599
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_format.py">
599600
<SubType>Code</SubType>
@@ -624,9 +625,6 @@
624625
<Compile Include="command_modules\azure-cli-role\azure\cli\command_modules\role\_params.py" />
625626
<Compile Include="command_modules\azure-cli-role\azure\cli\command_modules\role\__init__.py" />
626627
<Compile Include="command_modules\azure-cli-role\setup.py" />
627-
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\custom.py">
628-
<SubType>Code</SubType>
629-
</Compile>
630628
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\commands.py">
631629
<SubType>Code</SubType>
632630
</Compile>
@@ -934,6 +932,7 @@
934932
<Folder Include="command_modules\azure-cli-storage\azure\cli\" />
935933
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\" />
936934
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\" />
935+
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\" />
937936
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\tests\" />
938937
<Folder Include="command_modules\azure-cli-vm\" />
939938
<Folder Include="command_modules\azure-cli-vm\azure\" />
@@ -1057,7 +1056,6 @@
10571056
<Content Include="command_modules\azure-cli-lab\azure\cli\command_modules\lab\tests\docdbenv_paramters.json" />
10581057
<Content Include="command_modules\azure-cli-lab\azure\cli\command_modules\lab\tests\lab_template.json" />
10591058
<Content Include="command_modules\azure-cli-lab\HISTORY.rst" />
1060-
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\autoscale-parameters-template.json" />
10611059
<Content Include="command_modules\azure-cli-monitor\HISTORY.rst" />
10621060
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\zone_files\fail1.txt" />
10631061
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\zone_files\fail2.txt" />

scripts/ci/build.sh

+3-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ EOL
107107
for name in $(ls src/command_modules | grep azure-cli-); do
108108
if [ "$name" == "azure-cli-appservice" ]; then continue; fi
109109
if [ "$name" == "azure-cli-backup" ]; then continue; fi
110-
if [ "$name" == "azure-cli-batchai" ]; then continue; fi
111-
if [ "$name" == "azure-cli-cosmosdb" ]; then continue; fi
112-
if [ "$name" == "azure-cli-dla" ]; then continue; fi
113-
if [ "$name" == "azure-cli-dls" ]; then continue; fi
110+
if [ "$name" == "azure-cli-batchai" ]; then continue; fi
111+
if [ "$name" == "azure-cli-dla" ]; then continue; fi
112+
if [ "$name" == "azure-cli-dls" ]; then continue; fi
114113
if [ "$name" == "azure-cli-eventgrid" ]; then continue; fi
115114
if [ "$name" == "azure-cli-find" ]; then continue; fi
116115
if [ "$name" == "azure-cli-interactive" ]; then continue; fi

scripts/ci/test_static.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ run_style azure.cli.command_modules.cognitiveservices
4848
run_style azure.cli.command_modules.configure
4949
run_style azure.cli.command_modules.container
5050
run_style azure.cli.command_modules.consumption
51-
#run_style azure.cli.command_modules.cosmosdb
51+
run_style azure.cli.command_modules.cosmosdb
5252
run_style azure.cli.command_modules.dla
5353
run_style azure.cli.command_modules.dls
5454
run_style azure.cli.command_modules.eventgrid

src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/__init__.py

+37-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,44 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6-
import azure.cli.command_modules.cosmosdb._help # pylint: disable=unused-import
6+
from knack.events import EVENT_INVOKER_PRE_PARSE_ARGS
77

8+
from azure.cli.core import AzCommandsLoader
89

9-
def load_params(_):
10-
import azure.cli.command_modules.cosmosdb._params # pylint: disable=redefined-outer-name, unused-variable
10+
from azure.cli.command_modules.cosmosdb._help import helps # pylint: disable=unused-import
1111

1212

13-
def load_commands():
14-
import azure.cli.command_modules.cosmosdb.commands # pylint: disable=redefined-outer-name, unused-variable
13+
def _documentdb_deprecate(_, args):
14+
if args[0] == 'documentdb':
15+
from azure.cli.core.util import CLIError
16+
raise CLIError('All documentdb commands have been renamed to cosmosdb')
17+
18+
19+
class CosmosDbCommandsLoader(AzCommandsLoader):
20+
21+
def __init__(self, cli_ctx=None):
22+
from azure.cli.core.commands import CliCommandType
23+
from azure.cli.command_modules.cosmosdb._client_factory import cf_cosmosdb_document
24+
from azure.cli.command_modules.cosmosdb._command_type import CosmosDbCommandGroup
25+
cosmosdb_custom = CliCommandType(
26+
operations_tmpl='azure.cli.command_modules.cosmosdb.custom#{}',
27+
client_factory=cf_cosmosdb_document)
28+
29+
cli_ctx.register_event(EVENT_INVOKER_PRE_PARSE_ARGS, _documentdb_deprecate)
30+
31+
super(CosmosDbCommandsLoader, self).__init__(cli_ctx=cli_ctx,
32+
min_profile='2017-03-10-profile',
33+
custom_command_type=cosmosdb_custom,
34+
command_group_cls=CosmosDbCommandGroup)
35+
36+
def load_command_table(self, args):
37+
from azure.cli.command_modules.cosmosdb.commands import load_command_table
38+
load_command_table(self, args)
39+
return self.command_table
40+
41+
def load_arguments(self, command):
42+
from azure.cli.command_modules.cosmosdb._params import load_arguments
43+
load_arguments(self, command)
44+
45+
46+
COMMAND_LOADER_CLS = CosmosDbCommandsLoader

src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_client_factory.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
# --------------------------------------------------------------------------------------------
55

66
import os
7-
from azure.cli.core import __version__ as core_version
87

8+
from knack.log import get_logger
99
from knack.util import CLIError
1010

11+
from azure.cli.core import __version__ as core_version
12+
13+
14+
logger = get_logger(__name__)
15+
16+
1117
NO_CREDENTIALS_ERROR_MESSAGE = """
1218
No credentials specified to access Cosmos DB service. Please provide any of the following:
1319
(1) resource group name and account name
@@ -37,7 +43,7 @@ def _get_url_connection(url_collection, account_name):
3743
return None
3844

3945

40-
def get_document_client_factory(kwargs):
46+
def cf_cosmosdb_document(cli_ctx, kwargs):
4147
from pydocumentdb import document_client
4248
service_type = document_client.DocumentClient
4349

@@ -50,7 +56,7 @@ def get_document_client_factory(kwargs):
5056

5157
if name and resource_group and not key:
5258
# if resource group name is provided find key
53-
keys = cf_cosmosdb().database_accounts.list_keys(resource_group, name)
59+
keys = cf_cosmosdb(cli_ctx).database_accounts.list_keys(resource_group, name)
5460
key = keys.primary_master_key
5561

5662
url_connection = _get_url_connection(url_connection, name)
@@ -70,7 +76,11 @@ def get_document_client_factory(kwargs):
7076
return client
7177

7278

73-
def cf_cosmosdb(**_):
79+
def cf_cosmosdb(cli_ctx, **_):
7480
from azure.cli.core.commands.client_factory import get_mgmt_service_client
7581
from azure.mgmt.cosmosdb import CosmosDB
76-
return get_mgmt_service_client(CosmosDB)
82+
return get_mgmt_service_client(cli_ctx, CosmosDB)
83+
84+
85+
def cf_db_accounts(cli_ctx, _):
86+
return cf_cosmosdb(cli_ctx).database_accounts

src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_command_type.py

+51-38
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,54 @@
33
# Licensed under the MIT License. See License.txt in the project root for license information.
44
# --------------------------------------------------------------------------------------------
55

6-
from azure.cli.core.commands import create_command, command_table
7-
8-
9-
def cli_cosmosdb_data_plane_command(name,
10-
operation, client_factory, transform=None,
11-
table_transformer=None, exception_handler=None):
12-
"""Registers an Azure CLI Cosmos DB Data Plane command. These commands always include the
13-
parameters which can be used to obtain a cosmosdb client."""
14-
15-
if not exception_handler:
16-
from ._exception_handler import generic_exception_handler
17-
exception_handler = generic_exception_handler
18-
19-
command = create_command(__name__, name, operation, transform, table_transformer,
20-
client_factory, exception_handler=exception_handler)
21-
22-
# add parameters required to create a cosmosdb client
23-
group_name = 'Cosmos DB Account'
24-
25-
command.add_argument('db_resource_group_name', '--resource-group-name', '-g',
26-
arg_group=group_name,
27-
help='name of the resource group. Must be used in conjunction with '
28-
'cosmosdb account name.')
29-
command.add_argument('db_account_name', '--name', '-n', arg_group=group_name,
30-
help='Cosmos DB account name. Must be used in conjunction with '
31-
'either name of the resource group or cosmosdb account key.')
32-
33-
command.add_argument('db_account_key', '--key', required=False, default=None,
34-
arg_group=group_name,
35-
help='Cosmos DB account key. Must be used in conjunction with cosmosdb '
36-
'account name or url-connection.')
37-
38-
command.add_argument('db_url_connection', '--url-connection', required=False, default=None,
39-
arg_group=group_name,
40-
help='Cosmos DB account url connection. Must be used in conjunction with '
41-
'cosmosdb account key.')
42-
43-
command_table[command.name] = command
6+
from azure.cli.core.commands import AzCommandGroup
7+
8+
9+
class CosmosDbCommandGroup(AzCommandGroup):
10+
11+
def _create_cosmosdb_command(self, name, method_name=None, command_type_name=None, **kwargs):
12+
"""Registers an Azure CLI Cosmos DB Data Plane command. These commands always include the
13+
parameters which can be used to obtain a cosmosdb client."""
14+
15+
merged_kwargs = self._flatten_kwargs(kwargs, command_type_name)
16+
if 'exception_handler' not in merged_kwargs:
17+
from ._exception_handler import generic_exception_handler
18+
merged_kwargs['exception_handler'] = generic_exception_handler
19+
if command_type_name == 'command_type':
20+
command_name = self.command(name, method_name, **merged_kwargs)
21+
else:
22+
command_name = self.custom_command(name, method_name, **merged_kwargs)
23+
24+
command = self.command_loader.command_table[command_name]
25+
26+
# add parameters required to create a cosmosdb client
27+
group_name = 'Cosmos DB Account'
28+
command.add_argument('db_resource_group_name', '--resource-group-name', '-g',
29+
arg_group=group_name,
30+
help='name of the resource group. Must be used in conjunction with '
31+
'cosmosdb account name.')
32+
command.add_argument('db_account_name', '--name', '-n', arg_group=group_name,
33+
help='Cosmos DB account name. Must be used in conjunction with '
34+
'either name of the resource group or cosmosdb account key.')
35+
36+
command.add_argument('db_account_key', '--key', required=False, default=None,
37+
arg_group=group_name,
38+
help='Cosmos DB account key. Must be used in conjunction with cosmosdb '
39+
'account name or url-connection.')
40+
41+
command.add_argument('db_url_connection', '--url-connection', required=False, default=None,
42+
arg_group=group_name,
43+
help='Cosmos DB account url connection. Must be used in conjunction with '
44+
'cosmosdb account key.')
45+
46+
def cosmosdb_command(self, name, method_name=None, command_type=None, **kwargs):
47+
command_type_name = 'command_type'
48+
if command_type:
49+
kwargs[command_type_name] = command_type
50+
self._create_cosmosdb_command(name, method_name, command_type_name, **kwargs)
51+
52+
def cosmosdb_custom(self, name, method_name=None, command_type=None, **kwargs):
53+
command_type_name = 'custom_command_type'
54+
if command_type:
55+
kwargs[command_type_name] = command_type
56+
self._create_cosmosdb_command(name, method_name, command_type_name, **kwargs)

src/command_modules/azure-cli-cosmosdb/azure/cli/command_modules/cosmosdb/_exception_handler.py

+5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
# --------------------------------------------------------------------------------------------
55

66
import json
7+
8+
from knack.log import get_logger
79
from knack.util import CLIError
810

911

12+
logger = get_logger(__name__)
13+
14+
1015
def duplicate_resource_exception_handler(ex):
1116
# wraps DocumentDB 409 error in CLIError
1217
from pydocumentdb.errors import HTTPFailure

0 commit comments

Comments
 (0)