-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add lookup mechanism for ID to apiversion #19437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 35 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
bd06bf1
WIP: updates
bquantump a11a769
WIP: updates
bquantump 71dabee
Merge branch 'feature/mgmt-track2' of https://github.com/Azure/azure-…
bquantump b72173b
Update test
bquantump 79cb491
Update ResourceListOperationsTest.cs
bquantump f4162e3
Update CreateSingleVmExample.cs
bquantump ff8d333
Update CreateSingleVmExample.cs
bquantump 9fa1e0c
Update CreateSingleVmExample.cs
bquantump a9da33b
Update CreateSingleVmExample.cs
bquantump e35f07c
WIP: Updates
bquantump d884f56
Add extra line
bquantump fcc2aab
Update
bquantump 4f2dae5
WIP:
bquantump aee7547
WIP: updates
bquantump 6b7ba0f
Merge branch 'mgmt-track2' of https://github.com/AME-Redmond/azure-sd…
bquantump db3b7f4
WIp: updates
bquantump e5fd067
Remove API version extensions
bquantump 93da13a
WIP: updates
bquantump baacd99
Updates
bquantump c398fb8
Update test
bquantump 8e5f11d
WIP updates
bquantump 2f47da4
Merge branch 'feature/mgmt-track2' into stevens_5117
bquantump 757015c
Updates
bquantump 2bc016e
WIP
bquantump 6bdf327
Remove blank lines
bquantump 45c23e7
Update filtering
bquantump af3d5e6
Updates
bquantump 5ec195c
Update
bquantump 847e593
WIP: updates
bquantump d76be3f
WIP: updates
bquantump f267bf3
Add space
bquantump 26f4385
Remove debug
bquantump 9e26522
Updates
bquantump fcba084
WIP: updates
bquantump 3dc030d
WIP
bquantump 10f6ad0
Change the accessbility to virtual for Resource.Id
b8291aa
WIP: updates
bquantump db74f22
WIP Updates
bquantump ff09cb4
Merge branch 'mgmt-track2' of https://github.com/AME-Redmond/azure-sd…
bquantump 428f6bc
WIP updates
bquantump 2013b78
WIP
bquantump 6a2dec1
WIP
bquantump 9ea1997
WIP:
bquantump 00b5d31
revert
bquantump 431479a
WIP: updates
bquantump 69a6f6b
WIP: updates
bquantump 804515d
WIP: updated
bquantump b01050c
WIP
bquantump File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
sdk/resourcemanager/Azure.ResourceManager.Core/src/ApiVersions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using Azure.ResourceManager.Resources; | ||
| using System.Reflection; | ||
| using System.Linq; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Threading.Tasks; | ||
| using System.Threading; | ||
|
|
||
| namespace Azure.ResourceManager.Core | ||
| { | ||
| /// <summary> | ||
| /// A class representing Azure resource manager client options. | ||
| /// </summary> | ||
| public class ApiVersions | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="ApiVersions"/> class. | ||
| /// </summary> | ||
| internal ApiVersions(AzureResourceManagerClientOptions clientOptions) | ||
| { | ||
| BuildApiTable(clientOptions); | ||
| } | ||
|
|
||
| private Dictionary<string, PropertyWrapper> _loadedResourceToApiVersions = new Dictionary<string, PropertyWrapper>(); | ||
| private Dictionary<string, string> _nonLoadedResourceToApiVersion = new Dictionary<string, string>(); | ||
|
|
||
| private void BuildApiTable(AzureResourceManagerClientOptions clientOptions) | ||
| { | ||
| var methods = GetExtensionMethods(); | ||
| foreach (var method in methods) | ||
| { | ||
| if (method.Name.EndsWith("RestApiVersions", StringComparison.Ordinal)) | ||
| { | ||
| var apiObject = method.Invoke(null, new object[] { clientOptions }); | ||
| var properties = apiObject.GetType().GetProperties(); | ||
| foreach (var prop in properties) | ||
| { | ||
| if (prop.GetValue(apiObject) is ApiVersionsBase propVal) | ||
| { | ||
| var key = propVal.ResourceType; | ||
| _loadedResourceToApiVersions.Add(key.ToString(), new PropertyWrapper(prop, apiObject)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static IEnumerable<MethodInfo> GetExtensionMethods() | ||
| { | ||
| // See TODO ADO #5692 | ||
| var results = | ||
| from assembly in AppDomain.CurrentDomain.GetAssemblies() | ||
| where assembly.GetName().ToString().StartsWith("Azure.", StringComparison.Ordinal ) || assembly.GetName().ToString().StartsWith("Proto.", StringComparison.Ordinal) | ||
| from type in assembly.GetTypes() | ||
| where type.IsSealed && !type.IsGenericType && !type.IsNested && type.Name.Equals("AzureResourceManagerClientOptionsExtensions", StringComparison.Ordinal) | ||
| from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public) | ||
| where method.IsDefined(typeof(ExtensionAttribute), false) | ||
| where method.GetParameters()[0].ParameterType == typeof(AzureResourceManagerClientOptions) | ||
| select method; | ||
| return results; | ||
| } | ||
|
|
||
| private string LoadApiVersion(ProvidersOperations providers, ResourceIdentifier id, CancellationToken cancellationToken) | ||
| { | ||
| var results = providers.Get(id.Type.Namespace, null, cancellationToken); | ||
|
bquantump marked this conversation as resolved.
Outdated
|
||
| foreach (var type in results.Value.ResourceTypes) | ||
| { | ||
| if (type.ResourceType.Equals(id.Type.Type)) | ||
| { | ||
| _nonLoadedResourceToApiVersion.Add(id.Type.ToString(), type.ApiVersions[0]); | ||
| return type.ApiVersions[0]; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private async Task<string> LoadApiVersionAsync(ProvidersOperations providers, ResourceIdentifier id, CancellationToken cancellationToken) | ||
| { | ||
| var results = await providers.GetAsync(id.Type.Namespace, null, cancellationToken).ConfigureAwait(false); | ||
| foreach (var type in results.Value.ResourceTypes) | ||
| { | ||
| if (type.ResourceType.Equals(id.Type.Type)) | ||
| { | ||
| _nonLoadedResourceToApiVersion.Add(id.Type.ToString(), type.ApiVersions[0]); | ||
| return type.ApiVersions[0]; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| internal string TyrGetApiVersion(ProvidersOperations providers, ResourceIdentifier resourceId, CancellationToken cancellationToken) | ||
|
bquantump marked this conversation as resolved.
Outdated
|
||
| { | ||
| string val; | ||
| if (TryGetApiVersion(resourceId, out val)) | ||
| { | ||
| return val; | ||
| } | ||
| return LoadApiVersion(providers, resourceId, cancellationToken); | ||
| } | ||
|
|
||
| internal async Task<string> TyrGetApiVersionAsync(ProvidersOperations providers, ResourceIdentifier resourceId, CancellationToken cancellationToken) | ||
|
bquantump marked this conversation as resolved.
Outdated
|
||
| { | ||
| string val; | ||
| if (TryGetApiVersion(resourceId, out val)) | ||
| { | ||
| return val; | ||
| } | ||
| return await LoadApiVersionAsync(providers, resourceId, cancellationToken).ConfigureAwait(false); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the API version give a resource ID if it exist, else will return null. | ||
| /// </summary> | ||
| /// <returns> API version string. </returns> | ||
|
bquantump marked this conversation as resolved.
Outdated
|
||
| public bool TryGetApiVersion(string resourceId, out string val) | ||
| { | ||
| PropertyWrapper propertyWrapper; | ||
| if (_loadedResourceToApiVersions.TryGetValue(resourceId, out propertyWrapper)) | ||
| { | ||
| val = propertyWrapper.Info.GetValue(propertyWrapper.PropertyObject).ToString(); | ||
|
bquantump marked this conversation as resolved.
Outdated
|
||
| return true; | ||
| } | ||
|
|
||
| if (_nonLoadedResourceToApiVersion.TryGetValue(resourceId, out val)) | ||
| { | ||
| return true; | ||
| } | ||
| val = null; | ||
| return false; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Set the API version given a resource ID | ||
| /// </summary> | ||
| public void SetApiVersion(string resourceId, string apiVersion) | ||
| { | ||
| PropertyWrapper propertyWrapper; | ||
| if (_loadedResourceToApiVersions.TryGetValue(resourceId, out propertyWrapper)) | ||
| { | ||
| Type type = propertyWrapper.Info.PropertyType; | ||
| ConstructorInfo ctor = type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string) }, null); | ||
| propertyWrapper.Info.SetValue(propertyWrapper.PropertyObject, ctor.Invoke(new object[] { apiVersion })); | ||
| } | ||
| else | ||
| { | ||
| _nonLoadedResourceToApiVersion[resourceId] = apiVersion; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.