Scripts/Remove-AzApiManagementDefaults.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
param(
    [Parameter(Mandatory = $true)][string] $ResourceGroupName,
    [Parameter(Mandatory = $true)][string] $ServiceName
)

Write-Host "Start removing Azure API Management defaults..."
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName 
$exceptionOccurred = $false
$failedActions = @()

Write-Host "Checking if 'echo' API exists..."
$echoExists = $true
try {
    Get-AzApiManagementApi -Context $apimContext -ApiId 'echo-api' -ErrorAction Stop | Out-Null
}
catch {
    if ($_.Exception.Response.StatusCode -eq 'NotFound' -or $_.TargetObject.Response.StatusCode -eq 'NotFound') {
        $echoExists = $false
        Write-Host "The 'echo' API does not exist, skipping removal..."
    }
    else {
        Write-Error $_
        $exceptionOccurred = $true
        $failedActions += "getting the 'echo-api'"
    }
}
if ($echoExists) {
    try {
        Write-Host "Removing 'echo' API..."
        Remove-AzApiManagementApi -Context $apimContext -ApiId 'echo-api' -ErrorAction Stop | Out-Null
    }
    catch {
        Write-Error "Failed to remove the 'echo' API"
        $exceptionOccurred = $true
        $failedActions += "removing the 'echo-api'"
    }
}

Write-Host "Checking if 'starter' product exists..."
$starterExists = $true
try {
    Get-AzApiManagementProduct -Context $apimContext -ProductId 'starter' -ErrorAction Stop | Out-Null
}
catch {
    if ($_.Exception.Response.StatusCode -eq 'NotFound' -or $_.TargetObject.Response.StatusCode -eq 'NotFound') {
        $starterExists = $false
        Write-Host "The 'starter' product does not exist, skipping removal..."
    }
    else {
        Write-Error $_
        $exceptionOccurred = $true
        $failedActions += "getting the 'starter' product"
    }
}
if ($starterExists) { 
    try {
        Write-Host "Removing 'starter' product..."
        Remove-AzApiManagementProduct -Context $apimContext -ProductId 'starter' -DeleteSubscriptions -ErrorAction Stop | Out-Null
    }
    catch {
        Write-Error "Failed to remove the 'starter' product"
        $exceptionOccurred = $true
        $failedActions += "removing the 'starter' product"
    }
}

Write-Host "Checking if 'unlimited' product exists..."
$unlimitedExists = $true
try {
    Get-AzApiManagementProduct -Context $apimContext -ProductId 'unlimited' -ErrorAction Stop | Out-Null
}
catch {
    if ($_.Exception.Response.StatusCode -eq 'NotFound' -or $_.TargetObject.Response.StatusCode -eq 'NotFound') {
        $unlimitedExists = $false
    Write-Host "The 'unlimited' product does not exist, skipping removal..."
    }
    else {
        Write-Error $_
        $exceptionOccurred = $true
        $failedActions += "getting the 'unlimited' product"
    }
}
if ($unlimitedExists) { 
    try {
        Write-Host "Removing 'unlimited' product..."
        Remove-AzApiManagementProduct -Context $apimContext -ProductId 'unlimited' -DeleteSubscriptions -ErrorAction Stop | Out-Null
    }
    catch {
        Write-Error "Failed to remove the 'unlimited' product"
        $exceptionOccurred = $true
        $failedActions += "removing the 'unlimited' product"
    }
}

if ($exceptionOccurred)
{
    $failedActionsString = $failedActions -join ", "
    throw "These action(s) failed: $failedActionsString"
}

Write-Host "Finished removing Azure API Management defaults!"