Bylaw

Gallery › BuiltInPolicyTest › Azure built-in policies

[Preview]: Append a tag and its value to resources - This built-in is created for versioning test.

Converted Policy Append Disabled Mode Indexed Version 1.0.0-preview Preview

Appends the specified tag and value when any resource which is missing this tag is created or updated. Does not modify the tags of resources created before this policy was applied until those resources are changed. Does not apply to resource groups. New 'modify' effect policies are available that support remediation of tags on existing resources (see https://aka.ms/modifydoc).

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONAddTag.json · 66 lines
{
  "properties": {
    "displayName": "[Preview]: Append a tag and its value to resources - This built-in is created for versioning test.",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Appends the specified tag and value when any resource which is missing this tag is created or updated. Does not modify the tags of resources created before this policy was applied until those resources are changed. Does not apply to resource groups. New 'modify' effect policies are available that support remediation of tags on existing resources (see https://aka.ms/modifydoc).",
    "metadata": {
      "version": "1.0.0-preview",
      "category": "BuiltInPolicyTest",
      "preview": true
    },
    "version": "1.0.0-preview",
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "Name of the tag, such as 'environment'"
        },
        "allowedValues": [
          "TestCategory",
          "ServiceID"
        ]
      },
      "tagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Value",
          "description": "Value of the tag, such as 'production'"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Append",
          "Disabled"
        ],
        "defaultValue": "Append"
      }
    },
    "policyRule": {
      "if": {
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "exists": "false"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": [
          {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "value": "[parameters('tagValue')]"
          }
        ]
      }
    },
    "versions": [
      "1.0.0-PREVIEW"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/125b7269-7ec9-47bf-8beb-8865b61c2c95",
  "name": "125b7269-7ec9-47bf-8beb-8865b61c2c95"
}
Bylaw125b7269-7ec9-47bf-8beb-8865b61c2c95.bylaw.ps1 · 42 lines
# Converted from AddTag.json by ConvertTo-Bylaw.
Policy '125b7269-7ec9-47bf-8beb-8865b61c2c95' {

    Properties @{
        DisplayName   = '[Preview]: Append a tag and its value to resources - This built-in is created for versioning test.'
        Description   = 'Appends the specified tag and value when any resource which is missing this tag is created or updated. Does not modify the tags of resources created before this policy was applied until those resources are changed. Does not apply to resource groups. New ''modify'' effect policies are available that support remediation of tags on existing resources (see https://aka.ms/modifydoc).'
        Version       = '1.0.0-preview'
        Category      = 'BuiltInPolicyTest'
        Preview       = $true
        PolicyVersion = '1.0.0-preview'
        Mode          = 'Indexed'
    }

    Parameter TagName @{
        Type          = 'String'
        AllowedValues = @('TestCategory', 'ServiceID')
        DisplayName   = 'Tag Name'
        Description   = 'Name of the tag, such as ''environment'''
    }
    Parameter TagValue @{
        Type        = 'String'
        DisplayName = 'Tag Value'
        Description = 'Value of the tag, such as ''production'''
    }
    Parameter Effect @{
        AllowedValues = @('Append', 'Disabled')
        Default       = 'Append'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource * {

        Require 'tag (Param TagName) exists' {
            Tag (Param TagName) | Must -Exist
        }
    }

    Append -Effect (Param Effect) {
        Tag (Param TagName) | Add (Param TagValue)
    }
}

Source: built-in-policies/policyDefinitions/BuiltInPolicyTest/AddTag.json in Azure/azure-policy, under the MIT licence.