Using Powershell, how to you add a version to an a

2019-09-01 16:27发布

Using Powershell, how to you add a version to an azure api management API?

Im using New-AzureRmApiManagementApito create the API, and using version 5.1.2 of AzureRM.ApiManagement

2条回答
Melony?
2楼-- · 2019-09-01 17:07

I have implemented an Azure API Management Version Set under APIM via Azure REST API in PowerShell.

With reference of Api-versioning-with-swagger-azure-api-management-services

Microsoft Azure PowerShell documentation doesn't contain any hints and I couldn't find any examples of it on the internet so far. It seems to be not possible versioning API inside APIM using the Swagger Open API format with cmdlet though Import-AzureRmApiManagementApi and New-AzureRmApiManagementApiVersionSet Powershell cmdlet is available .

While searching for a solution, I came across this post from Ronald Wildenberg where he uses direct REST API calls in his Powershell script, instead of cmdlets, and that's how he manages to create a versioned API inside Azure API Manager.

param(
[string]$tenantId = "",
[string]$subscriptionId = "",
[string]$resourceGroupName = "",
[string]$apimServiceName = "",
[string]$clientId = "",
[string]$clientSecret = "",
[string]$apiName = "",
[string]$backendUrl = "",
[bool] $apiContainsMultipleVersions = $true
)

class ApiManagerConnection {
    [ValidateNotNullOrEmpty()][string]$TenantId
    [ValidateNotNullOrEmpty()][string]$SubscriptionId
    [ValidateNotNullOrEmpty()][string]$ResourceGroupName
    [ValidateNotNullOrEmpty()][string]$ApiManagerServiceName
    [ValidateNotNullOrEmpty()][string]$AccessToken
}

function Get-AccessToken ($clientId, $clientSecret, $tenantId) {
    $body = "grant_type=client_credentials&client_id=$clientId&client_secret=$clientSecret&resource=https://management.core.windows.net/"
    $result = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" -Body $body -ContentType 'application/x-www-form-urlencoded'
    $result.access_token
}

function Read-SwaggerHtml ($backendUrl) {
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest "$backendUrl/swagger"
}

function Get-AllSwaggerFilePaths ($swaggerHtml) {
    $paths = @()
    Select-String '/swagger/v\d/swagger.json' -input $swaggerHtml -AllMatches | ForEach-Object {
        $paths = $_.matches -split ' '
    }
    $paths
}

function New-ApiVersionSet ([ApiManagerConnection] $connection, $apiName) {
    $createOrUpdateApiVersionSetBody = @{
        name = $apiName
        versioningScheme = "Segment"
    }

    Invoke-RestMethod `
        -Method Put `
        -Uri ("https://management.azure.com/subscriptions/" + $connection.SubscriptionId +
        "/resourceGroups/" + $connection.ResourceGroupName +
        "/providers/Microsoft.ApiManagement/service/" + $connection.ApiManagerServiceName +
        "/api-version-sets/" + $apiName + "VersionSet" +
        "?api-version=2018-01-01") `
        -Headers @{ Authorization = ("Bearer " + $connection.AccessToken)
        "Content-Type" = "application/json"
    } `
        -Body ($createOrUpdateApiVersionSetBody | ConvertTo-Json -Compress -Depth 3)
}

function New-VersionedApi ([ApiManagerConnection] $connection, $apiVersionSet, $version, $backendUrl, $apiId) {
    $createOrUpdateApiBody = @{
        properties = @{
            description = "Temp API"
            apiVersion = $version
            apiVersionSetId = $apiVersionSet.id
            displayName = "Temp API"
            path = "temp-api"
            protocols = , "https"
            serviceUrl = $backendUrl
        }
    }

    Invoke-RestMethod `
        -Method Put `
        -Uri ("https://management.azure.com/subscriptions/" + $connection.SubscriptionId +
        "/resourceGroups/" + $connection.ResourceGroupName +
        "/providers/Microsoft.ApiManagement/service/" + $connection.ApiManagerServiceName +
        "/apis/" + $apiId +
        "?api-version=2018-01-01") `
        -Headers @{ Authorization = ("Bearer " + $connection.AccessToken)
        "Content-Type" = "application/json" 
    } `
        -Body ($createOrUpdateApiBody | ConvertTo-Json -Compress -Depth 3)
}

function Update-VersionedApi ([ApiManagerConnection] $connection, $api, $apiVersionSet, $version, $serviceUrl, $apiId) {
    $createOrUpdateApiBody = @{
        properties = @{
            description = $api.Description
            apiVersion = $version
            apiVersionSetId = $apiVersionSet.id
            displayName = $api.Name
            path = $api.Path
            protocols = , "https"
            serviceUrl = $serviceUrl
        }
    }

    Invoke-RestMethod `
        -Method Put `
        -Uri ("https://management.azure.com/subscriptions/" + $connection.SubscriptionId +
        "/resourceGroups/" + $connection.ResourceGroupName +
        "/providers/Microsoft.ApiManagement/service/" + $connection.ApiManagerServiceName +
        "/apis/" + $apiId +
        "?api-version=2018-01-01") `
        -Headers @{ Authorization = ("Bearer " + $connection.AccessToken)
        "Content-Type" = "application/json" 
    } `
        -Body ($createOrUpdateApiBody | ConvertTo-Json -Compress -Depth 3)
}

