4

I am trying to use ARM templates to deploy my API management service and have everything working except policyContent. Basically it wants the policyContent as "Json escaped Xml Encoded contents of the Policy." This is very hard to maintain and was trying to find a way to take an XML file and inject the contents into this string, or some better way. I would like not to have to write a program to maintain these strings because it feels like something that shouldn't be so complicated.

Policy Reference

Example with string

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

Tim Scriv
  • 670
  • 7
  • 17

2 Answers2

5

You can maintain your policies into an XML file and reference it like this:

{
    "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'))]"
    ]
}

Your policy.xml file must be available online and will look like this:

<policies>
    <inbound>
        <rate-limit calls="3" renewal-period="10" />
        <base />
    </inbound>
    <outbound>
        <base />
    </outbound>
    <backend>
        <base />
    </backend>
    <on-error>
        <base />
    </on-error>
</policies>
Sacha Bruttin
  • 4,083
  • 2
  • 21
  • 17
  • This looks perfect. I found the documentation for this here:https://learn.microsoft.com/en-nz/azure/templates/microsoft.apimanagement/service/products/policies. – Tim Scriv Jun 26 '18 at 13:07
  • Is the Bicep somehow required for this to work? I do not find this clear from the docs. Is there something specific that I have to do to connect to the repo this way to fetch policy files? I can see you use a parameter from a parameters file. – Karol Haliński Aug 23 '23 at 13:08
1

Well, the only thing I can think of (because nothing native in arm templates can help you) is read the input from a file and convert it to JSON:

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

It might work without replacing those unicodes back to <>, no idea.

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Yeah I wrote a powershell to do something similar to this. It just feels so hacky to not have a good way to load the xml properly. Thanks though. – Tim Scriv Feb 17 '18 at 21:29
  • I would love someone disprove me, but I'm afraid this is how it is now :( – 4c74356b41 Feb 18 '18 at 06:51
  • 1
    We agree this is ugly and painful. However, we need to live within the constraints of ARM templates being JSON only. We are in the planning stages of writing tooling that will make it much easier to create deployment templates from an OpenAPI description and a policy document. Please bear with us, we are working on making it better. – Darrel Miller Feb 19 '18 at 14:17
  • Is that tool bicep @DarrelMiller? – FEST Jul 09 '21 at 16:12
  • No. As far as I know BICEP wasn't a thing back then. I don't know what happened to the plans as I left the team soon after this comment. – Darrel Miller Jul 23 '21 at 13:26