【问题标题】:Policy AllowedValues for the SamplingPercentage property of an ApplicationInsights resourceApplicationInsights 资源的 SamplingPercentage 属性的策略 AllowedValues
【发布时间】:2020-04-27 14:05:39
【问题描述】:

我正在尝试构建一个 Azure 策略,它将审核我们的 Azure 租户中的 ApplicationInsights 资源,该资源的 SamplingPercentage 值大于参数化值。目前,参数设置为“Float”类型(因为您可以指定33.312.58.3):

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [{
        "value": "type",
        "equals": "Microsoft.Insights/components"
      },
      {
        "value": "SamplingPercentage",
        "greater": "[parameters('Maximum')]"
      }]      
    },
    "then": {
      "effect": "audit"
    }
  },
  "parameters": {
    "Maximum": {
      "type": "Float",
      "metadata": {
        "displayName": "Maximum",
        "description": "Sets the maximum allowed sampling percentage."
      }
    }
  }
}

通过 Azure 门户设置 SamplingPercentage 时,您会看到一个有效选项列表。我想将这些包含在 Maximum 参数的 AllowedValues 属性中。

第一个想法是使用 strongType(我假设它会提示 Azure 门户根据类型注入允许的值)...我一直无法找到这样的值。第二次尝试失败,因为它似乎无法创建整数的Array。当我将Maximum 参数的Type 更改为String 并声明参数时,第三次尝试失败:

"parameters": {
   "Maximum": {
     "type": "Array",
     "metadata": {
       "displayName": "Maximum",
       "description": "Sets the maximum allowed sampling percentage."
     },
     "allowedValues": [ "100", "50", "33.3", "25", "12.5", "8.3", "4", "2", "1" ]
   }
 }

以及相关的条件

{
   "value": "SamplingPercentage",
   "greater": "[float(parameters('Maximum'))]"
}

导致此错误的原因:

使用无效参数调用了内部异常“模板语言函数“float”。该值无法转换为目标类型。'

我做错了什么?

【问题讨论】:

    标签: azure azure-policy


    【解决方案1】:

    以下策略规则将审核 SamplingPercentage 大于参数化值的 ApplicationInsights 资源:

    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Insights/components"
          },
          {
            "value": "[float(field('Microsoft.Insights/components/SamplingPercentage'))]",
            "greater": "[float(parameters('Maximum'))]"
          }
        ]
      },
      "then": {
        "effect": "audit"
      }
    },
    "parameters": {
      "Maximum": {
        "type": "String",
        "metadata": {
          "displayName": "Maximum",
          "description": "Sets the maximum allowed sampling percentage."
        },
        "allowedValues": [
          "100",
          "50",
          "33.3",
          "25",
          "12.5",
          "8.3",
          "4",
          "2",
          "1"
        ]
      }
    }
    

    您的政策出了什么问题:

    1. 参数Maximum的类型应该是String,而不是Array
      • allowedValues 始终是一个数组。参数的类型就是要赋值的类型
    2. 无法直接访问属性(如 SamplingType)。您必须使用 aliases (Microsoft.Insights/components/SamplingPercentage)。
    3. condition 中,"value" 将评估文字值。使用 "field"(或 field('alias') 函数)评估请求负载中的属性。

    【讨论】:

      猜你喜欢
      • 2021-08-01
      • 2017-03-06
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 2020-01-26
      • 2020-05-07
      • 1970-01-01
      • 2020-09-04
      相关资源
      最近更新 更多