From 43d9e0a26aa9063f1757e276175d3628de298cfd Mon Sep 17 00:00:00 2001 From: Rohit Mahamuni <44378577+romahamu@users.noreply.github.com> Date: Fri, 27 Mar 2020 15:04:51 -0400 Subject: [PATCH] Add Alias and resource name completer for get cache & get storage target. (#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. --- .../HPCCache/Commands/GetAzHpcCache.cs | 10 +++-- .../Commands/GetAzHpcCacheStorageTarget.cs | 38 +++++++++++++++---- .../HPCCache/Models/HpcCacheBaseCmdlet.cs | 3 ++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/StorageCache/HPCCache/Commands/GetAzHpcCache.cs b/src/StorageCache/HPCCache/Commands/GetAzHpcCache.cs index 0e5aa546528b..9ac506fc5e59 100644 --- a/src/StorageCache/HPCCache/Commands/GetAzHpcCache.cs +++ b/src/StorageCache/HPCCache/Commands/GetAzHpcCache.cs @@ -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; @@ -30,6 +31,7 @@ public class GetAzHpcCache : HpcCacheBaseCmdlet /// Gets or sets resource Group Name. /// [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; } @@ -37,19 +39,21 @@ public class GetAzHpcCache : HpcCacheBaseCmdlet /// Gets or sets cache Name. /// [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; } /// 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) diff --git a/src/StorageCache/HPCCache/Commands/GetAzHpcCacheStorageTarget.cs b/src/StorageCache/HPCCache/Commands/GetAzHpcCacheStorageTarget.cs index 7f1d80e1bcb3..14cee5965adf 100644 --- a/src/StorageCache/HPCCache/Commands/GetAzHpcCacheStorageTarget.cs +++ b/src/StorageCache/HPCCache/Commands/GetAzHpcCacheStorageTarget.cs @@ -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; @@ -23,14 +25,14 @@ namespace Microsoft.Azure.Commands.HPCCache /// /// Get StorageTargets on Cache. /// - [Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HpcCacheStorageTarget")] + [Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HpcCacheStorageTarget", DefaultParameterSetName = FieldsParameterSet)] [OutputType(typeof(PSHpcStorageTarget))] public class GetAzHpcCacheStorageTarget : HpcCacheBaseCmdlet { /// /// Gets or sets resource Group Name. /// - [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; } @@ -38,29 +40,51 @@ public class GetAzHpcCacheStorageTarget : HpcCacheBaseCmdlet /// /// Gets or sets cache Name. /// - [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; } + /// /// Gets or sets storage target name. /// - [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; } /// 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) diff --git a/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs b/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs index bc92a23e6f9a..90d72ebc3d2a 100644 --- a/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs +++ b/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs @@ -25,7 +25,10 @@ namespace Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models /// 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";