Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hakant/f39a7995fe7c1e519099576587f7f6d5 to your computer and use it in GitHub Desktop.
Save hakant/f39a7995fe7c1e519099576587f7f6d5 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment