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

Add Subscription RP commands #5453

Closed
wants to merge 16 commits into from
36 changes: 34 additions & 2 deletions azure-cli2017.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
<LaunchProvider>Standard Python launcher</LaunchProvider>
<InterpreterId>MSBuild|{54f4b6dc-0859-46dc-99bb-b275c9d0aca3}|$(MSBuildProjectFullPath)</InterpreterId>
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging>
<CommandLineArguments>
</CommandLineArguments>
<CommandLineArguments></CommandLineArguments>
<InterpreterArguments>
</InterpreterArguments>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
Expand Down Expand Up @@ -597,6 +598,22 @@
<Compile Include="command_modules\azure-cli-resource\setup.py" />
<Compile Include="command_modules\azure-cli-storage\azure\cli\command_modules\storage\__init__.py" />
<Compile Include="command_modules\azure-cli-storage\setup.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\commands.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\custom.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\test_subscription_definition_commands.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\__init__.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\__init__.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\_client_factory.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\_exception_handler.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\_completers.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\_help.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\_params.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\__init__.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\command_modules\__init__.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\cli\__init__.py" />
<Compile Include="command_modules\azure-cli-subscription\azure\__init__.py" />
<Compile Include="command_modules\azure-cli-subscription\azure_bdist_wheel.py" />
<Compile Include="command_modules\azure-cli-subscription\setup.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\commands.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\custom.py" />
<Compile Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\disk_encryption.py" />
Expand Down Expand Up @@ -920,6 +937,14 @@
<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-storage\azure\cli\command_modules\storage\tests\latest\" />
<Folder Include="command_modules\azure-cli-subscription\" />
<Folder Include="command_modules\azure-cli-subscription\azure\" />
<Folder Include="command_modules\azure-cli-subscription\azure\cli\" />
<Folder Include="command_modules\azure-cli-subscription\azure\cli\command_modules\" />
<Folder Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\" />
<Folder Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\" />
<Folder Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\" />
<Folder Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\recordings\" />
<Folder Include="command_modules\azure-cli-vm\" />
<Folder Include="command_modules\azure-cli-vm\azure\" />
<Folder Include="command_modules\azure-cli-vm\azure\cli\" />
Expand Down Expand Up @@ -1107,6 +1132,13 @@
<Content Include="command_modules\azure-cli-servicefabric\README.rst" />
<Content Include="command_modules\azure-cli-sql\HISTORY.rst" />
<Content Include="command_modules\azure-cli-storage\HISTORY.rst" />
<Content Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\recordings\test_create_subscription_definitions.yaml" />
<Content Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\recordings\test_list_subscription_definitions.yaml" />
<Content Include="command_modules\azure-cli-subscription\azure\cli\command_modules\subscription\tests\latest\recordings\test_show_subscription_definitions.yaml" />
<Content Include="command_modules\azure-cli-subscription\HISTORY.rst" />
<Content Include="command_modules\azure-cli-subscription\MANIFEST.in" />
<Content Include="command_modules\azure-cli-subscription\README.rst" />
<Content Include="command_modules\azure-cli-subscription\setup.cfg" />
<Content Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\tests\latest\aliases.json" />
<Content Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\tests\latest\keyvault\policy.json" />
<Content Include="command_modules\azure-cli-vm\azure\cli\command_modules\vm\tests\latest\sample-public.json" />
Expand Down
1 change: 1 addition & 0 deletions doc/sphinx/azhelpgen/doc_source_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"role": "src/command_modules/azure-cli-role/azure/cli/command_modules/role/_help.py",
"sql": "src/command_modules/azure-cli-sql/azure/cli/command_modules/sql/_help.py",
"storage": "src/command_modules/azure-cli-storage/azure/cli/command_modules/storage/_help.py",
"subscription": "src/command_modules/azure-cli-subscription/azure/cli/command_modules/subscription/_help.py",
"tag": "src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_help.py",
"vm": "src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_help.py",
"vmss": "src/command_modules/azure-cli-vm/azure/cli/command_modules/vm/_help.py",
Expand Down
1 change: 1 addition & 0 deletions src/azure-cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
'azure-cli-role',
'azure-cli-sql',
'azure-cli-storage',
'azure-cli-subscription',
'azure-cli-vm',
'azure-cli-servicefabric'
]
Expand Down
10 changes: 10 additions & 0 deletions src/command_modules/azure-cli-subscription/HISTORY.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. :changelog:

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

0.1.0
+++++++++++++++++++++

* Initial preview release.

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

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


7 changes: 7 additions & 0 deletions src/command_modules/azure-cli-subscription/azure/__init__.py
Original file line number Diff line number Diff line change
@@ -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__)
Original file line number Diff line number Diff line change
@@ -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__)
Original file line number Diff line number Diff line change
@@ -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__)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# --------------------------------------------------------------------------------------------
# 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 import AzCommandsLoader

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


class SubscriptionCommandsLoader(AzCommandsLoader):

def __init__(self, cli_ctx=None):
from azure.cli.core.commands import CliCommandType
subscription_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.subscription.custom#{}')
super(SubscriptionCommandsLoader, self).__init__(cli_ctx=cli_ctx, custom_command_type=subscription_custom,
min_profile="2017-03-10-profile")

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

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