function Read-SwaggerVersionJson ($swaggerJsonUrl, $version) {
    Invoke-WebRequest $swaggerJsonUrl -OutFile "swagger$version.json"
}

function Update-SwaggerPaths ($apiSwaggerSuffix, $version, $multiVersion) {
    if ($multiVersion) {
        (Get-Content "swagger$version.json").replace($apiSwaggerSuffix, '') | Set-Content "swagger$version.json"
    }
}

function Remove-DeletedApiVersions ($apiMgmtContext, $apiName, $multiVersion) {
    if ($multiVersion) {
        $existingApis = Get-AzureRmApiManagementApi -Context $apiMgmtContext | Where-Object ApiId -Match "^$apiName-.*"
        foreach ($existingApi in $existingApis) {
            if (!$apisSeen.Contains($existingApi.ApiId)) {
                Write-Host ("Deleting " + $existingApi.ApiId)
                Remove-AzureRmApiManagementApi -Context $apiMgmtContext -ApiId $existingApi.ApiId
            }
        }
    }
}

$accessToken = Get-AccessToken -clientId $clientId -clientSecret $clientSecret -tenantId $tenantId
$apiManagerConnection = [ApiManagerConnection]@{
    TenantId = $tenantId
    SubscriptionId = $subscriptionId
    ResourceGroupName = $resourceGroupName
    ApiManagerServiceName = $apimServiceName
    AccessToken = $accessToken
}


$swaggerHtml = Read-SwaggerHtml -backendUrl $backendUrl
Write-Host ("Swagger HTML file is downloaded...")
$swaggerPaths = Get-AllSwaggerFilePaths -swaggerHtml $swaggerHtml
Write-Host ("Swagger JSON paths are discovered...")
Write-Host ("")

foreach ($swaggerPath in $swaggerPaths) {
    $swaggerJsonUrl = $backendUrl + $swaggerPath
    $swaggerPath -match 'v\d' | Out-Null
    $version = $matches[0]
    $apiId = "$apiName-" + $version.ToUpper()
    $apisSeen += $apiId
    if ($apiContainsMultipleVersions) {
        $apiSwaggerSuffix = "/api/$version"
        $apiManagerSuffix = "/api"
    }
    else {
        $apiSwaggerSuffix = ""
        $apiManagerSuffix = ""
    }
    $serviceUrl = "$backendUrl$apiSwaggerSuffix"

    Write-Host ("*** Starting to process '" + $swaggerPath + "' ***")
    Read-SwaggerVersionJson -swaggerJsonUrl $swaggerJsonUrl -version $version
    Write-Host ("Swagger JSON file is downloaded...")
    Update-SwaggerPaths -apiSwaggerSuffix $apiSwaggerSuffix -version $version -multiVersion $apiContainsMultipleVersions
    Write-Host ("Swagger JSON file is updated...")
    $apiVersionSet = New-ApiVersionSet -connection $apiManagerConnection -apiName $apiName
    Write-Host ("Api version set is created...")
    $api = New-VersionedApi -connection $apiManagerConnection -apiVersionSet $apiVersionSet -version $version -backendUrl $backendUrl -apiId $apiId
    Write-Host ("A versioned Api is created based on the version set...")
    $ApiMgmtContext = New-AzureRmApiManagementContext -ResourceGroupName $apiManagerConnection.ResourceGroupName -ServiceName $apiManagerConnection.ApiManagerServiceName
    $api = Import-AzureRmApiManagementApi -Context $ApiMgmtContext -SpecificationFormat "Swagger" -SpecificationPath "swagger$version.json" -Path "$apiName$apiManagerSuffix" -ApiId $apiId
    Write-Host ("A Swagger definition is imported into the versioned Api...")
    $api = Update-VersionedApi -connection $apiManagerConnection -api $api -apiVersionSet $apiVersionSet -version $version -serviceUrl $serviceUrl -apiId $apiId
    Write-Host ("Versioned Api is updated to its final state...")
    Write-Host ("*** Finished processing '" + $swaggerPath + "' ***")
}

Remove-DeletedApiVersions -apiMgmtContext $ApiMgmtContext -apiName $apiName -multiVersion $apiContainsMultipleVersions
查看更多
The star\"
3楼-- · 2019-09-01 17:26

To add the module version you can use the command Install-Module -Name AzureRM.ApiManagement -RequiredVersion 6.0.0

For further information check this link PowerShell gallery

查看更多
登录 后发表回答