Skip to content

Commit 6ffea89

Browse files
Raj Shahtjprescott
authored andcommitted
Add management groups commands to azure-cli-resource (#6426)
* Add mgmt groups to resource, use latest SDK, update tests * Bump up version and edit history * Add wheel file * style check * Changes suggested in PR/after review * More changes suggested in the PR and rerun tests * Style check * Some build fixes * Minor fix * Fix build errors, rerun tests in prod, update help * Supress extension * Remove private whl file
1 parent 2cd0ed3 commit 6ffea89

23 files changed

+6472
-5
lines changed

src/command_modules/azure-cli-resource/HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
Release History
44
===============
55

6+
2.0.30
7+
++++++
8+
* add `account management-group` commands.
9+
610
2.0.29
711
++++++
812
* Minor changes

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ class ResourceCommandsLoader(AzCommandsLoader):
1212

1313
def __init__(self, cli_ctx=None):
1414
from azure.cli.core.commands import CliCommandType
15+
from azure.cli.core import ModExtensionSuppress
1516
resource_custom = CliCommandType(operations_tmpl='azure.cli.command_modules.resource.custom#{}')
1617
super(ResourceCommandsLoader, self).__init__(cli_ctx=cli_ctx,
17-
custom_command_type=resource_custom)
18+
custom_command_type=resource_custom,
19+
suppress_extension=ModExtensionSuppress(
20+
__name__, 'managementgroups', '0.1.0',
21+
reason='The management groups commands are now in CLI.',
22+
recommend_remove=True))
1823

1924
def load_command_table(self, args):
2025
from azure.cli.command_modules.resource.commands import load_command_table

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ def _resource_managedapps_client_factory(cli_ctx, **_):
4646
return get_mgmt_service_client(cli_ctx, ApplicationClient)
4747

4848

49+
def _resource_managementgroups_client_factory(cli_ctx, **_):
50+
from azure.cli.core.commands.client_factory import get_mgmt_service_client
51+
from azure.mgmt.managementgroups import ManagementGroupsAPI
52+
return get_mgmt_service_client(cli_ctx, ManagementGroupsAPI, subscription_bound=False)
53+
54+
4955
def cf_resource_groups(cli_ctx, _):
5056
return _resource_client_factory(cli_ctx).resource_groups
5157

@@ -96,3 +102,11 @@ def cf_resource_managedapplications(cli_ctx, _):
96102

97103
def cf_resource_managedappdefinitions(cli_ctx, _):
98104
return _resource_managedapps_client_factory(cli_ctx).application_definitions
105+
106+
107+
def cf_management_groups(cli_ctx, _):
108+
return _resource_managementgroups_client_factory(cli_ctx).management_groups
109+
110+
111+
def cf_management_group_subscriptions(cli_ctx, _):
112+
return _resource_managementgroups_client_factory(cli_ctx).management_group_subscriptions

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_completers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from azure.cli.command_modules.resource._client_factory import (
99
_resource_policy_client_factory, _resource_client_factory)
1010

11+
from azure.cli.command_modules.profile.custom import _load_subscriptions
12+
1113

