Bylaw

Gallery › SQL Server › Azure Community Policy

Enable hybrid use for SQL IaaS

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode All Version 1.0.0

This Policy will enable HUB for any SQL IaaS enrolled in the SQL Resource Provider with the SKU of Standard or Enterprise.

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

Azure Policy JSONazurepolicy.json · 111 lines
{
  "name": "f37d820e-9feb-4b79-8eb5-fda7b0cf5e12",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Enable hybrid use for SQL IaaS",
    "description": "This Policy will enable HUB for any SQL IaaS enrolled in the SQL Resource Provider with the SKU of Standard or Enterprise.",
    "metadata": {
      "category": "SQL Server",
      "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.SqlVirtualMachine/SqlVirtualMachines"
          },
          {
            "anyOf": [
              {
                "field": "Microsoft.SqlVirtualMachine/SqlVirtualMachines/sqlImageSku",
                "equals": "Standard"
              },
              {
                "field": "Microsoft.SqlVirtualMachine/SqlVirtualMachines/sqlImageSku",
                "equals": "Enterprise"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.SqlVirtualMachine/sqlVirtualMachines",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/8e3af657-a8ff-443c-a75c-2fe8c4bcb635"
          ],
          "name": "[field('name')]",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.SqlVirtualMachine/SqlVirtualMachines/sqlServerLicenseType",
                "equals": "AHUB"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "sqlServerName": {
                    "type": "string"
                  },
                  "location": {
                    "type": "string"
                  },
                  "virtualMachineResourceId": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "name": "[parameters('sqlServerName')]",
                    "type": "Microsoft.SqlVirtualMachine/sqlVirtualMachines",
                    "apiVersion": "2017-03-01-preview",
                    "location": "[parameters('location')]",
                    "properties": {
                      "virtualMachineResourceId": "[parameters('virtualMachineResourceId')]",
                      "sqlServerLicenseType": "AHUB"
                    }
                  }
                ]
              },
              "parameters": {
                "sqlServerName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "virtualMachineResourceId": {
                  "value": "[field('Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId')]"
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylawf37d820e-9feb-4b79-8eb5-fda7b0cf5e12.bylaw.ps1 · 40 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'f37d820e-9feb-4b79-8eb5-fda7b0cf5e12' {

    Properties @{
        DisplayName = 'Enable hybrid use for SQL IaaS'
        Description = 'This Policy will enable HUB for any SQL IaaS enrolled in the SQL Resource Provider with the SKU of Standard or Enterprise.'
        Category    = 'SQL Server'
        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.SqlVirtualMachine/SqlVirtualMachines' {

        Require 'sqlImageSku is not Standard' {
            Field sqlImageSku | Must -Not -Be 'Standard' -IfPresent
        }

        Require 'sqlImageSku is not Enterprise' {
            Field sqlImageSku | Must -Not -Be 'Enterprise' -IfPresent
        }
    }

    DeployIfNotExists 'Microsoft.SqlVirtualMachine/sqlVirtualMachines' -Role 'Owner' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'sqlServerLicenseType is AHUB' {
            Field sqlServerLicenseType | Must -Be 'AHUB'
        }
        Deployment -TemplateFile './templates/f37d820e-9feb-4b79-8eb5-fda7b0cf5e12.json' -Parameters ([ordered]@{
                sqlServerName            = (Arm '[field(''name'')]')
                location                 = (Arm '[field(''location'')]')
                virtualMachineResourceId = (Arm '[field(''Microsoft.SqlVirtualMachine/SqlVirtualMachines/virtualMachineResourceId'')]')
            })
    }
}

Its deployment templates are written beside it:

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