Skip to content

Commit

Permalink
Fix for output type and null subscriptions collection in ResourceGraph (
Browse files Browse the repository at this point in the history
#15135)

* Fixed null subscriptions exception, added change log

* Added output type fix and updated tests
Changes from PR #15125

* regenerated ps1xml, updated examples

* Updated md file for the output type and changelog

* added breaking change issues csv after rebasing with master

* added parenthesses to csv to fix parsing issue

Co-authored-by: makharch <makharch@microsoft.com>
  • Loading branch information
fenwickt and makharch authored Jun 10, 2021
1 parent dbf0b22 commit d0fb2f6
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Run simple query
function Search-AzureRmGraph-Query
{
$queryResult = Search-AzGraph 'Resources | where tags != "" | project id, tags, properties | limit 2'

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResult
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResult
Assert-Null $queryResult.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResult.Data
Assert-AreEqual 2 $queryResult.Data.Count
Expand Down Expand Up @@ -55,7 +55,7 @@ function Search-AzureRmGraph-PagedQuery
# Page size was artificially set to 2 rows
$queryResult = Search-AzGraph "project id" -First 3 -Skip 2

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResult
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResult
Assert-IsInstance System.String $queryResult.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResult.Data
Assert-AreEqual 3 $queryResult.Data.Count
Expand Down Expand Up @@ -95,17 +95,17 @@ function Search-AzureRmGraph-Subscriptions
$queryResultOneSub = Search-AzGraph $query -Subscription $testSubId
$queryResultMultipleSubs = Search-AzGraph $query -Subscription @($testSubId, $nonExsitentTestSubId)

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResultSubsFromContext
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResultSubsFromContext
Assert-Null $queryResultSubsFromContext.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResultSubsFromContext.Data
Assert-AreEqual $testSubId $queryResultSubsFromContext.Data.subscriptionId

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResultOneSub
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResultOneSub
Assert-Null $queryResultOneSub.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResultOneSub.Data
Assert-AreEqual $testSubId $queryResultOneSub.Data.subscriptionId

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResultMultipleSubs
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResultMultipleSubs
Assert-Null $queryResultMultipleSubs.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResultMultipleSubs.Data
Assert-AreEqual $testSubId $queryResultMultipleSubs.Data.subscriptionId
Expand All @@ -126,12 +126,12 @@ function Search-AzureRmGraph-ManagementGroups
$queryResultOneMg = Search-AzGraph $query -ManagementGroup $testMgId1
$queryResultMultipleMgs = Search-AzGraph $query -ManagementGroup @($testMgId1, $testMgId2, $nonExistentTestMgId) -AllowPartialScope

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResultOneMg
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResultOneMg
Assert-Null $queryResultOneMg.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResultOneMg.Data
Assert-AreEqual $testSubId $queryResultOneMg.Data.subscriptionId

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResultMultipleMgs
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResultMultipleMgs
Assert-Null $queryResultMultipleMgs.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResultMultipleMgs.Data
Assert-AreEqual $testSubId $queryResultMultipleMgs.Data.subscriptionId
Expand All @@ -145,7 +145,7 @@ function Search-AzureRmGraph-SkipTokenQuery
{
$queryResult = Search-AzGraph "project id, properties" -SkipToken "ew0KICAiJGlkIjogIjEiLA0KICAiTWF4Um93cyI6IDMsDQogICJSb3dzVG9Ta2lwIjogMywNCiAgIkt1c3RvQ2x1c3RlclVybCI6ICJodHRwczovL2FyZy1ldXMtc2l4LXNmLmFyZy5jb3JlLndpbmRvd3MubmV0Ig0KfQ=="

Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse $queryResult
Assert-IsInstance Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse[PSObject] $queryResult
Assert-IsInstance System.String $queryResult.SkipToken
Assert-IsInstance System.Collections.Generic.List[PSObject] $queryResult.Data
Assert-AreEqual 3 $queryResult.Data.Count
Expand Down
4 changes: 2 additions & 2 deletions src/ResourceGraph/ResourceGraph/Az.ResourceGraph.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ RequiredAssemblies = 'Microsoft.Azure.Management.ResourceGraph.dll',
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
FormatsToProcess = 'ResourceGraph.format.ps1xml',
FormatsToProcess = 'Microsoft.Azure.PowerShell.Cmdlets.ResourceGraph.generated.format.ps1xml',
'ResourceGraph.Autorest\Az.ResourceGraph.format.ps1xml'

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
NestedModules = @('Az.ResourceGraph.psm1',
NestedModules = @('Microsoft.Azure.PowerShell.Cmdlets.ResourceGraph.dll',
'ResourceGraph.Autorest\Az.ResourceGraph.psm1')

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
Expand Down
2 changes: 2 additions & 0 deletions src/ResourceGraph/ResourceGraph/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Additional information about change #1
-->
## Upcoming Release
* Fixed the output print issue for `Search-AzGraph` by updating the output type to Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse`1[[System.Management.Automation.PSObject]].
* Fixed the issue when Search-AzGraph fails if no subscriptions are stored in the context.

## Version 0.10.0
* Changed output of `Search-AzGraph` to PSResourceGraphResponse which wrapped previous output under Data property.
Expand Down
8 changes: 4 additions & 4 deletions src/ResourceGraph/ResourceGraph/Cmdlets/SearchAzureRmGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Microsoft.Azure.Commands.ResourceGraph.Cmdlets
/// Search-AzGraph cmdlet
/// </summary>
/// <seealso cref="Microsoft.Azure.Commands.ResourceGraph.Utilities.ResourceGraphBaseCmdlet" />
[Cmdlet(VerbsCommon.Search, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Graph", DefaultParameterSetName = "SubscriptionScopedQuery"), OutputType(typeof(PSResourceGraphResponse))]
[Cmdlet(VerbsCommon.Search, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Graph", DefaultParameterSetName = "SubscriptionScopedQuery"), OutputType(typeof(PSResourceGraphResponse<PSObject>))]
public class SearchAzureRmGraph : ResourceGraphBaseCmdlet
{
/// <summary>
Expand Down Expand Up @@ -150,7 +150,7 @@ public override void ExecuteCmdlet()
IList<string> subscriptions = null;
if (managementGroups == null)
{
subscriptions = this.GetSubscriptions().ToList();
subscriptions = this.GetSubscriptions()?.ToList();
if (subscriptions != null && subscriptions.Count > SubscriptionLimit)
{
subscriptions = subscriptions.Take(SubscriptionLimit).ToList();
Expand All @@ -160,7 +160,7 @@ public override void ExecuteCmdlet()
}
}

var psResourceGraphResponse = new PSResourceGraphResponse();
var psResourceGraphResponse = new PSResourceGraphResponse<PSObject>();
QueryResponse response = null;

var resultTruncated = false;
Expand Down Expand Up @@ -262,7 +262,7 @@ private IEnumerable<string> GetSubscriptions()
}

var accountSubscriptions = this.DefaultContext.Account.GetSubscriptions();
if (accountSubscriptions.Length > 0)
if (accountSubscriptions?.Length > 0)
{
return accountSubscriptions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0"?>
<Configuration>
<ViewDefinitions>
<View>
<Name>Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse</Name>
<Name>Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse`1[[System.Management.Automation.PSObject]]</Name>
<ViewSelectedBy>
<TypeName>Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse</TypeName>
<TypeName>Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse`1[[System.Management.Automation.PSObject]]</TypeName>
</ViewSelectedBy>
<ListControl>
<ListEntries>
<ListEntry>
<ListItems>
<ListItem>
<PropertyName>SkipToken</PropertyName>
<ItemSelectionCondition>
<ScriptBlock>
$_.SkipToken -ne $null
</ScriptBlock>
</ItemSelectionCondition>
</ListItem>
<ListItem>
<PropertyName>Data</PropertyName>
</ListItem>
</ListItem>
</ListItems>
</ListEntry>
</ListEntries>
</ListControl>
</View>
</ViewDefinitions>
</Configuration>
</Configuration>
40 changes: 38 additions & 2 deletions src/ResourceGraph/ResourceGraph/Models/PSResourceGraphResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,53 @@

namespace Microsoft.Azure.Commands.ResourceGraph.Models
{
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.WindowsAzure.Commands.Common.Attributes;

public class PSResourceGraphResponse
public class PSResourceGraphResponse<PSObject> : IList<PSObject>
{
[Ps1Xml(Target = ViewControl.List)]
public string SkipToken { get; set; }

[Ps1Xml(Target = ViewControl.List)]
public IList<PSObject> Data { get; set; }
public PSObject this[int index]
{
get => Data[index];
set => Data[index] = value;
}

public IEnumerator<PSObject> GetEnumerator()
{
return Data.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public bool IsReadOnly => Data.IsReadOnly;

public int Count => Data.Count;

public void Add(PSObject value) => Data.Add(value);

public void Clear() => Data.Clear();

public bool Contains(PSObject value) => Data.Contains(value);

public void CopyTo(PSObject[] array, int index) => Data.CopyTo(array, index);

public int IndexOf(PSObject value) => Data.IndexOf(value);

public void Insert(int index, PSObject value) => Data.Insert(index, value);

public void Remove(PSObject value) => Data.Remove(value);

public void RemoveAt(int index) => Data.RemoveAt(index);

bool ICollection<PSObject>.Remove(PSObject item) => Data.Remove(item);
}
}
27 changes: 19 additions & 8 deletions src/ResourceGraph/ResourceGraph/help/Search-AzGraph.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ name : nt
type : microsoft.compute/virtualmachinescalesets
location : eastus
tags : @{resourceType=Service Fabric; clusterName=gov-art-int-nt-a}
ResourceId : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.Compute/virtualMachineScaleSets/nt
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.EventGrid/topics/egtopic-1
name : egtopic-1
type : microsoft.eventgrid/topics
location : westus2
tags :
ResourceId : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.EventGrid/topics/egtopic-1
```

Simple resources query requesting a subset of resource fields.
Expand All @@ -64,34 +66,43 @@ A complex query on resources featuring field selection, filtering and summarizin

### Example 3
```powershell
PS C:\> Search-AzGraph -Query 'project id, name' -SkipToken 'skiptokenvaluefromthepreviousquery=='
PS C:\> $response = Search-AzGraph -Query "project id, name, type, location" -First 2
PS C:\> Search-AzGraph -Query "project id, name, type, location" -SkipToken $response.SkipToken
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-b/providers/Microsoft.Compute/virtualMachineScaleSets/nt2
name : nt2
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/17ni
name : 17ni
type : microsoft.network/networkinterfaces
location : westeurope
ResourceId : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/test/providers/Microsoft.Network/networkInterfaces/17ni
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-b/providers/Microsoft.EventGrid/topics/egtopic-2
name : egtopic-2
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/17nsg
name : 17nsg
type : microsoft.network/networksecuritygroups
location : westeurope
ResourceId : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/test/providers/Microsoft.Network/networkSecurityGroups/17nsg
```

A query with the skip token passed from the previous query results
A query with the skip token passed from the previous query results. Please note that keeping id in the results is mandatory to get back a skip token.

### Example 4
```powershell
PS C:\> Search-AzGraph -Query 'project id, name, type, location, tags' -First 2 -ManagementGroup 'MyManagementGroupId' -AllowPartialScope
PS C:\> Search-AzGraph -Query "project id, name, type, location, tags" -First 2 -ManagementGroup MyManagementGroupId -AllowPartialScope
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.Compute/virtualMachineScaleSets/nt
name : nt
type : microsoft.compute/virtualmachinescalesets
location : eastus
tags : @{resourceType=Service Fabric; clusterName=gov-art-int-nt-a}
ResourceId : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.Compute/virtualMachineScaleSets/nt
id : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.EventGrid/topics/egtopic-1
name : egtopic-1
type : microsoft.eventgrid/topics
location : westus2
tags :
ResourceId : /subscriptions/1ef51df4-f8a9-4b69-9919-1ef51df4eff6/resourceGroups/Service-INT-a/providers/Microsoft.EventGrid/topics/egtopic-1
```

A query scoped to the management group that allows the query to succeed with partial scope result if MyManagementGroupId has more than N subscriptions underneath.
Expand Down Expand Up @@ -229,7 +240,7 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
## OUTPUTS
### Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse
### Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse`1[[System.Management.Automation.PSObject]]

## NOTES

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"AssemblyFileName","ClassName","Target","Severity","ProblemId","Description","Remediation"
"Az.ResourceGraph","Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph","Search-AzGraph","0","1020","The cmdlet 'Search-AzGraph' no longer has output type 'Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse'.","Make cmdlet 'Search-AzGraph' return type 'Microsoft.Azure.Commands.ResourceGraph.Models.PSResourceGraphResponse'."

0 comments on commit d0fb2f6

Please sign in to comment.