APIM API Export to Open API v3 Json Format does not Deploy using Bicep

Warren Kinley 56 Reputation points
2024-08-21T23:03:33.5466667+00:00

I have exported my API containing 3 operations into an Open API v3 Json file. They use 3 schemas for request-response. If I delete my API in APIM and import from this file they are recreated correctly in APIM. However, when I come to use Bicep and YAML to deploy I see the following exception: syntax error near unexpected token `('

I have tried many things to no avail. I have another API that is imported using Bicep successfully.

resource apimServiceName_apiName 'Microsoft.ApiManagement/service/apis@2023-09-01-preview' = {
  name: '${apimServiceName}/${name}'
  properties: {
    format: definitionFormat
    value: definitionContent
    path: apiPath
  }
}

YAML that calls into the Bicep above:

steps:
  - task: AzureCLI@2
    displayName: 'Add API : ${{ parameters.name }}'
    inputs:
      azureSubscription: ${{ parameters.azureServiceConnection }}
      scriptType: bash
      scriptLocation: inlineScript
      inlineScript: |
        az deployment group create \
          --resource-group '${{ parameters.resourceGroupName }}' \
          --name 'iac.${{ parameters.apiManagementServiceName }}' \
          --template-file '../sourceArtifact/resources/apimanagement/cli/apis/APIM.AddApiFromDefinition.bicep' \
          --parameters 'apimServiceName=${{ parameters.apiManagementServiceName }}' \
          --parameters 'name=${{ parameters.name }}' \
          --parameters 'definitionFormat=${{ parameters.definitionFormat }}' \
          --parameters 'definitionContent=${{ parameters.definitionContent }}' \
          --parameters 'apiPath=${{ parameters.apiPath }}'

YAML that calls the one above:

- task: PowerShell@2
     name: getDefinitionContent
     displayName: 'Load File Content'
     inputs:
       targetType: 'inline'
       script: |
         $content = Get-Content -Path ../sourceArtifact/resources/apimanagement/resources/apis/Internal API.openapi+json.json" -Raw
         echo $content
         Write-Output "##vso[task.setvariable variable=fileContent;isoutput=true]$content"
  
   - template: ../apimanagement/cli/apis/APIM.AddApiFromDefinition.yaml
     parameters:
       azureServiceConnection: ${{ parameters.azureServiceConnection }}
       resourceGroupName: '$(coreResourceGroupName)'
       apiManagementServiceName: '$(coreApimInstance)'
       name: 'internal-api'
       definitionFormat: 'openapi+json'
       definitionContent: '$(getDefinitionContent.fileContent)'
       apiPath: '/internal/erp'

The open api json file is flattened.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,161 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Warren Kinley 56 Reputation points
    2024-08-22T09:07:02.1333333+00:00

    Fixed it! It might have been the decoding or the fact that I specified the YAML parameters incorrectly???

    - task: PowerShell@2
        name: getHCMDefinitionContent
        displayName: 'Load File Content - Oracle HCM Internal API'
        inputs:
          targetType: 'inline'
    #      script: |
    #        $content = Get-Content -Path "../sourceArtifact/resources/apimanagement/resources/apis/Oracle HCM Internal API.openapi+json.json" -Raw
    #        echo $content
    #        Write-Output "##vso[task.setvariable variable=fileContent;isoutput=true]$content"
          script: |
                  $content = Get-Content -Path "../sourceArtifact/resources/apimanagement/resources/apis/Oracle HCM Internal API.openapi+json.json" -Raw
                  $encodedContent = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($content))
                  Write-Output "##vso[task.setvariable variable=fileContent;isoutput=true]$encodedContent"
    

    Then:

    steps:
      - task: AzureCLI@2
        displayName: 'Add API : ${{ parameters.name }}'
        inputs:
          azureSubscription: ${{ parameters.azureServiceConnection }}
          scriptType: bash
          scriptLocation: inlineScript
          inlineScript: |
            # Decode the definitionContent from base64
            decodedDefinitionContent=$(echo "${{ parameters.definitionContent }}" | base64 --decode)
            
            echo "Decoded Definition Content:"
            echo "$decodedDefinitionContent"
            
            # Use the decoded definition content in the az deployment command
            az deployment group create \
              --resource-group '${{ parameters.resourceGroupName }}' \
              --name 'iac.${{ parameters.apiManagementServiceName }}' \
              --template-file '../sourceArtifact/resources/apimanagement/cli/apis/APIM.AddApiFromDefinition.bicep' \
              --parameters apimServiceName='${{ parameters.apiManagementServiceName }}' \
              --parameters name='${{ parameters.name }}' \
              --parameters definitionFormat='${{ parameters.definitionFormat }}' \
              --parameters definitionContent="$decodedDefinitionContent" \
              --parameters apiPath='${{ parameters.apiPath }}'
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.