【问题标题】:ARM Template API Management Deploy PolicyContentARM 模板 API 管理部署策略内容
【发布时间】:2018-02-17 19:54:14
【问题描述】:

我正在尝试使用 ARM 模板来部署我的 API 管理服务,并让除 policyContent 之外的所有内容都正常工作。基本上它希望 policyContent 为“Json 转义了策略的 Xml 编码内容”。这很难维护,并且试图找到一种方法来获取 XML 文件并将内容注入到这个字符串中,或​​者一些更好的方法。我希望不必编写程序来维护这些字符串,因为感觉不应该那么复杂。

Policy Reference

Example with string

{ "name": "policy", "type": "Microsoft.ApiManagement/service/apis/policies", "apiVersion": "2017-03-01", "properties": { "policyContent": "string" } }

【问题讨论】:

    标签: azure azure-resource-manager azure-api-management azure-resource-group


    【解决方案1】:

    您可以将您的策略​​维护到一个 XML 文件中并像这样引用它:

    {
        "apiVersion": "2018-01-01",
        "name": "policy",
        "type": "Microsoft.ApiManagement/service/policies",
        "properties": {
            "policyContent": "[concat(parameters('repoBaseUrl'), '/policy.xml')]",
            "contentFormat": "rawxml-link"
        },
        "dependsOn": [
            "[resourceId('Microsoft.ApiManagement/service/', parameters('ApimServiceName'))]"
        ]
    }
    

    您的policy.xml 文件必须在线可用,并且如下所示:

    <policies>
        <inbound>
            <rate-limit calls="3" renewal-period="10" />
            <base />
        </inbound>
        <outbound>
            <base />
        </outbound>
        <backend>
            <base />
        </backend>
        <on-error>
            <base />
        </on-error>
    </policies>
    

    【讨论】:

    【解决方案2】:

    好吧,我唯一能想到的(因为 arm 模板中没有任何原生模板可以帮助您)是从文件中读取输入并将其转换为 JSON:

    $xml = (Get-Content file -Raw).ToString()
    ($xml | ConvertTo-Json -Compress) -replace '\\u003c','<' ) -replace '\\u003e','>'
    

    不知道将那些 unicode 替换回 &lt;&gt; 也可以工作。

    【讨论】:

    • 是的,我写了一个 powershell 来做类似的事情。没有一种正确加载 xml 的好方法感觉很糟糕。不过还是谢谢。
    • 我希望有人反驳我,但恐怕现在就是这样:(
    • 我们同意这是丑陋而痛苦的。但是,我们需要在 ARM 模板仅是 JSON 的约束中生存。我们正处于编写工具的计划阶段,这将使从 OpenAPI 描述和策略文档创建部署模板变得更加容易。请多多包涵,我们正在努力让它变得更好。
    • 那个工具是二头肌@DarrelMiller?
    • 没有。据我所知,当时 BICEP 还不是一回事。我不知道计划发生了什么,因为我在发表评论后不久就离开了团队。
    猜你喜欢
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 2021-02-01
    • 2020-02-06
    相关资源
    最近更新 更多