微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Azure中强制实施标记值的整数

如何解决如何在Azure中强制实施标记值的整数

我有Azure策略,该策略拒绝评估以下错误的表达式。我认为问题是,当您在门户网站中提供标记值时,即使打算传递整数值,它们也会作为字符串传递给ARM。试图弄清楚如何对标记强制使用整数值

{
 "field": "tags['Longevity']","less": 1
}

错误

"The policy assignment 'd9c1d0b06de841559a1cbafe' associated with the policy deFinition 'dee67dc2-7393-4c02-916f-92511146c970' Could not be evaluated. 
A 'less' or 'lessOrEquals' or 'greater' or 'greaterOrEquals' expression expects operands of same type for comparison. The supported types are string,integer,float,ISO 8601 datetime. Please either fix the 
policy or remove the policy assignment to unblock. See https://aka.ms/policy-conditions for usage details."

enter image description here

解决方法

Microsoft支持提供了答案 答案是下面的预期的工作。必须使用concat()函数看起来要避免抱怨方括号内的方括号

{
 "value": "[int(field(concat('tags[','Longevity',']')))]","less": 0
},
,

我还可以通过在政策中提供以下格式(即“ less”:1)来重现错误。

"field": "[concat('tags[',parameters('tagName'),']')]","less": 1

enter image description here

我看到标记的基础系统类型是“ System.Collections.Generic.Dictionary`2 + ValueCollection [System.String,System.String] ”,所以是的,您的假设是正确的,即,当您在门户网站中提供标记值时,即使打算传递整数值,它们也会作为字符串传递给ARM。

我已经尝试按照以下策略对标记值强制使用整数(作为字符串),并且我认为它可以工作(仅适用于正数),因为如果我提供了

  • 以下策略的第26行中的“ 1”,然后如果我尝试创建资源(例如Azure VM),则在提供的标记值为0的情况下通过了验证,并且在所有其他情况下验证均失败。
  • 以下策略的第26行中的“ li”为“ 2”,然后如果我尝试创建资源(例如Azure VM),则在提供的标记值在-1和1之间且在所有其他情况下,验证均失败的情况下,验证通过。 >
  • ...
  • ...
  • ...

因此,我相信,尽管标签的基础系统类型是String,但如果我们以condition的形式提供“ less”,则后端将进行数字字符串比较。

{
  "properties": {
    "displayName": "Require a tag and its value on resources Test","policyType": "Custom","mode": "Indexed","metadata": {
      "version": "1.0.1","createdBy": "xxxxxxxxxxxxxxxxxxxxxxx","createdOn": "2020-10-22T07:58:29.0108355Z","updatedBy": "xxxxxxxxxxxxxxxxxxxxxxx","updatedOn": "2020-10-22T10:45:42.4517009Z"
    },"parameters": {
      "tagName": {
        "type": "String","metadata": {
          "displayName": "Tag Name","description": "Name of the tag,such as 'environment'"
        }
      }
    },"policyRule": {
      "if": {
        "not": {
          "field": "[concat('tags[',"less": "1"
        }
      },"then": {
        "effect": "deny"
      }
    }
  },"id": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxx/providers/Microsoft.Authorization/policyDefinitions/xxxxxxxxxxxxxxxxxxxxxxx","type": "Microsoft.Authorization/policyDefinitions","name": "xxxxxxxxxxxxxxxxxxxxxxx"
}

但是,如果有兴趣,可以在this Uservoice或反馈论坛中提出功能请求,以允许使用非字符串类型的标记值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。