Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DocumentDB Azure CLI support #1815

Merged
merged 16 commits into from Feb 1, 2017
5 changes: 5 additions & 0 deletions src/command_modules/azure-cli-documentdb/HISTORY.rst
@@ -0,0 +1,5 @@
.. :changelog:

Release History
===============

1 change: 1 addition & 0 deletions src/command_modules/azure-cli-documentdb/MANIFEST.in
@@ -0,0 +1 @@
include *.rst
7 changes: 7 additions & 0 deletions src/command_modules/azure-cli-documentdb/README.rst
@@ -0,0 +1,7 @@
Microsoft Azure CLI 'documentdb' Command Module
==================================

This package is for the 'documentdb' module.
i.e. 'az documentdb'


7 changes: 7 additions & 0 deletions src/command_modules/azure-cli-documentdb/azure/__init__.py
@@ -0,0 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pkg_resources
pkg_resources.declare_namespace(__name__)
@@ -0,0 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pkg_resources
pkg_resources.declare_namespace(__name__)
@@ -0,0 +1,7 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import pkg_resources
pkg_resources.declare_namespace(__name__)
@@ -0,0 +1,12 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import azure.cli.command_modules.documentdb._help # pylint: disable=unused-import

def load_params(_):
import azure.cli.command_modules.documentdb._params #pylint: disable=redefined-outer-name

def load_commands():
import azure.cli.command_modules.documentdb.commands #pylint: disable=redefined-outer-name
@@ -0,0 +1,9 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

def cf_documentdb(_):
from azure.cli.core.commands.client_factory import get_mgmt_service_client
from azure.mgmt.documentdb import DocumentDB
return get_mgmt_service_client(DocumentDB)
@@ -0,0 +1,13 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.cli.core.help_files import helps #pylint: disable=unused-import

#pylint: disable=line-too-long

helps['documentdb'] = """
type: group
short-summary: Commands to manage your Azure DocumentDB (NoSQL) database accounts.
"""
@@ -0,0 +1,52 @@
# --------------------------------------------------------------------------------------------
# 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
from azure.cli.core.commands.parameters import (
get_resource_name_completion_list,
enum_choice_list,
name_type,
ignore_type)
from azure.cli.core.commands import register_cli_argument
import azure.cli.core.commands.arm # pylint: disable=unused-import
from azure.mgmt.documentdb.models.document_db_enums import KeyKind
from azure.mgmt.documentdb.models.document_db_enums import DefaultConsistencyLevel
from azure.mgmt.documentdb.models.failover_policy import FailoverPolicy
from azure.mgmt.documentdb.models.location import Location


def validate_failover_policies(ns):
''' Extracts multiple space-separated failoverPolicies in regionName=failoverPriority format '''
fp_dict = []
for item in ns.failover_policies:
comps = item.split('=', 1)
fp_dict.append(FailoverPolicy(comps[0], int(comps[1])))
ns.failover_policies = fp_dict

def validate_locations(ns):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these are identical in logic, you could just have "validate_failover_policies_or_locations" or something. If in the future you think they might be handled differently, then leaving them like this is fine. If they will always move together, then this becomes an added maintenance burden for your team.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I believe the 'location' object could change in the future so I will leave it as is

''' Extracts multiple space-separated locations in regionName=failoverPriority format '''
loc_dict = []
for item in ns.locations:
comps = item.split('=', 1)
loc_dict.append(Location(location_name=comps[0], failover_priority=int(comps[1])))
ns.locations = loc_dict

def validate_ip_range_filter(ns):
if ns.ip_range_filter:
ns.ip_range_filter = ",".join(ns.ip_range_filter)

register_cli_argument('documentdb', 'account_name', arg_type=name_type, help='Name of the DocumentDB Database Account', completer=get_resource_name_completion_list('Microsoft.DocumentDb/databaseAccounts'), id_part="name")

register_cli_argument('documentdb regenerate-key', 'key_kind', **enum_choice_list(KeyKind))
register_cli_argument('documentdb failover-priority-change', 'failover_policies', validator=validate_failover_policies, help="space separated failover policies in 'regionName=failoverPriority' format. E.g \"East US\"=0", nargs='+')

register_cli_argument('documentdb create', 'account_name', completer=None)
register_cli_argument('documentdb create', 'resource_group_name', help="name of the resource group")
register_cli_argument('documentdb create', 'resource_group_location', ignore_type)
register_cli_argument('documentdb create', 'locations', validator=validate_locations, help="space separated locations in 'regionName=failoverPriority' format. E.g \"East US\"=0. Failover priority values are 0 for write regions and greater than 0 for read regions. A failover priority value must be unique and less than the total number of regions.", nargs='+')
register_cli_argument('documentdb create', 'default_consistency_level', help="default consistency level of the DocumentDB Database account", **enum_choice_list(DefaultConsistencyLevel))
register_cli_argument('documentdb create', 'max_staleness_prefix', help="when used with Bounded Staleness consistency, this value represents the number of stale requests tolerated. Accepted range for this value is 1 - 2,147,483,647", type=int)
register_cli_argument('documentdb create', 'max_interval', help="when used with Bounded Staleness consistency, this value represents the time amount of staleness (in seconds) tolerated. Accepted range for this value is 1 - 100", type=int)
register_cli_argument('documentdb create', 'ip_range_filter', validator=validate_ip_range_filter, help="firewall support. Specifies the set of IP addresses or IP address ranges in CIDR form to be included as the allowed list of client IPs for a given database account. IP addresses/ranges must be comma separated and must not contain any spaces", nargs='+')
@@ -0,0 +1,22 @@
# --------------------------------------------------------------------------------------------
# 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

