Skip to content

huangpf PR: dev <- Azure:dev #670

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 5 commits into from
Dec 8, 2016
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
2 changes: 2 additions & 0 deletions src/ResourceManager/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
- Additional information about change #1
-->
## Current Release
* Support ResourceNameEquals and ResourceGroupNameEquals as parameters for Find-AzureRmResource
- Users can now use ResourceNameEquals and ResourceGroupNameEquals with Find-AzureRmResource

## Version 3.3.0
* Lookup of AAD group by Id now uses GetObjectsByObjectId AAD Graph call instead of Groups/<id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public static class QueryFilterBuilder
/// <summary>
/// Creates a filter from the given properties.
/// </summary>
/// <param name="subscriptionIds">The subscriptions to query.</param>
/// <param name="subscriptionId">The subscription to query.</param>
/// <param name="resourceGroup">The name of the resource group/</param>
/// <param name="resourceType">The resource type.</param>
/// <param name="resourceName">The resource name.</param>
/// <param name="tagName">The tag name.</param>
/// <param name="tagValue">The tag value.</param>
/// <param name="filter">The filter.</param>
public static string CreateFilter(
Guid[] subscriptionIds,
string subscriptionId,
string resourceGroup,
string resourceType,
string resourceName,
Expand All @@ -47,21 +47,9 @@ public static string CreateFilter(
{
var filterStringBuilder = new StringBuilder();

if (subscriptionIds.CoalesceEnumerable().Any())
if (!string.IsNullOrWhiteSpace(subscriptionId))
{
if (subscriptionIds.Length > 1)
{
filterStringBuilder.Append("(");
}

filterStringBuilder.Append(subscriptionIds
.Select(subscriptionId => string.Format("subscriptionId EQ '{0}'", subscriptionId))
.ConcatStrings(" OR "));

if (subscriptionIds.Length > 1)
{
filterStringBuilder.Append(")");
}
filterStringBuilder.AppendFormat("subscriptionId EQ '{0}'", subscriptionId);
}

if (!string.IsNullOrWhiteSpace(resourceGroup))
Expand All @@ -74,33 +62,13 @@ public static string CreateFilter(
filterStringBuilder.AppendFormat("resourceGroup EQ '{0}'", resourceGroup);
}

var remainderFilter = QueryFilterBuilder.CreateFilter(resourceType, resourceName, tagName, tagValue, filter, nameContains, resourceGroupNameContains);

if (filterStringBuilder.Length > 0 && !string.IsNullOrWhiteSpace(remainderFilter))
{
return filterStringBuilder.ToString() + " AND " + remainderFilter;
}

return filterStringBuilder.Length > 0
? filterStringBuilder.ToString()
: remainderFilter;
}

/// <summary>
/// Creates a filter from the given properties.
/// </summary>
/// <param name="resourceType">The resource type.</param>
/// <param name="resourceName">The resource name.</param>
/// <param name="tagName">The tag name.</param>
/// <param name="tagValue">The tag value.</param>
/// <param name="filter">The filter.</param>
public static string CreateFilter(string resourceType, string resourceName, string tagName, string tagValue, string filter, string nameContains = null, string resourceGroupNameContains = null)
{
var filterStringBuilder = new StringBuilder();
var substringStringBuilder = new StringBuilder();

if (!string.IsNullOrWhiteSpace(resourceType))
{
if (filterStringBuilder.Length > 0)
{
filterStringBuilder.Append(" AND ");
}

filterStringBuilder.AppendFormat("resourceType EQ '{0}'", resourceType);
}

Expand Down Expand Up @@ -134,24 +102,24 @@ public static string CreateFilter(string resourceType, string resourceName, stri
filterStringBuilder.AppendFormat("tagValue EQ '{0}'", tagValue);
}

if (!string.IsNullOrWhiteSpace(nameContains))
if (!string.IsNullOrWhiteSpace(resourceGroupNameContains))
{
if (filterStringBuilder.Length > 0)
{
filterStringBuilder.Append(" AND ");
}

filterStringBuilder.AppendFormat("substringof('{0}', name)", nameContains);
filterStringBuilder.AppendFormat("substringof('{0}', resourceGroup)", resourceGroupNameContains);
}

if (!string.IsNullOrWhiteSpace(resourceGroupNameContains))
if (!string.IsNullOrWhiteSpace(nameContains))
{
if (filterStringBuilder.Length > 0)
{
filterStringBuilder.Append(" AND ");
}

filterStringBuilder.AppendFormat("substringof('{0}', resourceGroup)", resourceGroupNameContains);
filterStringBuilder.AppendFormat("substringof('{0}', name)", nameContains);
}

if (!string.IsNullOrWhiteSpace(filter))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
private readonly List<Guid> subscriptionIds = new List<Guid>();

/// <summary>
/// Gets or sets the resource name parameter.
/// Gets or sets the resource name for query as contains.
/// </summary>
[Alias("Name")]
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name substring. e.g. if your resource name is testResource, you can specify test.")]
Expand All @@ -67,6 +67,15 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
[ValidateNotNullOrEmpty]
public string ResourceNameContains { get; set; }

/// <summary>
/// Gets or sets the resource name for query as equals.
/// </summary>
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name for a full match. e.g. if your resource name is testResource, you can specify testResource.")]
[Parameter(ParameterSetName = FindAzureResourceCmdlet.ListTenantResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name for a full match. e.g. if your resource name is testResource, you can specify testResource.")]
[Parameter(ParameterSetName = FindAzureResourceCmdlet.MultiSubscriptionListResourcesParameterSet, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource name for a full match. e.g. if your resource name is testResource, you can specify testResource.")]
[ValidateNotNullOrEmpty]
public string ResourceNameEquals { get; set; }

/// <summary>
/// Gets or sets the resource type parameter.
/// </summary>
Expand Down Expand Up @@ -118,14 +127,22 @@ public sealed class FindAzureResourceCmdlet : ResourceManagerCmdletBase
public string TagValue { get; set; }

/// <summary>
/// Gets or sets the resource group name.
/// Gets or sets the resource group name for query as contains.
/// </summary>
[Alias("ResourceGroupName")]
[Parameter(Mandatory = false, ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name substring.")]
[Parameter(Mandatory = false, ParameterSetName = FindAzureResourceCmdlet.MultiSubscriptionListResourcesParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name substring.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupNameContains { get; set; }

/// <summary>
/// Gets or sets the resource group name for query as equals.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = FindAzureResourceCmdlet.ListResourcesParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name for a full match.")]
[Parameter(Mandatory = false, ParameterSetName = FindAzureResourceCmdlet.MultiSubscriptionListResourcesParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource group name for a full match.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupNameEquals { get; set; }

/// <summary>
/// Gets or sets the expand properties property.
/// </summary>
Expand Down Expand Up @@ -254,8 +271,10 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesTypeCollect
extensionResourceName: null);

var odataQuery = QueryFilterBuilder.CreateFilter(
subscriptionId: null,
resourceGroup: this.ResourceGroupNameEquals,
resourceType: null,
resourceName: null,
resourceName: this.ResourceNameEquals,
tagName: this.TagName,
tagValue: this.TagValue,
filter: this.ODataQuery,
Expand All @@ -279,10 +298,10 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInTenant()
{
var filterQuery = QueryFilterBuilder
.CreateFilter(
subscriptionIds: new Guid[] { this.SubscriptionId.Value },
resourceGroup: null,
subscriptionId: this.SubscriptionId.HasValue ? this.SubscriptionId.Value.ToString() : null,
resourceGroup: this.ResourceGroupNameEquals,
resourceType: this.ResourceType,
resourceName: null,
resourceName: this.ResourceNameEquals,
tagName: this.TagName,
tagValue: this.TagValue,
filter: this.ODataQuery,
Expand All @@ -306,8 +325,10 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInResourceG
{
var filterQuery = QueryFilterBuilder
.CreateFilter(
subscriptionId: null,
resourceGroup: this.ResourceGroupNameEquals,
resourceType: this.ResourceType,
resourceName: null,
resourceName: this.ResourceNameEquals,
tagName: this.TagName,
tagValue: this.TagValue,
filter: this.ODataQuery,
Expand All @@ -333,8 +354,10 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInSubscript
{
var filterQuery = QueryFilterBuilder
.CreateFilter(
subscriptionId: null,
resourceGroup: null,
resourceType: this.ResourceType,
resourceName: null,
resourceName: this.ResourceNameEquals,
tagName: this.TagName,
tagValue: this.TagValue,
filter: this.ODataQuery,
Expand Down Expand Up @@ -429,7 +452,7 @@ private bool IsResourceTypeCollectionGet()
private bool IsSubscriptionLevelResourceTypeCollectionGet()
{
return this.SubscriptionId.HasValue &&
this.ResourceGroupNameContains == null &&
!this.ResourceGroupNameAvailable() &&
(this.ResourceType != null || this.ExtensionResourceType != null);
}

Expand All @@ -439,7 +462,7 @@ private bool IsSubscriptionLevelResourceTypeCollectionGet()
private bool IsResourceGroupLevelResourceTypeCollectionGet()
{
return this.SubscriptionId.HasValue &&
this.ResourceGroupNameContains != null &&
this.ResourceGroupNameAvailable() &&
(this.ResourceType != null || this.ExtensionResourceType != null);
}

Expand All @@ -450,7 +473,7 @@ private bool IsResourceGroupLevelResourceTypeCollectionGet()
private bool IsTenantLevelResourceTypeCollectionGet()
{
return this.SubscriptionId == null &&
this.ResourceGroupNameContains == null &&
!this.ResourceGroupNameAvailable() &&
(this.ResourceType != null || this.ExtensionResourceType != null);
}

Expand All @@ -460,7 +483,7 @@ private bool IsTenantLevelResourceTypeCollectionGet()
private bool IsSubscriptionLevelQuery()
{
return this.SubscriptionId.HasValue &&
this.ResourceGroupNameContains == null;
!this.ResourceGroupNameAvailable();
}

/// <summary>
Expand All @@ -469,11 +492,19 @@ private bool IsSubscriptionLevelQuery()
private bool IsResourceGroupLevelQuery()
{
return this.SubscriptionId.HasValue &&
this.ResourceGroupNameContains != null &&
this.ResourceGroupNameAvailable() &&
(this.TagName != null ||
this.TagValue != null ||
this.ResourceType != null ||
this.ExtensionResourceType != null);
}

/// <summary>
/// Returns true if resource group name is availabe in parameters.
/// </summary>
private bool ResourceGroupNameAvailable()
{
return this.ResourceGroupNameContains != null || this.ResourceGroupNameEquals != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ private async Task<JObject> GetResource()
.ConfigureAwait(continueOnCapturedContext: false);

var odataQuery = QueryFilterBuilder.CreateFilter(
subscriptionId: null,
resourceGroup: null,
resourceType: null,
resourceName: null,
tagName: null,
Expand Down Expand Up @@ -339,6 +341,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesTypeCollect
extensionResourceName: this.ExtensionResourceName);

var odataQuery = QueryFilterBuilder.CreateFilter(
subscriptionId: null,
resourceGroup: null,
resourceType: null,
resourceName: null,
tagName: null,
Expand All @@ -362,7 +366,7 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInTenant()
{
var filterQuery = QueryFilterBuilder
.CreateFilter(
subscriptionIds: null,
subscriptionId: null,
resourceGroup: this.ResourceGroupName,
resourceType: this.ResourceType,
resourceName: this.ResourceName,
Expand All @@ -387,6 +391,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInResourceG
{
var filterQuery = QueryFilterBuilder
.CreateFilter(
subscriptionId: null,
resourceGroup: null,
resourceType: this.ResourceType,
resourceName: this.ResourceName,
tagName: null,
Expand All @@ -412,6 +418,8 @@ private async Task<ResponseWithContinuation<JObject[]>> ListResourcesInSubscript
{
var filterQuery = QueryFilterBuilder
.CreateFilter(
subscriptionId: null,
resourceGroup: null,
resourceType: this.ResourceType,
resourceName: this.ResourceName,
tagName: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ function Test-FindAResource
New-AzureRmResource -Name $rname2 -Location $rglocation -Tags @{testtag = "testval"} -ResourceGroupName $rgname -ResourceType $resourceType -PropertyObject @{"key" = "value"} -SkuObject @{ Name = "A0" } -ApiVersion $apiversion -Force
$expected = Find-AzureRmResource -ResourceNameContains test -ResourceGroupNameContains $rgname
Assert-AreEqual 2 @($expected).Count

$expected = Find-AzureRmResource -ResourceGroupNameEquals $rgname -ResourceNameEquals $rname
Assert-NotNull $expected
Assert-AreEqual $actual.ResourceId $expected[0].ResourceId
}

<#
Expand Down
Loading