Skip to content

Commit 0c98155

Browse files
authored
feat: Update formatting of code, align error handling (#424)
* format code * aligned try catch functionality * updated api versions * revert api change for backwards compatibility * some more formatting alignment * aligned param
1 parent 2799d18 commit 0c98155

84 files changed

Lines changed: 1193 additions & 1289 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

docs/preview/03-Features/powershell/azure-management.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ For more information on API Management and soft deletion see [here](https://docs
2626
| `EnvironmentName`| no | The name of the Azure environment where the Azure API Management instance resides. (default: `AzureCloud`) |
2727
| `AccessToken` | no | The access token to be used to remove the Azure API Management instance. |
2828
| | | When not provided, it will be retrieved from the current context (Get-AzContext). |
29-
| `ApiVersion ` | no | The version of the management API to be used. (default: `2021-08-01`) |
29+
| `ApiVersion ` | no | The version of the management API to be used. (default: `2022-08-01`) |
3030

3131
> :bulb: The `ApiVersion` has successfully been tested with version `2021-08-01`.
3232
@@ -49,7 +49,7 @@ For more information on API Management and soft deletion see [here](https://docs
4949
| `EnvironmentName`| no | The name of the Azure environment where the Azure API Management instance resides. (default: `AzureCloud`) |
5050
| `AccessToken` | no | The access token to be used to restore the Azure API Management instance. |
5151
| | | When not provided, it will be retrieved from the current context (Get-AzContext). |
52-
| `ApiVersion ` | no | The version of the management API to be used. (default: `2021-08-01`) |
52+
| `ApiVersion ` | no | The version of the management API to be used. (default: `2022-08-01`) |
5353

5454
> :bulb: The `ApiVersion` has successfully been tested with version `2021-08-01`.
5555

src/Arcus.Scripting.ARM/Arcus.Scripting.ARM.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
The file path to the ARM template to inject the external files into.
1313
#>
1414
function Inject-ArmContent {
15-
param (
15+
param(
1616
[string] $Path = $PSScriptRoot
1717
)
1818
. $PSScriptRoot\Scripts\Inject-ArmContent.ps1 -Path $Path

src/Arcus.Scripting.ARM/Scripts/Inject-ArmContent.ps1

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
${ FileToInject = '.\Parent Directory\file.json', InjectAsJsonObject }
1111
#>
1212

13-
param (
13+
param(
1414
[string] $Path = $PSScriptRoot
1515
)
1616

1717
function Get-FullyQualifiedChildFilePath {
1818
param(
19-
[parameter(mandatory=$true)] [string] $ParentFilePath,
20-
[parameter(mandatory=$true)] [string] $ChildFilePath
19+
[parameter(mandatory = $true)] [string] $ParentFilePath,
20+
[parameter(mandatory = $true)] [string] $ChildFilePath
2121
)
2222

2323
$parentDirectoryPath = Split-Path $ParentFilePath -Parent
@@ -46,7 +46,7 @@ function InjectFile {
4646
# afterwards character classes and backreferencing to select the optional single or double quotes
4747
$fileToInjectPathRegex = [regex] "^(?:FileToInject\s*=\s*)?([`"`']?)(?<File>.*?)\1?$";
4848
$fileMatch = $fileToInjectPathRegex.Match($filePart)
49-
if ($fileMatch.Success -ne $True){
49+
if ($fileMatch.Success -ne $True) {
5050
throw "The file part '$filePart' of the injection instruction could not be parsed correctly"
5151
}
5252

@@ -102,8 +102,7 @@ function InjectFile {
102102
ConvertFrom-Json $newString
103103

104104
$surroundContentWithDoubleQuotes = $False
105-
}
106-
catch {
105+
} catch {
107106
Write-Warning "Content to inject into ARM template file '$filePath' cannot be parsed as a JSON object!"
108107
}
109108
}

src/Arcus.Scripting.ActiveDirectory/Arcus.Scripting.ActiveDirectory.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#>
1414
function List-AzADAppRoleAssignments {
1515
param(
16-
[Parameter(Mandatory = $true)][string] $ClientId = $(throw "ClientId is required"),
17-
[Parameter(Mandatory = $false)][string] $RolesAssignedToClientId
16+
[Parameter(Mandatory = $true)][string] $ClientId = $(throw "ClientId is required"),
17+
[Parameter(Mandatory = $false)][string] $RolesAssignedToClientId
1818
)
1919
. $PSScriptRoot\Scripts\List-AzADAppRoleAssignments.ps1 -ClientId $ClientId -RolesAssignedToClientId $RolesAssignedToClientId
2020
}

src/Arcus.Scripting.ActiveDirectory/Scripts/Add-AzADAppRoleAssignment.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ try {
2727
Write-Verbose "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', adding the role"
2828

2929
$newRole = @{
30-
"DisplayName" = $Role
31-
"Description" = $Role
32-
"Value" = $Role
33-
"Id" = [Guid]::NewGuid().ToString()
34-
"IsEnabled" = $true
35-
"allowedMemberTypes" = @("User", "Application")
36-
}
30+
"DisplayName" = $Role
31+
"Description" = $Role
32+
"Value" = $Role
33+
"Id" = [Guid]::NewGuid().ToString()
34+
"IsEnabled" = $true
35+
"allowedMemberTypes" = @("User", "Application")
36+
}
3737

3838
$adApplication.AppRole += $newRole
3939

src/Arcus.Scripting.ActiveDirectory/Scripts/List-AzADAppRoleAssignments.ps1

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ if ($RolesAssignedToClientId -ne '') {
2424
}
2525

2626
try {
27-
if ($adApplication.AppRole.Count -eq 0)
28-
{
27+
if ($adApplication.AppRole.Count -eq 0) {
2928
Write-Warning "No roles found in Active Directory Application '$($adApplication.DisplayName)'"
3029
}
3130

3231
foreach ($appRole in $adApplication.AppRole) {
3332
Write-Host "Found role '$($appRole.Value)' on Active Directory Application '$($adApplication.DisplayName)'" -ForegroundColor Green
3433
if ($RolesAssignedToClientId -ne '') {
35-
$appRoleAssignments = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object {($_.AppRoleId -eq $appRole.Id) -and ($_.PrincipalId -eq $adServicePrincipalRolesAssignedTo.Id)}
34+
$appRoleAssignments = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object { ($_.AppRoleId -eq $appRole.Id) -and ($_.PrincipalId -eq $adServicePrincipalRolesAssignedTo.Id) }
3635
} else {
3736
$appRoleAssignments = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object AppRoleId -eq $appRole.Id
3837
}

src/Arcus.Scripting.ActiveDirectory/Scripts/Remove-AzADAppRoleAssignment.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ try {
2727
if ($adApplication.AppRole.Value -notcontains $Role) {
2828
Write-Warning "Active Directory Application '$($adApplication.DisplayName)' does not contain the role '$Role', skipping removal"
2929
} else {
30-
$appRole = $adApplication.AppRole | Where-Object {($_.DisplayName -eq $Role)}
31-
$appRoleAssignment = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object {($_.AppRoleId -eq $appRole.Id) -and ($_.PrincipalId -eq $adServicePrincipalRoleRemoveFrom.Id)}
30+
$appRole = $adApplication.AppRole | Where-Object { ($_.DisplayName -eq $Role) }
31+
$appRoleAssignment = Get-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $adServicePrincipal.Id | Where-Object { ($_.AppRoleId -eq $appRole.Id) -and ($_.PrincipalId -eq $adServicePrincipalRoleRemoveFrom.Id) }
3232

3333
if ($appRoleAssignment) {
3434
Remove-MgServicePrincipalAppRoleAssignment -ServicePrincipalId $adServicePrincipalRoleRemoveFrom.Id -AppRoleAssignmentId $appRoleAssignment.Id

src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.psm1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ function Create-AzApiManagementUserAccount {
164164
[string][parameter(Mandatory = $false)] $Note,
165165
[switch][parameter(Mandatory = $false)] $SendNotification = $false,
166166
[string][parameter(Mandatory = $false)][ValidateSet('invite', 'signup')] $ConfirmationType = "invite",
167-
[string][parameter(Mandatory = $false)] $ApiVersion = "2021-08-01",
167+
[string][parameter(Mandatory = $false)] $ApiVersion = "2022-08-01",
168168
[string][parameter(Mandatory = $false)] $SubscriptionId,
169169
[string][parameter(Mandatory = $false)] $AccessToken
170170
)
@@ -211,7 +211,7 @@ function Create-AzApiManagementUserAccountsFromConfig {
211211
[string][parameter(Mandatory = $true)] $ServiceName = $(throw "API management service name is required"),
212212
[string][Parameter(Mandatory = $true)] $ConfigurationFile = $(throw "Name of configuration file is required"),
213213
[switch][parameter(Mandatory = $false)] $StrictlyFollowConfigurationFile = $false,
214-
[string][parameter(Mandatory = $false)] $ApiVersion = "2021-08-01",
214+
[string][parameter(Mandatory = $false)] $ApiVersion = "2022-08-01",
215215
[string][parameter(Mandatory = $false)] $SubscriptionId,
216216
[string][parameter(Mandatory = $false)] $AccessToken
217217
)
@@ -308,12 +308,12 @@ Export-ModuleMember -Function Import-AzApiManagementProductPolicy
308308
The name of the Azure API Management instance.
309309
#>
310310
function Remove-AzApiManagementDefaults {
311-
param(
312-
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group is required"),
313-
[Parameter(Mandatory = $true)][string] $ServiceName = $(throw "Service name for API Management service name is required")
314-
)
311+
param(
312+
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group is required"),
313+
[Parameter(Mandatory = $true)][string] $ServiceName = $(throw "Service name for API Management service name is required")
314+
)
315315

316-
. $PSScriptRoot\Scripts\Remove-AzApiManagementDefaults.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
316+
. $PSScriptRoot\Scripts\Remove-AzApiManagementDefaults.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
317317
}
318318

319319
Export-ModuleMember -Function Remove-AzApiManagementDefaults
@@ -423,7 +423,7 @@ function Restore-AzApiManagementService {
423423
[Parameter(Mandatory = $true)][string] $StorageAccountResourceGroupName = $(throw = "Resource group for storage account is required"),
424424
[Parameter(Mandatory = $true)][string] $StorageAccountName = $(throw "Name for the Azure storage account is required"),
425425
[Parameter(Mandatory = $true)][string] $ServiceName = $(throw "Service name for API Management service name is required"),
426-
[Parameter(Mandatory = $true)][string] $ContainerName =$(throw "Name of the source container is required"),
426+
[Parameter(Mandatory = $true)][string] $ContainerName = $(throw "Name of the source container is required"),
427427
[Parameter(Mandatory = $true)][string] $BlobName = $(throw "Name of the Azure storage blob is required"),
428428
[Parameter(Mandatory = $false)][switch] $PassThru = $false,
429429
[Parameter(Mandatory = $false)][Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer] $DefaultProfile = $null

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
param(
2-
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group is required"),
3-
[Parameter(Mandatory = $true)][string] $ServiceName = $(throw "API management service name is required"),
4-
[Parameter(Mandatory = $true)][string] $ApiId = $(throw "API ID is required"),
5-
[Parameter(Mandatory = $true)][string] $OperationId = $(throw "Operation ID is required"),
6-
[Parameter(Mandatory = $true)][string] $Method = $(throw "Method is required"),
7-
[Parameter(Mandatory = $true)][string] $UrlTemplate = $(throw "URL template is required"),
8-
[Parameter(Mandatory = $false)][string] $OperationName = $OperationId,
9-
[Parameter(Mandatory = $false)][string] $Description = "",
10-
[Parameter(Mandatory = $false)][string] $PolicyFilePath = ""
2+
[Parameter(Mandatory = $true)][string] $ResourceGroupName = $(throw "Resource group is required"),
3+
[Parameter(Mandatory = $true)][string] $ServiceName = $(throw "API management service name is required"),
4+
[Parameter(Mandatory = $true)][string] $ApiId = $(throw "API ID is required"),
5+
[Parameter(Mandatory = $true)][string] $OperationId = $(throw "Operation ID is required"),
6+
[Parameter(Mandatory = $true)][string] $Method = $(throw "Method is required"),
7+
[Parameter(Mandatory = $true)][string] $UrlTemplate = $(throw "URL template is required"),
8+
[Parameter(Mandatory = $false)][string] $OperationName = $OperationId,
9+
[Parameter(Mandatory = $false)][string] $Description = "",
10+
[Parameter(Mandatory = $false)][string] $PolicyFilePath = ""
1111
)
1212

1313
$apim = Get-AzApiManagement -ResourceGroupName $ResourceGroupName -Name $ServiceName
@@ -19,12 +19,9 @@ $apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName
1919
New-AzApiManagementOperation -Context $apimContext -ApiId $ApiId -OperationId $OperationId -Name $OperationName -Method $Method -UrlTemplate $UrlTemplate -Description $Description
2020
Write-Host "New API operation '$OperationName' was added on Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
2121

22-
if($OperationId -eq "" -or $PolicyFilePath -eq "")
23-
{
22+
if ($OperationId -eq "" -or $PolicyFilePath -eq "") {
2423
Write-Warning "No policy has been defined for Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
25-
}
26-
else
27-
{
24+
} else {
2825
Write-Verbose "Updating policy of the operation '$OperationId' in API '$ApiId' of the Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'..."
2926
Set-AzApiManagementPolicy -Context $apimContext -ApiId $ApiId -OperationId $OperationId -PolicyFilePath $PolicyFilePath
3027
Write-Host "Updated policy of the operation '$OperationId' in API '$ApiId' of the Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'" -ForegroundColor Green

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ param(
99
[string][parameter(Mandatory = $false)] $Note,
1010
[switch][parameter(Mandatory = $false)] $SendNotification = $false,
1111
[string][parameter(Mandatory = $false)][ValidateSet('invite', 'signup')] $ConfirmationType = "invite",
12-
[string][parameter(Mandatory = $false)] $ApiVersion = "2021-08-01",
12+
[string][parameter(Mandatory = $false)] $ApiVersion = "2022-08-01",
1313
[string][parameter(Mandatory = $false)] $SubscriptionId,
1414
[string][parameter(Mandatory = $false)] $AccessToken
1515
)
@@ -30,8 +30,7 @@ if ($SubscriptionId -eq "" -or $AccessToken -eq "") {
3030
$apimMgmtEndpoint = "https://management.azure.com/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.ApiManagement/service/$ServiceName/users/$($UserId)?notify=$SendNotification&api-version=$ApiVersion"
3131
$fullUrl = $apimMgmtEndpoint.Replace('{subscriptionId}', $SubscriptionId)
3232

33-
try
34-
{
33+
try {
3534
if ($ConfirmationType -eq 'invite') {
3635
Write-Verbose "Attempting to invite $FirstName $LastName ($mailAddress) for Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
3736
} else {
@@ -40,22 +39,22 @@ try
4039

4140
$jsonRequest = ConvertTo-Json -Depth 3 @{
4241
'properties' = @{
43-
'firstName' = $FirstName
44-
'lastName' = $LastName
45-
'email' = $MailAddress
42+
'firstName' = $FirstName
43+
'lastName' = $LastName
44+
'email' = $MailAddress
4645
'confirmation' = $ConfirmationType
47-
'password' = $Password
48-
'note' = $Note
46+
'password' = $Password
47+
'note' = $Note
4948
}
5049
}
5150

5251
$params = @{
53-
Method = 'Put'
54-
Headers = @{
55-
'authorization'="Bearer $AccessToken"
52+
Method = 'Put'
53+
Headers = @{
54+
'authorization' = "Bearer $AccessToken"
5655
}
57-
URI = $fullUrl
58-
Body = $jsonRequest
56+
URI = $fullUrl
57+
Body = $jsonRequest
5958
ContentType = 'application/json'
6059
}
6160

@@ -74,6 +73,5 @@ try
7473

7574
return $UserId
7675
} catch {
77-
Write-Error $_
78-
throw "Failed to create an account for $FirstName $LastName ($MailAddress) for Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'"
76+
throw "Failed to create an account for $FirstName $LastName ($MailAddress) for Azure API Management instance '$ServiceName' in resource group '$ResourceGroupName'. Details: $($_.Exception.Message)"
7977
}

0 commit comments

Comments
 (0)