from azure.cli.core.commands import cli_command
from azure.cli.command_modules.documentdb._client_factory import (cf_documentdb)

custom_path = 'azure.mgmt.documentdb.operations.database_accounts_operations#{}'

cli_command(__name__, 'documentdb show', custom_path.format('DatabaseAccountsOperations.get'), cf_documentdb)
cli_command(__name__, 'documentdb list', custom_path.format('DatabaseAccountsOperations.list_by_resource_group'), cf_documentdb)
cli_command(__name__, 'documentdb list-all', custom_path.format('DatabaseAccountsOperations.list'), cf_documentdb)
cli_command(__name__, 'documentdb list-keys', custom_path.format('DatabaseAccountsOperations.list_keys'), cf_documentdb)
cli_command(__name__, 'documentdb list-read-only-keys', custom_path.format('DatabaseAccountsOperations.list_read_only_keys'), cf_documentdb)
cli_command(__name__, 'documentdb regenerate-key', custom_path.format('DatabaseAccountsOperations.regenerate_key'), cf_documentdb)
cli_command(__name__, 'documentdb check-name-exists', custom_path.format('DatabaseAccountsOperations.check_name_exists'), cf_documentdb)
cli_command(__name__, 'documentdb delete', custom_path.format('DatabaseAccountsOperations.delete'), cf_documentdb)
cli_command(__name__, 'documentdb failover-priority-change', custom_path.format('DatabaseAccountsOperations.failover_priority_change'), cf_documentdb)
cli_command(__name__, 'documentdb create', 'azure.cli.command_modules.documentdb.custom#cli_documentdb_create', cf_documentdb)
@@ -0,0 +1,52 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

# --locations
# -ipRangeFilter
# -defaultConsistencyLevel
# - MaxIntervalInSeconds
# - MaxStalenessPrefix
# - resource-group-location
# -Kind

from azure.mgmt.documentdb.models import (
ConsistencyPolicy,
DatabaseAccountCreateUpdateParameters,
)

# pylint:disable=too-many-arguments
def cli_documentdb_create(client,
resource_group_name,
account_name,
locations,
default_consistency_level=None,
max_staleness_prefix=100,
max_interval=5,
ip_range_filter=None):
# pylint:disable=line-too-long
"""Create a new Azure DocumentDB database account.
"""

consistency_policy = None
if default_consistency_level is not None:
consistency_policy = ConsistencyPolicy(default_consistency_level, max_staleness_prefix, max_interval)


from azure.mgmt.resource.resources import ResourceManagementClient
from azure.cli.core.commands.client_factory import get_mgmt_service_client
resource_client = get_mgmt_service_client(ResourceManagementClient)
rg = resource_client.resource_groups.get(resource_group_name)
resource_group_location = rg.location # pylint: disable=no-member

params = DatabaseAccountCreateUpdateParameters(
resource_group_location,
locations,
consistency_policy=consistency_policy,
ip_range_filter=ip_range_filter)

async_docdb_create = client.database_accounts.create_or_update(resource_group_name, account_name, params)
docdb_account = async_docdb_create.result() # Will be empty, shouldn't
docdb_account = client.database_accounts.get(resource_group_name, account_name) # Workaround
return docdb_account
@@ -0,0 +1,4 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
@@ -0,0 +1,4 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
2 changes: 2 additions & 0 deletions src/command_modules/azure-cli-documentdb/setup.cfg
@@ -0,0 +1,2 @@
[bdist_wheel]
universal=1
58 changes: 58 additions & 0 deletions src/command_modules/azure-cli-documentdb/setup.py
@@ -0,0 +1,58 @@
#!/usr/bin/env python

# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------


from codecs import open
from setuptools import setup

VERSION = '0.1.1b1+dev'

# The full list of classifiers is available at
# https://pypi.python.org/pypi?%3Aaction=list_classifiers
CLASSIFIERS = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'License :: OSI Approved :: MIT License',
]

DEPENDENCIES = [
'azure-mgmt-documentdb==0.1.0',
'azure-cli-core'
]

with open('README.rst', 'r', encoding='utf-8') as f:
README = f.read()
with open('HISTORY.rst', 'r', encoding='utf-8') as f:
HISTORY = f.read()

setup(
name='azure-cli-documentdb',
version=VERSION,
description='Microsoft Azure Command-Line Tools DocumentDB Command Module',
long_description=README + '\n\n' + HISTORY,
license='MIT',
author='Microsoft Corporation',
author_email='azpycli@microsoft.com',
url='https://github.com/Azure/azure-cli',
classifiers=CLASSIFIERS,
namespace_packages=[
'azure',
'azure.cli',
'azure.cli.command_modules',
],
packages=[
'azure.cli.command_modules.documentdb',
],
install_requires=DEPENDENCIES,
)