Skip to content

Commit 2916566

Browse files
Add SetApiVersionsFromProfile in ArmClientOptions (Azure#32831)
1 parent cf56516 commit 2916566

20 files changed

+1239
-222
lines changed

sdk/resourcemanager/Azure.ResourceManager/api/Azure.ResourceManager.netstandard2.0.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public sealed partial class ArmClientOptions : Azure.Core.ClientOptions
4040
public ArmClientOptions() { }
4141
public Azure.ResourceManager.ArmEnvironment? Environment { get { throw null; } set { } }
4242
public void SetApiVersion(Azure.Core.ResourceType resourceType, string apiVersion) { }
43+
public void SetApiVersionsFromProfile(Azure.ResourceManager.AzureStackProfile profile) { }
4344
}
4445
public abstract partial class ArmCollection
4546
{
@@ -108,6 +109,10 @@ protected internal ArmResource(Azure.ResourceManager.ArmClient client, Azure.Cor
108109
public virtual Azure.ResourceManager.Resources.TagResource GetTagResource() { throw null; }
109110
protected virtual bool TryGetApiVersion(Azure.Core.ResourceType resourceType, out string apiVersion) { throw null; }
110111
}
112+
public enum AzureStackProfile
113+
{
114+
Profile20200901Hybrid = 0,
115+
}
111116
}
112117
namespace Azure.ResourceManager.ManagementGroups
113118
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.ComponentModel;
88
using System.Linq;
9+
using System.Text.Json;
910
using System.Threading;
1011
using System.Threading.Tasks;
1112
using Azure.Core;
@@ -27,8 +28,7 @@ public partial class ArmClient
2728
private bool? _canUseTagResource;
2829

2930
internal virtual Dictionary<ResourceType, string> ApiVersionOverrides { get; } = new Dictionary<ResourceType, string>();
30-
31-
internal ConcurrentDictionary<string, Dictionary<string, string>> ResourceApiVersionCache { get; } = new ConcurrentDictionary<string, Dictionary<string, string>>();
31+
internal ConcurrentDictionary<string, Dictionary<string, string>> ResourceApiVersionCache { get; } = new ConcurrentDictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
3232
internal ConcurrentDictionary<string, string> NamespaceVersionCache { get; } = new ConcurrentDictionary<string, string>();
3333

3434
/// <summary>
@@ -116,7 +116,7 @@ private void CopyApiVersionOverrides(ArmClientOptions options)
116116
ApiVersionOverrides.Add(keyValuePair.Key, keyValuePair.Value);
117117
if (!ResourceApiVersionCache.TryGetValue(keyValuePair.Key.Namespace, out var apiVersionCache))
118118
{
119-
apiVersionCache = new Dictionary<string, string>();
119+
apiVersionCache = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
120120
ResourceApiVersionCache.TryAdd(keyValuePair.Key.Namespace, apiVersionCache);
121121
}
122122
apiVersionCache.Add(keyValuePair.Key.Type, keyValuePair.Value);

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Reflection;
8+
using System.Text.Json;
69
using Azure.Core;
710
using Azure.ResourceManager.Resources;
811

@@ -35,5 +38,42 @@ public void SetApiVersion(ResourceType resourceType, string apiVersion)
3538

3639
ResourceApiVersionOverrides[resourceType] = apiVersion;
3740
}
41+
42+
/// <summary>
43+
/// Sets the api versions from an Azure Stack profile.
44+
/// </summary>
45+
public void SetApiVersionsFromProfile(AzureStackProfile profile)
46+
{
47+
var assembly = Assembly.GetExecutingAssembly();
48+
using (Stream stream = assembly.GetManifestResourceStream(profile.GetManifestName()))
49+
{
50+
var allProfile = BinaryData.FromStream(stream).ToObjectFromJson<Dictionary<string, Dictionary<string, object>>>();
51+
var armProfile = allProfile["resource-manager"];
52+
foreach (var keyValuePair in armProfile)
53+
{
54+
var namespaceName = keyValuePair.Key;
55+
var element = (JsonElement)keyValuePair.Value;
56+
57+
foreach (var apiVersionProperty in element.EnumerateObject())
58+
{
59+
var apiVersion = apiVersionProperty.Name;
60+
foreach (var resourceTypeItem in apiVersionProperty.Value.EnumerateArray())
61+
{
62+
string resourceTypeName = default;
63+
foreach (var property in resourceTypeItem.EnumerateObject())
64+
{
65+
if (property.NameEquals("resourceType"))
66+
{
67+
resourceTypeName = property.Value.GetString();
68+
break;
69+
}
70+
}
71+
var resourceType = $"{namespaceName}/{resourceTypeName}";
72+
ResourceApiVersionOverrides[resourceType] = apiVersion;
73+
}
74+
}
75+
}
76+
}
77+
}
3878
}
3979
}

0 commit comments

Comments
 (0)