azure-policy icon indicating copy to clipboard operation
azure-policy copied to clipboard

"Alias request: "Microsoft.Network/virtualNetworks/resourceGroups"

Open jayaakshayan opened this issue 7 years ago • 3 comments

Scenario

What are you trying to achieve using Azure Policy?

Allow VNET creation only from particular resource group

Example policy definition

Please include a sample policy definition that shows how you would like to use this new alias. { "mode": "All", "policyRule": { "if": { "allOf": [ { "field": "type", "equals": "Microsoft.Network/virtualNetworks" }, { "field": "Microsoft.Network/virtualNetworks/resourceGroups", "notcontains": "[concat('resourceGroups/',parameters('resourceGroupName'))]" } ] }, "then": { "effect": "deny" } }, "parameters": { "resourceGroupName": { "type": "String", "metadata": { "displayName": "Resource Group Name", "description": null, "strongType": "ExistingResourceGroups" } } } }

jayaakshayan avatar Apr 24 '19 08:04 jayaakshayan

@jayaakshayan

There is a policy function that will return the resource group. Below is the section of your policy definition that I would recommend replacing with the example below that uses the "resourcegroup().name) function:

Instead of this:


"field": "Microsoft.Network/virtualNetworks/resourceGroups",

"notcontains": "[concat('resourceGroups/',parameters('resourceGroupName'))]"


Try this:

"value": "[resourceGroup().name]",

"notEquals": "[parameters('resourceGroupName')]"

Here are some links on the documentation regarding this. Do a search for "resourcegroup" on these pages and you can see more examples: https://docs.microsoft.com/en-us/azure/governance/policy/concepts/definition-structure#policy-function-example

https://docs.microsoft.com/en-us/azure/governance/policy/concepts/definition-structure#value-examples

mylameaccount avatar Apr 25 '19 05:04 mylameaccount

The full set of aliases for Microsoft.Network/virtualNetworks has been published. That does not include an alias for ResourceGroups which isn't a payload field that can have policies applied. The suggestion above by @mylameaccount is a good approach for that scenario.

mentat9 avatar May 03 '19 16:05 mentat9

Thanks Lame & Mentat, I have used the same approach and achieved what i required.

"value": "[resourceGroup().name]",

"notEquals": "[parameters('resourceGroupName')]"

jayaakshayan avatar May 04 '19 05:05 jayaakshayan