Skip to content

Commit 7711c03

Browse files
authored
Implement new feature to operate resources with default api version first instead of latest api version. (#27657)
1 parent d3f00a7 commit 7711c03

File tree

83 files changed

+70651
-255853
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+70651
-255853
lines changed

src/Resources/ResourceManager/Components/ApiVersionHelper.cs

Lines changed: 111 additions & 116 deletions
Large diffs are not rendered by default.

src/Resources/ResourceManager/Implementation/CmdletBase/ResourceManagerCmdletBaseWithAPiVersion.cs

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System.Management.Automation;
16-
using System.Threading.Tasks;
17-
using System.Collections.Generic;
1816

1917
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2018
{
@@ -27,49 +25,29 @@ public abstract class ResourceManagerCmdletBaseWithApiVersion : ResourceManagerC
2725
[ValidateNotNullOrEmpty]
2826
public virtual string ApiVersion { get; set; }
2927

30-
private Dictionary<string, string> GetCmdletHeaders()
31-
{
32-
return new Dictionary<string, string>
33-
{
34-
{"ParameterSetName", this.ParameterSetName },
35-
{"CommandName", this.CommandRuntime.ToString() }
36-
};
37-
}
38-
3928
/// <summary>
4029
/// Determines the API version.
4130
/// </summary>
4231
/// <param name="resourceId">The resource Id.</param>
43-
/// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
44-
protected Task<string> DetermineApiVersion(string resourceId, bool? pre = null)
32+
protected string DetermineApiVersion(string resourceId)
4533
{
46-
return string.IsNullOrWhiteSpace(this.ApiVersion)
47-
? Components.ApiVersionHelper.DetermineApiVersion(
48-
context: DefaultContext,
49-
resourceId: resourceId,
50-
cancellationToken: this.CancellationToken.Value,
51-
pre: pre ?? this.Pre,
52-
cmdletHeaderValues: this.GetCmdletHeaders())
53-
: Task.FromResult(this.ApiVersion);
34+
if (!string.IsNullOrWhiteSpace(ApiVersion))
35+
return ApiVersion;
36+
37+
return Components.ApiVersionHelper.DetermineApiVersion(DefaultContext, resourceId, Pre);
5438
}
5539

5640
/// <summary>
5741
/// Determines the API version.
5842
/// </summary>
5943
/// <param name="providerNamespace">The provider namespace.</param>
6044
/// <param name="resourceType">The resource type.</param>
61-
/// <param name="pre">When specified, indicates if pre-release API versions should be considered.</param>
62-
protected Task<string> DetermineApiVersion(string providerNamespace, string resourceType, bool? pre = null)
45+
protected string DetermineApiVersion(string providerNamespace, string resourceType)
6346
{
64-
return string.IsNullOrWhiteSpace(this.ApiVersion)
65-
? Components.ApiVersionHelper.DetermineApiVersion(
66-
DefaultContext,
67-
providerNamespace: providerNamespace,
68-
resourceType: resourceType,
69-
cancellationToken: this.CancellationToken.Value,
70-
pre: pre ?? this.Pre,
71-
cmdletHeaderValues: this.GetCmdletHeaders())
72-
: Task.FromResult(this.ApiVersion);
47+
if (!string.IsNullOrWhiteSpace(ApiVersion))
48+
return ApiVersion;
49+
50+
return Components.ApiVersionHelper.DetermineApiVersion(DefaultContext, providerNamespace, resourceType, Pre);
7351
}
7452
}
7553
}

src/Resources/ResourceManager/Implementation/Resource/GetAzureResourceCmdlet.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2222
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
2323
using Microsoft.Azure.Commands.ResourceManager.Common;
2424
using Microsoft.Azure.Management.ResourceManager.Models;
25-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2625
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2726
using Newtonsoft.Json.Linq;
2827
using System;
@@ -36,7 +35,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
3635
/// <summary>
3736
/// Cmdlet to get existing resources.
3837
/// </summary>
39-
[GenericBreakingChangeWithVersion("The API version for the resource type will be updated to use the default version instead of the latest.", "14.0.0", "8.0.0")]
4038
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Resource", DefaultParameterSetName = ByTagNameValueParameterSet), OutputType(typeof(PSResource))]
4139
public sealed class GetAzureResourceCmdlet : ResourceManagerCmdletBaseWithApiVersion
4240
{
@@ -229,7 +227,7 @@ private bool ShouldConstructResourceId(out string resourceId)
229227
ResourceType,
230228
Name);
231229

