Introducing a new way to review your PRs.

Read the announcement

azure / azure-cli

Azure Command-Line Interface

View on GitHub

13 documents · Updated Nov 16, 2026

docs / resource-role-policy-commands

Resource, Role, Identity, Policy, Managed Services, and Marketplace Ordering Commands

Overview

This doc covers the Azure CLI management command modules under src/azure-cli/azure/cli/command_modules/resource/**, role/**, identity/**, policyinsights/**, managedservices/**, and marketplaceordering/**, plus the managed identity CLI interface guideline in doc/managed_identity_command_guideline.md. Routing note: changes in these paths are owned here when they affect command registration, argument shaping, SDK client selection, scope/resource-ID handling, ARM/RBAC/policy/identity custom logic, or the marketplace terms flow.

The covered modules all follow the Azure CLI command-loader pattern: each module's __init__.py defines an AzCommandsLoader, imports help, sets a default CliCommandType when needed, and delegates command registration to commands.py plus argument registration to _params.py. resource, identity, policyinsights, and managedservices also call azure.cli.core.aaz.load_aaz_command_table before loading hand-written command table entries, so generated AAZ commands and custom overrides coexist in the same namespace (src/…/__init__.py, identity/__init__.py, policyinsights/__init__.py, managedservices/__init__.py). marketplaceordering is the older generated/manual split: src/…/__init__.py loads generated.commands, then optional manual.commands, and merges custom implementations through marketplaceordering/custom.py.

src/…/commands.py is the broad ARM surface. It registers resource groups, generic resources, providers/features, tags, deployments at group/subscription/management-group/tenant scope, deployment stacks and stack what-if, locks, resource links, managed applications, management groups, Bicep, and Resource Management private links. Most command groups point at SDK operation groups from resource/_client_factory.py; custom flows live in resource/custom.py. ARM template deployment logic parses JSON/JSONC, Bicep, .bicepparam, template specs, URI templates, missing parameters, what-if formatting, and validation in resource/custom.py, with validators and default deployment names in resource/_validators.py. Generic resource, provider registration, tag, lock, management group, managed application, deployment stack, and Bicep command handlers are also concentrated in resource/custom.py.

Azure Policy CRUD commands are registered from resource/commands.py but implemented as wrapper classes in resource/policy.py over AAZ-generated command classes under resource/aaz/latest/policy/**. resource/policy.py preserves legacy behavior by overriding generated argument schemas and pre/post operations: it fills default scopes, resolves policy or policy set definition names to IDs, generates assignment/exemption names, normalizes --not-scopes, supports legacy key/value shorthand for AAZAnyTypeArg, injects version into policy definition/set-definition version request bodies, resolves user-assigned identity IDs by calling resource.custom._get_resource_id, and creates role assignments for policy assignment system identities through azure.cli.core.commands.arm.assign_identity.

src/…/commands.py owns RBAC and Microsoft Graph-backed az ad commands. RBAC commands use role/custom.py with authorization clients from role/_client_factory.py; Graph commands share a GraphClient from role/_msgrpah via the public graph_client_factory exported by role/__init__.py. role/custom.py handles role definitions, role assignments, assignment change logs, deny assignments, Graph applications/service principals/users/groups, credentials, app permissions, federated credentials, and ad sp create-for-rbac. It adapts newer TypeSpec azure-mgmt-authorization model shapes back to the legacy flat camelCase dictionaries with _role_assignment_to_dict and _deny_assignment_to_dict, resolves principals through Microsoft Graph unless --assignee-object-id is used, and preserves idempotency for role assignment create conflicts and several Graph owner/add flows.

src/azure-cli/azure/cli/command_modules/identity is the user-assigned managed identity module. identity/__init__.py loads generated AAZ commands for core identity create/update/list/delete/show and identity federated-credential operations; identity/commands.py only adds identity list-resources and identity list-operations. The custom identity/custom.py list_identity_resources handler deliberately uses _msi_list_resources_client from identity/_client_factory.py, which pins MSI API version 2022-01-31-preview because the newer 2023-01-31 API removed listAssociatedResources. identity/_params.py adds CLI-facing --isolation-scope and tag/location arguments for generated create/update commands. doc/managed_identity_command_guideline.md is the cross-module CLI contract: resource create commands should expose --mi-system-assigned/--mi-user-assigned, while existing-resource identity subgroups should use identity assign/remove/show with --system-assigned and --user-assigned.

src/azure-cli/azure/cli/command_modules/policyinsights handles policy events, states, remediations, metadata, and attestations. policyinsights/commands.py maps events/states/remediations/metadata to PolicyInsightsClient operation groups from policyinsights/_client_factory.py; attestation create/update/delete/show are custom dispatchers because generated AAZ commands are split by resource, resource-group, and subscription scope. policyinsights/custom.py builds QueryOptions, derives scope-specific resource IDs from management group, subscription, resource group, or resource arguments, uses sdk_no_wait for trigger scan, resolves remediation policy assignment names to full IDs through Resource Policy policy_assignments, and invokes AAZ attestation classes under policyinsights/aaz/latest/policy_insights/attestation/**. policyinsights/_params.py shares resource ID arguments and policy completers from the resource module and validates non-ID resource names with policyinsights/_validators.py.

src/azure-cli/azure/cli/command_modules/managedservices wraps generated AAZ managed services registration definitions and assignments. managedservices/commands.py installs custom wrapper classes from managedservices/custom.py directly into self.command_table. Those classes hide the generated raw scope argument, compute subscription or resource-group scopes, generate definition/assignment GUIDs when omitted, convert definition/assignment IDs to (scope, name), and simplify definition create authorization input to --principal-id plus --role-definition-id. The module has no meaningful hand-written argument registration (managedservices/_params.py is a no-op) because argument surgery is done in the wrapper classes' _build_arguments_schema methods.

src/azure-cli/azure/cli/command_modules/marketplaceordering exposes experimental az term show and az term accept for VM marketplace terms. marketplaceordering/…/commands.py registers both commands against the marketplace agreements operation group from generated/_client_factory.py. generated/custom.py implements term_show as a fixed offer_type="virtualmachine" client.get; manual/custom.py implements term_accept by first fetching the offer, copying the links/signature/retrieve time into a dict, setting accepted=True, and calling client.create. Parameters are simple publisher/product/plan strings in generated/_params.py.

Coupling

  • If generated AAZ policy command signatures or payload schemas under src/azure-cli/azure/cli/command_modules/resource/aaz/latest/policy/** change, verify src/…/policy.py; it overrides generated schemas, pre-operations, serialization, nullable settings, and body version injection rather than relying on generated defaults.
  • If role assignment or deny assignment SDK models change, verify src/…/custom.py adapters and table transformers in src/…/commands.py; downstream output assumes the legacy flat camelCase keys such as roleDefinitionId, principalName, denyAssignmentName, and scope.
  • If a command can avoid Graph lookup by accepting object IDs, keep src/…/_params.py and role/custom.py behavior aligned: --assignee-object-id/--assignee-principal-type is the documented path when Microsoft Graph is unavailable or unauthorized.
  • If managed identity syntax is added to resource modules outside identity, check doc/managed_identity_command_guideline.md and src/…/_validators.py; policy assignment identity validation still distinguishes legacy --assign-identity from --mi-system-assigned/--mi-user-assigned and enforces that --identity-scope/--role only apply to system identities.
  • If policy insights resource-scope arguments change, update both src/…/_params.py and policyinsights/custom.py; validators only require namespace/type for non-ID --resource, while the custom handlers build final ARM IDs and strip leading/trailing slashes before remediation calls.
  • If MSI API versions are updated, verify src/…/_client_factory.py; identity list-resources intentionally pins 2022-01-31-preview because the newer MSI API no longer supports listAssociatedResources.
  • If marketplace term accept payload shape changes, update src/…/custom.py along with generated/custom.py; term_accept depends on term_show-style client.get returning license/privacy/marketplace links, retrieve time, and signature before sending accepted=True.
  • If generated managed services definitions/assignments change argument names, check src/…/custom.py; it hides and rewrites generated scope, definition, and assignment args directly in _build_arguments_schema and pre_operations.

Risks

  • src/…/custom.py mutates the Azure SDK HTTP pipeline with JsonCTemplatePolicy for local template deployments. If this policy is not applied when template_link is absent, large or whitespace-sensitive ARM/Bicep templates can be serialized as escaped strings and the service may fail deserialization; the debug log can also misrepresent the actual bytes sent.
  • src/…/custom.py has high recent churn around deployment stacks and stack what-if (for example fixes for stack what-if pretty-printing and property body rendering in recent ARM commits). If stack what-if result models or path rendering change, DeploymentStacksWhatIfResultFormatter usage in stack what-if handlers can regress user-visible output even when service calls succeed.
  • src/…/custom.py depends on explicit adapters for TypeSpec authorization SDK models. If new RBAC fields are added but not copied into _RA_FLAT_KEYS, _DA_FLAT_KEYS, or the deny-assignment nested converters, CLI JSON/table output and update round-trips can silently omit them.
  • src/…/custom.py create_service_principal_for_rbac retries Graph service principal creation and role assignment creation to ride out directory replication. If retryable error text changes, the command can fail before replication completes, leaving an application or credential created without the intended role assignment.
  • src/…/custom.py resolves non-ID remediation policy assignments by listing subscription-level policy assignments by name. If multiple assignments share a name or the target is outside that list's scope, remediation creation fails or requires a full assignment ID.