Skip to content

Commit

Permalink
[ARM]Support policy exemption (#17565)
Browse files Browse the repository at this point in the history
* Support policy exemption

* Fixed style issues

* Fixed missing parameter help

* Addressed linter error and re-recorded tests

* Addressed comments

* Addressed comments

* fixed style issue

* Record sql test

* skip test_sql_midb_logreplay_mgmt

* Fixed style

* Addressed comments

* addressed comments
  • Loading branch information
robga committed Apr 9, 2021
1 parent 6c81f0d commit a3604d1
Show file tree
Hide file tree
Showing 37 changed files with 263,530 additions and 100,375 deletions.
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/profiles/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def default_api_version(self):
ResourceType.MGMT_RESOURCE_FEATURES: '2015-12-01',
ResourceType.MGMT_RESOURCE_LINKS: '2016-09-01',
ResourceType.MGMT_RESOURCE_LOCKS: '2016-09-01',
ResourceType.MGMT_RESOURCE_POLICY: '2019-09-01',
ResourceType.MGMT_RESOURCE_POLICY: '2020-09-01',
ResourceType.MGMT_RESOURCE_RESOURCES: '2020-10-01',
ResourceType.MGMT_RESOURCE_SUBSCRIPTIONS: '2019-11-01',
ResourceType.MGMT_RESOURCE_DEPLOYMENTSCRIPTS: '2020-10-01',
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def test_policy_insights_remediation_management_group(self):
@AllowLargeResponse()
def test_policy_insights_remediation_complete(self):
self.kwargs.update({
'pan': '09d18943ace14946aff83c21',
'pan': '98904c39668a4f70804aef09',
'rg': 'az-cli-policy-insights-test',
'rn': self.create_random_name('azurecli-test-remediation', 40)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def cf_policy_set_definitions(cli_ctx, _):
return _resource_policy_client_factory(cli_ctx).policy_set_definitions


def cf_policy_exemptions(cli_ctx, _):
return _resource_policy_client_factory(cli_ctx).policy_exemptions


def cf_management_locks(cli_ctx, _):
return _resource_lock_client_factory(cli_ctx).management_locks

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def get_policy_assignment_completion_list(cmd, prefix, namespace, **kwargs): #
return [i.name for i in result]


@Completer
def get_policy_exemption_completion_list(cmd, prefix, namespace, **kwargs): # pylint: disable=unused-argument
policy_client = _resource_policy_client_factory(cmd.cli_ctx)
result = policy_client.policy_exemptions.list()
return [i.name for i in result]


@Completer
def get_providers_completion_list(cmd, prefix, namespace, **kwargs): # pylint: disable=unused-argument
rcf = _resource_client_factory(cmd.cli_ctx)
Expand Down
73 changes: 73 additions & 0 deletions src/azure-cli/azure/cli/command_modules/resource/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,79 @@
--definition-groups "[{ \\"name\\": \\"CostSaving\\" }, { \\"name\\": \\"Organizational\\" } ]"
"""

helps['policy exemption'] = """
type: group
short-summary: Manage resource policy exemptions.
"""

helps['policy exemption create'] = """
type: command
short-summary: Create a policy exemption.
examples:
- name: Create a policy exemption in default subscription.
text: |
az policy exemption create -n exemptTestVM \\
--policy-assignment "/subscriptions/mySubId/providers/Microsoft.Authorization/policyAssignments/limitVMSku" \\
--exemption-category "Waiver"
- name: Create a policy exemption in the resource group.
text: |
az policy exemption create -n exemptTestVM \\
--policy-assignment "/subscriptions/mySubId/providers/Microsoft.Authorization/policyAssignments/limitVMSku" \\
--exemption-category "Waiver" \\
--resource-group "myResourceGroup"
- name: Create a policy exemption in a management group.
text: |
az policy exemption create -n exemptTestVM \\
--policy-assignment "/providers/Microsoft.Management/managementGroups/myMG/providers/Microsoft.Authorization/policyAssignments/limitVMSku" \\
--exemption-category "Waiver" \\
--scope "/providers/Microsoft.Management/managementGroups/myMG"
"""

helps['policy exemption delete'] = """
type: command
short-summary: Delete a policy exemption.
examples:
- name: Delete a policy exemption.
text: |
az policy exemption delete --name MyPolicyExemption --resource-group "myResourceGroup"
crafted: true
"""

helps['policy exemption list'] = """
type: command
short-summary: List policy exemptions.
"""

helps['policy exemption show'] = """
type: command
short-summary: Show a policy exemption.
examples:
- name: Show a policy exemption.
text: |
az policy exemption show --name MyPolicyExemption --resource-group "myResourceGroup"
crafted: true
"""

helps['policy exemption update'] = """
type: command
short-summary: Update a policy exemption.
examples:
- name: Update a policy exemption.
text: |
az policy exemption update -n exemptTestVM \\
--exemption-category "Mitigated"
- name: Update a policy exemption in the resource group.
text: |
az policy exemption update -n exemptTestVM \\
--exemption-category "Mitigated" \\
--resource-group "myResourceGroup"
- name: Update a policy exemption in a management group.
text: |
az policy exemption update -n exemptTestVM \\
--exemption-category "Mitigated" \\
--scope "/providers/Microsoft.Management/managementGroups/myMG"
"""

helps['provider'] = """
type: group
short-summary: Manage resource providers.
Expand Down
23 changes: 20 additions & 3 deletions src/azure-cli/azure/cli/command_modules/resource/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def load_arguments(self, _):

from azure.mgmt.resource.locks.models import LockLevel
from azure.mgmt.resource.managedapplications.models import ApplicationLockLevel
from azure.mgmt.resource.policy.models import (ExemptionCategory, EnforcementMode)

from azure.cli.core.api import get_subscription_id_list
from azure.cli.core.commands.parameters import (
Expand All @@ -22,7 +23,7 @@ def load_arguments(self, _):
from knack.arguments import ignore_type, CLIArgumentType

from azure.cli.command_modules.resource._completers import (
get_policy_completion_list, get_policy_set_completion_list, get_policy_assignment_completion_list,
get_policy_completion_list, get_policy_set_completion_list, get_policy_assignment_completion_list, get_policy_exemption_completion_list,
get_resource_types_completion_list, get_providers_completion_list)
from azure.cli.command_modules.resource._validators import (
validate_lock_parameters, validate_resource_lock, validate_group_lock, validate_subscription_lock, validate_metadata, RollbackAction,
Expand Down Expand Up @@ -188,7 +189,7 @@ def load_arguments(self, _):

with self.argument_context('policy assignment create', resource_type=ResourceType.MGMT_RESOURCE_POLICY, min_api='2017-06-01-preview') as c:
c.argument('policy_set_definition', options_list=['--policy-set-definition', '-d'], help='Name or id of the policy set definition.')
c.argument('sku', options_list=['--sku', '-s'], help='policy sku.', arg_type=get_enum_type(['free', 'standard']))
c.argument('sku', options_list=['--sku', '-s'], help='policy sku.', arg_type=get_enum_type(['free', 'standard']), deprecate_info=c.deprecate(hide=True))
c.argument('notscopes', options_list='--not-scopes', nargs='+')

with self.argument_context('policy assignment create', resource_type=ResourceType.MGMT_RESOURCE_POLICY, min_api='2018-05-01') as c:
Expand All @@ -200,7 +201,7 @@ def load_arguments(self, _):
c.argument('identity_role', arg_type=identity_role_type)

with self.argument_context('policy assignment create', resource_type=ResourceType.MGMT_RESOURCE_POLICY, min_api='2019-06-01') as c:
c.argument('enforcement_mode', options_list=['--enforcement-mode', '-e'], help='Enforcement mode of the policy assignment, e.g. Default, DoNotEnforce. Please visit https://aka.ms/azure-policyAssignment-enforcement-mode for more information.', arg_type=get_enum_type(['Default', 'DoNotEnforce']))
c.argument('enforcement_mode', options_list=['--enforcement-mode', '-e'], help='Enforcement mode of the policy assignment, e.g. Default, DoNotEnforce. Please visit https://aka.ms/azure-policyAssignment-enforcement-mode for more information.', arg_type=get_enum_type(EnforcementMode))

with self.argument_context('policy assignment identity', resource_type=ResourceType.MGMT_RESOURCE_POLICY, min_api='2018-05-01') as c:
c.argument('identity_scope', arg_type=identity_scope_type)
Expand All @@ -221,6 +222,22 @@ def load_arguments(self, _):
with self.argument_context('policy set-definition create', min_api='2017-06-01-preview', resource_type=ResourceType.MGMT_RESOURCE_POLICY) as c:
c.argument('name', options_list=['--name', '-n'], help='Name of the new policy set definition.')

with self.argument_context('policy exemption', min_api='2020-09-01', resource_type=ResourceType.MGMT_RESOURCE_POLICY) as c:
c.ignore('_subscription')
c.argument('name', options_list=['--name', '-n'], completer=get_policy_exemption_completion_list, help='Name of the policy exemption.')
c.argument('scope', help='Scope to which this policy exemption applies.')
c.argument('disable_scope_strict_match', options_list=['--disable-scope-strict-match', '-i'], action='store_true', help='Include policy exemptions either inherited from parent scope or at child scope.')
c.argument('display_name', help='Display name of the policy exemption.')
c.argument('description', help='Description of policy exemption.')
c.argument('exemption_category', options_list=['--exemption-category', '-e'], help='The policy exemption category of the policy exemption', arg_type=get_enum_type(ExemptionCategory))
c.argument('policy_definition_reference_ids', nargs='+', options_list=['--policy-definition-reference-ids', '-r'], help='The policy definition reference ids to exempt in the initiative (policy set).')
c.argument('expires_on', help='The expiration date and time (in UTC ISO 8601 format yyyy-MM-ddTHH:mm:ssZ) of the policy exemption.')
c.argument('metadata', nargs='+', validator=validate_metadata, help='Metadata in space-separated key=value pairs.')

with self.argument_context('policy exemption create', min_api='2020-09-01', resource_type=ResourceType.MGMT_RESOURCE_POLICY) as c:
c.argument('name', options_list=['--name', '-n'], help='Name of the new policy exemption.')
c.argument('policy_assignment', options_list=['--policy-assignment', '-a'], help='The referenced policy assignment Id for the policy exemption.')

with self.argument_context('group') as c:
c.argument('tag', tag_type)
c.argument('tags', tags_type)
Expand Down
15 changes: 14 additions & 1 deletion src/azure-cli/azure/cli/command_modules/resource/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from azure.cli.core.commands.arm import handle_template_based_exception
from azure.cli.command_modules.resource._client_factory import (
cf_resource_groups, cf_providers, cf_features, cf_tags, cf_deployments,
cf_deployment_operations, cf_policy_definitions, cf_policy_set_definitions, cf_resource_links,
cf_deployment_operations, cf_policy_definitions, cf_policy_set_definitions, cf_policy_exemptions, cf_resource_links,
cf_resource_deploymentscripts, cf_resource_managedapplications, cf_resource_managedappdefinitions, cf_management_groups, cf_management_group_subscriptions, cf_resource_templatespecs)
from azure.cli.command_modules.resource._validators import process_deployment_create_namespace, process_ts_create_or_update_namespace, _validate_template_spec, _validate_template_spec_out

Expand Down Expand Up @@ -112,6 +112,12 @@ def load_command_table(self, _):
resource_type=ResourceType.MGMT_RESOURCE_POLICY
)

resource_policy_exemptions_sdk = CliCommandType(
operations_tmpl='azure.mgmt.resource.policy.operations#PolicyExemptionsOperations.{}',
client_factory=cf_policy_exemptions,
resource_type=ResourceType.MGMT_RESOURCE_POLICY
)

resource_lock_sdk = CliCommandType(
operations_tmpl='azure.mgmt.resource.locks.operations#ManagementLocksOperations.{}',
resource_type=ResourceType.MGMT_RESOURCE_LOCKS
Expand Down Expand Up @@ -387,6 +393,13 @@ def load_command_table(self, _):
g.custom_show_command('show', 'get_policy_setdefinition')
g.custom_command('update', 'update_policy_setdefinition')

with self.command_group('policy exemption', resource_policy_exemptions_sdk, is_preview=True, resource_type=ResourceType.MGMT_RESOURCE_POLICY, min_api='2020-09-01') as g:
g.custom_command('create', 'create_policy_exemption')
g.custom_command('delete', 'delete_policy_exemption')
g.custom_command('list', 'list_policy_exemption')
g.custom_show_command('show', 'get_policy_exemption')
g.custom_command('update', 'update_policy_exemption')

with self.command_group('lock', resource_type=ResourceType.MGMT_RESOURCE_LOCKS) as g:
g.custom_command('create', 'create_lock')
g.custom_command('delete', 'delete_lock')
Expand Down
Loading

0 comments on commit a3604d1

Please sign in to comment.