Skip to content

Update properties of Search-AzGraph in regard to the new GA API version #14639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.0.0" />
<PackageReference Include="Microsoft.Azure.Management.ResourceGraph" Version="2.1.0" />
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions src/ResourceGraph/ResourceGraph.Test/ScenarioTests/Common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ Validates an object is instance of a type
#>
function Assert-IsInstance
{
param([object] $obj, [Type] $type)
param([Type] $type, [object] $obj)

Assert-AreEqual $obj.GetType() $type
Assert-AreEqual $type $obj.GetType()
}

<#
Expand All @@ -40,8 +40,8 @@ Validates property count of a custom object
#>
function Assert-PropertiesCount
{
param([PSCustomObject] $obj, [int] $count)
param([int] $count, [PSCustomObject] $obj)

$properties = $obj.PSObject.Properties
Assert-AreEqual $([System.Linq.Enumerable]::ToArray($properties).Count) $count
Assert-AreEqual $count $([System.Linq.Enumerable]::ToArray($properties).Count)
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,19 @@ public void Subscriptions()

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void InlcudeSubscriptionNames()
public void ManagementGroups()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-IncludeSubscriptionNames");
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-ManagementGroups");
}

[Fact(Skip = "Fails on Linux. Equality assertion fails. Investigation needed.")]
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SkipTokenQuery()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-SkipTokenQuery");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void QueryError()
{
Expand All @@ -67,9 +74,9 @@ public void QueryError()

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void SubscriptionQueryError()
public void SubscriptionAndManagementGroupQueryError()
{
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-SubscriptionQueryError");
TestController.NewInstance.RunPowerShellTest(_logger, "Search-AzureRmGraph-SubscriptionAndManagementGroupQueryError");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ function Search-AzureRmGraph-Query
{
$queryResult = Search-AzGraph "project id, tags, properties | limit 2"

Assert-IsInstance $queryResult Object[]
Assert-AreEqual $queryResult.Count 2

Assert-IsInstance $queryResult[0] System.Management.Automation.PSCustomObject
Assert-IsInstance $queryResult[1] System.Management.Automation.PSCustomObject
Assert-PropertiesCount $queryResult[0] 4
Assert-PropertiesCount $queryResult[1] 4

Assert-IsInstance $queryResult[0].id String
Assert-IsInstance $queryResult[1].id String
Assert-IsInstance $queryResult[0].ResourceId String
Assert-IsInstance $queryResult[1].ResourceId String
Assert-IsInstance $queryResult[0].tags System.Management.Automation.PSCustomObject
Assert-IsInstance $queryResult[1].tags System.Management.Automation.PSCustomObject
Assert-IsInstance $queryResult[0].properties System.Management.Automation.PSCustomObject
Assert-IsInstance $queryResult[1].properties System.Management.Automation.PSCustomObject
Assert-IsInstance Object[] $queryResult
Assert-AreEqual 2 $queryResult.Count

Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[0]
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[1]
Assert-PropertiesCount 4 $queryResult[0]
Assert-PropertiesCount 4 $queryResult[1]

Assert-IsInstance String $queryResult[0].id
Assert-IsInstance String $queryResult[1].id
Assert-IsInstance String $queryResult[0].ResourceId
Assert-IsInstance String $queryResult[1].ResourceId
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[0].tags
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[1].tags
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[0].properties
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[1].properties

Assert-AreEqual $queryResult[0].id $queryResult[0].ResourceId
Assert-AreEqual $queryResult[1].id $queryResult[1].ResourceId

Assert-PropertiesCount $queryResult[0].properties 6
Assert-PropertiesCount $queryResult[1].properties 4
Assert-PropertiesCount 2 $queryResult[0].properties
Assert-PropertiesCount 4 $queryResult[1].properties
}

<#
Expand All @@ -53,24 +53,24 @@ function Search-AzureRmGraph-PagedQuery
# Page size was artificially set to 2 rows
$queryResult = Search-AzGraph "project id" -First 3 -Skip 2

Assert-IsInstance $queryResult Object[]
Assert-AreEqual $queryResult.Count 3
Assert-IsInstance Object[] $queryResult
Assert-AreEqual 3 $queryResult.Count

Assert-IsInstance $queryResult[0] System.Management.Automation.PSCustomObject
Assert-IsInstance $queryResult[1] System.Management.Automation.PSCustomObject
Assert-IsInstance $queryResult[2] System.Management.Automation.PSCustomObject
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[0]
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[1]
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[2]

Assert-PropertiesCount $queryResult[0] 2
Assert-PropertiesCount $queryResult[1] 2
Assert-PropertiesCount $queryResult[2] 2
Assert-PropertiesCount 2 $queryResult[0]
Assert-PropertiesCount 2 $queryResult[1]
Assert-PropertiesCount 2 $queryResult[2]

Assert-IsInstance $queryResult[0].id String
Assert-IsInstance $queryResult[1].id String
Assert-IsInstance $queryResult[2].id String
Assert-IsInstance String $queryResult[0].id
Assert-IsInstance String $queryResult[1].id
Assert-IsInstance String $queryResult[2].id

Assert-IsInstance $queryResult[0].ResourceId String
Assert-IsInstance $queryResult[1].ResourceId String
Assert-IsInstance $queryResult[2].ResourceId String
Assert-IsInstance String $queryResult[0].ResourceId
Assert-IsInstance String $queryResult[1].ResourceId
Assert-IsInstance String $queryResult[2].ResourceId

Assert-True { $queryResult[0].id.Length -gt 0 }
Assert-True { $queryResult[1].id.Length -gt 0 }
Expand All @@ -83,45 +83,78 @@ Run query with subscriptions explicitly passed
#>
function Search-AzureRmGraph-Subscriptions
{
$testSubId1 = "11111111-1111-1111-1111-111111111111"
$testSubId2 = "22222222-2222-2222-2222-222222222222"
$mockedSubscriptionId = "00000000-0000-0000-0000-000000000000"
$testSubId = "eaab1166-1e13-4370-a951-6ed345a48c15"
$nonExsitentTestSubId = "000b1166-1e13-4370-a951-6ed345a48c16"
$query = "distinct subscriptionId | order by subscriptionId asc"

$queryResultNoSubs = Search-AzGraph $query
$queryResultOneSub = Search-AzGraph $query -Subscription $testSubId1
$queryResultMultipleSubs = Search-AzGraph $query -Subscription @($testSubId1, $testSubId2)
$queryResultTenant = Search-AzGraph $query
$queryResultOneSub = Search-AzGraph $query -Subscription $testSubId
$queryResultMultipleSubs = Search-AzGraph $query -Subscription @($testSubId, $nonExsitentTestSubId)

Assert-IsInstance $queryResultNoSubs System.Management.Automation.PSCustomObject
Assert-AreEqual $queryResultNoSubs.subscriptionId $mockedSubscriptionId
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResultTenant
Assert-AreEqual $testSubId $queryResultTenant.subscriptionId

Assert-IsInstance $queryResultOneSub System.Management.Automation.PSCustomObject
Assert-AreEqual $queryResultOneSub.subscriptionId $testSubId1
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResultOneSub
Assert-AreEqual $testSubId $queryResultOneSub.subscriptionId

Assert-IsInstance System.Management.Automation.PSCustomObject $queryResultMultipleSubs
Assert-AreEqual $testSubId $queryResultMultipleSubs.subscriptionId
}

<#
.SYNOPSIS
Run query with management groups explicitly passed
#>
function Search-AzureRmGraph-ManagementGroups
{
$testSubId = "eaab1166-1e13-4370-a951-6ed345a48c15"
$testMgId1 = "f686d426-8d16-42db-81b7-ab578e110ccd"
$testMgId2 = "makharchMg"
$nonExistentTestMgId = "nonExistentMg"
$query = "distinct subscriptionId | order by subscriptionId asc"

$queryResultTenant = Search-AzGraph $query
$queryResultOneMg = Search-AzGraph $query -ManagementGroup $testMgId1
$queryResultMultipleMgs = Search-AzGraph $query -ManagementGroup @($testMgId1, $testMgId2, $nonExistentTestMgId) -AllowPartialScope

Assert-IsInstance System.Management.Automation.PSCustomObject $queryResultTenant
Assert-AreEqual $testSubId $queryResultTenant.subscriptionId

Assert-IsInstance $queryResultMultipleSubs Object[]
Assert-AreEqual $queryResultMultipleSubs.Count 2
Assert-AreEqual $queryResultMultipleSubs[0].subscriptionId $testSubId1
Assert-AreEqual $queryResultMultipleSubs[1].subscriptionId $testSubId2
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResultOneMg
Assert-AreEqual $testSubId $queryResultOneMg.subscriptionId

Assert-IsInstance System.Management.Automation.PSCustomObject $queryResultMultipleMgs
Assert-AreEqual $testSubId $queryResultMultipleMgs.subscriptionId
}

<#
.SYNOPSIS
Run query with subscriptions explicitly passed
Run simple query with the skip token
#>
function Search-AzureRmGraph-IncludeSubscriptionNames
function Search-AzureRmGraph-SkipTokenQuery
{
$mockedScopeId = "00000000-0000-0000-0000-000000000000"
$mockedSubscriptionName = "Test Subscription"
$mockedTenantName = "Test Tenant"
$query = "project subscriptionId, tenantId, subscriptionDisplayName, tenantDisplayName"

$queryResult = Search-AzGraph $query -Include "DisplayNames"

Assert-IsInstance $queryResult System.Management.Automation.PSCustomObject
Assert-AreEqual $queryResult.subscriptionId $mockedScopeId
Assert-AreEqual $queryResult.tenantId $mockedScopeId
Assert-AreEqual $queryResult.subscriptionDisplayName $mockedSubscriptionName
Assert-AreEqual $queryResult.tenantDisplayName $mockedTenantName
$queryResult = Search-AzGraph "project id, properties" -SkipToken "ew0KICAiJGlkIjogIjEiLA0KICAiTWF4Um93cyI6IDMsDQogICJSb3dzVG9Ta2lwIjogNiwNCiAgIkt1c3RvQ2x1c3RlclVybCI6ICJodHRwczovL2FybXRvcG9sb2d5Lmt1c3RvLndpbmRvd3MubmV0Ig0KfQ=="

Assert-IsInstance Object[] $queryResult
Assert-AreEqual 3 $queryResult.Count

Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[0]
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[1]
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[2]

Assert-IsInstance String $queryResult[0].id
Assert-IsInstance String $queryResult[1].id
Assert-IsInstance String $queryResult[2].id
Assert-IsInstance String $queryResult[0].ResourceId
Assert-IsInstance String $queryResult[1].ResourceId
Assert-IsInstance String $queryResult[2].ResourceId
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[0].properties
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[1].properties
Assert-IsInstance System.Management.Automation.PSCustomObject $queryResult[2].properties

Assert-AreEqual $queryResult[0].id $queryResult[0].ResourceId
Assert-AreEqual $queryResult[1].id $queryResult[1].ResourceId
Assert-AreEqual $queryResult[2].id $queryResult[2].ResourceId
}

<#
Expand All @@ -130,23 +163,30 @@ Run malformed query
#>
function Search-AzureRmGraph-QueryError
{
$expectedErrorId = 'InvalidQuery,' + [Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph].FullName
$expectedErrorDetails = '{
$expectedErrorId = 'BadRequest,' + [Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph].FullName
$expectedErrorDetailsRegex = [regex]::escape('{
"error": {
"code": "InvalidQuery",
"message": "Query validation error",
"code": "BadRequest",
"message": "Please provide below info when asking for support: timestamp = 2021-03-25T') + '.{17}?' + [regex]::escape(', correlationId = ') + '.{36}?' + [regex]::escape('.",
"details": [
{
"code": "InvalidQuery",
"message": "Query is invalid. Please refer to the documentation for the Azure Resource Graph service and fix the error before retrying."
},
{
"code": "ParserFailure",
"message": "Parser failure",
"message": "ParserFailure",
"line": 1,
"characterPositionInLine": 11,
"token": "<EOF>",
"expectedToken": "Ÿ"
"expectedToken": "Ǐ"
}
]
}
}'
}')

$expectedInnerCode = "InvalidQuery"
$expectedInnerMessage = "Query is invalid. Please refer to the documentation for the Azure Resource Graph service and fix the error before retrying."

try
{
Expand All @@ -155,43 +195,45 @@ function Search-AzureRmGraph-QueryError
}
catch [Exception]
{
Assert-AreEqual $PSItem.FullyQualifiedErrorId $expectedErrorId
Assert-AreEqual $PSItem.ErrorDetails.Message $expectedErrorDetails
Assert-IsInstance $PSItem.Exception Microsoft.Azure.Management.ResourceGraph.Models.ErrorResponseException
Assert-IsInstance $PSItem.Exception.Body Microsoft.Azure.Management.ResourceGraph.Models.ErrorResponse
Assert-AreEqual $expectedErrorId $PSItem.FullyQualifiedErrorId
Assert-Match $expectedErrorDetailsRegex $PSItem.ErrorDetails.Message
Assert-IsInstance Microsoft.Azure.Management.ResourceGraph.Models.ErrorResponseException $PSItem.Exception
Assert-IsInstance Microsoft.Azure.Management.ResourceGraph.Models.ErrorResponse $PSItem.Exception.Body

Assert-NotNull $PSItem.Exception.Body.Error.Code
Assert-NotNull $PSItem.Exception.Body.Error.Message
Assert-NotNull $PSItem.Exception.Body.Error.Details
Assert-AreEqual $PSItem.Exception.Body.Error.Details.Count 1
Assert-AreEqual 2 $PSItem.Exception.Body.Error.Details.Count

Assert-AreEqual $expectedInnerCode $PSItem.Exception.Body.Error.Details[0].Code
Assert-AreEqual $expectedInnerMessage $PSItem.Exception.Body.Error.Details[0].Message

Assert-NotNull $PSItem.Exception.Body.Error.Details[0].Code
Assert-NotNull $PSItem.Exception.Body.Error.Details[0].Message
Assert-NotNull $PSItem.Exception.Body.Error.Details[0].AdditionalProperties
Assert-AreEqual $PSItem.Exception.Body.Error.Details[0].AdditionalProperties.Count 4
Assert-NotNull $PSItem.Exception.Body.Error.Details[1].Code
Assert-NotNull $PSItem.Exception.Body.Error.Details[1].Message
Assert-NotNull $PSItem.Exception.Body.Error.Details[1].AdditionalProperties
Assert-AreEqual 4 $PSItem.Exception.Body.Error.Details[1].AdditionalProperties.Count
}
}

<#
.SYNOPSIS
Run query with no subscriptions
Run query with both subscriptions and management groups present
#>
function Search-AzureRmGraph-SubscriptionQueryError
function Search-AzureRmGraph-SubscriptionAndManagementGroupQueryError
{
$expectedErrorId = '400,' + [Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph].FullName
$expectedErrorId = 'AmbiguousParameterSet,' + [Microsoft.Azure.Commands.ResourceGraph.Cmdlets.SearchAzureRmGraph].FullName
$expectedErrorMessage =
'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'')'
'Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.'

try
{
Search-AzGraph "project id, type" -Subscription @()
Search-AzGraph "project id, type" -Subscription 'a' -ManagementGroup 'b'
Assert-True $false # Expecting an error
}
catch [Exception]
{
Assert-AreEqual $expectedErrorId $PSItem.FullyQualifiedErrorId
Assert-AreEqual $expectedErrorId $PSItem.FullyQualifiedErrorId
Assert-AreEqual $expectedErrorMessage $PSItem.Exception.Message

Assert-IsInstance $PSItem.Exception System.ArgumentException
Assert-IsInstance System.Management.Automation.ParameterBindingException $PSItem.Exception
}
}
}
Loading