Skip to content

Add location filter to Get-AzureRMResourceProvider and add location output #976

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 1 commit into from
Sep 29, 2015
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 @@ -91,7 +91,7 @@ public void GetsResourceProviderTests()
{
new ProviderResourceType
{
Locations = new[] {"West US", "East US"},
Locations = new[] {"West US", "East US", "South US"},
Name = "TestResource2"
}
}
Expand Down Expand Up @@ -197,6 +197,52 @@ public void GetsResourceProviderTests()
this.cmdlet.ExecuteCmdlet();

this.VerifyGetCallPatternAndReset();

// 4. List only registered providers with location
this.cmdlet.Location = "South US";
this.cmdlet.ListAvailable = false;
this.cmdlet.ProviderNamespace = null;

this.commandRuntimeMock
.Setup(m => m.WriteObject(It.IsAny<object>()))
.Callback((object obj) =>
{
Assert.IsType<PSResourceProvider[]>(obj);

var providers = (PSResourceProvider[])obj;
Assert.Equal(0, providers.Length);
});

this.cmdlet.ParameterSetOverride = GetAzureProviderCmdlet.ListAvailableParameterSet;

this.cmdlet.ExecuteCmdlet();

this.VerifyListCallPatternAndReset();

// 5. List all providers
this.cmdlet.ListAvailable = true;
this.cmdlet.Location = "South US";
this.cmdlet.ProviderNamespace = null;

this.commandRuntimeMock
.Setup(m => m.WriteObject(It.IsAny<object>()))
.Callback((object obj) =>
{
var providers = (PSResourceProvider[])obj;
Assert.Equal(0, providers.Length);

var provider = providers.Single();
Assert.Equal(UnregisteredProviderNamespace, provider.ProviderNamespace);

Assert.Equal(1, provider.ResourceTypes.Length);

var resourceType = provider.ResourceTypes.Single();
Assert.Equal(ResourceTypeName, resourceType.ResourceTypeName);
});

this.cmdlet.ExecuteCmdlet();

this.VerifyListCallPatternAndReset();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

namespace Microsoft.Azure.Commands.Resources.Models
{
using System;
using System.Linq;

/// <summary>
/// Definition of a resource provider and its registration state
/// </summary>
Expand All @@ -33,5 +36,19 @@ public class PSResourceProvider
/// Gets or sets the resource types belonging to this provider.
/// </summary>
public PSResourceProviderResourceType[] ResourceTypes { get; set; }

/// <summary>
/// Gets the locations for the provider.
/// </summary>
public string[] Locations
{
get
{
return this.ResourceTypes
.SelectMany(type => type.Locations)
.Distinct(StringComparer.InvariantCultureIgnoreCase)
.ToArray();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,26 @@ internal List<PSPermission> GetResourcePermissions(ResourceIdentifier identity)
return null;
}

public virtual PSResourceProvider[] ListPSResourceProviders(string providerName = null)
public virtual PSResourceProvider[] ListPSResourceProviders(string providerName = null, bool listAvailable = false, string location = null)
{
return this.ListResourceProviders(providerName: providerName, listAvailable: false)
.Select(provider => provider.ToPSResourceProvider())
.ToArray();
}
var providers = this.ListResourceProviders(providerName: providerName, listAvailable: listAvailable);

public virtual PSResourceProvider[] ListPSResourceProviders(bool listAvailable)
{
return this.ListResourceProviders(providerName: null, listAvailable: listAvailable)
if (string.IsNullOrEmpty(location))
{
return providers
.Select(provider => provider.ToPSResourceProvider())
.ToArray();
}

foreach (var provider in providers)
{
provider.ResourceTypes = provider.ResourceTypes
.Where(type => !type.Locations.Any() || this.ContainsNormalizedLocation(type.Locations.ToArray(), location))
.ToList();
}

return providers
.Where(provider => provider.ResourceTypes.Any())
.Select(provider => provider.ToPSResourceProvider())
.ToArray();
}
Expand Down Expand Up @@ -439,6 +449,16 @@ public virtual List<Provider> ListResourceProviders(string providerName = null,
}
}

private bool ContainsNormalizedLocation(string[] locations, string location)
{
return locations.Any(existingLocation => this.NormalizeLetterOrDigitToUpperInvariant(existingLocation).Equals(this.NormalizeLetterOrDigitToUpperInvariant(location)));
}

private string NormalizeLetterOrDigitToUpperInvariant(string value)
{
return value != null ? new string(value.Where(c => char.IsLetterOrDigit(c)).ToArray()).ToUpperInvariant() : null;
}

private bool IsProviderRegistered(Provider provider)
{
return string.Equals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Microsoft.Azure.Commands.Providers
{
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Resources.Models;

Expand All @@ -41,6 +42,13 @@ public class GetAzureProviderCmdlet : ResourcesBaseCmdlet
[ValidateNotNullOrEmpty]
public string ProviderNamespace { get; set; }

/// <summary>
/// Gets or sets the provider namespace
/// </summary>
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The location to look for provider namespace.")]
[ValidateNotNullOrEmpty]
public string Location { get; set; }

/// <summary>
/// Gets or sets a value indicating if unregistered providers should be included in the listing
/// </summary>
Expand All @@ -52,20 +60,34 @@ public class GetAzureProviderCmdlet : ResourcesBaseCmdlet
/// </summary>
protected override void ProcessRecord()
{
var parameterSetName = this.DetermineParameterSetName();
var providers = this.ResourcesClient.ListPSResourceProviders(providerName: this.ProviderNamespace, listAvailable: this.ListAvailable, location: this.Location);

switch (parameterSetName)
if (!string.IsNullOrEmpty(this.ProviderNamespace))
{
case GetAzureProviderCmdlet.IndividualProviderParameterSet:
this.WriteObject(this.ResourcesClient.ListPSResourceProviders(providerName: this.ProviderNamespace), enumerateCollection: true);
break;

case GetAzureProviderCmdlet.ListAvailableParameterSet:
this.WriteObject(this.ResourcesClient.ListPSResourceProviders(listAvailable: this.ListAvailable), enumerateCollection: true);
break;
var expandedProviders = providers
.SelectMany(provider =>
provider.ResourceTypes
.Select(type =>
new PSResourceProvider
{
ProviderNamespace = provider.ProviderNamespace,
RegistrationState = provider.RegistrationState,
ResourceTypes = new[]
{
new PSResourceProviderResourceType
{
ResourceTypeName = type.ResourceTypeName,
Locations = type.Locations,
ApiVersions = type.ApiVersions,
}
}
}));

default:
throw new ApplicationException(string.Format("Unknown parameter set encountered: '{0}'", this.ParameterSetName));
this.WriteObject(expandedProviders, enumerateCollection: true);
}
else
{
this.WriteObject(providers, enumerateCollection: true);
}
}
}
Expand Down