Skip to content

Commit 9b6260c

Browse files
vivsriausderekbekoe
authored andcommitted
Add policy set definition commands (#4515)
* Initial commit for policy set definition commands * Add sku, policysetdefinition to policy assignment create * move policy set definition under api check * Address some feedback * Add notScopes to assignment create, new record for existing test to ensure no regression * Fix not scopes issues * fix pylint issue * Check for free sku in policy assignment * Add checks for not scopes * Add policy set tests * Fix policy record * Fix pylint * nits * Fix CI issue * Fix error message format * fix check style issue
1 parent e1ec6ed commit 9b6260c

File tree

17 files changed

+1117
-88
lines changed

17 files changed

+1117
-88
lines changed

azure-cli.pyproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,7 @@
995995
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\param-validation-params.json" />
996996
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\param-validation-ref-params.json" />
997997
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\param-validation-template.json" />
998+
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\sample_policy_set.json" />
998999
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\sample_policy_rule.json" />
9991000
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\simple_deploy.json" />
10001001
<Content Include="command_modules\azure-cli-resource\azure\cli\command_modules\resource\tests\simple_deploy_parameters.json" />

src/azure-cli-core/azure/cli/core/profiles/_shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(self, import_prefix, client_name):
6666
ResourceType.MGMT_RESOURCE_FEATURES: '2015-12-01',
6767
ResourceType.MGMT_RESOURCE_LINKS: '2016-09-01',
6868
ResourceType.MGMT_RESOURCE_LOCKS: '2016-09-01',
69-
ResourceType.MGMT_RESOURCE_POLICY: '2016-12-01',
69+
ResourceType.MGMT_RESOURCE_POLICY: '2017-06-01-preview',
7070
ResourceType.MGMT_RESOURCE_RESOURCES: '2017-05-10',
7171
ResourceType.MGMT_RESOURCE_SUBSCRIPTIONS: '2016-06-01',
7272
ResourceType.DATA_STORAGE: '2017-04-17'

src/command_modules/azure-cli-acr/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
DEPENDENCIES = [
3333
'azure-cli-core',
34-
'azure-mgmt-resource==1.2.0rc2',
34+
'azure-mgmt-resource==1.2.0rc3',
3535
'azure-mgmt-storage==1.2.0',
3636
'azure-mgmt-containerregistry==0.3.1',
3737
]

src/command_modules/azure-cli-network/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
'azure-mgmt-network==1.5.0rc3',
3434
'azure-mgmt-trafficmanager==0.40.0',
3535
'azure-mgmt-dns==1.0.1',
36-
'azure-mgmt-resource==1.2.0rc2',
36+
'azure-mgmt-resource==1.2.0rc3',
3737
'azure-cli-core',
3838
'mock'
3939
]

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Release History
66
+++++++++++++++++++
77
* policy: support to show built-in policy definition.
88
* policy: support mode parameter for creating policy definitions.
9+
* policy: add policy set definition commands.
10+
* policy: add sku and policysetdefinition parameters when creating policy assignment
911
* managedapp definition: support to create managedapp definition using create-ui-definition and main-template.
1012
* BREAKING CHANGE: managedapp: Update to latest ARM package, which includes changing resource type from appliances to applications and applianceDefinitions to applicationDefinitions.
1113
* resource invoke-action: supports ability to invoke any action onto resource, also supports user-specified url to post.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def cf_policy_definitions(_):
7878
return _resource_policy_client_factory().policy_definitions
7979

8080

