How to deny the use of dashes in wildcard for "like" condition for naming convention Azure policy

Kateryna Balaban 0 Reputation points
2023-03-30T14:33:37.42+00:00

The pattern for naming convention is:

xx-yy-zz* in lowercase where:
xx* is the resource type, for example - rg

yy* is the cost center: cc and then goes 4 numbers

zz* is the project, application or service can include the version, the env, the location if you need multiple resource groups: just random letters and numbers
The most interesting part: we cannot use dash inside xx*, yy* and zz*.

I tried many different options.
Finally I used "like" condition:
"like": "[concat(parameters('namingConvention'), parameters('allowedNumbers'), '-*')]"

For naming convention I have set the default value : "rg-cc". Then I created an array with all allowed numbers that come after cc. And then I put the wildcard.

Everything works fine except for the fact that I can still use dashes in that wildcard.
So my question is how to dissallow the use of dashes in this wildcard

Thank you,
Kateryna

Azure Policy
Azure Policy
An Azure service that is used to implement corporate governance and standards at scale for Azure resources.
1,019 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2023-04-04T03:29:53.59+00:00

    Hi @Kateryna Balaban I didn't find a specific examples but if you tried pattern matching and it didn't work, try using a regular expression. If that doesn't work let me know.

    {
        "if": {
            "allOf": [
                {
                    "field": "type",
                    "equals": "Microsoft.Resources/subscriptions/resourceGroups"
                },
                {
                    "not": {
                        "field": "name",
                        "match": "[concat(parameters('namingConvention'), parameters('allowedNumbers'),'^[a-zA-Z0-9_]*$'"]
                    }
                }
            ]
        },
        "then": {
            "effect": "deny"
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.