Skip to content
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

Fix issue when providing both tag name and value for Get-AzureRmResource #6771

Merged
merged 2 commits into from
Jul 25, 2018
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 @@ -188,14 +188,26 @@ private void RunSimpleCmdlet()
result = FilterResourceByWildcard(result);
}

if (!string.IsNullOrEmpty(this.TagName))
if (!string.IsNullOrEmpty(this.TagName) && !string.IsNullOrEmpty(this.TagValue))
{
result = result.Where(r => r.Tags != null && r.Tags.Keys != null && r.Tags.Keys.Where(k => string.Equals(k, this.TagName, StringComparison.OrdinalIgnoreCase)).Any());
result = result.Where(r => r.Tags != null &&
r.Tags.Keys != null &&
r.Tags.Keys.Where(k => string.Equals(k, this.TagName, StringComparison.OrdinalIgnoreCase))
.Any(k => string.Equals(r.Tags[k], this.TagValue, StringComparison.OrdinalIgnoreCase)));
}

if (!string.IsNullOrEmpty(this.TagValue))
else if (!string.IsNullOrEmpty(this.TagName))
{
result = result.Where(r => r.Tags != null &&
r.Tags.Keys != null &&
r.Tags.Keys.Where(k => string.Equals(k, this.TagName, StringComparison.OrdinalIgnoreCase))
.Any());
}
else if (!string.IsNullOrEmpty(this.TagValue))
{
result = result.Where(r => r.Tags != null && r.Tags.Values != null && r.Tags.Values.Where(v => string.Equals(v, this.TagValue, StringComparison.OrdinalIgnoreCase)).Any());
result = result.Where(r => r.Tags != null &&
r.Tags.Values != null &&
r.Tags.Values.Where(v => string.Equals(v, this.TagValue, StringComparison.OrdinalIgnoreCase))
.Any());
}

WriteObject(result, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ function Test-FindAResource-ByTag
$rgname = Get-ResourceGroupName
$rname = "testname"
$rname2 = "test2name"
$rname3 = "test3name"
$rglocation = Get-Location "Microsoft.Resources" "resourceGroups" "West US"
$apiversion = "2014-04-01"
$resourceType = "Providers.Test/statefulResources"
Expand All @@ -483,22 +484,42 @@ function Test-FindAResource-ByTag
{
# Test
New-AzureRmResourceGroup -Name $rgname -Location $rglocation
$actual = New-AzureRmResource -Name $rname -Location $rglocation -Tags @{ScenarioTestTag = "ScenarioTestVal"} -ResourceGroupName $rgname -ResourceType $resourceType -PropertyObject @{"key" = "value"} -SkuObject @{ Name = "A0" } -ApiVersion $apiversion -Force
$actual1 = New-AzureRmResource -Name $rname -Location $rglocation -Tags @{ScenarioTestTag = "ScenarioTestVal"} -ResourceGroupName $rgname -ResourceType $resourceType -ApiVersion $apiversion -Force
$actual2 = New-AzureRmResource -Name $rname2 -Location $rglocation -Tags @{ScenarioTestTag = $null} -ResourceGroupName $rgname -ResourceType $resourceType -ApiVersion $apiversion -Force
$actual3 = New-AzureRmResource -Name $rname3 -Location $rglocation -Tags @{ScenarioTestTag = "RandomTestVal"; RandomTestVal = "ScenarioTestVal"} -ResourceGroupName $rgname -ResourceType $resourceType -ApiVersion $apiversion -Force

# Test both Name and Value
$expected = Get-AzureRmResource -Tag @{ScenarioTestTag = "ScenarioTestVal"}
Assert-NotNull $expected
Assert-AreEqual $actual.ResourceId $expected[0].ResourceId
Assert-AreEqual $expected.Count 1
Assert-AreEqual $actual1.ResourceId $expected[0].ResourceId

$expected = Get-AzureRmResource -TagName "ScenarioTestTag" -TagValue "ScenarioTestVal"
Assert-NotNull $expected
Assert-AreEqual $expected.Count 1
Assert-AreEqual $actual1.ResourceId $expected[0].ResourceId

# Test just Name
$expected = Get-AzureRmResource -Tag @{ScenarioTestTag = $null}
Assert-NotNull $expected
Assert-AreEqual $actual.ResourceId $expected[0].ResourceId
Assert-AreEqual $expected.Count 3
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual1.ResourceId } }
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual2.ResourceId } }
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual3.ResourceId } }

$expected = Get-AzureRmResource -TagName "ScenarioTestTag"
Assert-NotNull $expected
Assert-AreEqual $actual.ResourceId $expected[0].ResourceId
Assert-AreEqual $expected.Count 3
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual1.ResourceId } }
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual2.ResourceId } }
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual3.ResourceId } }

$expected = Get-AzureRmResource -TagName "ScenarioTestTag" -TagValue "ScenarioTestVal"
# Test just Value
$expected = Get-AzureRmResource -TagValue "ScenarioTestVal"
Assert-NotNull $expected
Assert-AreEqual $actual.ResourceId $expected[0].ResourceId
Assert-AreEqual $expected.Count 2
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual1.ResourceId } }
Assert-NotNull { $expected | where { $_.ResourceId -eq $actual3.ResourceId } }
}
finally
{
Expand Down Expand Up @@ -722,4 +743,4 @@ function Test-RemoveASetOfResources
Get-AzureRmResource -ResourceName "*test*" -ResourceGroupName "*$rgname*" | Remove-AzureRmResource -Force
$expected = Get-AzureRmResource -ResourceName "*test*" -ResourceGroupName "*$rgname*"
Assert-Null $expected
}
}

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/ResourceManager/Resources/Commands.Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
## Current Release
* Updated help files to include full parameter types and correct input/output types.
* Fix piping scenario with `Set-AzureRmResource`
* Fix issue when providing both tag name and value for `Get-AzureRmResource`
- https://github.com/Azure/azure-powershell/issues/6765

## Version 6.2.1
* Update Roleassignment and roledefinition cmdlets:
Expand Down