Skip to content

Commit 6151629

Browse files
authored
[KeyVault] Enable key vault tag update (#13323)
* enable key vault tag update * update update-azkeyvault markdown file * Provide examples about tags update
1 parent fe50523 commit 6151629

File tree

7 files changed

+635
-250
lines changed

7 files changed

+635
-250
lines changed

src/KeyVault/KeyVault.Test/KeyVault.Test.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@
2525
<None Update="Scripts\ControlPlane\KeyVaultManagementTests.ps1" CopyToOutputDirectory="PreserveNewest" />
2626
</ItemGroup>
2727

28-
<ItemGroup>
29-
<Folder Include="ScenarioTests\PesterTests\" />
30-
</ItemGroup>
31-
3228
</Project>
3329

src/KeyVault/KeyVault.Test/Scripts/ControlPlane/KeyVaultManagementTests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,16 @@ function Test-UpdateKeyVault {
800800
$vault = $vault | Update-AzKeyVault -EnableRbacAuthorization $false
801801
Assert-False { $vault.EnableRbacAuthorization } "6. EnableRbacAuthorization should be false"
802802

803+
# Update Tags
804+
$vault = $vault | Update-AzKeyVault -Tag @{key = "value"}
805+
Assert-AreEqual 1 $vault.Tags.Count "7. Tags should contain a key-value pair (key, value)"
806+
Assert-True { $vault.Tags.Contains("key") } "7. Tags should contain a key-value pair (key, value)"
807+
Assert-AreEqual "value" $vault.Tags["key"] "7. Tags should contain a key-value pair (key, value)"
808+
809+
# Clean Tags
810+
$vault = $vault | Update-AzKeyVault -Tag @{}
811+
Assert-AreEqual 0 $vault.Tags.Count "8. Tags should be empty"
812+
803813
}
804814
finally {
805815
$rg | Remove-AzResourceGroup -Force

src/KeyVault/KeyVault.Test/SessionRecords/Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests.KeyVaultManagementTests/TestUpdateVault.json

Lines changed: 528 additions & 228 deletions
Large diffs are not rendered by default.

src/KeyVault/KeyVault/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Supported updating key vault tag
2122

2223
## Version 3.0.0
2324
* [Breaking Change] Deprecated parameter DisableSoftDelete in `New-AzKeyVault` and EnableSoftDelete in `Update-AzKeyVault`

src/KeyVault/KeyVault/Commands/UpdateAzureKeyVault.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@
1515
using Microsoft.Azure.Commands.KeyVault.Models;
1616
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1717
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
1918
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2019
using System;
21-
using System.Collections.Generic;
20+
using System.Collections;
2221
using System.Management.Automation;
23-
using System.Text;
2422

2523
namespace Microsoft.Azure.Commands.KeyVault
2624
{
@@ -56,6 +54,12 @@ public class UpdateTopLevelResourceCommand : KeyVaultManagementCmdletBase
5654
[Parameter(Mandatory = false, HelpMessage = "Enable or disable this key vault to authorize data actions by Role Based Access Control (RBAC).")]
5755
public bool? EnableRbacAuthorization { get; set; }
5856

57+
[Parameter(Mandatory = false,
58+
ValueFromPipelineByPropertyName = true,
59+
HelpMessage = "A hash table which represents resource tags.")]
60+
[Alias(Constants.TagsAlias)]
61+
public Hashtable Tag { get; set; }
62+
5963
public override void ExecuteCmdlet()
6064
{
6165
if (this.IsParameterBound(c => c.InputObject))
@@ -88,16 +92,14 @@ public override void ExecuteCmdlet()
8892

8993
if (this.ShouldProcess(this.VaultName, string.Format("Updating key vault '{0}' in resource group '{1}'.", this.VaultName, this.ResourceGroupName)))
9094
{
91-
var result = KeyVaultManagementClient.UpdateVault(existingResource,
92-
existingResource.AccessPolicies,
93-
existingResource.EnabledForDeployment,
94-
existingResource.EnabledForTemplateDeployment,
95-
existingResource.EnabledForDiskEncryption,
96-
null,
97-
EnablePurgeProtection.IsPresent ? (true as bool?) : null,
98-
EnableRbacAuthorization,
99-
null,
100-
existingResource.NetworkAcls
95+
var result = KeyVaultManagementClient.UpdateVault(
96+
existingResource,
97+
updatedParamater: new VaultCreationOrUpdateParameters
98+
{
99+
EnablePurgeProtection = this.EnablePurgeProtection.IsPresent ? (true as bool?) : null,
100+
EnableRbacAuthorization = this.EnableRbacAuthorization,
101+
Tags = this.Tag
102+
}
101103
);
102104

103105
WriteObject(result);

src/KeyVault/KeyVault/Models/VaultManagementClient.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,46 @@ public PSKeyVault GetVault(string vaultName, string resourceGroupName, ActiveDir
163163
}
164164
}
165165

166+
/// <summary>
167+
/// Update an existing vault. Only EnablePurgeProtection, EnableRbacAuthorization and Tags can be updated currently.
168+
/// </summary>
169+
/// <param name="existingVault">the existing vault</param>
170+
/// <param name="updatedParamater">updated paramater</param>
171+
/// <param name="adClient">the active directory client</param>
172+
/// <returns>the updated vault</returns>
173+
public PSKeyVault UpdateVault(
174+
PSKeyVault existingVault,
175+
VaultCreationOrUpdateParameters updatedParamater,
176+
ActiveDirectoryClient adClient = null)
177+
{
178+
if (existingVault == null)
179+
throw new ArgumentNullException("existingVault");
180+
if (existingVault.OriginalVault == null)
181+
throw new ArgumentNullException("existingVault.OriginalVault");
182+
183+
//Update the vault properties in the object received from server
184+
var properties = existingVault.OriginalVault.Properties;
185+
186+
if (!(properties.EnablePurgeProtection.HasValue && properties.EnablePurgeProtection.Value)
187+
&& updatedParamater.EnablePurgeProtection.HasValue
188+
&& updatedParamater.EnablePurgeProtection.Value)
189+
properties.EnablePurgeProtection = updatedParamater.EnablePurgeProtection;
190+
191+
properties.EnableRbacAuthorization = updatedParamater.EnableRbacAuthorization;
192+
193+
var response = KeyVaultManagementClient.Vaults.CreateOrUpdate(
194+
resourceGroupName: existingVault.ResourceGroupName,
195+
vaultName: existingVault.VaultName,
196+
parameters: new VaultCreateOrUpdateParameters
197+
{
198+
Location = existingVault.Location,
199+
Properties = properties,
200+
Tags = TagsConversionHelper.CreateTagDictionary(updatedParamater.Tags, validate: true)
201+
}
202+
);
203+
return new PSKeyVault(response, adClient);
204+
}
205+
166206
/// <summary>
167207
/// Update an existing vault. Only EnabledForDeployment and AccessPolicies can be updated currently.
168208
/// </summary>

src/KeyVault/KeyVault/help/Update-AzKeyVault.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,55 @@ Update the state of an Azure key vault.
1515
### UpdateByNameParameterSet (Default)
1616
```
1717
Update-AzKeyVault -ResourceGroupName <String> -VaultName <String> [-EnablePurgeProtection]
18-
[-EnableRbacAuthorization <Boolean>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
19-
[<CommonParameters>]
18+
[-EnableRbacAuthorization <Boolean>] [-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
19+
[-Confirm] [<CommonParameters>]
2020
```
2121

2222
### UpdateByInputObjectParameterSet
2323
```
2424
Update-AzKeyVault -InputObject <PSKeyVault> [-EnablePurgeProtection] [-EnableRbacAuthorization <Boolean>]
25-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
25+
[-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2626
```
2727

2828
### UpdateByResourceIdParameterSet
2929
```
3030
Update-AzKeyVault -ResourceId <String> [-EnablePurgeProtection] [-EnableRbacAuthorization <Boolean>]
31-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
31+
[-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
3232
```
3333

3434
## DESCRIPTION
3535
This cmdlet updates the state of an Azure key vault.
3636

3737
## EXAMPLES
3838

39-
### Example 2
39+
### Example 1: Enable purge protection
4040
```powershell
4141
PS C:\> Get-AzKeyVault -VaultName $keyVaultName -ResourceGroupName $resourceGroupName | Update-AzKeyVault -EnablePurgeProtection
4242
```
4343

4444
Enables purge protection using piping syntax.
4545

46+
### Example 2: Enable RBAC Authorization
47+
```powershell
48+
PS C:\> Get-AzKeyVault -VaultName $keyVaultName -ResourceGroupName $resourceGroupName | Update-AzKeyVault -EnableRbacAuthorization $true
49+
```
50+
51+
Enables RBAC Authorization using piping syntax.
52+
53+
### Example 3: Set tags
54+
```powershell
55+
PS C:\> Get-AzKeyVault -VaultName $keyVaultName | Update-AzKeyVault -Tags @{key = "value"}
56+
```
57+
58+
Sets the tags of a key vault named $keyVaultName.
59+
60+
### Example 4: Clean tags
61+
```powershell
62+
PS C:\> Get-AzKeyVault -VaultName $keyVaultName | Update-AzKeyVault -Tags @{}
63+
```
64+
65+
Deletes all tags of a key vault named $keyVaultName.
66+
4667
## PARAMETERS
4768

4869
### -DefaultProfile
@@ -137,6 +158,21 @@ Accept pipeline input: True (ByPropertyName)
137158
Accept wildcard characters: False
138159
```
139160
161+
### -Tag
162+
A hash table which represents resource tags.
163+
164+
```yaml
165+
Type: System.Collections.Hashtable
166+
Parameter Sets: (All)
167+
Aliases: Tags
168+
169+
Required: False
170+
Position: Named
171+
Default value: None
172+
Accept pipeline input: True (ByPropertyName)
173+
Accept wildcard characters: False
174+
```
175+
140176
### -VaultName
141177
Name of the key vault.
142178

0 commit comments

Comments
 (0)