Skip to content

Commit 3d906ff

Browse files
vaishali33shahabhijeet
authored andcommitted
Fix for showing on GET and persisting Tags on SET (if not given) for Database, Server and Elastic Pool (Azure#3206)
* Fixed Azure Sql Tags for Database, server and elastic pools. * Fixed Azure Sql Tags for Database, server and elastic pools on both GET and SET. Fixed SET server command. * Added tests and addressed comments from code review. * Added tests and addressed comments from code review.
1 parent 58f4edd commit 3d906ff

File tree

14 files changed

+98
-55
lines changed

14 files changed

+98
-55
lines changed

src/ResourceManager/Common/Commands.ResourceManager.Common/Tags/TagsConversionHelper.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections;
1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using System.Management.Automation;
1920
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Common.Properties.Resources;
2021

2122
namespace Microsoft.Azure.Commands.ResourceManager.Common.Tags
@@ -93,5 +94,20 @@ public static Hashtable CreateTagHashtable(IDictionary<string, string> dictionar
9394
}
9495
return tagsHashtable;
9596
}
97+
98+
public static Dictionary<string, string> ReadOrFetchTags(PSCmdlet cmdlet, Dictionary<string,string> tagsFromModel)
99+
{
100+
object tagsFromCli;
101+
if (cmdlet.MyInvocation.BoundParameters.TryGetValue("Tags", out tagsFromCli))
102+
{
103+
Hashtable tags = tagsFromCli as Hashtable;
104+
return TagsConversionHelper.CreateTagDictionary(tags, validate: true);
105+
}
106+
else
107+
{
108+
return tagsFromModel;
109+
}
110+
}
111+
96112
}
97113
}

src/ResourceManager/Sql/ChangeLog.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@
2323
- RetentionInDays
2424
* Removed the unsupported param "AuditAction" from Set-AzureSqlDatabaseServerAuditingPolicy
2525
* Added new param "AuditAction" to Set-AzureSqlDatabaseAuditingPolicy
26-
26+
* Fix for showing on GET and persisting Tags on SET (if not given) for Database, Server and Elastic Pool
27+
- If Tags is used in command it will save tags, if not it will not wipe out tags on resource.
2728
## Version 2.3.0
29+
* Fix for showing on GET and persisting Tags on SET (if not given) for Database, Server and Elastic Pool
30+
- If Tags is used in command it will save tags, if not it will not wipe out tags on resource.

