Skip to content

Commit 77ee674

Browse files
committed
Merge pull request #1012 from vivsriaus/LockIssue
Fix Lock issue
2 parents acb6c53 + 1f965d2 commit 77ee674

File tree

8 files changed

+631
-4
lines changed

8 files changed

+631
-4
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/NewAzureResourceLockCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ protected override void OnProcessRecord()
9494
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: true)
9595
.WaitOnOperation(operationResult: operationResult);
9696

97-
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
97+
this.WriteObject(this.GetOutputObjects(JObject.Parse(result)), enumerateCollection: true);
9898
});
9999
}
100100

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/RemoveAzureResourceLockCmdlet.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ protected override void OnProcessRecord()
6565
cancellationToken: this.CancellationToken.Value)
6666
.Result;
6767

68+
if(operationResult.HttpStatusCode == System.Net.HttpStatusCode.NoContent)
69+
{
70+
throw new PSInvalidOperationException(string.Format("The resource lock '{0}' could not be found.", resourceId));
71+
}
72+
6873
var managementUri = this.GetResourcesClient()
6974
.GetResourceManagementRequestUri(
7075
resourceId: resourceId,
@@ -75,7 +80,7 @@ protected override void OnProcessRecord()
7580
var result = this.GetLongRunningOperationTracker(activityName: activity, isResourceCreateOrUpdate: false)
7681
.WaitOnOperation(operationResult: operationResult);
7782

78-
this.WriteObject(this.GetOutputObjects(result.ToJToken()), enumerateCollection: true);
83+
this.WriteObject(true);
7984
});
8085
}
8186
}

src/ResourceManager/Resources/Commands.Resources.Test/Commands.Resources.Test.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
<Compile Include="ScenarioTests\ResourcesController.cs" />
204204
<Compile Include="ScenarioTests\DeploymentTests.cs" />
205205
<Compile Include="ScenarioTests\ResourceGroupTests.cs" />
206+
<Compile Include="ScenarioTests\ResourceLockTests.cs" />
206207
<Compile Include="ScenarioTests\ResourceTests.cs" />
207208
<Compile Include="ScenarioTests\RoleAssignmentTests.cs" />
208209
<Compile Include="ScenarioTests\RoleDefinitionTests.cs" />
@@ -293,6 +294,9 @@
293294
<None Include="ScenarioTests\ResourceGroupTests.ps1">
294295
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
295296
</None>
297+
<None Include="ScenarioTests\ResourceLockTests.ps1">
298+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
299+
</None>
296300
<None Include="ScenarioTests\ResourceTests.ps1">
297301
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
298302
</None>
@@ -350,6 +354,9 @@
350354
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceGroupTests\TestUpdatesExistingResourceGroup.json">
351355
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
352356
</None>
357+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceLockTests\TestResourceLockCRUDTest.json">
358+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
359+
</None>
353360
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceTests\TestCreatesNewComplexResource.json">
354361
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
355362
</None>

src/ResourceManager/Resources/Commands.Resources.Test/Models.ResourceGroups/ResourceClientTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ public void ShowsFailureErrorWhenResourceGroupWithDeploymentFails()
14081408
Properties = new DeploymentOperationProperties()
14091409
{
14101410
ProvisioningState = ProvisioningState.Failed,
1411-
StatusMessage = "A really bad error occured",
1411+
StatusMessage = "{\"Code\":\"Conflict\"}",
14121412
TargetResource = new TargetResource()
14131413
{
14141414
ResourceType = "Microsoft.Website",
@@ -1433,7 +1433,7 @@ public void ShowsFailureErrorWhenResourceGroupWithDeploymentFails()
14331433
f => f(string.Format("Resource {0} '{1}' failed with message '{2}'",
14341434
"Microsoft.Website",
14351435
resourceName,
1436-
"A really bad error occured")),
1436+
"{\"Code\":\"Conflict\"}")),
14371437
Times.Once());
14381438
}
14391439

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
16+
using Microsoft.WindowsAzure.Commands.ScenarioTest;
17+
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
18+
using Xunit;
19+
20+
namespace Microsoft.Azure.Commands.Resources.Test.ScenarioTests
21+
{
22+
public class ResourceLockTests : RMTestBase
23+
{
24+
[Fact]
25+
public void TestResourceLockCRUDTest()
26+
{
27+
ResourcesController.NewInstance.RunPsTest("Test-ResourceLockCRUD");
28+
}
29+
}
30+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# ----------------------------------------------------------------------------------
2+
#
3+
# Copyright Microsoft Corporation
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
# ----------------------------------------------------------------------------------
14+
15+
<#
16+
.SYNOPSIS
17+
Tests resource lock CRUD operations.
18+
#>
19+
function Test-ResourceLockCRUD
20+
{
21+
# Setup
22+
$rgname = Get-ResourceGroupName
23+
$rname = Get-ResourceName
24+
$rglocation = Get-ProviderLocation ResourceManagement
25+
$apiversion = "2014-04-01"
26+
27+
$rg = New-AzureRMResourceGroup -Name $rgname -Location $rglocation
28+
$actual = New-AzureRMResourceLock -LockName $rname -LockLevel CanNotDelete -Force -Scope $rg.ResourceId
29+
$expected = Get-AzureRMResourceLock -LockName $rname -Scope $rg.ResourceId
30+
31+
# Assert
32+
Assert-AreEqual $expected.Name $actual.Name
33+
Assert-AreEqual $expected.ResourceId $actual.ResourceId
34+
Assert-AreEqual $expected.ResourceName $actual.ResourceName
35+
Assert-AreEqual $expected.ResourceType $actual.ResourceType
36+
Assert-AreEqual $expected.LockId $actual.LockId
37+
38+
$expectedSet = Set-AzureRMResourceLock -LockId $expected.LockId -LockLevel CanNotDelete -LockNotes test -Force
39+
Assert-AreEqual $expectedSet.Properties.Notes "test"
40+
41+
$removed = Remove-AzureRMResourceLock -LockId $expectedSet.LockId -Force
42+
Assert-AreEqual True $removed
43+
44+
}

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceLockTests/TestResourceLockCRUDTest.json

Lines changed: 516 additions & 0 deletions
Large diffs are not rendered by default.

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,13 @@ private void WriteDeploymentProgress(string resourceGroup, string deploymentName
259259
errorMessage);
260260

261261
WriteError(statusMessage);
262+
263+
List<string> detailedMessage = ParseDetailErrorMessage(operation.Properties.StatusMessage);
264+
265+
if (detailedMessage != null)
266+
{
267+
detailedMessage.ForEach(s => WriteError(s));
268+
}
262269
}
263270
}
264271
}
@@ -276,6 +283,24 @@ public static string ParseErrorMessage(string statusMessage)
276283
}
277284
}
278285

286+
public static List<string> ParseDetailErrorMessage(string statusMessage)
287+
{
288+
if(!string.IsNullOrEmpty(statusMessage))
289+
{
290+
List<string> detailedMessage = new List<string>();
291+
dynamic errorMessage = JsonConvert.DeserializeObject(statusMessage);
292+
if(errorMessage.error != null && errorMessage.error.details !=null)
293+
{
294+
foreach(var detail in errorMessage.error.details)
295+
{
296+
detailedMessage.Add(detail.message.ToString());
297+
}
298+
}
299+
return detailedMessage;
300+
}
301+
return null;
302+
}
303+
279304
private DeploymentExtended WaitDeploymentStatus(
280305
string resourceGroup,
281306
string deploymentName,

0 commit comments

Comments
 (0)