1

I've read the documentation and while the policy fragment idea seems good for code reuse, the system doesn't seem to provide a way to deploy them in an automated way.

I've even exported the entire configuration of the apim to git and could not find my policy fragment.

1 Answers1

0

Seems like it's a very recent feature, we had the same problem, and as a first approach we decided to use terraform for deploying policy fragments from dev environment to stagging and production environments.

https://learn.microsoft.com/es-mx/azure/templates/microsoft.apimanagement/2021-12-01-preview/service/policyfragments?pivots=deployment-language-terraform

$computer> cat main.tf

terraform {
  required_providers {
    azapi = {
      source = "azure/azapi"
    }
  }
}

provider "azapi" {
}

resource "azapi_resource" "symbolicname" {
  type = "Microsoft.ApiManagement/service/policyFragments@2021-12-01-preview"
  name = “fragmentpolicyname”
  parent_id = "/subscriptions/[subscriptionid]/resourceGroups/[resourcegroupname]/providers/Microsoft.ApiManagement/service/[apimanagementservicename]”
  body = jsonencode({
    properties = {
      description = “fragment policy description”
      format = "xml" # it could also be rawxml
      value = <<EOF
<!--
    IMPORTANT:
    - Policy fragment are included as-is whenever they are referenced.
    - If using variables. Ensure they are setup before use.
    - Copy and paste your code here or simply start coding
 -->
 <fragment>
        //some magical code here that you will use in a lot of policies
 </fragment>
EOF
    }
  })
}

terraform init
terraform plan
terraform apply

You can integrate this part in your azure devops pipeline.