81+
def cf_policy_set_definitions(_):
82+
return _resource_policy_client_factory().policy_set_definitions
83+
84+
8185
def cf_management_locks(_):
8286
return _resource_lock_client_factory().management_locks
8387

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@
172172
}
173173
}
174174
}
175-
"""
175+
"""
176176
helps['policy definition delete'] = """
177177
type: command
178178
short-summary: Delete a policy definition.
@@ -189,6 +189,43 @@
189189
type: command
190190
short-summary: List policy definitions.
191191
"""
192+
helps['policy set-definition'] = """
193+
type: group
194+
short-summary: Manage resource policy set definitions.
195+
"""
196+
helps['policy set-definition create'] = """
197+
type: command
198+
short-summary: Create a policy set definition.
199+
parameters:
200+
- name: --definitions
201+
type: string
202+
short-summary: Policy definitions in JSON format, or a path to a file containing JSON rules.
203+
examples:
204+
- name: Create a policy set definition.
205+
text: |
206+
az policy setdefinition create -n readOnlyStorage --definitions \\
207+
[ \\
208+
{ \\
209+
"policyDefinitionId": "/subscriptions/mySubId/providers/Microsoft.Authorization/policyDefinitions/storagePolicy" \\
210+
} \\
211+
]
212+
"""
213+
helps['policy set-definition delete'] = """
214+
type: command
215+
short-summary: Delete a policy set definition.
216+
"""
217+
helps['policy set-definition show'] = """
218+
type: command
219+
short-summary: get a policy set definition.
220+
"""
221+
helps['policy set-definition update'] = """
222+
type: command
223+
short-summary: Update a policy set definition.
224+
"""
225+
helps['policy set-definition list'] = """
226+
type: command
227+
short-summary: List policy set definitions.
228+
"""
192229
helps['policy assignment'] = """
193230
type: group
194231
short-summary: Manage resource policy assignments.

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

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
from azure.cli.core.commands.parameters import (ignore_type, resource_group_name_type, tag_type,
1515
tags_type, get_resource_group_completion_list,
1616
enum_choice_list, no_wait_type, file_type)
17-
from .custom import (get_policy_completion_list, get_policy_assignment_completion_list,
18-
get_resource_types_completion_list, get_providers_completion_list)
17+
from .custom import (get_policy_completion_list, get_policy_set_completion_list,
18+
get_policy_assignment_completion_list, get_resource_types_completion_list,
19+
get_providers_completion_list)
1920
from ._validators import process_deployment_create_namespace, validate_lock_parameters
2021

2122
# BASIC PARAMETER CONFIGURATION
@@ -78,7 +79,6 @@
7879
help='mode of the new policy definition.',
7980
**enum_choice_list(PolicyMode))
8081

81-
8282
register_cli_argument('policy definition', 'display_name', help='display name of policy definition')
8383
register_cli_argument('policy definition', 'description', help='description of policy definition')
8484
register_cli_argument('policy assignment', 'name', options_list=('--name', '-n'), completer=get_policy_assignment_completion_list, help='name of the assignment')
@@ -88,10 +88,28 @@
8888
c.register_cli_argument('policy assignment create', 'params', options_list=('--params', '-p'),
8989
help='JSON formatted string or path to file with parameter values of policy rule')
9090

91+
with VersionConstraint(ResourceType.MGMT_RESOURCE_POLICY, min_api='2017-06-01-preview') as c:
92+
existing_policy_set_definition_name_type = CliArgumentType(options_list=('--name', '-n'), completer=get_policy_set_completion_list, help='The policy set definition name')
93+
c.register_cli_argument('policy set-definition', 'policy_set_definition_name', arg_type=existing_policy_set_definition_name_type)
94+
c.register_cli_argument('policy set-definition create', 'name', options_list=('--name', '-n'), help='name of the new policy set definition')
95+
c.register_cli_argument('policy set-definition', 'display_name', help='display name of policy set definition')
96+
c.register_cli_argument('policy set-definition', 'description', help='description of policy set definition')
97+
c.register_cli_argument('policy set-definition', 'params',
98+
help='JSON formatted string or a path to a file or uri with parameter definitions',
99+
type=file_type, completer=FilesCompleter())
100+
c.register_cli_argument('policy set-definition', 'definitions',
101+
help='JSON formatted string or a path to a file or uri with such content',
102+
type=file_type, completer=FilesCompleter())
103+
c.register_cli_argument('policy assignment create', 'policy_set_definition', options_list=('--policy-set-definition', '-d'),
104+
help='name or id of the policy set definition.')
105+
c.register_cli_argument('policy assignment create', 'sku', options_list=('--sku', '-s'),
106+
help='policy sku.', **enum_choice_list(['free', 'standard']))
107+
c.register_cli_argument('policy assignment create', 'notscopes', options_list=('--not-scopes'), nargs='+')
108+
91109
register_cli_argument('policy assignment', 'scope', help='scope at which this policy assignment applies to, e.g., /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333, /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup, or /subscriptions/0b1f6471-1bf0-4dda-aec3-111122223333/resourceGroups/myGroup/providers/Microsoft.Compute/virtualMachines/myVM')
92110
register_cli_argument('policy assignment', 'disable_scope_strict_match', action='store_true', help='include assignment either inhertied from parent scope or at child scope')
93111
register_cli_argument('policy assignment', 'display_name', help='display name of the assignment')
94-
register_cli_argument('policy assignment', 'policy', help='policy name or fully qualified id', completer=get_policy_completion_list)
112+
register_cli_argument('policy assignment', 'policy', help='name or id of the policy definition.', completer=get_policy_completion_list)
95113

96114
register_cli_argument('group', 'tag', tag_type)
97115
register_cli_argument('group', 'tags', tags_type)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
cf_deployments,
2222
cf_deployment_operations,
2323
cf_policy_definitions,
24+
cf_policy_set_definitions,
2425
cf_resource_links,
2526
cf_resource_managedapplications,
2627
cf_resource_managedappdefinitions)
@@ -125,6 +126,13 @@ def transform_deployments_list(result):
125126
cli_command(__name__, 'policy definition show', 'azure.cli.command_modules.resource.custom#get_policy_definition', exception_handler=empty_on_404)
126127
cli_command(__name__, 'policy definition update', 'azure.cli.command_modules.resource.custom#update_policy_definition')
127128

129+
if supported_api_version(ResourceType.MGMT_RESOURCE_POLICY, min_api='2017-06-01-preview'):
130+
cli_command(__name__, 'policy set-definition create', 'azure.cli.command_modules.resource.custom#create_policy_setdefinition')
131+
cli_command(__name__, 'policy set-definition delete', 'azure.mgmt.resource.policy.operations#PolicySetDefinitionsOperations.delete', cf_policy_set_definitions)
132+
cli_command(__name__, 'policy set-definition list', 'azure.mgmt.resource.policy.operations#PolicySetDefinitionsOperations.list', cf_policy_set_definitions)
133+
cli_command(__name__, 'policy set-definition show', 'azure.cli.command_modules.resource.custom#get_policy_setdefinition', exception_handler=empty_on_404)
134+
cli_command(__name__, 'policy set-definition update', 'azure.cli.command_modules.resource.custom#update_policy_setdefinition')
135+
128136
cli_command(__name__, 'lock create', 'azure.cli.command_modules.resource.custom#create_lock')
129137
cli_command(__name__, 'lock delete', 'azure.cli.command_modules.resource.custom#delete_lock')
130138
cli_command(__name__, 'lock list', 'azure.cli.command_modules.resource.custom#list_locks')

0 commit comments

Comments
 (0)