Skip to content

Commit 2c31e34

Browse files
authored
feat: Add Remove-AzApiManagementUser script (#321)
* added Remove-AzApiManagementUser script * fix create user docs * added docs for Remove-AzApiManagementUser * renamed scripts to prevent issues Co-authored-by: Pim Simons <pim.simons@codit.eu>
1 parent bd26e41 commit 2c31e34

7 files changed

Lines changed: 243 additions & 27 deletions

File tree

docs/preview/02-Features/powershell/azure-api-management.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ PS> Create-AzApiManagementApiOperation -ResourceGroupName $ResourceGroup -Servic
8989

9090
## Creating a new user in an Azure API Management service
9191

92-
Signup or invite a new user in an existing API in Azure API Management.
92+
Signup or invite a new user in an existing Azure API Management instance.
9393

9494
| Parameter | Mandatory | Description |
9595
| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -109,55 +109,78 @@ Signup or invite a new user in an existing API in Azure API Management.
109109

110110
**Example**
111111

112-
Invite a new user in an existing API in Azure API Management.
112+
Invite a new user in an existing Azure API Management instance.
113113

114114
```powershell
115-
PS> Create-AzApiManagementUser -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress
115+
PS> Create-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress
116116
# Attempting to invite $FirstName $LastName ($MailAddress)
117117
# Invitation has been sent to FirstName $LastName ($MailAddress)
118118
```
119119

120-
Invite a new user in an existing API in Azure API Management and specify a UserId.
120+
Invite a new user in an existing Azure API Management instance and specify a UserId.
121121

122122
```powershell
123-
PS> Create-AzApiManagementUser -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -UserId $UserId
123+
PS> Create-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -UserId $UserId
124124
# Attempting to invite $FirstName $LastName ($MailAddress)
125125
# Invitation has been sent to FirstName $LastName ($MailAddress)
126126
```
127127

128-
Invite a new user in an existing API in Azure API Management and include a note.
128+
Invite a new user in an existing Azure API Management instance and include a note.
129129

130130
```powershell
131-
PS> Create-AzApiManagementUser -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -Note $Note
131+
PS> Create-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -Note $Note
132132
# Attempting to invite $FirstName $LastName ($MailAddress)
133133
# Invitation has been sent to FirstName $LastName ($MailAddress)
134134
```
135135

136-
Invite a new user in an existing API in Azure API Management and send a notification.
136+
Invite a new user in an existing Azure API Management instance and send a notification.
137137

138138
```powershell
139-
PS> Create-AzApiManagementUser -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -SendNotification
139+
PS> Create-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -SendNotification
140140
# Attempting to invite $FirstName $LastName ($MailAddress)
141141
# Invitation has been sent to FirstName $LastName ($MailAddress)
142142
```
143143

144-
Signup a new user in an existing API in Azure API Management.
144+
Signup a new user in an existing Azure API Management instance.
145145

146146
```powershell
147-
PS> Create-AzApiManagementUser -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -ConfirmationType signup
147+
PS> Create-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -ConfirmationType signup
148148
# Attempting to create account for FirstName $LastName ($MailAddress)
149149
# Account has been created for FirstName $LastName ($MailAddress)
150150
# Since no password was provided, one has been generated. Please advise the user to change this password the first time logging in
151151
```
152152

153-
Signup a new user in an existing API in Azure API Management and specify a password.
153+
Signup a new user in an existing Azure API Management instance and specify a password.
154154

155155
```powershell
156-
PS> Create-AzApiManagementUser -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -Password $Password -ConfirmationType signup
156+
PS> Create-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -Password $Password -ConfirmationType signup
157157
# Attempting to create account for FirstName $LastName ($MailAddress)
158158
# Account has been created for FirstName $LastName ($MailAddress)
159159
```
160160

161+
## Removing a user from an Azure API Management service
162+
163+
Remove a user from an existing Azure API Management instance based on e-mail address.
164+
165+
| Parameter | Mandatory | Description |
166+
| ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
167+
| `ResourceGroupName` | yes | The resource group containing the Azure API Management instance |
168+
| `ServiceName` | yes | The name of the Azure API Management instance located in Azure |
169+
| `MailAddress` | yes | The email address of the user that is to be removed |
170+
| `SubscriptionId` | no | The Id of the subscription containing the Azure API Management instance. When not provided, it will be retrieved from the current context (Get-AzContext). |
171+
| `AccessToken` | no | The access token to be used to add the user to the Azure API Management instance. When not provided, it will be retrieved from the current context (Get-AzContext). |
172+
173+
**Example**
174+
175+
Remove a user from an existing Azure API Management instance.
176+
177+
```powershell
178+
PS> Remove-AzApiManagementUserAccount -ResourceGroupName $ResourceGroup -ServiceName $ServiceName -MailAddress $MailAddress
179+
# Retrieving the user account with e-mail '$MailAddress'
180+
# Attempting to remove the user account with e-mail '$MailAddress' and id '1'
181+
# Removed the user account with e-mail '$MailAddress' and id '1'
182+
```
183+
161184
## Importing a policy to a product in the Azure API Management instance
162185

163186
Imports a policy from a file to a product in Azure API Management.
Binary file not shown.

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

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Export-ModuleMember -Function Create-AzApiManagementApiOperation
111111
Creates a user in Azure API Management.
112112
113113
.Description
114-
Signup or invite a new user in an existing API in Azure API Management.
114+
Signup or invite a new user in an existing Azure API Management instance.
115115
116116
.Parameter ResourceGroupName
117117
The resource group containing the API Management service.
@@ -152,7 +152,7 @@ Export-ModuleMember -Function Create-AzApiManagementApiOperation
152152
.Parameter AccessToken
153153
[Optional] The access token to be used. When not provided, it will be retrieved from the current context (Get-AzContext).
154154
#>
155-
function Create-AzApiManagementUser {
155+
function Create-AzApiManagementUserAccount {
156156
param(
157157
[string][Parameter(Mandatory = $true)] $ResourceGroupName = $(throw "Resource group name is required"),
158158
[string][parameter(Mandatory = $true)] $ServiceName = $(throw "API management service name is required"),
@@ -169,13 +169,50 @@ function Create-AzApiManagementUser {
169169
[string][parameter(Mandatory = $false)] $AccessToken
170170
)
171171
if ($SendNotification) {
172-
. $PSScriptRoot\Scripts\Create-AzApiManagementUser.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -UserId $UserId -Password $Password -Note $Note -ConfirmationType $ConfirmationType -ApiVersion $ApiVersion -SubscriptionId $SubscriptionId -AccessToken $AccessToken -SendNotification
172+
. $PSScriptRoot\Scripts\Create-AzApiManagementUserAccount.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -UserId $UserId -Password $Password -Note $Note -ConfirmationType $ConfirmationType -ApiVersion $ApiVersion -SubscriptionId $SubscriptionId -AccessToken $AccessToken -SendNotification
173173
} else {
174-
. $PSScriptRoot\Scripts\Create-AzApiManagementUser.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -UserId $UserId -Password $Password -Note $Note -ConfirmationType $ConfirmationType -ApiVersion $ApiVersion -SubscriptionId $SubscriptionId -AccessToken $AccessToken
174+
. $PSScriptRoot\Scripts\Create-AzApiManagementUserAccount.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -FirstName $FirstName -LastName $LastName -MailAddress $MailAddress -UserId $UserId -Password $Password -Note $Note -ConfirmationType $ConfirmationType -ApiVersion $ApiVersion -SubscriptionId $SubscriptionId -AccessToken $AccessToken
175175
}
176176
}
177177

178-
Export-ModuleMember -Function Create-AzApiManagementUser
178+
Export-ModuleMember -Function Create-AzApiManagementUserAccount
179+
180+
<#
181+
.Synopsis
182+
Removes a user from Azure API Management.
183+
184+
.Description
185+
Remove a user from Azure API Management based on e-mail address.
186+
187+
.Parameter ResourceGroupName
188+
The resource group containing the API Management service.
189+
190+
.Parameter ServiceName
191+
The name of the API Management service located in Azure.
192+
193+
.Parameter MailAddress
194+
The e-mail address of the user.
195+
196+
.Parameter SubscriptionId
197+
[Optional] The Id of the subscription containing the Azure API Management service. When not provided, it will be retrieved from the current context (Get-AzContext).
198+
199+
.Parameter AccessToken
200+
[Optional] The access token to be used. When not provided, it will be retrieved from the current context (Get-AzContext).
201+
#>
202+
function Remove-AzApiManagementUserAccount {
203+
param(
204+
[string][Parameter(Mandatory = $true)] $ResourceGroupName = $(throw "Resource group name is required"),
205+
[string][parameter(Mandatory = $true)] $ServiceName = $(throw "API management service name is required"),
206+
[string][parameter(Mandatory = $true)] $MailAddress = $(throw "The mail-address of the user is required"),
207+
[string][parameter(Mandatory = $false)] $SubscriptionId,
208+
[string][parameter(Mandatory = $false)] $AccessToken
209+
)
210+
211+
. $PSScriptRoot\Scripts\Remove-AzApiManagementUserAccount.ps1 -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName -MailAddress $MailAddress
212+
213+
}
214+
215+
Export-ModuleMember -Function Remove-AzApiManagementUserAccount
179216

180217
<#
181218
.Synopsis

src/Arcus.Scripting.ApiManagement/Arcus.Scripting.ApiManagement.pssproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@
3333
<Compile Include="Arcus.Scripting.ApiManagement.psm1" />
3434
<Compile Include="Scripts\Backup-AzApiManagementService.ps1" />
3535
<Compile Include="Scripts\Create-AzApiManagementApiOperation.ps1" />
36-
<Compile Include="Scripts\Create-AzApiManagementUser.ps1" />
36+
<Compile Include="Scripts\Create-AzApiManagementUserAccount.ps1" />
3737
<Compile Include="Scripts\Import-AzApiManagementProductPolicy.ps1" />
3838
<Compile Include="Scripts\Remove-AzApiManagementDefaults.ps1" />
3939
<Compile Include="Scripts\Import-AzApiManagementApiPolicy.ps1" />
4040
<Compile Include="Scripts\Import-AzApiManagementOperationPolicy.ps1" />
41+
<Compile Include="Scripts\Remove-AzApiManagementUserAccount.ps1" />
4142
<Compile Include="Scripts\Restore-AzApiManagementService.ps1" />
4243
<Compile Include="Scripts\Set-AzApiManagementApiSubscriptionKey.ps1" />
4344
<Compile Include="Scripts\Upload-AzApiManagementCertificate.ps1" />

src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementUser.ps1 renamed to src/Arcus.Scripting.ApiManagement/Scripts/Create-AzApiManagementUserAccount.ps1

File renamed without changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
param(
2+
[string][Parameter(Mandatory = $true)] $ResourceGroupName = $(throw "Resource group name is required"),
3+
[string][parameter(Mandatory = $true)] $ServiceName = $(throw "API management service name is required"),
4+
[string][parameter(Mandatory = $true)] $MailAddress = $(throw "The mail-address of the user is required"),
5+
[string][parameter(Mandatory = $false)] $SubscriptionId,
6+
[string][parameter(Mandatory = $false)] $AccessToken
7+
)
8+
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+
}
13+
$apimContext = New-AzApiManagementContext -ResourceGroupName $ResourceGroupName -ServiceName $ServiceName
14+
15+
if ($SubscriptionId -eq "" -or $AccessToken -eq "") {
16+
# Request accessToken in case the script contains no records
17+
$token = Get-AzCachedAccessToken
18+
19+
$AccessToken = $token.AccessToken
20+
$SubscriptionId = $token.SubscriptionId
21+
}
22+
23+
try {
24+
Write-Host "Retrieving the user account with e-mail '$mailAddress'"
25+
$apimUser = Get-AzApiManagementUser -Context $apimContext -Email $MailAddress
26+
27+
if ($apimUser -ne $null) {
28+
$apimUserId = $apimUser.UserId
29+
30+
Write-Host "Attempting to remove the user account with e-mail '$mailAddress' and id '$apimUserId'"
31+
Remove-AzApiManagementUser -Context $apimContext -UserId $apimUserId
32+
Write-Host "Removed the user account with e-mail '$mailAddress' and id '$apimUserId'"
33+
} else {
34+
Write-Host "User account with e-mail '$mailAddress' not found in the APIM instance '$ServiceName'"
35+
}
36+
}
37+
catch {
38+
Write-Host $_
39+
throw "Failed to remove the user account for '$MailAddress' in the APIM instance '$ServiceName'"
40+
}

0 commit comments

Comments
 (0)