Skip to content

Commit 02125be

Browse files
authored
Merge pull request #3107 from TianoMS/tiano-d2
Write error when getting non-existing lock.
2 parents e2b100e + 4bb49a7 commit 02125be

File tree

5 files changed

+259
-6
lines changed

5 files changed

+259
-6
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/Lock/GetAzureResourceLockCmdlet.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,25 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17+
using System;
18+
using System.Collections.Concurrent;
19+
using System.Management.Automation;
20+
using System.Threading.Tasks;
1721
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
1822
using Microsoft.Azure.Commands.ResourceManager.Common;
1923
using Newtonsoft.Json.Linq;
20-
using System.Management.Automation;
21-
using System.Threading.Tasks;
2224

2325
/// <summary>
2426
/// Gets the resource lock.
2527
/// </summary>
2628
[Cmdlet(VerbsCommon.Get, "AzureRmResourceLock"), OutputType(typeof(PSObject))]
2729
public class GetAzureResourceLockCmdlet : ResourceLockManagementCmdletBase
2830
{
31+
/// <summary>
32+
/// Contains the errors that encountered while satifying the request.
33+
/// </summary>
34+
private readonly ConcurrentBag<ErrorRecord> errors = new ConcurrentBag<ErrorRecord>();
35+
2936
/// <summary>
3037
/// Gets or sets the extension resource name parameter.
3138
/// </summary>
@@ -57,20 +64,51 @@ protected override void OnProcessRecord()
5764
getNextPage: nextLink => this.GetNextLink<JObject>(nextLink),
5865
cancellationToken: this.CancellationToken,
5966
action: resources => this.WriteObject(sendToPipeline: this.GetOutputObjects(resources), enumerateCollection: true));
67+
68+
if (this.errors.Count != 0)
69+
{
70+
foreach (var error in this.errors)
71+
{
72+
this.WriteError(error);
73+
}
74+
}
6075
}
6176

6277
/// <summary>
6378
/// Queries the ARM cache and returns the cached resource that match the query specified.
6479
/// </summary>
6580
private async Task<ResponseWithContinuation<JObject[]>> GetResources()
6681
{
67-
if (!string.IsNullOrWhiteSpace(this.LockName))
82+
var response = new ResponseWithContinuation<JObject[]> { Value = new JObject[0] };
83+
84+
try
85+
{
86+
if (!string.IsNullOrWhiteSpace(this.LockName))
87+
{
88+
var resource = await this.GetResource().ConfigureAwait(continueOnCapturedContext: false);
89+
90+
response.Value = resource.AsArray();
91+
}
92+
else
93+
{
94+
response = await this.ListResourcesTypeCollection().ConfigureAwait(continueOnCapturedContext: false);
95+
}
96+
}
97+
catch (Exception ex)
6898
{
69-
var resource = await this.GetResource().ConfigureAwait(continueOnCapturedContext: false);
70-
return new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
99+
if (ex.IsFatal())
100+
{
101+
throw;
102+
}
103+
104+
ex = (ex is AggregateException)
105+
? (ex as AggregateException).Flatten()
106+
: ex;
107+
108+
this.errors.Add(new ErrorRecord(ex, "ErrorGettingResourceLock", ErrorCategory.CloseError, null));
71109
}
72110

73-
return await this.ListResourcesTypeCollection().ConfigureAwait(continueOnCapturedContext: false);
111+
return response;
74112
}
75113

76114
/// <summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@
455455
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceLockTests\TestResourceLockCRUDTest.json">
456456
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
457457
</None>
458+
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceLockTests\TestResourceLockNonExisting.json">
459+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
460+
</None>
458461
<None Include="SessionRecords\Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ResourceTests\TestCreatesNewComplexResource.json">
459462
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
460463
</None>

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourceLockTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@ public void TestResourceLockCRUDTest()
3232
{
3333
ResourcesController.NewInstance.RunPsTest("Test-ResourceLockCRUD");
3434
}
35+
36+
[Fact]
37+
public void TestResourceLockNonExisting()
38+
{
39+
ResourcesController.NewInstance.RunPsTest("Test-ResourceLockNonExisting");
40+
}
3541
}
3642
}

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ResourceLockTests.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,25 @@ function Test-ResourceLockCRUD
5454