232-
this.DefaultApiVersion = DetermineApiVersion(resourceId).Result;
230+
DefaultApiVersion = DetermineApiVersion(resourceId);
233231

234232
return true;
235233
}
@@ -328,9 +326,7 @@ private async Task<JObject> GetResource()
328326

329327
#pragma warning restore 618
330328

331-
var apiVersion = await this
332-
.DetermineApiVersion(resourceId: resourceId)
333-
.ConfigureAwait(continueOnCapturedContext: false);
329+
var apiVersion = DetermineApiVersion(resourceId);
334330

335331
var odataQuery = QueryFilterBuilder.CreateFilter(
336332
subscriptionId: null,
@@ -487,10 +483,7 @@ private async Task<Resource<JToken>> GetPopulatedResource(Resource<JToken> resou
487483
{
488484
try
489485
{
490-
var apiVersion = await this.DetermineApiVersion(
491-
resourceId: resource.Id,
492-
pre: this.Pre)
493-
.ConfigureAwait(continueOnCapturedContext: false);
486+
var apiVersion = DetermineApiVersion(resource.Id);
494487

495488
return await this
496489
.GetResourcesClient()

src/Resources/ResourceManager/Implementation/Resource/InvokeAzureResourceActionCmdlet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
1717
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1818
using Microsoft.WindowsAzure.Commands.Common;
19-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2019
using Newtonsoft.Json.Linq;
2120
using System.Collections;
2221
using System.Management.Automation;
@@ -25,7 +24,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2524
/// <summary>
2625
/// A cmdlet that invokes a resource action.
2726
/// </summary>
28-
[GenericBreakingChangeWithVersion("The API version for the resource type will be updated to use the default version instead of the latest.", "14.0.0", "8.0.0")]
2927
[Cmdlet("Invoke", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "ResourceAction", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet), OutputType(typeof(PSObject))]
3028
public sealed class InvokAzureResourceActionCmdlet : ResourceManipulationCmdletBase
3129
{
@@ -61,7 +59,7 @@ protected override void OnProcessRecord()
6159
resourceId,
6260
() =>
6361
{
64-
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
62+
var apiVersion = DetermineApiVersion(resourceId);
6563
var parameters = this.GetParameters();
6664

6765
var operationResult = this.GetResourcesClient()

src/Resources/ResourceManager/Implementation/Resource/MoveAzureResourceCmdlet.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,7 @@ private void RunCmdlet()
135135
destinationResourceGroup,
136136
() =>
137137
{
138-
var apiVersion = this
139-
.DetermineApiVersion(
140-
providerNamespace: Constants.MicrosoftResourceNamesapce,
141-
resourceType: Constants.ResourceGroups)
142-
.Result;
138+
var apiVersion = DetermineApiVersion(Constants.MicrosoftResourceNamesapce, Constants.ResourceGroups);
143139

144140
var parameters = new ResourceBatchMoveParameters
145141
{

src/Resources/ResourceManager/Implementation/Resource/NewAzureResourceCmdlet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2121
using Microsoft.WindowsAzure.Commands.Common;
22-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2322
using Newtonsoft.Json.Linq;
2423
using System.Collections;
2524
using System.Linq;
@@ -28,7 +27,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2827
/// <summary>
2928
/// A cmdlet that creates a new azure resource.
3029
/// </summary>
31-
[GenericBreakingChangeWithVersion("The API version for the resource type will be updated to use the default version instead of the latest.", "14.0.0", "8.0.0")]
3230
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Resource", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet), OutputType(typeof(PSObject))]
3331
public sealed class NewAzureResourceCmdlet : ResourceManipulationCmdletBase
3432
{
@@ -102,7 +100,7 @@ protected override void OnProcessRecord()
102100
resourceId,
103101
() =>
104102
{
105-
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
103+
var apiVersion = DetermineApiVersion(resourceId);
106104
var resourceBody = this.GetResourceBody();
107105

108106
var operationResult = this.GetResourcesClient()

src/Resources/ResourceManager/Implementation/Resource/RemoveAzureResourceCmdlet.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
1817
using System.Management.Automation;
1918

2019
/// <summary>
2120
/// A cmdlet that removes an azure resource.
2221
/// </summary>
23-
[GenericBreakingChangeWithVersion("The API version for the resource type will be updated to use the default version instead of the latest.", "14.0.0", "8.0.0")]
2422
[Cmdlet("Remove", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Resource", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet), OutputType(typeof(bool))]
2523
public class RemoveAzureResourceCmdlet : ResourceManipulationCmdletBase
2624
{
@@ -43,7 +41,7 @@ protected override void OnProcessRecord()
4341
resourceId,
4442
() =>
4543
{
46-
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
44+
var apiVersion = DetermineApiVersion(resourceId);
4745

4846
var operationResult = this.GetResourcesClient()
4947
.DeleteResource(

src/Resources/ResourceManager/Implementation/Resource/SetAzureResourceCmdlet.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2020
using Microsoft.WindowsAzure.Commands.Common;
21-
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2221
using Newtonsoft.Json.Linq;
2322
using SdkExtensions;
2423
using SdkModels;
@@ -31,7 +30,6 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
3130
/// <summary>
3231
/// A cmdlet that creates a new azure resource.
3332
/// </summary>
34-
[GenericBreakingChangeWithVersion("The API version for the resource type will be updated to use the default version instead of the latest.", "14.0.0", "8.0.0")]
3533
[Cmdlet("Set", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Resource", SupportsShouldProcess = true, DefaultParameterSetName = ResourceManipulationCmdletBase.ResourceIdParameterSet), OutputType(typeof(PSObject))]
3634
public sealed class SetAzureResourceCmdlet : ResourceManipulationCmdletBase
3735
{
@@ -111,7 +109,7 @@ protected override void OnProcessRecord()
111109
resourceId,
112110
() =>
113111
{
114-
var apiVersion = this.DetermineApiVersion(resourceId: resourceId).Result;
112+
var apiVersion = this.DetermineApiVersion(resourceId);
115113
var resourceBody = this.GetResourceBody();
116114

117115
var operationResult = this.ShouldUsePatchSemantics()
@@ -236,9 +234,7 @@ private bool ShouldUsePatchSemantics()
236234
private async Task<JObject> GetResource()
237235
{
238236
var resourceId = this.GetResourceId();
239-
var apiVersion = await this
240-
.DetermineApiVersion(resourceId: resourceId)
241-
.ConfigureAwait(continueOnCapturedContext: false);
237+
var apiVersion = DetermineApiVersion(resourceId);
242238

243239
return await this
244240
.GetResourcesClient()

src/Resources/Resources.Test/ScenarioTests/InvokeResourceActionTests.ps1

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,26 @@ Tests invoking registering resource provider action
1919
function Test-InvokeResourceActionsWithResouceId
2020
{
2121
# Setup
22-
$rpName = "Microsoft.ApiManagement"
22+
$rpName = "Microsoft.AwsConnector"
2323
$action = "Register"
2424
$subId = GetDefaultSubscriptionId
2525
$resourceId = $subId + "/providers/" + $rpName
26-
Unregister-AzResourceProvider -ProviderNamespace $rpName
2726

2827
try
2928
{
3029
# Test
31-
$res = Invoke-AzResourceAction -ResourceId $resourceId -action $action -Force
32-
30+
$res = Invoke-AzResourceAction -ResourceId $resourceId -Action $action -Force
3331

3432
# Assert
3533
Assert-AreEqual $res.registrationState Registering
3634

37-
$statusChanged = $false
38-
39-
# within two minutes, the registrationState should change to "Registered"
40-
$timeout = new-timespan -Minutes 2
41-
$sw = [diagnostics.stopwatch]::StartNew()
35+
do {
36+
$rp = Get-AzResourceProvider | Where-Object ProviderNamespace -eq $rpName
4237

43-
while ($sw.elapsed -lt $timeout){
44-
$rp = Get-AzResourceProvider | where ProviderNamespace -eq $rpName
45-
if($rp -ne $null -and $rp.registrationState -eq "Registered") {
46-
$statusChanged = $true
47-
break
48-
}
49-
Start-TestSleep -Seconds 20
50-
}
38+
Start-TestSleep -Seconds 10
39+
} while ($rp.RegistrationState -ne "Registered")
5140

52-
Assert-True { $statusChanged }
41+
Assert-AreEqual $rp.RegistrationState Registered
5342
}
5443
finally
5544
{

0 commit comments

Comments
 (0)