Bylaw

Gallery › General › Azure built-in policies

Do not allow deletion of resource types

Converted Policy DenyAction Disabled Mode All Version 1.0.1

This policy enables you to specify the resource types that your organization can protect from accidentals deletion by blocking delete calls using deny action effect.

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

Azure Policy JSONResourceType_denyActionDelete.json · 55 lines
{
  "properties": {
    "displayName": "Do not allow deletion of resource types",
    "policyType": "BuiltIn",
    "mode": "All",
    "description": "This policy enables you to specify the resource types that your organization can protect from accidentals deletion by blocking delete calls using deny action effect.",
    "metadata": {
      "version": "1.0.1",
      "category": "General"
    },
    "version": "1.0.1",
    "parameters": {
      "listOfResourceTypesDisallowedForDeletion": {
        "type": "Array",
        "metadata": {
          "displayName": "Resource types that cannot be deleted",
          "description": "The list of resource types that cannot be deleted.",
          "strongType": "resourceTypes"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "DenyAction",
          "Disabled"
        ],
        "defaultValue": "DenyAction"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": "[parameters('listOfResourceTypesDisallowedForDeletion')]"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "actionNames": [
            "delete"
          ]
        }
      }
    },
    "versions": [
      "1.0.1",
      "1.0.0-PREVIEW"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/78460a36-508a-49a4-b2b2-2f5ec564f4bb",
  "name": "78460a36-508a-49a4-b2b2-2f5ec564f4bb"
}
Bylaw78460a36-508a-49a4-b2b2-2f5ec564f4bb.bylaw.ps1 · 34 lines
# Converted from ResourceType_denyActionDelete.json by ConvertTo-Bylaw.
Policy '78460a36-508a-49a4-b2b2-2f5ec564f4bb' {

    Properties @{
        DisplayName   = 'Do not allow deletion of resource types'
        Description   = 'This policy enables you to specify the resource types that your organization can protect from accidentals deletion by blocking delete calls using deny action effect.'
        Version       = '1.0.1'
        Category      = 'General'
        PolicyVersion = '1.0.1'
        Mode          = 'All'
    }

    Parameter ListOfResourceTypesDisallowedForDeletion @{
        Type        = 'Array'
        DisplayName = 'Resource types that cannot be deleted'
        Description = 'The list of resource types that cannot be deleted.'
        Metadata    = [ordered]@{ strongType = 'resourceTypes' }
    }
    Parameter Effect @{
        AllowedValues = @('DenyAction', 'Disabled')
        Default       = 'DenyAction'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource * {

        Require 'type is not one of the ListOfResourceTypesDisallowedForDeletion parameter' {
            Field type | Must -Not -BeIn (Param ListOfResourceTypesDisallowedForDeletion) -IfPresent
        }
    }

    DenyAction Delete -Effect (Param Effect)
}

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