5555
$removed = Remove-AzureRMResourceLock -ResourceId $actual.ResourceId -Force
5656
Assert-AreEqual True $removed
57+
}
58+
59+
<#
60+
.SYNOPSIS
61+
Tests resource lock which does not exist.
62+
#>
63+
function Test-ResourceLockNonExisting
64+
{
65+
# Setup
66+
$rgname = Get-ResourceGroupName
67+
$rglocation = Get-ProviderLocation ResourceManagement
68+
69+
$rg = New-AzureRMResourceGroup -Name $rgname -Location $rglocation
70+
Assert-AreEqual $rgname $rg.ResourceGroupName
71+
72+
$lock = Get-AzureRMResourceLock -LockName "NonExisting" -Scope $rg.ResourceId -ErrorAction SilentlyContinue
73+
74+
Assert-True { $Error[0] -like "*LockNotFound : The lock 'NonExisting' could not be found." }
75+
Assert-Null $lock
76+
77+
$Error.Clear()
5778
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"Entries": [
3+
{
4+
"RequestUri": "/subscriptions/fb3a3d6b-44c8-44f5-88c9-b20917c9b96b/resourcegroups/onesdk9493?api-version=2016-07-01",
5+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvZmIzYTNkNmItNDRjOC00NGY1LTg4YzktYjIwOTE3YzliOTZiL3Jlc291cmNlZ3JvdXBzL29uZXNkazk0OTM/YXBpLXZlcnNpb249MjAxNi0wNy0wMQ==",
6+
"RequestMethod": "HEAD",
7+
"RequestBody": "",
8+
"RequestHeaders": {
9+
"x-ms-client-request-id": [
10+
"797df75b-e7d6-44ec-9391-8e884b7c6138"
11+
],
12+
"accept-language": [
13+
"en-US"
14+
],
15+
"User-Agent": [
16+
"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient/1.2.0-preview"
17+
]
18+
},
19+
"ResponseBody": "",
20+
"ResponseHeaders": {
21+
"Content-Length": [
22+
"102"
23+
],
24+
"Content-Type": [
25+
"application/json; charset=utf-8"
26+
],
27+
"Expires": [
28+
"-1"
29+
],
30+
"Pragma": [
31+
"no-cache"
32+
],
33+
"x-ms-failure-cause": [
34+
"gateway"
35+
],
36+
"x-ms-ratelimit-remaining-subscription-reads": [
37+
"14999"
38+
],
39+
"x-ms-request-id": [
40+
"ccbd1a38-28fd-4a52-b039-63fbd8dfb058"
41+
],
42+
"x-ms-correlation-request-id": [
43+
"ccbd1a38-28fd-4a52-b039-63fbd8dfb058"
44+
],
45+
"x-ms-routing-request-id": [
46+
"CENTRALUS:20161021T062754Z:ccbd1a38-28fd-4a52-b039-63fbd8dfb058"
47+
],
48+
"Strict-Transport-Security": [
49+
"max-age=31536000; includeSubDomains"
50+
],
51+
"Cache-Control": [
52+
"no-cache"
53+
],
54+
"Date": [
55+
"Fri, 21 Oct 2016 06:27:54 GMT"
56+
]
57+
},
58+
"StatusCode": 404
59+
},
60+
{
61+
"RequestUri": "/subscriptions/fb3a3d6b-44c8-44f5-88c9-b20917c9b96b/resourcegroups/onesdk9493?api-version=2016-07-01",
62+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvZmIzYTNkNmItNDRjOC00NGY1LTg4YzktYjIwOTE3YzliOTZiL3Jlc291cmNlZ3JvdXBzL29uZXNkazk0OTM/YXBpLXZlcnNpb249MjAxNi0wNy0wMQ==",
63+
"RequestMethod": "PUT",
64+
"RequestBody": "{\r\n \"location\": \"West US\"\r\n}",
65+
"RequestHeaders": {
66+
"Content-Type": [
67+
"application/json; charset=utf-8"
68+
],
69+
"Content-Length": [
70+
"29"
71+
],
72+
"x-ms-client-request-id": [
73+
"18a8c7b0-defc-4d39-8825-24c0d27b93f5"
74+
],
75+
"accept-language": [
76+
"en-US"
77+
],
78+
"User-Agent": [
79+
"Microsoft.Azure.Management.ResourceManager.ResourceManagementClient/1.2.0-preview"
80+
]
81+
},
82+
"ResponseBody": "{\r\n \"id\": \"/subscriptions/fb3a3d6b-44c8-44f5-88c9-b20917c9b96b/resourceGroups/onesdk9493\",\r\n \"name\": \"onesdk9493\",\r\n \"location\": \"westus\",\r\n \"properties\": {\r\n \"provisioningState\": \"Succeeded\"\r\n }\r\n}",
83+
"ResponseHeaders": {
84+
"Content-Length": [
85+
"173"
86+
],
87+
"Content-Type": [
88+
"application/json; charset=utf-8"
89+
],
90+
"Expires": [
91+
"-1"
92+
],
93+
"Pragma": [
94+
"no-cache"
95+
],
96+
"x-ms-ratelimit-remaining-subscription-writes": [
97+
"1199"
98+
],
99+
"x-ms-request-id": [
100+
"d461ce03-c2eb-41b6-981e-3dbe6ade174c"
101+
],
102+
"x-ms-correlation-request-id": [
103+
"d461ce03-c2eb-41b6-981e-3dbe6ade174c"
104+
],
105+
"x-ms-routing-request-id": [
106+
"CENTRALUS:20161021T062755Z:d461ce03-c2eb-41b6-981e-3dbe6ade174c"
107+
],
108+
"Strict-Transport-Security": [
109+
"max-age=31536000; includeSubDomains"
110+
],
111+
"Cache-Control": [
112+
"no-cache"
113+
],
114+
"Date": [
115+
"Fri, 21 Oct 2016 06:27:55 GMT"
116+
]
117+
},
118+
"StatusCode": 201
119+
},
120+
{
121+
"RequestUri": "/subscriptions/fb3a3d6b-44c8-44f5-88c9-b20917c9b96b/resourceGroups/onesdk9493/providers/Microsoft.Authorization/locks/NonExisting?api-version=2015-01-01",
122+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnMvZmIzYTNkNmItNDRjOC00NGY1LTg4YzktYjIwOTE3YzliOTZiL3Jlc291cmNlR3JvdXBzL29uZXNkazk0OTMvcHJvdmlkZXJzL01pY3Jvc29mdC5BdXRob3JpemF0aW9uL2xvY2tzL05vbkV4aXN0aW5nP2FwaS12ZXJzaW9uPTIwMTUtMDEtMDE=",
123+
"RequestMethod": "GET",
124+
"RequestBody": "",
125+
"RequestHeaders": {
126+
"User-Agent": [
127+
"AzurePowershell/v3.0.0.0"
128+
],
129+
"ParameterSetName": [
130+
"A lock at the specified scope."
131+
],
132+
"CommandName": [
133+
"Get-AzureRmResourceLock"
134+
]
135+
},
136+
"ResponseBody": "{\r\n \"error\": {\r\n \"code\": \"LockNotFound\",\r\n \"message\": \"The lock 'NonExisting' could not be found.\"\r\n }\r\n}",
137+
"ResponseHeaders": {
138+
"Content-Length": [
139+
"88"
140+
],
141+
"Content-Type": [
142+
"application/json; charset=utf-8"
143+
],
144+
"Expires": [
145+
"-1"
146+
],
147+
"Pragma": [
148+
"no-cache"
149+
],
150+
"x-ms-request-id": [
151+
"centralus:d15c2abf-8138-4e1f-82e5-4c3cfac7ee40"
152+
],
153+
"x-ms-ratelimit-remaining-subscription-reads": [
154+
"14999"
155+
],
156+
"x-ms-correlation-request-id": [
157+
"a5f1b021-84e3-4ade-a91a-53c3a9161541"
158+
],
159+
"x-ms-routing-request-id": [
160+
"CENTRALUS:20161021T062756Z:a5f1b021-84e3-4ade-a91a-53c3a9161541"
161+
],
162+
"Strict-Transport-Security": [
163+
"max-age=31536000; includeSubDomains"
164+
],
165+
"Cache-Control": [
166+
"no-cache"
167+
],
168+
"Date": [
169+
"Fri, 21 Oct 2016 06:27:55 GMT"
170+
]
171+
},
172+
"StatusCode": 404
173+
}
174+
],
175+
"Names": {
176+
"Test-ResourceLockNonExisting": [
177+
"onesdk9493"
178+
]
179+
},
180+
"Variables": {
181+
"SubscriptionId": "fb3a3d6b-44c8-44f5-88c9-b20917c9b96b",
182+
"TenantId": "72f988bf-86f1-41af-91ab-2d7cd011db47",
183+
"Domain": "microsoft.com"
184+
}
185+
}

0 commit comments

Comments
 (0)