1214
@Completer
1315
def get_policy_completion_list(cmd, prefix, namespace, **kwargs): # pylint: disable=unused-argument
@@ -46,3 +48,13 @@ def get_resource_types_completion_list(cmd, prefix, namespace, **kwargs): # pyl
4648
for r in p.resource_types:
4749
types.append(p.namespace + '/' + r.resource_type)
4850
return types
51+
52+
53+
@Completer
54+
def get_subscription_id_list(cmd, prefix, namespace, **kwargs): # pylint: disable=unused-argument
55+
subscriptions = _load_subscriptions(cmd.cli_ctx)
56+
result = []
57+
for subscription in subscriptions:
58+
result.append(subscription['id'])
59+
result.append(subscription['name'])
60+
return result
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
from azure.cli.core.util import CLIError
7+
8+
9+
def managementgroups_exception_handler(ex):
10+
from azure.mgmt.managementgroups.models import ErrorResponseException
11+
if isinstance(ex, ErrorResponseException):
12+
if ex.error.error:
13+
raise CLIError(ex.error.error)
14+
else:
15+
raise CLIError(ex.error)
16+
else:
17+
import sys
18+
from six import reraise
19+
reraise(*sys.exc_info())

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_help.py

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,155 @@
152152
text: >
153153
az account lock update --name lockName --notes newNotesHere --lock-type CanNotDelete
154154
"""
155+
helps['account management-group'] = """
156+
type: group
157+
short-summary: Manage Azure Management Groups.
158+
"""
159+
160+
helps['account management-group subscription'] = """
161+
type: group
162+
short-summary: Subscription operations for Management Groups.
163+
"""
164+
165+
helps['account management-group list'] = """
166+
type: command
167+
short-summary: List all management groups.
168+
long-summary: List of all management groups in the current tenant.
169+
examples:
170+
- name: List all management groups
171+
text: >
172+
az account management-group list
173+
"""
174+
175+
helps['account management-group show'] = """
176+
type: command
177+
short-summary: Get a specific management group.
178+
long-summary: Get the details of the management group.
179+
parameters:
180+
- name: --name -n
181+
type: string
182+
short-summary: Name of the management group.
183+
- name: --expand -e
184+
type: bool
185+
short-summary: If given, lists the children in the first level of hierarchy.
186+
- name: --recurse -r
187+
type: bool
188+
short-summary: If given, lists the children in all levels of hierarchy.
189+
examples:
190+
- name: Get a management group.
191+
text: >
192+
az account management-group show --name GroupName
193+
- name: Get a management group with children in the first level of hierarchy.
194+
text: >
195+
az account management-group show --name GroupName -e
196+
- name: Get a management group with children in all levels of hierarchy.
197+
text: >
198+
az account management-group show --name GroupName -e -r
199+
"""
200+
201+
helps['account management-group create'] = """
202+
type: command
203+
short-summary: Create a new management group.
204+
long-summary: Create a new management group.
205+
parameters:
206+
- name: --name -n
207+
type: string
208+
short-summary: Name of the management group.
209+
- name: --display-name -d
210+
type: string
211+
short-summary: Sets the display name of the management group. If null, the group name is set as the display name.
212+
- name: --parent -p
213+
type: string
214+
short-summary: Sets the parent of the management group. Can be the fully qualified id or the name of the management group. If null, the root tenant group is set as the parent.
215+
examples:
216+
- name: Create a new management group.
217+
text: >
218+
az account management-group create --name GroupName
219+
- name: Create a new management group with a specific display name.
220+
text: >
221+
az account management-group create --name GroupName --display-name DisplayName
222+
- name: Create a new management group with a specific parent.
223+
text: >
224+
az account management-group create --name GroupName --parent ParentId/ParentName
225+
- name: Create a new management group with a specific display name and parent.
226+
text: >
227+
az account management-group create --name GroupName --display-name DisplayName --parent ParentId/ParentName
228+
"""
229+
230+
helps['account management-group update'] = """
231+
type: command
232+
short-summary: Update an existing management group.
233+
long-summary: Update an existing management group.
234+
parameters:
235+
- name: --name -n
236+
type: string
237+
short-summary: Name of the management group.
238+
- name: --display-name -d
239+
type: string
240+
short-summary: Updates the display name of the management group. If null, no change is made.
241+
- name: --parent -p
242+
type: string
243+
short-summary: Update the parent of the management group. Can be the fully qualified id or the name of the management group. If null, no change is made.
244+
examples:
245+
- name: Update an existing management group with a specific display name.
246+
text: >
247+
az account management-group update --name GroupName --display-name DisplayName
248+
- name: Update an existing management group with a specific parent.
249+
text: >
250+
az account management-group update --name GroupName --parent ParentId/ParentName
251+
- name: Update an existing management group with a specific display name and parent.
252+
text: >
253+
az account management-group update --name GroupName --display-name DisplayName --parent ParentId/ParentName
254+
"""
255+
256+
helps['account management-group delete'] = """
257+
type: command
258+
short-summary: Delete an existing management group.
259+
long-summary: Delete an existing management group.
260+
parameters:
261+
- name: --name -n
262+
type: string
263+
short-summary: Name of the management group.
264+
examples:
265+
- name: Delete an existing management group
266+
text: >
267+
az account management-group delete --name GroupName
268+
"""
269+
270+
helps['account management-group subscription add'] = """
271+
type: command
272+
short-summary: Add a subscription to a management group.
273+
long-summary: Add a subscription to a management group.
274+
parameters:
275+
- name: --name -n
276+
type: string
277+
short-summary: Name of the management group.
278+
- name: --subscription -s
279+
type: string
280+
short-summary: Subscription Id or Name
281+
examples:
282+
- name: Add a subscription to a management group.
283+
text: >
284+
az account management-group subscription add --name GroupName --subscription Subscription
285+
"""
286+
287+
helps['account management-group subscription remove'] = """
288+
type: command
289+
short-summary: Remove an existing subscription from a management group.
290+
long-summary: Remove an existing subscription from a management group.
291+
parameters:
292+
- name: --name -n
293+
type: string
294+
short-summary: Name of the management group.
295+
- name: --subscription -s
296+
type: string
297+
short-summary: Subscription Id or Name
298+
examples:
299+
- name: Remove an existing subscription from a management group.
300+
text: >
301+
az account management-group subscription remove --name GroupName --subscription Subscription
302+
"""
303+
155304
helps['policy'] = """
156305
type: group
157306
short-summary: Manage resource policies.

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/_params.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def load_arguments(self, _):
2121

2222
from azure.cli.command_modules.resource._completers import (
2323
get_policy_completion_list, get_policy_set_completion_list, get_policy_assignment_completion_list,
24-
get_resource_types_completion_list, get_providers_completion_list)
24+
get_resource_types_completion_list, get_providers_completion_list, get_subscription_id_list)
2525
from azure.cli.command_modules.resource._validators import (
2626
validate_lock_parameters, validate_resource_lock, validate_group_lock, validate_subscription_lock, validate_metadata)
2727

