Skip to content

Commit

Permalink
Add support for storage target create. (Azure#17)
Browse files Browse the repository at this point in the history
* Add PS wrapper classes required for storage targets.

* Add help for create storage target.

* Add new storage target cmdlet.

* Update resource designer for storage target.
  • Loading branch information
romahamu authored Feb 27, 2020
1 parent 5656b82 commit fcfe1ed
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/StorageCache/HPCCache/Az.HPCCache.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ CmdletsToExport = 'Get-AzHpcCacheSku' , 'Get-AzHpcCacheUsageModels',
'Remove-AzHpcCache', 'Update-AzHpcCache',
'Start-AzHpcCache', 'Flush-AzHpcCache',
'Stop-AzHpcCache', 'Upgrade-AzHpcCache',
'Remove-AzHpcCacheStorageTarget'
'Remove-AzHpcCacheStorageTarget', 'New-AzHpcCacheStorageTarget'

# Variables to export from this module
# VariablesToExport = @()
Expand Down
193 changes: 193 additions & 0 deletions src/StorageCache/HPCCache/Commands/NewAzHpcStorageTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
namespace Microsoft.Azure.Commands.HPCCache
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Net;
using Microsoft.Azure.Commands.Common.Strategies;
using Microsoft.Azure.Commands.HPCCache.Properties;
using Microsoft.Azure.Management.StorageCache;
using Microsoft.Azure.Management.StorageCache.Models;
using Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models;
using Microsoft.Rest.Azure;
using Microsoft.WindowsAzure.Commands.Utilities.Common;

/// <summary>
/// NewAzHpcStorageTarget.
/// </summary>
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "HpcCacheStorageTarget")]
[OutputType(typeof(PSHpcStorageTarget))]
public class NewAzHpcStorageTarget : HpcCacheBaseCmdlet
{
private const string NfsStorageTargetParameterSet = "NfsParameterSet";
private const string ClfsStorageTargetParameterSet = "ClfsParameterSet";
private StorageTarget storageTarget;

/// <summary>
/// Gets or sets resource Group Name.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, HelpMessage = "Name of resource group under which you want to create cache.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

/// <summary>
/// Gets or sets resource CacheName.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true, Mandatory = true, HelpMessage = "Name of cache.")]
[ValidateNotNullOrEmpty]
public string CacheName { get; set; }

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

/// <summary>
/// Gets or sets CLFS storage target.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = ClfsStorageTargetParameterSet, HelpMessage = "Create CLFS storage target.")]
[ValidateNotNullOrEmpty]
public SwitchParameter CLFS { get; set; }

/// <summary>
/// Gets or sets NFS storage target.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = NfsStorageTargetParameterSet, HelpMessage = "Create NFS storage target.")]
[ValidateNotNullOrEmpty]
public SwitchParameter NFS { get; set; }

/// <summary>
/// Gets or sets CLFS storage target StorageContainerID.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = ClfsStorageTargetParameterSet, HelpMessage = "StorageContainerID.")]
[ValidateNotNullOrEmpty]
public string StorageContainerID { get; set; }

/// <summary>
/// Gets or sets NFS storage target hostname.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = NfsStorageTargetParameterSet, HelpMessage = "NFS host name.")]
[ValidateNotNullOrEmpty]
public string HostName { get; set; }

/// <summary>
/// Gets or sets NFS storage target usage model.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = NfsStorageTargetParameterSet, HelpMessage = "NFS usage model.")]
[ValidateNotNullOrEmpty]
public string UsageModel { get; set; }

/// <summary>
/// Gets or sets junctions.
/// </summary>
[Parameter(Mandatory = false, ParameterSetName = ClfsStorageTargetParameterSet, HelpMessage = "Junction.")]
[Parameter(Mandatory = false, ParameterSetName = NfsStorageTargetParameterSet, HelpMessage = "Junction.")]
[ValidateNotNullOrEmpty]
public Hashtable[] Junctions { get; set; }

/// <summary>
/// Gets or sets AsJob.
/// </summary>
[Parameter(Mandatory = false, HelpMessage = "AsJob.")]
public SwitchParameter AsJob { get; set; }

/// <inheritdoc/>
public override void ExecuteCmdlet()
{
this.storageTarget = this.CLFS.IsPresent ? this.CreateClfsStorageTargetParameters() : this.CreateNfsStorageTargetParameters();
if (this.IsParameterBound(c => c.Junctions))
{
this.storageTarget.Junctions = new List<NamespaceJunction>();
foreach (var junction in this.Junctions)
{
var nameSpaceJunction = HashtableToDictionary<string, string>(junction);
this.storageTarget.Junctions.Add(
new NamespaceJunction(
nameSpaceJunction.GetOrNull("namespacePath"),
nameSpaceJunction.GetOrNull("targetPath"),
nameSpaceJunction.GetOrNull("nfsExport")));
}
}

this.DoesStorageTargetExists();
var results = new List<PSHpcStorageTarget>() { this.CreateStorageTargetModel() };
this.WriteObject(results, enumerateCollection: true);
}

private PSHpcStorageTarget CreateStorageTargetModel()
{
return new PSHpcStorageTarget(
this.HpcCacheClient.StorageTargets.CreateOrUpdate(
this.ResourceGroupName,
this.CacheName,
this.StorageTargetName,
this.storageTarget));
}

private bool DoesStorageTargetExists()
{
try
{
var resource = this.HpcCacheClient.StorageTargets.Get(
this.ResourceGroupName,
this.CacheName,
this.StorageTargetName);

throw new Exception(string.Format(Resources.UpgradeHpcCache, this.StorageTargetName, this.CacheName));
}
catch (CloudErrorException e)
{
if (e.Body.Error.Code == "NotFound")
{
return false;
}

throw;
}
}

/// <summary>
/// Create CLFS storage target parameters.
/// </summary>
/// <returns>CLFS storage target parameters.</returns>
private StorageTarget CreateClfsStorageTargetParameters()
{
ClfsTarget clfsTarget = new ClfsTarget()
{
Target = this.StorageContainerID,
};

StorageTarget storageTargetParameters = new StorageTarget
{
TargetType = "clfs",
Clfs = clfsTarget,
};

return storageTargetParameters;
}

/// <summary>
/// Create CLFS storage target parameters.
/// </summary>
/// <returns>CLFS storage target parameters.</returns>
private StorageTarget CreateNfsStorageTargetParameters()
{
Nfs3Target nfs3Target = new Nfs3Target()
{
Target = this.HostName,
UsageModel = this.UsageModel,
};

StorageTarget storageTargetParameters = new StorageTarget
{
TargetType = "nfs3",
Nfs3 = nfs3Target,
};

return storageTargetParameters;
}
}
}
17 changes: 17 additions & 0 deletions src/StorageCache/HPCCache/Models/HpcCacheBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

