Skip to content

Commit 1faef08

Browse files
committed
Add "Tag" parameter to FindAzureResourceCmdlet.
Change parameter sets. Change Parameter Set. Add comments. Disallow combining other filters with tag filters as the service does not support it. Address code review comments. Address code review comments. Record Scenario Test for find resource by tag. Update command help. Update help. Add new file to csproj. Correct output settings for the json file for the new test. Add Change Log. Fix indentation in ChangeLog.md
1 parent 7a22019 commit 1faef08

File tree

8 files changed

+1259
-598
lines changed

8 files changed

+1259
-598
lines changed

src/ResourceManager/Resources/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
## Version 3.4.0
2323
* Support ResourceNameEquals and ResourceGroupNameEquals as parameters for Find-AzureRmResource
2424
- Users can now use ResourceNameEquals and ResourceGroupNameEquals with Find-AzureRmResource
25+
* Support Tag as parameters for Find-AzureRmResource
26+
- Users can now use Tag parameter with Find-AzureRmResource
27+
- Fixed the issue where illegal combinations of TagName, TagValue with other search parameters was allowed in Find-AzureRmResource and would result in users getting exception from the service by disallowing such combinations.
2528

2629
## Version 3.3.0
2730
* Lookup of AAD group by Id now uses GetObjectsByObjectId AAD Graph call instead of Groups/<id>

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Components/TagsHelper.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
2121
using System.Collections;
2222
using System.Collections.Generic;
2323
using System.Linq;
24+
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
2425

2526
/// <summary>
2627
/// Helper class for tags.
@@ -52,5 +53,57 @@ internal static Hashtable GetTagsHashtable(InsensitiveDictionary<string> tags)
5253
? null
5354
: TagsConversionHelper.CreateTagHashtable(tags);
5455
}
56+
57+
/// <summary>
58+
/// Resolves the tag name given the tagObj and tagName parameters. If both are specified then tagObj takes precedence.
59+
/// </summary>
60+
/// <param name="tagObjParameter">Parameter containing the tag name-value specified as a hashset.</param>
61+
/// <param name="tagNameParameter">Parameter containing the tag name specified individually.</param>
62+
/// <returns>The resolved tag name.</returns>
63+
internal static string GetTagNameFromParameters(Hashtable tagObjParameter, string tagNameParameter)
64+
{
65+
PSTagValuePair tagValuePair = null;
66+
if (tagObjParameter != null)
67+
{
68+
tagValuePair = TagsConversionHelper.Create(tagObjParameter);
69+
70+
if (tagValuePair == null)
71+
{
72+
throw new ArgumentException(ProjectResources.InvalidTagFormat);
73+
}
74+
}
75+
76+
if (tagValuePair != null)
77+
{
78+
return tagValuePair.Name;
79+
}
80+
return tagNameParameter;
81+
}
82+
83+
/// <summary>
84+
/// Resolves the tag value given the tagObj and tagValue parameters. If both are specified then tagObj takes precedence.
85+
/// </summary>
86+
/// <param name="tagObjParameter">Parameter containing the tag name-value specified as a hashset.</param>
87+
/// <param name="tagValueParameter">Parameter containing the tag value specified individually.</param>
88+
/// <returns>The resolved tag value.</returns>
89+
internal static string GetTagValueFromParameters(Hashtable tagObjParameter, string tagValueParameter)
90+
{
91+
PSTagValuePair tagValuePair = null;
92+
if (tagObjParameter != null)
93+
{
94+
tagValuePair = TagsConversionHelper.Create(tagObjParameter);
95+
96+
if (tagValuePair == null)
97+
{
98+
throw new ArgumentException(ProjectResources.InvalidTagFormat);
99+
}
100+
}
101+
102+
if (tagValuePair != null)
103+
{
104+
return tagValuePair.Value;
105+
}
106+
return tagValueParameter;
107+
}
55108
}
56109
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/Resource/FindAzureResourceCmdlet.cs

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17+
using Common.Tags;
1718
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
1819
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
1920
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2021
using Microsoft.Azure.Commands.ResourceManager.Common;
2122
using Newtonsoft.Json.Linq;
2223
using System;
24+
using System.Collections;
2325
using System.Collections.Concurrent;
2426
using System.Collections.Generic;
2527
using System.Linq;
2628
using System.Management.Automation;
2729
using System.Threading.Tasks;
30+
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
2831

2932
/// <summary>
3033
/// Cmdlet to get existing resources from ARM cache.
@@ -52,6 +55,16 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
5255
/// </summary>
5356
internal const string MultiSubscriptionListResourcesParameterSet = "Get a resources using a multi-subscription query.";
5457