@@ -209,3 +209,21 @@ def load_arguments(self, _):
209209
c.argument('authorizations', options_list=('--authorizations', '-a'), nargs='+', help="space-separated authorization pairs in a format of <principalId>:<roleDefinitionId>")
210210
c.argument('createUiDefinition', options_list=('--create-ui-definition', '-c'), help='JSON formatted string or a path to a file with such content', type=file_type)
211211
c.argument('mainTemplate', options_list=('--main-template', '-t'), help='JSON formatted string or a path to a file with such content', type=file_type)
212+
213+
with self.argument_context('account') as c:
214+
c.argument('subscription', options_list=['--subscription', '-s'], help='Name or ID of subscription.', completer=get_subscription_id_list)
215+
216+
with self.argument_context('account management-group') as c:
217+
c.argument('group_name', options_list=['--name', '-n'])
218+
219+
with self.argument_context('account management-group show') as c:
220+
c.argument('expand', options_list=['--expand', '-e'], action='store_true')
221+
c.argument('recurse', options_list=['--recurse', '-r'], action='store_true')
222+
223+
with self.argument_context('account management-group create') as c:
224+
c.argument('display_name', options_list=['--display-name', '-d'])
225+
c.argument('parent', options_list=['--parent', '-p'])
226+
227+
with self.argument_context('account management-group update') as c:
228+
c.argument('display_name', options_list=['--display-name', '-d'])
229+
c.argument('parent_id', options_list=['--parent', '-p'])

src/command_modules/azure-cli-resource/azure/cli/command_modules/resource/commands.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
from azure.cli.command_modules.resource._client_factory import (
1616
cf_resource_groups, cf_providers, cf_features, cf_tags, cf_deployments,
1717
cf_deployment_operations, cf_policy_definitions, cf_policy_set_definitions, cf_resource_links,
18-
cf_resource_managedapplications, cf_resource_managedappdefinitions)
18+
cf_resource_managedapplications, cf_resource_managedappdefinitions, cf_management_groups, cf_management_group_subscriptions)
1919
from azure.cli.command_modules.resource._validators import process_deployment_create_namespace
2020

21+
from ._exception_handler import managementgroups_exception_handler
22+
2123

2224
# Resource group commands
2325
def transform_resource_group_list(result):
@@ -119,6 +121,24 @@ def load_command_table(self, _):
119121
resource_type=ResourceType.MGMT_RESOURCE_RESOURCES
120122
)
121123

124+
resource_managementgroups_sdk = CliCommandType(
125+
operations_tmpl='azure.mgmt.managementgroups.operations.management_groups_operations#ManagementGroupsOperations.{}',
126+
client_factory=cf_management_groups,
127+
exception_handler=managementgroups_exception_handler
128+
)
129+
130+
resource_managementgroups_subscriptions_sdk = CliCommandType(
131+
operations_tmpl='azure.mgmt.managementgroups.operations.management_group_subscriptions_operations#ManagementGroupSubscriptionsOperations.{}',
132+
client_factory=cf_management_group_subscriptions,
133+
exception_handler=managementgroups_exception_handler
134+
)
135+
136+
resource_managementgroups_update_type = CliCommandType(
137+
operations_tmpl='azure.cli.command_modules.resource.custom#{}',
138+
client_factory=cf_management_groups,
139+
exception_handler=managementgroups_exception_handler
140+
)
141+
122142
with self.command_group('account lock', resource_lock_sdk, resource_type=ResourceType.MGMT_RESOURCE_LOCKS) as g:
123143
g.custom_command('create', 'create_lock')
124144
g.custom_command('delete', 'delete_lock')
@@ -244,3 +264,22 @@ def load_command_table(self, _):
244264
g.command('delete', 'delete')
245265
g.custom_command('show', 'show_applicationdefinition')
246266
g.command('list', 'list_by_resource_group', exception_handler=empty_on_404)
267+
268+
with self.command_group('account management-group', resource_managementgroups_sdk, client_factory=cf_management_groups) as g:
269+
g.custom_command('list', 'cli_managementgroups_group_list')
270+
g.custom_command('show', 'cli_managementgroups_group_show')
271+
g.custom_command('create', 'cli_managementgroups_group_create')
272+
g.custom_command('delete', 'cli_managementgroups_group_delete')
273+
g.generic_update_command(
274+
'update',
275+
getter_name='cli_managementgroups_group_update_get',
276+
getter_type=resource_managementgroups_update_type,
277+
setter_name='cli_managementgroups_group_update_set',
278+
setter_type=resource_managementgroups_update_type,
279+
custom_func_name='cli_managementgroups_group_update_custom_func',
280+
custom_func_type=resource_managementgroups_update_type,
281+
exception_handler=managementgroups_exception_handler)
282+
283+
with self.command_group('account management-group subscription', resource_managementgroups_subscriptions_sdk, client_factory=cf_management_group_subscriptions) as g:
284+
g.custom_command('add', 'cli_managementgroups_subscription_add')
285+
g.custom_command('remove', 'cli_managementgroups_subscription_remove')

0 commit comments

Comments
 (0)