COMMAND_LOADER_CLS = SubscriptionCommandsLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------


def cf_subscription(cli_ctx, **_):
from azure.cli.core.commands.client_factory import _get_mgmt_service_client
from azure.mgmt.subscription import SubscriptionClient
client, _ = _get_mgmt_service_client(cli_ctx, SubscriptionClient, subscription_bound=False)
return client


def subscription_definitions_mgmt_client_factory(cli_ctx, kwargs):
return cf_subscription(cli_ctx, **kwargs).subscription_definitions
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# --------------------------------------------------------------------------------------------
# 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.decorators import Completer


@Completer
def get_offer_type_completion_list(cmd, prefix, namespace): # pylint: disable=unused-argument
return ['MS-AZR-0017P', 'MS-AZR-0148P']
Copy link
Member

Choose a reason for hiding this comment

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

A completer is overkill for this. They are only used when you need to query the server for completions. Instead use: arg_type=get_enum_typ(['MS-AZR-0017P', 'MS-AZR-0148P']) in your params.py file for this parameter.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from knack.util import CLIError


def subscription_exception_handler(ex):
from azure.mgmt.subscription.models import ErrorResponseException
if isinstance(ex, ErrorResponseException):
message = ex.error.error.message
raise CLIError(message)
else:
import sys
from six import reraise
reraise(*sys.exc_info())
Original file line number Diff line number Diff line change
@@ -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.
# --------------------------------------------------------------------------------------------

from knack.help_files import helps


helps['subscriptiondefinition'] = """
Copy link
Member

Choose a reason for hiding this comment

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

This is a very lengthy name for a top-level command. Also, the account subgroup is already designated for subscriptions, so it would be more logical for these command to live there. az account definition ....

Copy link
Member Author

Choose a reason for hiding this comment

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

Moved it under the account subgroup. Agree the name is long, but unfortunately I believe "definition" is too generic. Is there precedent for using shortened names, such as "subdef", so it becomes "account subdef list"?

type: group
short-summary: Manage Azure Subscription Definitions.
"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# --------------------------------------------------------------------------------------------
# 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.command_modules.subscription._completers import get_offer_type_completion_list


# pylint: disable=line-too-long
def load_arguments(self, _):
with self.argument_context('subscriptiondefinition show') as c:
c.argument('subscription_definition_name', options_list=['--name', '-n'], required=False, help='Name of the subscription definition to show.')
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to call out "...to show". That way you only need to apply this alias once, with a scope of account definition.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.


with self.argument_context('subscriptiondefinition create') as c:
c.argument('name', options_list=['--name', '-n'], required=True, help='Name of the subscription definition.')
c.argument('offer_type', options_list=['--offer_type', '-ot'], required=True, help='The subscription\'s offer type.', completer=get_offer_type_completion_list)
Copy link
Member

Choose a reason for hiding this comment

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

You cannot have a two-character short option. If anything there shouldn't be a short option, but if you really want one, -t would be the most appropriate.

You cannot use underscore in you long options. --offer_type should be --offer-type.

Just use arg_type=get_enum_type(...) here instead of a completer.

Finally, you don't need/should not provide required=True/False. The CLI will infer that from your command signature based on whether you provided a default value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

c.argument('subscription_display_name', options_list=['--subscription_display_name', '-sdn'], required=False, help='The subscription display name of the subscription definition.')
Copy link
Member

Choose a reason for hiding this comment

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

Same comments as above. Also, I would recommend shortening --subscription-display-name to simply --display-name

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. Regarding subscription-display-name - just "display-name" doesn't properly capture the semantic here, so I'm leaving that as-is.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# --------------------------------------------------------------------------------------------
# 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 CliCommandType
from azure.cli.command_modules.subscription._client_factory import (
subscription_definitions_mgmt_client_factory)
from ._exception_handler import subscription_exception_handler


def load_command_table(self, _):
subscription_definition_util = CliCommandType(
operations_tmpl='azure.mgmt.subscription.operations.subscription_definitions_operations#SubscriptionDefinitionsOperations.{}',
client_factory=subscription_definitions_mgmt_client_factory,
exception_handler=subscription_exception_handler
)

with self.command_group('subscriptiondefinition', subscription_definition_util, client_factory=subscription_definitions_mgmt_client_factory) as g:
g.command('list', 'list')
g.command('show', 'get')
g.custom_command('create', 'cli_subscription_create_subscription_definition')
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

from azure.mgmt.subscription.models import SubscriptionDefinition


def cli_subscription_create_subscription_definition(client, name, offer_type, subscription_display_name=None):
"""Create a subscription definition."""
Copy link
Member

Choose a reason for hiding this comment

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

Don't use custom command doc strings to populate help. Instead, add a command entry to you help.py file with this as the short-summary. The reason is because the live docs edit functionality will link directly to that, and will never find it here.

new_def = SubscriptionDefinition(
subscription_display_name=subscription_display_name if subscription_display_name else name,
offer_type=offer_type)

return client.create(name, new_def)
Original file line number Diff line number Diff line change
@@ -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.
# --------------------------------------------------------------------------------------------
Original file line number Diff line number Diff line change
@@ -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.
# --------------------------------------------------------------------------------------------
Loading