Search code examples
azureazure-devopsazure-pipelinesazure-cli

Pipeline failing with ERROR: not enough values to unpack (expected 2, got 1) when tried to migrate APIM platform version from stv1 to stv2


I am trying to migrate APIM from platform version stv1 to stv2, using the below script using Azure CLI

- task: AzureCLI@2
  displayName: "Migrating Platform Version of APIM to stv2"
  inputs:
    azureSubscription: ${{ parameters.Subscription }}
    scriptType: 'pscore' 
    scriptLocation: 'inlineScript' 
    inlineScript: |
        $APIMName = "$(api_management_service_name)"
        $ResourceGroup = "$(resource_group)"
        $APIM_PlatformVersion = az apim show --name $APIMName --resource-group $ResourceGroup --query platformVersion --output tsv
        if($APIM_PlatformVersion -contains 'stv1'){
          $headers = @{
            'Content-Type' = 'application/json' 
          }
           $APIM_ResourceId = az apim show --name $APIMName --resource-group $ResourceGroup --query id --output tsv
           Write-Host "Migrating to stv2 as the current version is $APIM_PlatformVersion"
           # Call REST API to migrate to stv2 and preserve VIP address
           az rest --method post --uri "$APIM_ResourceId/migrateToStv2?api-version=2023-03-01-preview" --body '{\"mode\": \"PreserveIp\"}' --headers $headers
        }
        else{
           Write-Host "APIM already in $APIM_PlatformVersion"
        }

Got an error stating:

ERROR: The command failed with an unexpected error. Here is the traceback:
ERROR: not enough values to unpack (expected 2, got 1)

Solution

  • I can reproduce the same issue when using the same PowerShell script sample.

    The cause of the issue is the format of the headers.

    Refer to this doc: az rest

    --headers

    Space-separated headers in KEY=VALUE format or JSON string. Use @{file} to load from a file.

    To solve this issue, you can use the KEY=VALUE format in headers of the az rest command:

    For example:

    az rest --method post --uri "$APIM_ResourceId/migrateToStv2?api-version=2023-03-01-preview" --body '{\"mode\": \"PreserveIp\"}' --headers Content-Type=application/json
    

    Task sample:

    - task: AzureCLI@2
      displayName: "Migrating Platform Version of APIM to stv2"
      inputs:
        azureSubscription: ${{ parameters.Subscription }}
        scriptType: 'pscore' 
        scriptLocation: 'inlineScript' 
        inlineScript: |
            $APIMName = "$(api_management_service_name)"
            $ResourceGroup = "$(resource_group)"
            $APIM_PlatformVersion = az apim show --name $APIMName --resource-group $ResourceGroup --query platformVersion --output tsv
            if($APIM_PlatformVersion -contains 'stv1'){
               $APIM_ResourceId = az apim show --name $APIMName --resource-group $ResourceGroup --query id --output tsv
               Write-Host "Migrating to stv2 as the current version is $APIM_PlatformVersion"
               # Call REST API to migrate to stv2 and preserve VIP address
               az rest --method post --uri "$APIM_ResourceId/migrateToStv2?api-version=2023-03-01-preview" --body '{\"mode\": \"PreserveIp\"}' --headers Content-Type=application/json
    
            }
            else{
               Write-Host "APIM already in $APIM_PlatformVersion"
            }