namespace Microsoft.Azure.PowerShell.Cmdlets.HPCCache.Models
{
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Management.StorageCache;

Expand Down Expand Up @@ -47,5 +50,19 @@ public IStorageCacheManagementClient HpcCacheClient
this.hpcCacheClientWrapper = new HpcCacheManagementClientWrapper(value);
}
}

/// <summary>
/// HashtableToDictionary.
/// </summary>
/// <typeparam name="TK">Key.</typeparam>
/// <typeparam name="TV">Value.</typeparam>
/// <param name="table">Hashtable.</param>
/// <returns>Dictionary.</returns>
public static Dictionary<TK, TV> HashtableToDictionary<TK, TV>(Hashtable table)
{
return table
.Cast<DictionaryEntry>()
.ToDictionary(kvp => (TK)kvp.Key, kvp => (TV)kvp.Value);
}
}
}
38 changes: 38 additions & 0 deletions src/StorageCache/HPCCache/Models/PSHpcClfsTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// ----------------------------------------------------------------------------------
//
// 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 StorageCacheModels = Microsoft.Azure.Management.StorageCache.Models;

/// <summary>
/// PSHpcClfsTarget.
/// </summary>
public class PSHpcClfsTarget
{
/// <summary>
/// Initializes a new instance of the <see cref="PSHpcClfsTarget"/> class.
/// </summary>
/// <param name="clfsTarget">clfsTarget.</param>
public PSHpcClfsTarget(StorageCacheModels.ClfsTarget clfsTarget)
{
this.Target = clfsTarget.Target;
}

/// <summary>
/// Gets or Sets Clfs Target.
/// </summary>
public string Target { get; set; }
}
}
44 changes: 44 additions & 0 deletions src/StorageCache/HPCCache/Models/PSHpcNfs3Target.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// ----------------------------------------------------------------------------------
//
// 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 StorageCacheModels = Microsoft.Azure.Management.StorageCache.Models;

/// <summary>
/// PSHpcNfs3Target.
/// </summary>
public class PSHpcNfs3Target
{
/// <summary>
/// Initializes a new instance of the <see cref="PSHpcNfs3Target"/> class.
/// </summary>
/// <param name="nfs3Target">nfs3Target.</param>
public PSHpcNfs3Target(StorageCacheModels.Nfs3Target nfs3Target)
{
this.Target = nfs3Target.Target;
this.UsageModel = nfs3Target.UsageModel;
}

/// <summary>
/// Gets or Sets NFS3 Target.
/// </summary>
public string Target { get; set; }

/// <summary>
/// Gets or Sets storageTarget UsageModel.
/// </summary>
public string UsageModel { get; set; }
}
}
Loading

0 comments on commit fcfe1ed

Please sign in to comment.