Last active
February 4, 2019 10:43
-
-
Save stuartleeks/ba34f7866b8435cd32ea to your computer and use it in GitHub Desktop.
Script to expose commands for backing up and restoring Azure API Management configuration. See http://blogs.msdn.com/b/stuartleeks/archive/2015/04/29/azure-api-management-backing-up-and-restoring-configuration.aspx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This accompanies http://blogs.msdn.com/b/stuartleeks/archive/2015/04/29/azure-api-management-backing-up-and-restoring-configuration.aspx | |
# | |
# see http://azure.microsoft.com/en-us/documentation/articles/api-management-howto-disaster-recovery-backup-restore/ | |
# Auth for ARM: https://msdn.microsoft.com/library/dn790557.aspx | |
function Get-ArmToken | |
{ | |
# Somewhat dirty approach as it uses values not directly obtained from cmdlets! But it saves provisioning an Azure AD application! | |
[CmdletBinding()] | |
param ( | |
[Parameter()] | |
$Subscription = $null | |
) | |
process { | |
if ($Subscription -eq $null){ | |
$Subscription = Get-AzureSubscription -Current | |
} | |
$tenantId = $subscription.TenantId | |
$context = [Microsoft.Azure.Common.Extensions.AzureSession]::CurrentContext | |
$authenticationFactory = [Microsoft.Azure.Common.Extensions.AzureSession]::AuthenticationFactory | |
$showDialog = [Microsoft.Azure.Common.Extensions.Authentication.ShowDialog]::Auto | |
$endpoint = [Microsoft.Azure.Common.Extensions.Models.AzureEnvironment+Endpoint]::ActiveDirectoryServiceEndpointResourceId | |
$accessTokenResponse = $authenticationFactory.Authenticate($context.Account, $context.Environment, $tenantId, $null, $showDialog, $endpoint) | |
$accessToken = $accessTokenResponse.AccessToken | |
$accessToken | |
} | |
} | |
function Start-ApimBackup { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Token, | |
[Parameter(Mandatory=$true)] | |
[Microsoft.WindowsAzure.Commands.Profile.Models.PSAzureSubscription] | |
$Subscription, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ResourceGroupName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ServiceName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StorageAccountName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StorageAccessKey, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StorageContainerName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$BackupName | |
) | |
process { | |
if ($Subscription -eq $null){ | |
$Subscription = Get-AzureSubscription -Current | |
} | |
$subscriptionId = $Subscription.SubscriptionId | |
$url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.ApiManagement/service/$ServiceName/backup?api-version=2014-02-14" | |
$body = "{ | |
storageAccount : `"$StorageAccountName`", | |
accessKey : `"$StorageAccessKey`", | |
containerName : `"$StorageContainerName`", | |
backupName : `"$BackupName`" | |
}" | |
$headers = @{ "Authorization" = "Bearer $Token"} | |
try | |
{ | |
$operationResult = Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body -ContentType "application/json" | |
return New-Object PSObject -Property @{ | |
Success = $operationResult.StatusCode -eq 202 | |
StatusCode=$operationResult.StatusCode | |
StatusDescription = $operationResult.StatusDescription | |
StatusLocation = $operationResult.Headers["Location"] | |
} | |
} | |
catch | |
{ | |
return New-Object PSObject -Property @{ | |
Success = $false | |
StatusCode = -1 | |
StatusDescription = $error[0].Exception.Message | |
StatusLocation = $null | |
} | |
} | |
} | |
} | |
function Start-ApimRestore { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Token, | |
[Parameter(Mandatory=$true)] | |
[Microsoft.WindowsAzure.Commands.Profile.Models.PSAzureSubscription] | |
$Subscription, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ResourceGroupName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$ServiceName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StorageAccountName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StorageAccessKey, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StorageContainerName, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$BackupName | |
) | |
process { | |
if ($Subscription -eq $null){ | |
$Subscription = Get-AzureSubscription -Current | |
} | |
$subscriptionId = $Subscription.SubscriptionId | |
$url = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.ApiManagement/service/$ServiceName/restore?api-version=2014-02-14" | |
$body = "{ | |
storageAccount : `"$StorageAccountName`", | |
accessKey : `"$StorageAccessKey`", | |
containerName : `"$StorageContainerName`", | |
backupName : `"$BackupName`" | |
}" | |
$headers = @{ "Authorization" = "Bearer $Token"} | |
try | |
{ | |
$operationResult = Invoke-WebRequest -Method Post -Uri $url -Headers $headers -Body $body -ContentType "application/json" | |
return New-Object PSObject -Property @{ | |
Success = $operationResult.StatusCode -eq 202 | |
StatusCode = $operationResult.StatusCode | |
StatusDescription = $operationResult.StatusDescription | |
StatusLocation = $operationResult.Headers["Location"] | |
} | |
} | |
catch | |
{ | |
return New-Object PSObject -Property @{ | |
Success = $false | |
StatusCode = -1 | |
StatusDescription = $error[0].Exception.Message | |
StatusLocation = $null | |
} | |
} | |
} | |
} | |
function Get-ApimOperationStatus { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Token, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StatusLocation | |
) | |
process { | |
$headers = @{ "Authorization" = "Bearer $Token"} | |
$operationResult = Invoke-WebRequest -Method Get -Uri $StatusLocation -Headers $headers | |
return New-Object psobject -Property @{ | |
Success = ($operationResult.StatusCode -eq 200) -or ($operationResult.StatusCode -eq 202) | |
Completed = $operationResult.StatusCode -eq 200 | |
StatusCode = $operationResult.StatusCode | |
} | |
} | |
} | |
function Wait-ApimOperation { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$true)] | |
[string] | |
$Token, | |
[Parameter(Mandatory=$true)] | |
[string] | |
$StatusLocation, | |
[Parameter()] | |
[int] | |
$RetryTime = 20 # seconds | |
) | |
process { | |
$response = $null | |
while ($true) { | |
$status = Get-ApimOperationStatus -Token $Token -StatusLocation $StatusLocation | |
if((-not $status.Success) -or $status.Completed){ | |
return $status | |
} | |
"Waiting..." | Write-Host | |
Start-Sleep -Seconds $RetryTime | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment