Bylaw

Gallery › SQL › Azure Community Policy

Deploy hybrid use for SQL PaaS

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This Policy will enable HUB for Azure SQL. It does not work for Elastic Pools or SQL MI.

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

Azure Policy JSONazurepolicy.json · 116 lines
{
  "name": "ef304f71-9431-4a7d-86b5-e318b0262e4e",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Deploy hybrid use for SQL PaaS",
    "description": "This Policy will enable HUB for Azure SQL. It does not work for Elastic Pools or SQL MI.",
    "metadata": {
      "category": "SQL",
      "version": "1.0.0"
    },
    "mode": "All",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Sql/servers/databases"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.Sql/servers/databases/currentSku.tier",
                "equals": "GeneralPurpose"
              },
              {
                "field": "Microsoft.Sql/servers/databases/currentSku.tier",
                "equals": "Hyperscale"
              },
              {
                "field": "Microsoft.Sql/servers/databases/currentSku.tier",
                "equals": "BusinessCritical"
              }
            ]
          },
          {
            "field": "Microsoft.Sql/servers/databases/currentSku.name",
            "notContains": "GP_S"
          },
          {
            "field": "Microsoft.Sql/servers/databases/currentSku.name",
            "notEquals": "ElasticPool"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Sql/servers/databases",
          "name": "[field('fullname')]",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Sql/servers/databases/licenseType",
                "equals": "BasePrice"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "sqlServerDatabaseName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "name": "[parameters('sqlServerDatabaseName')]",
                    "type": "Microsoft.Sql/servers/databases",
                    "apiVersion": "2017-10-01-preview",
                    "location": "[parameters('location')]",
                    "properties": {
                      "licenseType": "BasePrice"
                    }
                  }
                ]
              },
              "parameters": {
                "sqlServerDatabaseName": {
                  "value": "[field('fullname')]"
                },
                "location": {
                  "value": "[field('location')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylawef304f71-9431-4a7d-86b5-e318b0262e4e.bylaw.ps1 · 44 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'ef304f71-9431-4a7d-86b5-e318b0262e4e' {

    Properties @{
        DisplayName = 'Deploy hybrid use for SQL PaaS'
        Description = 'This Policy will enable HUB for Azure SQL. It does not work for Elastic Pools or SQL MI.'
        Category    = 'SQL'
        Version     = '1.0.0'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Sql/servers/databases' {

        When {
            AnyOf {
                Field currentSku.tier | Must -Be 'GeneralPurpose'
                Field currentSku.tier | Must -Be 'Hyperscale'
                Field currentSku.tier | Must -Be 'BusinessCritical'
            }
            Field currentSku.name | Must -Not -ContainText 'GP_S'
        }

        Require 'currentSku.name is ElasticPool' {
            Field currentSku.name | Must -Be 'ElasticPool'
        }
    }

    DeployIfNotExists 'Microsoft.Sql/servers/databases' -Role 'Owner' -Name (Arm '[field(''fullname'')]') -Effect (Param Effect) {
        Require 'licenseType is BasePrice' {
            Field licenseType | Must -Be 'BasePrice'
        }
        Deployment -TemplateFile './templates/ef304f71-9431-4a7d-86b5-e318b0262e4e.json' -Parameters ([ordered]@{
                sqlServerDatabaseName = (Arm '[field(''fullname'')]')
                location              = (Arm '[field(''location'')]')
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/SQL/deploy-hybrid-use-for-sql-paas/azurepolicy.json in Azure/Community-Policy, under the MIT licence.