azureapimazure-bicep

Bicep - Using OpenAPI to create API definition and Xml Policies for each operation


I have got a requirement in which my client would like to supply an OpenAPI specification to create the API with the operations and need developers to create operation policies. I understand this can be done on all operations for the API, however each operation policy is different. Is this possible through Bicep to use OpenAPI then use use bicep to assign the different xml policy?

param apiName string = 'reallycool-api-dev'
param apimName  string = 'apim-coolness-01'
resource apimServiceResource 'Microsoft.ApiManagement/service@2022-04-01-preview' existing  = {
  name: apimName
}

resource apiService  'Microsoft.ApiManagement/service/apis@2023-05-01-preview' = {
  name: apiName
  parent: apimServiceResource
  properties: {
    displayName                                     : 'ReallyCool-API-dev'
    isCurrent                                       : true
    description                                     : 'ReallyCool API-dev'
    protocols                                       : ['https']    
    subscriptionRequired                            : true
    format: 'openapi+json'
    value                                           : loadTextContent('OpenAPI ReallyCoolAPI.json')
    path                                            : 'reallycool/dev'
    subscriptionKeyParameterNames: {
        header : 'Ocp-Apim-Subscription-Key'
        query   : 'subscription-key'
    }
    }    
}         



resource firstOperationPolicy 'Microsoft.ApiManagement/service/apis/operations/policies@2022-09-01-preview'    = {   
    name: '${apimName}/api/operations/operationNamefromOpenAPIspec/policy'   
    parent: apiService   properties: {
                  format: 'rawxml'
                  value: loadTextContent('TestPolicy.xml')   
        } 
        }

I get the following error:

Error BCP170: Expected resource name to not contain any "/" characters. Child resources with a parent resource reference (via the parent property or via nesting) must not contain a fully-qualified name.

Solution

  • Need to know the operation Id in the openAPI.json specification on what policy you need to apply the policy.xml to. format: '${apimName}/${apiName}/operationIdfromOpenAPIspec/policy'

    Need to supply the full name of the policy resource ie:

    param apiName string = 'reallycool-api-dev'
    param apimName  string = 'apim-coolness-01'
    
    resource firstOperationPolicy 'Microsoft.ApiManagement/service/apis/operations/policies@2022-09-01-preview'    = {   
        name: '${apimName}/${apiName}/operationIdfromOpenAPIspec/policy'             
        properties: {
                      format: 'rawxml'
                      value: loadTextContent('TestPolicy.xml')   
            } 
    }