Skip to content

Commit 93608d3

Browse files
ilahatwyunchi-ms
andauthored
Fix the handling of long-running operation statuses for New-AzManagedApplication and Set-AzManagedApplication (Azure#13679)
* AzureManagedApplications Fix the handling of long-runing operation statuses for New-AzureManagedApplication and Set-AzureManagedApplication * Update ChangeLog.md Co-authored-by: Yunchi Wang <[email protected]>
1 parent c1a0878 commit 93608d3

File tree

6 files changed

+770
-352
lines changed

6 files changed

+770
-352
lines changed

src/Resources/ResourceManager/Implementation/ManagedApplications/ManagedApplicationCmdletBase.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
2828
using System.Collections.Generic;
2929
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Application;
3030
using Commands.Common.Authentication.Abstractions;
31+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Operations;
3132

3233
/// <summary>
3334
/// Base class for policy assignment cmdlets.
@@ -146,5 +147,33 @@ protected JToken GetObjectFromParameter(string parameter)
146147
? JToken.FromObject(FileUtilities.DataStore.ReadFileAsText(path))
147148
: JToken.FromObject(parameter);
148149
}
150+
151+
/// <summary>
152+
/// Processes and outputs the result of the operation.
153+
/// </summary>
154+
protected void ProcessResult(string result, string resourceId, string apiVersion)
155+
{
156+
if (result.TryConvertTo<AzureAsyncOperationResource>(out var operationResource) && operationResource != null)
157+
{
158+
if (String.Equals(operationResource.Status, "Succeeded", StringComparison.OrdinalIgnoreCase))
159+
{
160+
var getResult = this.GetResourcesClient()
161+
.GetResource<JObject>(
162+
resourceId: resourceId,
163+
apiVersion: apiVersion,
164+
cancellationToken: this.CancellationToken.Value)
165+
.Result;
166+
this.WriteObject(this.GetOutputObjects("ManagedApplicationId", getResult), enumerateCollection: true);
167+
}
168+
else
169+
{
170+
this.WriteObject(operationResource.Error.ToJToken());
171+
}
172+
}
173+
else
174+
{
175+
throw new InvalidOperationException("The operation failed because the operation status could not be de-serialized.");
176+
}
177+
}
149178
}
150179
}

src/Resources/ResourceManager/Implementation/ManagedApplications/NewAzureManagedApplication.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Common.ArgumentCompleters;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Application;
21+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Operations;
2122
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2223
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2324
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -127,7 +128,8 @@ protected override void OnProcessRecord()
127128
var activity = string.Format("PUT {0}", managementUri.PathAndQuery);
128129
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
129130
.WaitOnOperation(operationResult: operationResult);
130-
this.WriteObject(this.GetOutputObjects("ManagedApplicationId", JObject.Parse(result)), enumerateCollection: true);
131+
132+
this.ProcessResult(result, resourceId, apiVersion);
131133
}
132134
}
133135

src/Resources/ResourceManager/Implementation/ManagedApplications/SetAzureManagedApplication.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1818
using Common.ArgumentCompleters;
1919
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
2020
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Application;
21+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Operations;
2122
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
2223
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2324
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -150,7 +151,8 @@ protected override void OnProcessRecord()
150151
var activity = string.Format("PUT {0}", managementUri.PathAndQuery);
151152
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
152153
.WaitOnOperation(operationResult: operationResult);
153-
this.WriteObject(this.GetOutputObjects("ManagedApplicationId", JObject.Parse(result)), enumerateCollection: true);
154+
155+
this.ProcessResult(result, resourceId, apiVersion);
154156
}
155157
}
156158

src/Resources/Resources.Test/ScenarioTests/ManagedApplicationTests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ function Test-ManagedApplicationCRUD
2929
# Test
3030
New-AzResourceGroup -Name $rgname -Location $rglocation
3131

32-
$appDef = New-AzManagedApplicationDefinition -Name $appDefName -ResourceGroupName $rgname -DisplayName $display -Description "Test" -Location $rglocation -LockLevel ReadOnly -PackageFileUri https://testclinew.blob.core.windows.net/files/vivekMAD.zip -Authorization 5e91139a-c94b-462e-a6ff-1ee95e8aac07:8e3af657-a8ff-443c-a75c-2fe8c4bcb635
33-
$actual = New-AzManagedApplication -Name $appName -ResourceGroupName $rgname -ManagedResourceGroupName $managedrgname -ManagedApplicationDefinitionId $appDef.ResourceId -Location $rglocation -Kind ServiceCatalog -Parameter "$TestOutputRoot\SampleManagedApplicationParameters.json"
32+
$appDef = New-AzManagedApplicationDefinition -Name $appDefName -ResourceGroupName $rgname -DisplayName $display -Description "Test" -Location $rglocation -LockLevel ReadOnly -PackageFileUri https://ilshat.blob.core.windows.net/packages/blank_working.zip -Authorization 6fd8564d-425b-4017-9121-ba334bab0be4:8e3af657-a8ff-443c-a75c-2fe8c4bcb635
33+
$actual = New-AzManagedApplication -Name $appName -ResourceGroupName $rgname -ManagedResourceGroupName $managedrgname -ManagedApplicationDefinitionId $appDef.ResourceId -Location $rglocation -Kind ServiceCatalog
3434
$expected = Get-AzManagedApplication -Name $appName -ResourceGroupName $rgname
3535
Assert-AreEqual $expected.Name $actual.Name
3636
Assert-AreEqual $expected.ManagedApplicationId $actual.ManagedApplicationId

0 commit comments

Comments
 (0)