Skip to content

Commit 2c37179

Browse files
authored
Merge pull request #9421 from OlhaTkachenko/SubscriptionFixes
Resource Graph: improving logic of getting subscriptions for query to ARG
2 parents 6332f2d + e949c16 commit 2c37179

File tree

5 files changed

+125
-5
lines changed

5 files changed

+125
-5
lines changed

src/ResourceGraph/ResourceGraph.Test/ScenarioTests/ResourceGraphQueryTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,12 @@ public void QueryError()
5757
{
5858
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-QueryError");
5959
}
60+
61+
[Fact]
62+
[Trait(Category.AcceptanceType, Category.CheckIn)]
63+
public void SubscriptionQueryError()
64+
{
65+
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-SubscriptionQueryError");
66+
}
6067
}
6168
}

src/ResourceGraph/ResourceGraph.Test/ScenarioTests/ResourceGraphQueryTests.ps1

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,27 @@ function Search-AzureRmGraph-QueryError
151151
Assert-AreEqual $PSItem.Exception.Body.Error.Details[0].AdditionalProperties.Count 4
152152
}
153153
}
154+
155+
<#
156+
.SYNOPSIS
157+
Run query with no subscriptions
158+
#>
159+
function Search-AzureRmGraph-SubscriptionQueryError
160+
{
161+
$expectedErrorId = '400,' + [Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph].FullName
162+
$expectedErrorMessage =
163+
'No subscriptions were found to run query. Please try to add them implicitly as param to your request (e.g. Search-AzGraph -Query '''' -Subscription ''11111111-1111-1111-1111-111111111111'')'
164+
165+
try
166+
{
167+
Search-AzGraph "project id, type" -Subscription @()
168+
Assert-True $false # Expecting an error
169+
}
170+
catch [Exception]
171+
{
172+
Assert-AreEqual $expectedErrorId $PSItem.FullyQualifiedErrorId
173+
Assert-AreEqual $expectedErrorMessage $PSItem.Exception.Message
174+
175+
Assert-IsInstance $PSItem.Exception System.ArgumentException
176+
}
177+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"Entries": [
3+
{
4+
"RequestUri": "/subscriptions?api-version=2016-06-01",
5+
"EncodedRequestUri": "L3N1YnNjcmlwdGlvbnM/YXBpLXZlcnNpb249MjAxNi0wNi0wMQ==",
6+
"RequestMethod": "GET",
7+
"RequestBody": "",
8+
"RequestHeaders": {
9+
"x-ms-client-request-id": [
10+
"31537677-7032-4654-becb-372f2eeebf21"
11+
],
12+
"accept-language": [
13+
"en-US"
14+
],
15+
"User-Agent": [
16+
"FxVersion/4.7.3163.0",
17+
"OSName/Windows10Enterprise",
18+
"OSVersion/6.3.17134",
19+
"Microsoft.Azure.Internal.Subscriptions.SubscriptionClient/4.1.0"
20+
]
21+
},
22+
"ResponseBody": "{\"value\": [{\"id\": \"/subscriptions/00000000-0000-0000-0000-000000000000\", \"subscriptionId\": \"00000000-0000-0000-0000-000000000000\", \"displayName\": \"Test Subscription\", \"state\": \"Enabled\", \"authorizationSource\": \"RoleBased\"}]}",
23+
"ResponseHeaders": {
24+
"Content-Length": [
25+
"987"
26+
],
27+
"Content-Type": [
28+
"application/json; charset=utf-8"
29+
],
30+
"Expires": [
31+
"-1"
32+
],
33+
"Pragma": [
34+
"no-cache"
35+
],
36+
"x-ms-ratelimit-remaining-tenant-reads": [
37+
"11999"
38+
],
39+
"x-ms-request-id": [
40+
"dc5b4ea5-4059-44fa-9d49-5c48f46224f4"
41+
],
42+
"x-ms-correlation-request-id": [
43+
"dc5b4ea5-4059-44fa-9d49-5c48f46224f4"
44+
],
45+
"x-ms-routing-request-id": [
46+
"WESTUS2:20180926T232942Z:dc5b4ea5-4059-44fa-9d49-5c48f46224f4"
47+
],
48+
"Strict-Transport-Security": [
49+
"max-age=31536000; includeSubDomains"
50+
],
51+
"X-Content-Type-Options": [
52+
"nosniff"
53+
],
54+
"Cache-Control": [
55+
"no-cache"
56+
],
57+
"Date": [
58+
"Wed, 26 Sep 2018 23:29:41 GMT"
59+
]
60+
},
61+
"StatusCode": 200
62+
}
63+
],
64+
"Names": {},
65+
"Variables": {
66+
"SubscriptionId": "00000000-0000-0000-0000-000000000000"
67+
}
68+
}

src/ResourceGraph/ResourceGraph/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Improving logic of getting subscriptions for query to ARG
2122

2223
## Version 0.7.2
2324
* Fix conversion to PSCustomObject[] for top-level arrays

src/ResourceGraph/ResourceGraph/Cmdlets/SearchAzureRmGraph.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class SearchAzureRmGraph : ResourceGraphBaseCmdlet
3333
/// The rows per page
3434
/// </summary>
3535
private const int RowsPerPage = 1000;
36-
36+
3737
/// <summary>
3838
/// Gets or sets the query.
3939
/// </summary>s
@@ -80,13 +80,25 @@ public int Skip
8080
get;
8181
set;
8282
}
83-
83+
8484
/// <summary>
8585
/// Executes the cmdlet.
8686
/// </summary>
8787
public override void ExecuteCmdlet()
8888
{
8989
var subscriptions = this.GetSubscriptions().ToList();
90+
if(subscriptions == null || subscriptions.Count == 0)
91+
{
92+
var exception = new ArgumentException("No subscriptions were found to run query. " +
93+
"Please try to add them implicitly as param to your request (e.g. Search-AzGraph -Query '' -Subscription '11111111-1111-1111-1111-111111111111')");
94+
95+
var errorRecord = new ErrorRecord(
96+
exception, "400",
97+
ErrorCategory.InvalidArgument, null);
98+
this.WriteError(errorRecord);
99+
return;
100+
}
101+
90102
var first = this.MyInvocation.BoundParameters.ContainsKey("First") ? this.First : 100;
91103
var skip = this.MyInvocation.BoundParameters.ContainsKey("Skip") ? this.Skip : 0;
92104

@@ -107,12 +119,10 @@ public override void ExecuteCmdlet()
107119
skip: requestSkip,
108120
skipToken: requestSkipToken);
109121

110-
var request = new QueryRequest(subscriptions, this.Query, requestOptions);
111-
122+
var request = new QueryRequest(subscriptions, this.Query, options: requestOptions);
112123
response = this.ResourceGraphClient.ResourcesWithHttpMessagesAsync(request)
113124
.Result
114125
.Body;
115-
116126
var requestResults = response.Data.ToPsObjects().ToList();
117127
results.AddRange(requestResults);
118128
this.WriteVerbose($"Received results: {requestResults.Count}");
@@ -158,6 +168,16 @@ private IEnumerable<string> GetSubscriptions()
158168
return this.Subscription;
159169
}
160170

171+
// Use selected subscription (for example, by command "Select-AzSubscription {subscriptionId}")
172+
if (this.TryGetDefaultContext(out var context))
173+
{
174+
var subscriptionId = context.Subscription?.Id;
175+
if(subscriptionId != null)
176+
{
177+
return new List<string> { subscriptionId };
178+
}
179+
}
180+
161181
var accountSubscriptions = this.DefaultContext.Account.GetSubscriptions();
162182
if (accountSubscriptions.Length > 0)
163183
{

0 commit comments

Comments
 (0)