Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Powershell,如何向azure api管理api添加版本?_Azure_Azure Powershell_Azure Api Management - Fatal编程技术网

使用Powershell,如何向azure api管理api添加版本?

使用Powershell,如何向azure api管理api添加版本?,azure,azure-powershell,azure-api-management,Azure,Azure Powershell,Azure Api Management,使用Powershell,如何向azure api管理api添加版本 Im使用新的AzureRMAPIManagementAPI创建API,并使用AzureRM.ApiManagement的5.1.2版添加模块版本。您可以使用命令Install module-Name AzureRM.ApiManagement-RequiredVersion 6.0.0 有关更多信息,请查看此链接我已通过PowerShell中的Azure REST API在APIM下实现了Azure API管理版本集 参考 M

使用Powershell,如何向azure api管理api添加版本


Im使用新的AzureRMAPIManagementAPI创建API,并使用AzureRM.ApiManagement的5.1.2版添加模块版本。您可以使用命令Install module-Name AzureRM.ApiManagement-RequiredVersion 6.0.0


有关更多信息,请查看此链接

我已通过PowerShell中的Azure REST API在APIM下实现了Azure API管理版本集

参考

Microsoft Azure PowerShell文档不包含任何提示,到目前为止,我在internet上找不到任何示例。虽然Import-AzureRmApiManagementApi和New-AzureRmApiManagementApiVersionSet-Powershell cmdlet可用,但似乎不可能使用cmdlet的Swagger Open API格式对APIM内部的API进行版本控制

在搜索解决方案时,我看到了这篇文章,他在Powershell脚本中使用直接REST API调用,而不是cmdlet,这就是他如何在Azure API Manager中创建版本化API的方法

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

看起来我必须升级到最新的模块v6.1.5