diff --git a/src/StorageCache/HPCCache/Commands/GetAzSku.cs b/src/StorageCache/HPCCache/Commands/GetAzSku.cs index b9c4d30aca3e..4c91757ba5e9 100644 --- a/src/StorageCache/HPCCache/Commands/GetAzSku.cs +++ b/src/StorageCache/HPCCache/Commands/GetAzSku.cs @@ -11,18 +11,25 @@ // See the License for the specific language governing permissions and // limitations under the License. // ---------------------------------------------------------------------------------- + namespace Microsoft.Azure.Commands.HPCCache { - using System; using System.Management.Automation; + using Microsoft.Azure.Management.StorageCache; + using Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models; + /// + /// Get Azure SKU related to HPC Cache service. + /// [Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Sku")] - public class GetAzSku : HpcCacheCmdletBase + public class GetAzSku : HpcCacheBaseCmdlet { + /// public override void ExecuteCmdlet() { - var skulist = CacheClient.GetSku(); - WriteObject(skulist); + // var skulist = CacheClient.GetSku(); + var skulist = this.HpcCacheClient.Skus.List(); + this.WriteObject(skulist); } } } diff --git a/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs b/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs new file mode 100644 index 000000000000..eddda63f6092 --- /dev/null +++ b/src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs @@ -0,0 +1,51 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models +{ + using Microsoft.Azure.Commands.ResourceManager.Common; + using Microsoft.Azure.Management.StorageCache; + + /// + /// Base Cmdlet class for HPC cache cmdlets. + /// + public abstract class HpcCacheBaseCmdlet : AzureRMCmdlet + { + private HpcCacheManagementClientWrapper hpcCacheClientWrapper; + + /// + /// Gets or Sets HPC Cache client. + /// + public IStorageCacheManagementClient HpcCacheClient + { + get + { + if (this.hpcCacheClientWrapper == null) + { + this.hpcCacheClientWrapper = new HpcCacheManagementClientWrapper(this.DefaultProfile.DefaultContext); + } + + this.hpcCacheClientWrapper.VerboseLogger = this.WriteVerboseWithTimestamp; + this.hpcCacheClientWrapper.ErrorLogger = this.WriteErrorWithTimestamp; + this.hpcCacheClientWrapper.HpcCacheManagementClient.ApiVersion = "2019-11-01"; + return this.hpcCacheClientWrapper.HpcCacheManagementClient; + } + + set + { + this.hpcCacheClientWrapper = new HpcCacheManagementClientWrapper(value); + } + } + } +} diff --git a/src/StorageCache/HPCCache/Models/HpcCacheClient.cs b/src/StorageCache/HPCCache/Models/HpcCacheClient.cs deleted file mode 100644 index 6cd10260fb25..000000000000 --- a/src/StorageCache/HPCCache/Models/HpcCacheClient.cs +++ /dev/null @@ -1,50 +0,0 @@ -namespace Microsoft.Azure.Commands.HPCCache -{ - using System.Net; - using Microsoft.Rest.Azure; - using Microsoft.Azure.Commands.Common.Authentication.Abstractions; - using Microsoft.Azure.Commands.Common.Authentication; - using Microsoft.Azure.Management.StorageCache; - using Microsoft.Azure.Management.Internal.Resources; - using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory; - using Microsoft.Azure.Management.StorageCache.Models; - - /// - /// - /// - - public class HpcCacheClient - { - private ResourceManagementClient _resourceManagementClient; - public StorageCacheManagementClient _client; - - public HpcCacheClient(IAzureContext context) - { - _client = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); - _client.ApiVersion = "2019-11-01"; - _resourceManagementClient = AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager); - } - // public HpcCacheClient() { } - public ResourceSkusResult GetSku() - { - _resourceManagementClient.Providers.Register("Microsoft.StorageCache"); - try - { - var response = _client.Skus.List(); - - return response; - } - catch (CloudException ce) - { - if (ce.Response.StatusCode == HttpStatusCode.NotFound) - { - return null; - } - throw; - } - } - } -} - - - diff --git a/src/StorageCache/HPCCache/Models/HpcCacheCmdletBase.cs b/src/StorageCache/HPCCache/Models/HpcCacheCmdletBase.cs deleted file mode 100644 index 3a15170b2189..000000000000 --- a/src/StorageCache/HPCCache/Models/HpcCacheCmdletBase.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace Microsoft.Azure.Commands.HPCCache - -{ - using ResourceManager.Common; - - /// - /// - public abstract class HpcCacheCmdletBase : AzureRMCmdlet - { - private HpcCacheClient cacheClient; - - public HpcCacheClient CacheClient - { - get - { - if (cacheClient == null) - { - cacheClient = new HpcCacheClient(DefaultProfile.DefaultContext); - } - return cacheClient; - } - - set - { - cacheClient = value; - } - } - } -} \ No newline at end of file diff --git a/src/StorageCache/HPCCache/Models/HpcCacheManagementClient.cs b/src/StorageCache/HPCCache/Models/HpcCacheManagementClient.cs new file mode 100644 index 000000000000..57815d2f5608 --- /dev/null +++ b/src/StorageCache/HPCCache/Models/HpcCacheManagementClient.cs @@ -0,0 +1,95 @@ +// ---------------------------------------------------------------------------------- +// +// Copyright Microsoft Corporation +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// ---------------------------------------------------------------------------------- + +namespace Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models +{ + using System; + using Microsoft.Azure.Commands.Common.Authentication; + using Microsoft.Azure.Commands.Common.Authentication.Abstractions; + using Microsoft.Azure.Management.StorageCache; + + /// + /// Hpc cache management client wrapper. + /// + public partial class HpcCacheManagementClientWrapper + { + /// + /// Initializes a new instance of the class. + /// + /// Azure context. + public HpcCacheManagementClientWrapper(IAzureContext context) + : this(AzureSession.Instance.ClientFactory.CreateArmClient(context, AzureEnvironment.Endpoint.ResourceManager)) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// Resource management client. + public HpcCacheManagementClientWrapper(IStorageCacheManagementClient resourceManagementClient) + { + this.HpcCacheManagementClient = resourceManagementClient; + } + + /// + /// Gets or sets hpc Cache management client. + /// + public IStorageCacheManagementClient HpcCacheManagementClient { get; set; } + + /// + /// Gets or sets verbose logging. + /// + public Action VerboseLogger { get; set; } + + /// + /// Gets or sets error logging. + /// + public Action ErrorLogger { get; set; } + + /// + /// Gets or sets warning Logger. + /// + public Action WarningLogger { get; set; } + + /// + /// Writes verbose. + /// + /// Verbose format. + /// Arguments to write verbose. + private void WriteVerbose(string verboseFormat, params object[] args) + { + this.VerboseLogger?.Invoke(string.Format(verboseFormat, args)); + } + + /// + /// Write warning. + /// + /// Warning format. + /// Arguments to write warning. + private void WriteWarning(string warningFormat, params object[] args) + { + this.WarningLogger?.Invoke(string.Format(warningFormat, args)); + } + + /// + /// Write error. + /// + /// Error format. + /// Arguments to write error. + private void WriteError(string errorFormat, params object[] args) + { + this.ErrorLogger?.Invoke(string.Format(errorFormat, args)); + } + } +} \ No newline at end of file diff --git a/src/StorageCache/HPCCache/Models/PSSku.cs b/src/StorageCache/HPCCache/Models/PSSku.cs deleted file mode 100644 index b86dbe0cfd47..000000000000 --- a/src/StorageCache/HPCCache/Models/PSSku.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Microsoft.Azure.Commands.HPCCache -{ - using Microsoft.Azure.Graph.RBAC.Version1_6.ActiveDirectory; - using Microsoft.Azure.Management.StorageCache.Models; - - internal class PSSku - { - public PSSku() - { - } - } -} \ No newline at end of file