如何用PowerShell添加path模式v1版本的Azure APIM API?求非门户替代方案
Hey there! Let's break down your Azure API Management questions with practical, portal-free solutions:
The modern Azure PowerShell Az module has full support for path-based versioning. Here's a step-by-step breakdown:
First, get your APIM instance context:
$apimContext = Get-AzApiManagement -ResourceGroupName "your-resource-group-name" -Name "your-apim-instance-name"Create an API version set configured for path mode:
$versionSet = New-AzApiManagementApiVersionSet -Context $apimContext ` -Name "v1-path-version-set" ` -Scheme "Path" ` -Description "API version set using path-based versioning"Create your API and link it to the version set, specifying the "v1" identifier as the path segment:
New-AzApiManagementApi -Context $apimContext ` -Name "YourTargetAPI" ` -ServiceUrl "https://your-backend-service-url.com" ` -Path "v1" ` -ApiVersionSetId $versionSet.Id ` -ApiVersion "v1" ` -Description "API with path-based version v1"
This will create an API where the version is accessed via the /v1 path prefix.
Import-AzureRmApiManagementApi doesn't support "path" version mode? You're right that the older Import-AzureRmApiManagementApi cmdlet lacks support for path-based versioning. Here are three solid portal-free alternatives:
Option 1: Switch to the Az module's Import-AzApiManagementApi
The updated Az module cmdlet supports versioning parameters. Follow these steps:
- Create the path-based version set (same as step 2 in the first question).
- Import your API specification while linking it to the version set:
Import-AzApiManagementApi -Context $apimContext ` -SpecificationFormat "OpenApi" ` -SpecificationPath "C:\path\to\your-api-spec.yaml" ` -ApiVersionSetId $versionSet.Id ` -ApiVersion "v1" ` -Path "v1"
Option 2: Use Azure REST API with PowerShell (if stuck on AzureRm)
If you can't switch to Az, you can directly call the Azure Management REST API using Invoke-RestMethod:
Get an access token for authentication:
$token = (Get-AzAccessToken -ResourceUrl "https://management.azure.com").Token $headers = @{ "Authorization" = "Bearer $token" "Content-Type" = "application/json" }Create the path-based version set:
$versionSetPayload = @{ properties = @{ displayName = "v1-path-version-set" versioningScheme = "Path" description = "Path-based version set for API import" } } | ConvertTo-Json $versionSetResponse = Invoke-RestMethod -Method Post ` -Uri "https://management.azure.com/subscriptions/{your-subscription-id}/resourceGroups/{your-resource-group}/providers/Microsoft.ApiManagement/service/{your-apim-instance}/apiVersionSets?api-version=2021-08-01" ` -Headers $headers ` -Body $versionSetPayload $versionSetId = $versionSetResponse.idImport your API with the version set linked:
$apiSpecContent = Get-Content "C:\path\to\your-api-spec.json" -Raw $importPayload = @{ properties = @{ serviceUrl = "https://your-backend-service-url.com" path = "v1" apiVersion = "v1" apiVersionSetId = $versionSetId contentFormat = "openapi+json" contentValue = $apiSpecContent } } | ConvertTo-Json Invoke-RestMethod -Method Put ` -Uri "https://management.azure.com/subscriptions/{your-subscription-id}/resourceGroups/{your-resource-group}/providers/Microsoft.ApiManagement/service/{your-apim-instance}/apis/YourTargetAPI?api-version=2021-08-01" ` -Headers $headers ` -Body $importPayloadDon't forget to replace all placeholder values (like subscription ID, resource group name) with your actual data.
Option 3: Use Azure CLI
If you're open to using CLI instead of PowerShell, this is a straightforward approach:
Create the path-based version set:
az apim api-version-set create --resource-group your-resource-group ` --service-name your-apim-instance ` --name v1-path-version-set ` --scheme Path ` --description "Path-based versioning for imported API"Import your API and link it to the version set:
az apim api import --resource-group your-resource-group ` --service-name your-apim-instance ` --name YourTargetAPI ` --specification-path ./your-api-spec.yaml ` --specification-format OpenApi ` --path v1 ` --api-version v1 ` --api-version-set-id $(az apim api-version-set show --resource-group your-resource-group --service-name your-apim-instance --name v1-path-version-set --query id -o tsv)
内容的提问来源于stack exchange,提问作者Janusz Nowak

