Skip to content

Commit bd26e41

Browse files
authored
feat: Add APIM context validation (#317)
* added apim context validation * fix apimContext name * Use Get-AzApiManagement to validate if the API Management instance exists * removed tabs Co-authored-by: Pim Simons <pim.simons@codit.eu>
1 parent 8d9d6fd commit bd26e41

8 files changed

Lines changed: 128 additions & 3 deletions

src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementApiOperation.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ param(
1010
[Parameter(Mandatory = $false)][string] $PolicyFilePath = ""
1111
)
1212

13+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
14+
if ($apim -eq $null) {
15+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
16+
}
1317
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1418

1519
New-AzApiManagementOperation -Context $apimContext -ApiId $ApiId -OperationId $OperationId -Name $OperationName -Method $Method -UrlTemplate $UrlTemplate -Description $Description

src/Arcus.Scripting.ApiManagement/Scripts/Import-AzApiManagementApiPolicy.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ param(
55
[Parameter(Mandatory = $true)][string] $policyFilePath
66
)
77

8+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
9+
if ($apim -eq $null) {
10+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
11+
}
812
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
913

1014
Write-Host "Updating policy of API '$ApiId'"

src/Arcus.Scripting.ApiManagement/Scripts/Import-AzApiManagementOperationPolicy.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ param(
66
[Parameter(Mandatory = $true)][string] $PolicyFilePath
77
)
88

9+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
10+
if ($apim -eq $null) {
11+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
12+
}
913
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1014

1115
Write-Host "Updating policy of the operation '$OperationId' in API '$ApiId'"

src/Arcus.Scripting.ApiManagement/Scripts/Import-AzApiManagementProductPolicy.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ param(
55
[Parameter(Mandatory = $true)][string] $PolicyFilePath
66
)
77

8+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
9+
if ($apim -eq $null) {
10+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
11+
}
812
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
913

1014
Write-Host "Updating policy of product '$ProductId'"

src/Arcus.Scripting.ApiManagement/Scripts/Remove-AzApiManagementDefaults.ps1

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ param(
44
)
55

66
Write-Host "Start removing Azure API Management defaults..."
7-
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
7+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
8+
if ($apim -eq $null) {
9+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
10+
}
11+
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
812
$exceptionOccurred = $false
913
$failedActions = @()
1014

src/Arcus.Scripting.ApiManagement/Scripts/Set-AzApiManagementApiSubscriptionKey.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ param(
66
[Parameter(Mandatory = $false)][string] $QueryParamName = "apiKey"
77
)
88

9+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
10+
if ($apim -eq $null) {
11+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
12+
}
913
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
1014
Write-Host "Using API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
1115

src/Arcus.Scripting.ApiManagement/Scripts/Upload-AzApiManagementCertificate.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ param(
55
[Parameter(Mandatory = $true)][string] $CertificatePassword = $(throw "Password for certificate is required")
66
)
77

8-
$context = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
8+
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
9+
if ($apim -eq $null) {
10+
throw "Unable to find the Azure API Management Instance $ServiceName in resource group $ResourceGroupName"
11+
}
12+
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
913
Write-Host "Using API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
1014

1115
Write-Verbose "Uploading private certificate at '$CertificateFilePath'..."
12-
New-AzApiManagementCertificate -Context $context -PfxFilePath $CertificateFilePath -PfxPassword $CertificatePassword
16+
New-AzApiManagementCertificate -Context $apimContext -PfxFilePath $CertificateFilePath -PfxPassword $CertificatePassword
1317
Write-Host "Uploaded private certificate at '$CertificateFilePath'"

src/Arcus.Scripting.Tests.Unit/Arcus.Scripting.ApiManagement.tests.ps1

Lines changed: 97 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)