58+
/// <summary>
59+
/// The list resources by tag object parameter set.
60+
/// </summary>
61+
internal const string ListResourcesByTagObjectParameterSet = "Lists resources by a tag object specified as a hashset.";
62+
63+
/// <summary>
64+
/// The list resources by tag name-value parameter set.
65+
/// </summary>
66+
internal const string ListResourcesByTagNameValueParameterSet = "Lists resources by a tag specified as a individual name and value parameters.";
67+
5568
/// <summary>
5669
/// Caches the current subscription ids to get all subscription ids in the pipeline.
5770
/// </summary>
@@ -100,6 +113,8 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
100113
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, Mandatory = false, HelpMessage = "The number of resources to retrieve.")]
101114
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListTenantResourcesParameterSet, Mandatory = false, HelpMessage = "The number of resources to retrieve.")]
102115
[Parameter(ParameterSetName = FindAzureResourceCmdlet.MultiSubscriptionListResourcesParameterSet, Mandatory = false, HelpMessage = "The number of resources to retrieve.")]
116+
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesByTagObjectParameterSet, Mandatory = false, HelpMessage = "The number of resources to retrieve.")]
117+
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesByTagNameValueParameterSet, Mandatory = false, HelpMessage = "The number of resources to retrieve.")]
103118
[ValidateNotNullOrEmpty]
104119
public int? Top { get; set; }
105120

@@ -110,19 +125,20 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
110125
[ValidateNotNullOrEmpty]
111126
public string ODataQuery { get; set; }
112127

128+
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesByTagObjectParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The tag filter for the OData query. The expected format is @{tagName=$null} or @{tagName = 'tagValue'}.")]
129+
public Hashtable Tag { get; set; }
130+
113131
/// <summary>
114132
/// Gets or sets the tag name.
115133
/// </summary>
116-
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, Mandatory = false, HelpMessage = "The name of the tag to query by.")]
117-
[Parameter(ParameterSetName = FindAzureResourceCmdlet.MultiSubscriptionListResourcesParameterSet, Mandatory = false, HelpMessage = "The name of the tag to query by.")]
134+
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesByTagNameValueParameterSet, Mandatory = false, HelpMessage = "The name of the tag to query by.")]
118135
[ValidateNotNullOrEmpty]
119136
public string TagName { get; set; }
120137

121138
/// <summary>
122139
/// Gets or sets the tag value.
123140
/// </summary>
124-
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, Mandatory = false, HelpMessage = "The value of the tag to query by.")]
125-
[Parameter(ParameterSetName = FindAzureResourceCmdlet.MultiSubscriptionListResourcesParameterSet, Mandatory = false, HelpMessage = "The value of the tag to query by.")]
141+
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesByTagNameValueParameterSet, Mandatory = false, HelpMessage = "The value of the tag to query by.")]
126142
[ValidateNotNullOrEmpty]
127143
public string TagValue { get; set; }
128144

@@ -275,8 +291,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesTypeCollect
275291
resourceGroup: this.ResourceGroupNameEquals,
276292
resourceType: null,
277293
resourceName: this.ResourceNameEquals,
278-
tagName: this.TagName,
279-
tagValue: this.TagValue,
294+
tagName: TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName),
295+
tagValue: TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue),
280296
filter: this.ODataQuery,
281297
resourceGroupNameContains: this.ResourceGroupNameContains,
282298
nameContains: this.ResourceNameContains);
@@ -302,8 +318,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInTenant()
302318
resourceGroup: this.ResourceGroupNameEquals,
303319
resourceType: this.ResourceType,
304320
resourceName: this.ResourceNameEquals,
305-
tagName: this.TagName,
306-
tagValue: this.TagValue,
321+
tagName: TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName),
322+
tagValue: TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue),
307323
filter: this.ODataQuery,
308324
resourceGroupNameContains: this.ResourceGroupNameContains,
309325
nameContains: this.ResourceNameContains);
@@ -329,8 +345,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInResourceG
329345
resourceGroup: this.ResourceGroupNameEquals,
330346
resourceType: this.ResourceType,
331347
resourceName: this.ResourceNameEquals,
332-
tagName: this.TagName,
333-
tagValue: this.TagValue,
348+
tagName: TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName),
349+
tagValue: TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue),
334350
filter: this.ODataQuery,
335351
resourceGroupNameContains: this.ResourceGroupNameContains,
336352
nameContains: this.ResourceNameContains);
@@ -358,8 +374,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInSubscript
358374
resourceGroup: null,
359375
resourceType: this.ResourceType,
360376
resourceName: this.ResourceNameEquals,
361-
tagName: this.TagName,
362-
tagValue: this.TagValue,
377+
tagName: TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName),
378+
tagValue: TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue),
363379
filter: this.ODataQuery,
364380
nameContains: this.ResourceNameContains);
365381

@@ -493,8 +509,8 @@ private bool IsResourceGroupLevelQuery()
493509
{
494510
return this.SubscriptionId.HasValue &&
495511
this.ResourceGroupNameAvailable() &&
496-
(this.TagName != null ||
497-
this.TagValue != null ||
512+
(TagsHelper.GetTagNameFromParameters(this.Tag, this.TagName) != null ||
513+
TagsHelper.GetTagValueFromParameters(this.Tag, this.TagValue) != null ||
498514
this.ResourceType != null ||
499515
this.ExtensionResourceType != null);
500516
}

0 commit comments

Comments
 (0)