Skip to content

Commit

Permalink
Convert CosmosDB to Knack (Azure#5118)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott authored Dec 18, 2017
1 parent 883e96d commit bc0f92d
Show file tree
Hide file tree
Showing 18 changed files with 1,354 additions and 380 deletions.
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ exclude =
scripts
doc
build_scripts
src/command_modules/azure-cli-cosmosdb
src/command_modules/azure-cli-monitor
30 changes: 14 additions & 16 deletions azure-cli.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\webhook.py" />
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_constants.py" />
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_docker_utils.py" />
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_factory.py" />
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_format.py" />
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_help.py" />
<Compile Include="command_modules\azure-cli-acr\azure\cli\command_modules\acr\_params.py" />
Expand Down Expand Up @@ -266,6 +265,9 @@
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_exception_handler.py" />
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_help.py" />
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_params.py" />
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\_validators.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-cosmosdb\azure\cli\command_modules\cosmosdb\__init__.py" />
<Compile Include="command_modules\azure-cli-cosmosdb\setup.py" />
<Compile Include="command_modules\azure-cli-consumption\azure\cli\command_modules\consumption\commands.py" />
Expand Down Expand Up @@ -397,7 +399,6 @@
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\tests\test_sas_token_auth.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\tests\_test_utils.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_constants.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_factory.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_help.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_params.py" />
<Compile Include="command_modules\azure-cli-iot\azure\cli\command_modules\iot\_utils.py" />
Expand Down Expand Up @@ -438,13 +439,7 @@
<Compile Include="command_modules\azure-cli-lab\azure\cli\command_modules\lab\__init__.py" />
<Compile Include="command_modules\azure-cli-lab\azure_bdist_wheel.py" />
<Compile Include="command_modules\azure-cli-lab\setup.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\actions.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\action_groups.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\activity_log_alerts.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\commands.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\custom.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\test_custom.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\test_monitor.py" />
<Compile Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\tests\test_monitor_action_groups.py" />
Expand Down Expand Up @@ -550,7 +545,6 @@
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_client_factory.py" />
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_help.py" />
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_params.py" />
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\_validators.py" />
<Compile Include="command_modules\azure-cli-servicefabric\azure\cli\command_modules\servicefabric\__init__.py" />
<Compile Include="command_modules\azure-cli-servicefabric\setup.py" />
<Compile Include="command_modules\azure-cli-sql\azure\cli\command_modules\sql\commands.py" />
Expand All @@ -572,8 +566,16 @@
<Compile Include="command_modules\azure-cli-rdbms\azure\cli\command_modules\rdbms\__init__.py" />
<Compile Include="command_modules\azure-cli-rdbms\azure_bdist_wheel.py" />
<Compile Include="command_modules\azure-cli-rdbms\setup.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\blob.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\file.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\account.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\acl.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\blob.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\cors.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\directory.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\file.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\logging.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\metrics.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\table.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\__init__.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\sdkutil.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\services_wrapper.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\storage_url_helpers.py" />
Expand All @@ -593,7 +595,6 @@
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\tests\test_storage_validators.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\url_quote_util.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\util.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_command_type.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_client_factory.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\_format.py">
<SubType>Code</SubType>
Expand Down Expand Up @@ -624,9 +625,6 @@
<Compile Include="command_modules\azure-cli-role\azure\cli\command_modules\role\_params.py" />
<Compile Include="command_modules\azure-cli-role\azure\cli\command_modules\role\__init__.py" />
<Compile Include="command_modules\azure-cli-role\setup.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\custom.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\commands.py">
<SubType>Code</SubType>
</Compile>
Expand Down Expand Up @@ -934,6 +932,7 @@
<Folder Include="command_modules\azure-cli-storage\azure\cli\" />
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\" />
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\" />
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\operations\" />
<Folder Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\tests\" />
<Folder Include="command_modules\azure-cli-vm\" />
<Folder Include="command_modules\azure-cli-vm\azure\" />
Expand Down Expand Up @@ -1057,7 +1056,6 @@
<Content Include="command_modules\azure-cli-lab\azure\cli\command_modules\lab\tests\docdbenv_paramters.json" />
<Content Include="command_modules\azure-cli-lab\azure\cli\command_modules\lab\tests\lab_template.json" />
<Content Include="command_modules\azure-cli-lab\HISTORY.rst" />
<Content Include="command_modules\azure-cli-monitor\azure\cli\command_modules\monitor\autoscale-parameters-template.json" />
<Content Include="command_modules\azure-cli-monitor\HISTORY.rst" />
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\zone_files\fail1.txt" />
<Content Include="command_modules\azure-cli-network\azure\cli\command_modules\network\tests\zone_files\fail2.txt" />
Expand Down
7 changes: 3 additions & 4 deletions scripts/ci/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,9 @@ EOL
for name in $(ls src/command_modules | grep azure-cli-); do
if [ "$name" == "azure-cli-appservice" ]; then continue; fi
if [ "$name" == "azure-cli-backup" ]; then continue; fi
if [ "$name" == "azure-cli-batchai" ]; then continue; fi
if [ "$name" == "azure-cli-cosmosdb" ]; then continue; fi
if [ "$name" == "azure-cli-dla" ]; then continue; fi
if [ "$name" == "azure-cli-dls" ]; then continue; fi
if [ "$name" == "azure-cli-batchai" ]; then continue; fi
if [ "$name" == "azure-cli-dla" ]; then continue; fi
if [ "$name" == "azure-cli-dls" ]; then continue; fi
if [ "$name" == "azure-cli-eventgrid" ]; then continue; fi
if [ "$name" == "azure-cli-find" ]; then continue; fi
if [ "$name" == "azure-cli-interactive" ]; then continue; fi
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/test_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ run_style azure.cli.command_modules.cognitiveservices
run_style azure.cli.command_modules.configure
run_style azure.cli.command_modules.container
run_style azure.cli.command_modules.consumption
#run_style azure.cli.command_modules.cosmosdb
run_style azure.cli.command_modules.cosmosdb
run_style azure.cli.command_modules.dla
run_style azure.cli.command_modules.dls
run_style azure.cli.command_modules.eventgrid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,44 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

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

from azure.cli.core import AzCommandsLoader

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


def load_commands():
import azure.cli.command_modules.cosmosdb.commands # pylint: disable=redefined-outer-name, unused-variable
def _documentdb_deprecate(_, args):
if args[0] == 'documentdb':
from azure.cli.core.util import CLIError
raise CLIError('All documentdb commands have been renamed to cosmosdb')


class CosmosDbCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
from azure.cli.command_modules.cosmosdb._client_factory import cf_cosmosdb_document
from azure.cli.command_modules.cosmosdb._command_type import CosmosDbCommandGroup
cosmosdb_custom = CliCommandType(
operations_tmpl='azure.cli.command_modules.cosmosdb.custom#{}',
client_factory=cf_cosmosdb_document)

cli_ctx.register_event(EVENT_INVOKER_PRE_PARSE_ARGS, _documentdb_deprecate)

super(CosmosDbCommandsLoader, self).__init__(cli_ctx=cli_ctx,
min_profile='2017-03-10-profile',
custom_command_type=cosmosdb_custom,
command_group_cls=CosmosDbCommandGroup)

def load_command_table(self, args):
from azure.cli.command_modules.cosmosdb.commands import load_command_table
load_command_table(self, args)
return self.command_table

def load_arguments(self, command):
from azure.cli.command_modules.cosmosdb._params import load_arguments
load_arguments(self, command)


COMMAND_LOADER_CLS = CosmosDbCommandsLoader
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
# --------------------------------------------------------------------------------------------

import os
from azure.cli.core import __version__ as core_version

from knack.log import get_logger
from knack.util import CLIError

from azure.cli.core import __version__ as core_version


logger = get_logger(__name__)


NO_CREDENTIALS_ERROR_MESSAGE = """
No credentials specified to access Cosmos DB service. Please provide any of the following:
(1) resource group name and account name
Expand Down Expand Up @@ -37,7 +43,7 @@ def _get_url_connection(url_collection, account_name):
return None


def get_document_client_factory(kwargs):
def cf_cosmosdb_document(cli_ctx, kwargs):
from pydocumentdb import document_client
service_type = document_client.DocumentClient

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

if name and resource_group and not key:
# if resource group name is provided find key
keys = cf_cosmosdb().database_accounts.list_keys(resource_group, name)
keys = cf_cosmosdb(cli_ctx).database_accounts.list_keys(resource_group, name)
key = keys.primary_master_key

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


def cf_cosmosdb(**_):
def cf_cosmosdb(cli_ctx, **_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.cosmosdb import CosmosDB
return get_mgmt_service_client(CosmosDB)
return get_mgmt_service_client(cli_ctx, CosmosDB)


def cf_db_accounts(cli_ctx, _):
return cf_cosmosdb(cli_ctx).database_accounts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,54 @@
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.commands import create_command, command_table


def cli_cosmosdb_data_plane_command(name,
operation, client_factory, transform=None,
table_transformer=None, exception_handler=None):
"""Registers an Azure CLI Cosmos DB Data Plane command. These commands always include the
parameters which can be used to obtain a cosmosdb client."""

if not exception_handler:
from ._exception_handler import generic_exception_handler
exception_handler = generic_exception_handler

command = create_command(__name__, name, operation, transform, table_transformer,
client_factory, exception_handler=exception_handler)

# add parameters required to create a cosmosdb client
group_name = 'Cosmos DB Account'

command.add_argument('db_resource_group_name', '--resource-group-name', '-g',
arg_group=group_name,
help='name of the resource group. Must be used in conjunction with '
'cosmosdb account name.')
command.add_argument('db_account_name', '--name', '-n', arg_group=group_name,
help='Cosmos DB account name. Must be used in conjunction with '
'either name of the resource group or cosmosdb account key.')

command.add_argument('db_account_key', '--key', required=False, default=None,
arg_group=group_name,
help='Cosmos DB account key. Must be used in conjunction with cosmosdb '
'account name or url-connection.')

command.add_argument('db_url_connection', '--url-connection', required=False, default=None,
arg_group=group_name,
help='Cosmos DB account url connection. Must be used in conjunction with '
'cosmosdb account key.')

command_table[command.name] = command
from azure.cli.core.commands import AzCommandGroup


class CosmosDbCommandGroup(AzCommandGroup):

def _create_cosmosdb_command(self, name, method_name=None, command_type_name=None, **kwargs):
"""Registers an Azure CLI Cosmos DB Data Plane command. These commands always include the
parameters which can be used to obtain a cosmosdb client."""

merged_kwargs = self._flatten_kwargs(kwargs, command_type_name)
if 'exception_handler' not in merged_kwargs:
from ._exception_handler import generic_exception_handler
merged_kwargs['exception_handler'] = generic_exception_handler
if command_type_name == 'command_type':
command_name = self.command(name, method_name, **merged_kwargs)
else:
command_name = self.custom_command(name, method_name, **merged_kwargs)

command = self.command_loader.command_table[command_name]

# add parameters required to create a cosmosdb client
group_name = 'Cosmos DB Account'
command.add_argument('db_resource_group_name', '--resource-group-name', '-g',
arg_group=group_name,
help='name of the resource group. Must be used in conjunction with '
'cosmosdb account name.')
command.add_argument('db_account_name', '--name', '-n', arg_group=group_name,
help='Cosmos DB account name. Must be used in conjunction with '
'either name of the resource group or cosmosdb account key.')

command.add_argument('db_account_key', '--key', required=False, default=None,
arg_group=group_name,
help='Cosmos DB account key. Must be used in conjunction with cosmosdb '
'account name or url-connection.')

command.add_argument('db_url_connection', '--url-connection', required=False, default=None,
arg_group=group_name,
help='Cosmos DB account url connection. Must be used in conjunction with '
'cosmosdb account key.')

def cosmosdb_command(self, name, method_name=None, command_type=None, **kwargs):
command_type_name = 'command_type'
if command_type:
kwargs[command_type_name] = command_type
self._create_cosmosdb_command(name, method_name, command_type_name, **kwargs)

def cosmosdb_custom(self, name, method_name=None, command_type=None, **kwargs):
command_type_name = 'custom_command_type'
if command_type:
kwargs[command_type_name] = command_type
self._create_cosmosdb_command(name, method_name, command_type_name, **kwargs)
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
# --------------------------------------------------------------------------------------------

import json

from knack.log import get_logger
from knack.util import CLIError


logger = get_logger(__name__)


def duplicate_resource_exception_handler(ex):
# wraps DocumentDB 409 error in CLIError
from pydocumentdb.errors import HTTPFailure
Expand Down
Loading

0 comments on commit bc0f92d

Please sign in to comment.