Skip to content

Commit

Permalink
Add Alias and resource name completer for get cache & get storage tar…
Browse files Browse the repository at this point in the history
…get. (Azure#46)

* Add CacheName as an alias & use ResourceNameCompleter.

* Add StorageTargetName as an alias and use ResourceNameCompleter.

* Alias initialization

* Get storage target support pipeline cache input.
  • Loading branch information
romahamu authored Mar 27, 2020
1 parent 566c12a commit 43d9e0a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
10 changes: 7 additions & 3 deletions src/StorageCache/HPCCache/Commands/GetAzHpcCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Microsoft.Azure.Commands.HPCCache
{
using System.Management.Automation;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.Management.StorageCache.Models;
using Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models;
Expand All @@ -30,26 +31,29 @@ public class GetAzHpcCache : HpcCacheBaseCmdlet
/// Gets or sets resource Group Name.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = false, HelpMessage = "Name of resource group under which you want to list cache(s).")]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets cache Name.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = false, HelpMessage = "Name of specific cache.")]
[Alias(CacheNameAlias)]
[ValidateNotNullOrEmpty]
public string CacheName { get; set; }
[ResourceNameCompleter("Microsoft.StorageCache/caches", nameof(ResourceGroupName))]
public string Name { get; set; }

/// <inheritdoc/>
public override void ExecuteCmdlet()
{
if (!string.IsNullOrEmpty(this.ResourceGroupName))
{
if (!string.IsNullOrEmpty(this.CacheName))
if (!string.IsNullOrEmpty(this.Name))
{
try
{
var singleCache = this.HpcCacheClient.Caches.Get(this.ResourceGroupName, this.CacheName);
var singleCache = this.HpcCacheClient.Caches.Get(this.ResourceGroupName, this.Name);
this.WriteObject(new PSHPCCache(singleCache), true);
}
catch (CloudErrorException ex)
Expand Down
38 changes: 31 additions & 7 deletions src/StorageCache/HPCCache/Commands/GetAzHpcCacheStorageTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace Microsoft.Azure.Commands.HPCCache
{
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Strategies;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.Management.StorageCache.Models;
using Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models;
Expand All @@ -23,44 +25,66 @@ namespace Microsoft.Azure.Commands.HPCCache
/// <summary>
/// Get StorageTargets on Cache.
/// </summary>
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HpcCacheStorageTarget")]
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HpcCacheStorageTarget", DefaultParameterSetName = FieldsParameterSet)]
[OutputType(typeof(PSHpcStorageTarget))]
public class GetAzHpcCacheStorageTarget : HpcCacheBaseCmdlet
{
/// <summary>
/// Gets or sets resource Group Name.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, HelpMessage = "Name of resource group cache is in.")]
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, HelpMessage = "Name of resource group cache is in.", ParameterSetName = FieldsParameterSet)]
[ResourceGroupCompleter]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets cache Name.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, HelpMessage = "Name of cache.")]
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, HelpMessage = "Name of cache.", ParameterSetName = FieldsParameterSet)]
[ValidateNotNullOrEmpty]
[ResourceNameCompleter("Microsoft.StorageCache/caches", nameof(ResourceGroupName))]
public string CacheName { get; set; }

[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The resource id of the Cache", ParameterSetName = ResourceIdParameterSet)]
[ValidateNotNullOrEmpty]
public string CacheId { get; set; }

[Parameter(ParameterSetName = ObjectParameterSet, Mandatory = true, ValueFromPipeline = true, HelpMessage = "The cache object to start.")]
[ValidateNotNullOrEmpty]
public PSHPCCache CacheObject { get; set; }

/// <summary>
/// Gets or sets storage target name.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = false, HelpMessage = "Name of storage target.")]
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = false, HelpMessage = "Name of storage target.", ParameterSetName = FieldsParameterSet)]
[Alias(StoragTargetNameAlias)]
[ValidateNotNullOrEmpty]
public string StorageTargetName { get; set; }
public string Name { get; set; }

/// <inheritdoc/>
public override void ExecuteCmdlet()
{
if (ParameterSetName == ResourceIdParameterSet)
{
var resourceIdentifier = new ResourceIdentifier(this.CacheId);
this.ResourceGroupName = resourceIdentifier.ResourceGroupName;
this.CacheName = resourceIdentifier.ResourceName;
}
else if (ParameterSetName == ObjectParameterSet)
{
this.ResourceGroupName = this.CacheObject.ResourceGroupName;
this.CacheName = this.CacheObject.CacheName;
}

if (!string.IsNullOrEmpty(this.ResourceGroupName))
{
if (!string.IsNullOrEmpty(this.CacheName))
{
if (!string.IsNullOrEmpty(this.StorageTargetName))
if (!string.IsNullOrEmpty(this.Name))
{
try
{
var singleST = this.HpcCacheClient.StorageTargets.Get(this.ResourceGroupName, this.CacheName, this.StorageTargetName);
var singleST = this.HpcCacheClient.StorageTargets.Get(this.ResourceGroupName, this.CacheName, this.Name);
this.WriteObject(new PSHpcStorageTarget(singleST), true);
}
catch (CloudErrorException ex)
Expand Down
3 changes: 3 additions & 0 deletions src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ namespace Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models
/// </summary>
public abstract class HpcCacheBaseCmdlet : AzureRMCmdlet
{

private HpcCacheManagementClientWrapper hpcCacheClientWrapper;
protected const string CacheNameAlias = "CacheName";
protected const string StoragTargetNameAlias = "StorageTargetName";

protected const string ResourceIdParameterSet = "ByResourceIdParameterSet";
protected const string ObjectParameterSet = "ByObjectParameterSet";
Expand Down

0 comments on commit 43d9e0a

Please sign in to comment.