Search code examples
azureazure-api-managementazure-bicep

Entity with specified identifier not found when applying policy on API operation with Bicep in Azure API Management


I would like to apply an outbound policy to some of the operations of my API. I use a Bicep file to accomplish that but I get the following error:

"statusMessage": {
        "status": "Failed",
        "error": {
          "code": "ValidationError",
          "message": "Entity with specified identifier not found"
        }
      }

This is the snippet for the policy:

resource relationApiSym 'Microsoft.ApiManagement/service/apis@2022-08-01' existing = {
  name: relationsApi.name
  parent: apiManagementInstance
  resource operationRegisterId 'operations@2022-08-01' existing = {
    name : 'post-registerrelation-id'
    resource policyId 'policies@2022-08-01' = {
      name: 'policy'
      properties: {
        format: 'xml'
        value:'<policies>\r\n<inbound>\r\n<base />\r\n</inbound>\r\n<backend>\r\n<base />\r\n</backend>\r\n<outbound>\r\n<base />\r\n<choose>\r\n<when condition="@(context.Response.StatusCode == 200)">\r\n<set-body>@{var response = context.Response.Body.As&lt;JObject&gt;();foreach (var key in new [] {"RelatieId"}) {response.Property (key).Remove ();}return response.ToString();}\r\n</set-body>\r\n</when>\r\n</choose>\r\n</outbound>\r\n<on-error>\r\n<base />\r\n</on-error>\r\n</policies>'      
      }
    }
  }
}

I see that the operation exisits with the name 'post-register-relation-id' but when i check the target url from the deploymentlog, I notice that there are no policies (yet)

Deploy snippet


Solution

  • I have used your code with few modifications and it worked for me.

    Here afreen-apimgmt-001 is the Azure API Management instance name, echo-api is the API name and create-resource is the operation name where I want to apply the policy.

    Make sure, the operation name you have taken is correct and present in Azure API management portal.

    enter image description here

    Code-

    resource relationApiSym 'Microsoft.ApiManagement/service/apis@2022-08-01' existing = {
      name: 'afreen-apimgmt-001/echo-api'
      resource operationRegisterId 'operations@2022-08-01' existing = {
        name : 'create-resource'
        resource policyId 'policies@2022-08-01' = {
          name: 'policy'
          properties: {
            format: 'xml'
            value:'<policies>\r\n<inbound>\r\n<base />\r\n</inbound>\r\n<backend>\r\n<base />\r\n</backend>\r\n<outbound>\r\n<base />\r\n<choose>\r\n<when condition="@(context.Response.StatusCode == 200)">\r\n<set-body>@{var response = context.Response.Body.As&lt;JObject&gt;();foreach (var key in new [] {"RelatieId"}) {response.Property (key).Remove ();}return response.ToString();}\r\n</set-body>\r\n</when>\r\n</choose>\r\n</outbound>\r\n<on-error>\r\n<base />\r\n</on-error>\r\n</policies>'      
          }
        }
      }
    }
    

    Output-

    enter image description here