Skip to content

Commit 5077e1f

Browse files
authored
Fix for API version cache (Azure#34974)
* Fix for Api version cache. * Combine override after fetching from remote * Get API version from override if exists * Avoid API request if requsted resource type exists in override * Revert unnecessary change.
1 parent b731d56 commit 5077e1f

File tree

5 files changed

+12972
-22
lines changed

5 files changed

+12972
-22
lines changed

sdk/resourcemanager/Azure.ResourceManager/src/ArmClient.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.ComponentModel;
88
using System.Linq;
9-
using System.Text.Json;
109
using System.Threading;
1110
using System.Threading.Tasks;
1211
using Azure.Core;
@@ -114,12 +113,6 @@ private void CopyApiVersionOverrides(ArmClientOptions options)
114113
foreach (var keyValuePair in options.ResourceApiVersionOverrides)
115114
{
116115
ApiVersionOverrides.Add(keyValuePair.Key, keyValuePair.Value);
117-
if (!ResourceApiVersionCache.TryGetValue(keyValuePair.Key.Namespace, out var apiVersionCache))
118-
{
119-
apiVersionCache = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
120-
ResourceApiVersionCache.TryAdd(keyValuePair.Key.Namespace, apiVersionCache);
121-
}
122-
apiVersionCache.Add(keyValuePair.Key.Type, keyValuePair.Value);
123116
}
124117
}
125118

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

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ internal virtual string GetApiVersion(ResourceType resourceType, CancellationTok
4242
{
4343
string version;
4444
Dictionary<string, string> resourceVersions;
45-
if (!Client.ResourceApiVersionCache.TryGetValue(resourceType.Namespace, out resourceVersions))
45+
if (!Client.ApiVersionOverrides.TryGetValue(resourceType, out version))
4646
{
47-
resourceVersions = LoadResourceVersionsFromApi(resourceType.Namespace, cancellationToken);
48-
Client.ResourceApiVersionCache.TryAdd(resourceType.Namespace, resourceVersions);
49-
}
50-
if (!resourceVersions.TryGetValue(resourceType.Type, out version))
51-
{
52-
throw new InvalidOperationException($"Invalid resource type {resourceType}");
47+
if (!Client.ResourceApiVersionCache.TryGetValue(resourceType.Namespace, out resourceVersions))
48+
{
49+
resourceVersions = LoadResourceVersionsFromApi(resourceType.Namespace, cancellationToken);
50+
Client.ResourceApiVersionCache.TryAdd(resourceType.Namespace, resourceVersions);
51+
}
52+
if (!resourceVersions.TryGetValue(resourceType.Type, out version))
53+
{
54+
throw new InvalidOperationException($"Invalid resource type {resourceType}");
55+
}
5356
}
5457
return version;
5558
}
@@ -59,14 +62,17 @@ internal virtual async ValueTask<string> GetApiVersionAsync(ResourceType resourc
5962
{
6063
string version;
6164
Dictionary<string, string> resourceVersions;
62-
if (!Client.ResourceApiVersionCache.TryGetValue(resourceType.Namespace, out resourceVersions))
63-
{
64-
resourceVersions = await LoadResourceVersionsFromApiAsync(resourceType.Namespace, cancellationToken).ConfigureAwait(false);
65-
Client.ResourceApiVersionCache.TryAdd(resourceType.Namespace, resourceVersions);
66-
}
67-
if (!resourceVersions.TryGetValue(resourceType.Type, out version))
65+
if (!Client.ApiVersionOverrides.TryGetValue(resourceType, out version))
6866
{
69-
throw new InvalidOperationException($"Invalid resource type {resourceType}");
67+
if (!Client.ResourceApiVersionCache.TryGetValue(resourceType.Namespace, out resourceVersions))
68+
{
69+
resourceVersions = await LoadResourceVersionsFromApiAsync(resourceType.Namespace, cancellationToken).ConfigureAwait(false);
70+
Client.ResourceApiVersionCache.TryAdd(resourceType.Namespace, resourceVersions);
71+
}
72+
if (!resourceVersions.TryGetValue(resourceType.Type, out version))
73+
{
74+
throw new InvalidOperationException($"Invalid resource type {resourceType}");
75+
}
7076
}
7177
return version;
7278
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Azure.Core;
55
using Azure.Core.TestFramework;
66
using Azure.ResourceManager.Resources;
7-
using Azure.ResourceManager.Resources.Models;
87
using NUnit.Framework;
98

109
namespace Azure.ResourceManager.Tests
@@ -230,5 +229,19 @@ public async Task StartUpdate()
230229
_ = await updateOp.WaitForCompletionAsync();
231230
});
232231
}
232+
233+
[Test]
234+
[RecordedTest]
235+
public async Task CreateWithApiVersionSetting()
236+
{
237+
var options = new ArmClientOptions();
238+
options.SetApiVersion(new ResourceType("Microsoft.Compute/test"), "2022-09-01");
239+
var client = GetArmClient(options);
240+
var rgOp = await (await client.GetDefaultSubscriptionAsync().ConfigureAwait(false)).GetResourceGroups().Construct(AzureLocation.WestUS2).CreateOrUpdateAsync(Recording.GenerateAssetName("testrg"));
241+
GenericResourceData data = ConstructGenericAvailabilitySet();
242+
var asetId = rgOp.Value.Id.AppendProviderResource("Microsoft.Compute", "availabilitySets", Recording.GenerateAssetName("test-aset"));
243+
var op = await client.GetGenericResources().CreateOrUpdateAsync(WaitUntil.Completed, asetId, data);
244+
Assert.NotNull(op.Value);
245+
}
233246
}
234247
}

0 commit comments

Comments
 (0)