src/ResourceManager/Sql/Commands.Sql.Test/ScenarioTests/DatabaseCrudTests.ps1

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,28 @@ function Test-CreateDatabaseInternal ($serverVersion, $location = "Japan East")
7878
# Create with all parameters
7979
$databaseName = Get-DatabaseName
8080
$db = New-AzureRmSqlDatabase -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -DatabaseName $databaseName `
81-
-CollationName "Japanese_Bushu_Kakusu_100_CS_AS" -MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic
81+
-CollationName "Japanese_Bushu_Kakusu_100_CS_AS" -MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic -Tags @{"tag_key"="tag_value"}
8282
Assert-AreEqual $db.DatabaseName $databaseName
8383
Assert-AreEqual $db.MaxSizeBytes 1GB
8484
Assert-AreEqual $db.Edition Basic
8585
Assert-AreEqual $db.CurrentServiceObjectiveName Basic
8686
Assert-AreEqual $db.CollationName "Japanese_Bushu_Kakusu_100_CS_AS"
87+
Assert-NotNull $db.Tags
88+
Assert-AreEqual True $db.Tags.ContainsKey("tag_key")
89+
Assert-AreEqual "tag_value" $db.Tags["tag_key"]
8790

8891
# Create with all parameters
8992
$databaseName = Get-DatabaseName
9093
$db = $server | New-AzureRmSqlDatabase -DatabaseName $databaseName `
91-
-CollationName "Japanese_Bushu_Kakusu_100_CS_AS" -MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic
94+
-CollationName "Japanese_Bushu_Kakusu_100_CS_AS" -MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic -Tags @{"tag_key"="tag_value"}
9295
Assert-AreEqual $db.DatabaseName $databaseName
9396
Assert-AreEqual $db.MaxSizeBytes 1GB
9497
Assert-AreEqual $db.Edition Basic
9598
Assert-AreEqual $db.CurrentServiceObjectiveName Basic
9699
Assert-AreEqual $db.CollationName "Japanese_Bushu_Kakusu_100_CS_AS"
100+
Assert-NotNull $db.Tags
101+
Assert-AreEqual True $db.Tags.ContainsKey("tag_key")
102+
Assert-AreEqual "tag_value" $db.Tags["tag_key"]
97103
}
98104
finally
99105
{
@@ -142,20 +148,26 @@ function Test-UpdateDatabaseInternal ($serverVersion, $location = "Japan East")
142148
{
143149
# Alter all properties
144150
$db1 = Set-AzureRmSqlDatabase -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName `
145-
-MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic
151+
-MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic -Tags @{"tag_key"="tag_new_value"}
146152
Assert-AreEqual $db1.DatabaseName $db.DatabaseName
147153
Assert-AreEqual $db1.MaxSizeBytes 1GB
148154
Assert-AreEqual $db1.Edition Basic
149155
Assert-AreEqual $db1.CurrentServiceObjectiveName Basic
150156
Assert-AreEqual $db1.CollationName $db.CollationName
157+
Assert-NotNull $db.Tags
158+
Assert-AreEqual True $db.Tags.ContainsKey("tag_key")
159+
Assert-AreEqual "tag_new_value" $db.Tags["tag_key"]
151160

152161
# Alter all properties using piping
153-
$db2 = $db1 | Set-AzureRmSqlDatabase -MaxSizeBytes 100GB -Edition Standard -RequestedServiceObjectiveName S1
162+
$db2 = $db1 | Set-AzureRmSqlDatabase -MaxSizeBytes 100GB -Edition Standard -RequestedServiceObjectiveName S1 -Tags @{"tag_key"="tag_new_value"}
154163
Assert-AreEqual $db2.DatabaseName $db.DatabaseName
155164
Assert-AreEqual $db2.MaxSizeBytes 100GB
156165
Assert-AreEqual $db2.Edition Standard
157166
Assert-AreEqual $db2.CurrentServiceObjectiveName S1
158167
Assert-AreEqual $db2.CollationName $db.CollationName
168+
Assert-NotNull $db.Tags
169+
Assert-AreEqual True $db.Tags.ContainsKey("tag_key")
170+
Assert-AreEqual "tag_new_value" $db.Tags["tag_key"]
159171

160172
# Create and alter data warehouse database.
161173
$databaseName = Get-DatabaseName
@@ -176,20 +188,26 @@ function Test-UpdateDatabaseInternal ($serverVersion, $location = "Japan East")
176188
{
177189
# Alter all properties
178190
$db1 = Set-AzureRmSqlDatabase -ResourceGroupName $db.ResourceGroupName -ServerName $db.ServerName -DatabaseName $db.DatabaseName `
179-
-MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic
191+
-MaxSizeBytes 1GB -Edition Basic -RequestedServiceObjectiveName Basic -Tags @{"tag_key"="tag_new_value"}
180192
Assert-AreEqual $db1.DatabaseName $db.DatabaseName
181193
Assert-AreEqual $db1.MaxSizeBytes 250GB
182194
Assert-AreEqual $db1.Edition Standard
183195
Assert-AreEqual $db1.CurrentServiceObjectiveName S0
184196
Assert-AreEqual $db1.CollationName $db.CollationName
197+
Assert-NotNull $db.Tags
198+
Assert-AreEqual True $db.Tags.ContainsKey("tag_key")
199+
Assert-AreEqual "tag_new_value" $db.Tags["tag_key"]
185200

186201
# Alter all properties using piping
187-
$db2 = $db1 | Set-AzureRmSqlDatabase -MaxSizeBytes 100GB -Edition Standard -RequestedServiceObjectiveName S1
202+
$db2 = $db1 | Set-AzureRmSqlDatabase -MaxSizeBytes 100GB -Edition Standard -RequestedServiceObjectiveName S1 -Tags @{"tag_key"="tag_new_value"}
188203
Assert-AreEqual $db2.DatabaseName $db.DatabaseName
189204
Assert-AreEqual $db2.MaxSizeBytes 1GB
190205
Assert-AreEqual $db2.Edition Basic
191206
Assert-AreEqual $db2.CurrentServiceObjectiveName Basic
192207
Assert-AreEqual $db2.CollationName $db.CollationName
208+
Assert-NotNull $db.Tags
209+
Assert-AreEqual True $db.Tags.ContainsKey("tag_key")
210+
Assert-AreEqual "tag_new_value" $db.Tags["tag_key"]
193211
}
194212
}
195213
finally

0 commit comments

Comments
 (0)