Skip to content

Commit c1b2d46

Browse files
m-nashArcturusZhangHarveyLink
authored
update generic resource tag updates (Azure#47772)
* wip to update generic resource tag updates * remove temp logging * re-record --------- Co-authored-by: Arcturus Zhang <[email protected]> Co-authored-by: Minghao Chen <[email protected]>
1 parent f01e2fe commit c1b2d46

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

sdk/resourcemanager/Azure.ResourceManager/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "net",
44
"TagPrefix": "net/resourcemanager/Azure.ResourceManager",
5-
"Tag": "net/resourcemanager/Azure.ResourceManager_9a9a9534b3"
5+
"Tag": "net/resourcemanager/Azure.ResourceManager_dd685d29e8"
66
}

sdk/resourcemanager/Azure.ResourceManager/src/Resources/Custom/GenericResource.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Azure.Core;
1111
using Azure.Core.Pipeline;
12+
using Azure.ResourceManager.Resources.Models;
1213

1314
[assembly: CodeGenSuppressType("GenericResourceFilter")]
1415
[assembly: CodeGenSuppressType("GenericResource")]
@@ -192,9 +193,8 @@ public async virtual Task<Response<GenericResource>> AddTagAsync(string key, str
192193
try
193194
{
194195
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
195-
var originalTags = await GetTagResource().GetAsync(cancellationToken).ConfigureAwait(false);
196-
originalTags.Value.Data.Properties.TagValues[key] = value;
197-
await GetTagResource().CreateOrUpdateAsync(WaitUntil.Completed, originalTags.Value.Data, cancellationToken: cancellationToken).ConfigureAwait(false);
196+
var tagPatch = new TagResourcePatch(TagPatchMode.Merge, new Tag(new Dictionary<string, string> { { key, value } }, null), null);
197+
await GetTagResource().UpdateAsync(WaitUntil.Completed, tagPatch, cancellationToken).ConfigureAwait(false);
198198
var originalResponse = await _resourcesRestClient.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false);
199199
return Response.FromValue(new GenericResource(Client, originalResponse.Value), originalResponse.GetRawResponse());
200200
}
@@ -222,9 +222,8 @@ public virtual Response<GenericResource> AddTag(string key, string value, Cancel
222222
try
223223
{
224224
var apiVersion = GetApiVersion(cancellationToken);
225-
var originalTags = GetTagResource().Get(cancellationToken);
226-
originalTags.Value.Data.Properties.TagValues[key] = value;
227-
GetTagResource().CreateOrUpdate(WaitUntil.Completed, originalTags.Value.Data, cancellationToken: cancellationToken);
225+
var tagPatch = new TagResourcePatch(TagPatchMode.Merge, new Tag(new Dictionary<string, string> { { key, value } }, null), null);
226+
GetTagResource().Update(WaitUntil.Completed, tagPatch, cancellationToken: cancellationToken);
228227
var originalResponse = _resourcesRestClient.GetById(Id, apiVersion, cancellationToken);
229228
return Response.FromValue(new GenericResource(Client, originalResponse.Value), originalResponse.GetRawResponse());
230229
}
@@ -251,10 +250,7 @@ public async virtual Task<Response<GenericResource>> SetTagsAsync(IDictionary<st
251250
try
252251
{
253252
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
254-
await GetTagResource().DeleteAsync(WaitUntil.Completed, cancellationToken: cancellationToken).ConfigureAwait(false);
255-
var originalTags = await GetTagResource().GetAsync(cancellationToken).ConfigureAwait(false);
256-
originalTags.Value.Data.Properties.TagValues.ReplaceWith(tags);
257-
await GetTagResource().CreateOrUpdateAsync(WaitUntil.Completed, originalTags.Value.Data, cancellationToken: cancellationToken).ConfigureAwait(false);
253+
await GetTagResource().CreateOrUpdateAsync(WaitUntil.Completed, new TagResourceData(new Tag(tags, null)), cancellationToken: cancellationToken).ConfigureAwait(false);
258254
var originalResponse = await _resourcesRestClient.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false);
259255
return Response.FromValue(new GenericResource(Client, originalResponse.Value), originalResponse.GetRawResponse());
260256
}
@@ -281,10 +277,7 @@ public virtual Response<GenericResource> SetTags(IDictionary<string, string> tag
281277
try
282278
{
283279
var apiVersion = GetApiVersion(cancellationToken);
284-
GetTagResource().Delete(WaitUntil.Completed, cancellationToken: cancellationToken);
285-
var originalTags = GetTagResource().Get(cancellationToken);
286-
originalTags.Value.Data.Properties.TagValues.ReplaceWith(tags);
287-
GetTagResource().CreateOrUpdate(WaitUntil.Completed, originalTags.Value.Data, cancellationToken: cancellationToken);
280+
GetTagResource().CreateOrUpdate(WaitUntil.Completed, new TagResourceData(new Tag(tags, null)), cancellationToken: cancellationToken);
288281
var originalResponse = _resourcesRestClient.GetById(Id, apiVersion, cancellationToken);
289282
return Response.FromValue(new GenericResource(Client, originalResponse.Value), originalResponse.GetRawResponse());
290283
}
@@ -311,9 +304,8 @@ public async virtual Task<Response<GenericResource>> RemoveTagAsync(string key,
311304
try
312305
{
313306
var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);
314-
var originalTags = await GetTagResource().GetAsync(cancellationToken).ConfigureAwait(false);
315-
originalTags.Value.Data.Properties.TagValues.Remove(key);
316-
await GetTagResource().CreateOrUpdateAsync(WaitUntil.Completed, originalTags.Value.Data, cancellationToken: cancellationToken).ConfigureAwait(false);
307+
var tagPatch = new TagResourcePatch(TagPatchMode.Delete, new Tag(new Dictionary<string, string> { { key, string.Empty } }, null), null);
308+
await GetTagResource().UpdateAsync(WaitUntil.Completed, tagPatch, cancellationToken: cancellationToken).ConfigureAwait(false);
317309
var originalResponse = await _resourcesRestClient.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false);
318310
return Response.FromValue(new GenericResource(Client, originalResponse.Value), originalResponse.GetRawResponse());
319311
}
@@ -340,9 +332,8 @@ public virtual Response<GenericResource> RemoveTag(string key, CancellationToken
340332
try
341333
{
342334
var apiVersion = GetApiVersion(cancellationToken);
343-
var originalTags = GetTagResource().Get(cancellationToken);
344-
originalTags.Value.Data.Properties.TagValues.Remove(key);
345-
GetTagResource().CreateOrUpdate(WaitUntil.Completed, originalTags.Value.Data, cancellationToken: cancellationToken);
335+
var tagPatch = new TagResourcePatch(TagPatchMode.Delete, new Tag(new Dictionary<string, string> { { key, string.Empty } }, null), null);
336+
GetTagResource().Update(WaitUntil.Completed, tagPatch, cancellationToken: cancellationToken);
346337
var originalResponse = _resourcesRestClient.GetById(Id, apiVersion, cancellationToken);
347338
return Response.FromValue(new GenericResource(Client, originalResponse.Value), originalResponse.GetRawResponse());
348339
}

sdk/resourcemanager/Azure.ResourceManager/tests/Scenario/GenericResourceOperationsTests.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ public async Task AddTag()
121121

122122
Assert.IsTrue(aset.Data.Tags.ContainsKey("key"));
123123
Assert.AreEqual("value", aset.Data.Tags["key"]);
124+
Assert.AreEqual(1, aset.Data.Tags.Count);
125+
126+
GenericResource aset2 = await aset.AddTagAsync("key2", "value2");
127+
128+
Assert.IsTrue(aset2.Data.Tags.ContainsKey("key2"));
129+
Assert.AreEqual("value2", aset2.Data.Tags["key2"]);
130+
Assert.AreEqual(2, aset2.Data.Tags.Count);
131+
132+
GenericResource aset3 = await aset2.AddTagAsync("key", "value3");
133+
134+
Assert.IsTrue(aset3.Data.Tags.ContainsKey("key"));
135+
Assert.AreEqual("value3", aset3.Data.Tags["key"]);
136+
Assert.AreEqual(2, aset3.Data.Tags.Count);
124137
}
125138

126139
[TestCase]
@@ -156,6 +169,15 @@ public async Task SetTags()
156169

157170
Assert.IsTrue(aset.Data.Tags.ContainsKey("key"));
158171
Assert.AreEqual("value", aset.Data.Tags["key"]);
172+
Assert.AreEqual(1, aset.Data.Tags.Count);
173+
174+
Dictionary<string, string> tags2 = new Dictionary<string, string>();
175+
tags2.Add("key2", "value2");
176+
GenericResource aset2 = await aset.SetTagsAsync(tags2);
177+
178+
Assert.IsTrue(aset2.Data.Tags.ContainsKey("key2"));
179+
Assert.AreEqual("value2", aset2.Data.Tags["key2"]);
180+
Assert.AreEqual(1, aset2.Data.Tags.Count);
159181
}
160182

161183
[TestCase]
@@ -168,12 +190,16 @@ public async Task RemoveTag()
168190

169191
Dictionary<string, string> tags = new Dictionary<string, string>();
170192
tags.Add("key", "value");
193+
tags.Add("key2", "value2");
171194
aset = await aset.SetTagsAsync(tags);
172195

196+
Assert.AreEqual(2, aset.Data.Tags.Count);
197+
173198
aset = await aset.RemoveTagAsync("key");
174199

175200
Assert.IsFalse(aset.Data.Tags.ContainsKey("key"));
176-
Assert.AreEqual(0, aset.Data.Tags.Count);
201+
Assert.IsTrue(aset.Data.Tags.ContainsKey("key2"));
202+
Assert.AreEqual(1, aset.Data.Tags.Count);
177203
}
178204

179205
[TestCase]

0 commit comments

Comments
 (0)