Skip to content

Migrate EncryptionScope and ObjectReplication cmdlets to Track2 SDK #19

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 2 commits into from
Jul 12, 2022
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
41 changes: 40 additions & 1 deletion src/Storage/Storage.Management/File/StorageFileBaseCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using StorageModels = Microsoft.Azure.Management.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down Expand Up @@ -85,6 +84,33 @@ protected struct ShareCreateExpand
internal const string Snapshots = "snapshots";
}

protected struct RootSquashType
{
internal const string NoRootSquash = "NoRootSquash";
internal const string RootSquash = "RootSquash";
internal const string AllSquash = "AllSquash";
}

protected struct ShareAccessTier
{
internal const string TransactionOptimized = "TransactionOptimized";
internal const string Hot = "Hot";
internal const string Cool = "Cool";
internal const string Premium = "Premium";
}

protected struct EncryptionScopeState
{
internal const string Enabled = "Enabled";
internal const string Disabled = "Disabled";
}

protected struct EnabledProtocols
{
internal const string SMB = "SMB";
internal const string NFS = "NFS";
}

public string ConnectStringArray(string[] stringArray, string seperator = ";")
{
if (stringArray == null)
Expand Down Expand Up @@ -123,6 +149,19 @@ public IStorageManagementClient StorageClient
set { storageClientWrapper = new StorageManagementClientWrapper(value); }
}

private Track2StorageManagementClient _track2StorageManagementClient;
public Track2StorageManagementClient StorageClientTrack2
{
get
{
return _track2StorageManagementClient ?? (_track2StorageManagementClient = new Track2StorageManagementClient(
Microsoft.Azure.Commands.Common.Authentication.AzureSession.Instance.ClientFactory,
DefaultContext));
}

set { _track2StorageManagementClient = value; }
}

public string SubscriptionId
{
get
Expand Down
34 changes: 18 additions & 16 deletions src/Storage/Storage.Management/Models/PSEncryptionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,33 @@
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.WindowsAzure.Commands.Common.Attributes;
using Microsoft.WindowsAzure.Commands.Common.Storage;
using Microsoft.WindowsAzure.Commands.Storage.Adapters;
using Microsoft.Azure.Storage;
using System;
using System.Collections.Generic;
using StorageModels = Microsoft.Azure.Management.Storage.Models;
using Track2 = Azure.ResourceManager.Storage;
using Track2Models = Azure.ResourceManager.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage.Models
{
// wrapper of EncryptionScope
public class PSEncryptionScope
{
public PSEncryptionScope(StorageModels.EncryptionScope scope)
public PSEncryptionScope(Track2.EncryptionScopeResource scope)
{
this.ResourceGroupName = ParseResourceGroupFromId(scope.Id);
this.StorageAccountName = ParseStorageAccountNameFromId(scope.Id);
this.Id = scope.Id;
this.Name = scope.Name;
this.Type = scope.Type;
this.LastModifiedTime = scope.LastModifiedTime;
this.CreationTime = scope.CreationTime;
this.Source = scope.Source;
this.State = scope.State;
this.KeyVaultProperties = scope.KeyVaultProperties is null ? null : new PSEncryptionScopeKeyVaultProperties(scope.KeyVaultProperties);
this.RequireInfrastructureEncryption = scope.RequireInfrastructureEncryption;
this.Name = scope.Data.Name;
this.Type = scope.Data.ResourceType;
this.LastModifiedTime = scope.Data.LastModifiedOn;
this.CreationTime = scope.Data.CreationOn;
this.Source = scope.Data.Source.ToString();
this.State = scope.Data.State.ToString();
this.KeyVaultProperties = scope.Data.KeyVaultProperties is null ? null : new PSEncryptionScopeKeyVaultProperties(scope.Data.KeyVaultProperties);
this.RequireInfrastructureEncryption = scope.Data.RequireInfrastructureEncryption;
}

[Ps1Xml(Label = "ResourceGroupName", Target = ViewControl.List, Position = 0)]
Expand All @@ -65,9 +64,9 @@ public PSEncryptionScope(StorageModels.EncryptionScope scope)
public bool? RequireInfrastructureEncryption { get; set; }

[Ps1Xml(Label = "LastModifiedTime", Target = ViewControl.List, Position = 4)]
public DateTime? LastModifiedTime { get; set; }
public DateTimeOffset? LastModifiedTime { get; set; }

public DateTime? CreationTime { get; set; }
public DateTimeOffset? CreationTime { get; set; }

public static string ParseResourceGroupFromId(string idFromServer)
{
Expand Down Expand Up @@ -98,11 +97,14 @@ public static string ParseStorageAccountNameFromId(string idFromServer)
//wrapper of EncryptionScopeKeyVaultProperties
public class PSEncryptionScopeKeyVaultProperties
{
public PSEncryptionScopeKeyVaultProperties(StorageModels.EncryptionScopeKeyVaultProperties keyVaultProperties)
public PSEncryptionScopeKeyVaultProperties(Track2Models.EncryptionScopeKeyVaultProperties keyVaultProperties)
{
if (keyVaultProperties != null)
{
this.keyUri = keyVaultProperties.KeyUri;
if (keyVaultProperties.KeyUri != null)
{
this.keyUri = keyVaultProperties.KeyUri.OriginalString;
}
}
}

Expand Down
109 changes: 44 additions & 65 deletions src/Storage/Storage.Management/Models/PSObjectReplicationPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Management.Storage.Models;
using Microsoft.WindowsAzure.Commands.Common.Attributes;
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using Track2 = Azure.ResourceManager.Storage;
using Track2Models = Azure.ResourceManager.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage.Models
{
Expand All @@ -35,7 +37,7 @@ public class PSObjectReplicationPolicy
[Ps1Xml(Label = "PolicyId", Target = ViewControl.Table, Position = 2)]
public string PolicyId { get; set; }
[Ps1Xml(Label = "EnabledTime", Target = ViewControl.Table, Position = 3)]
public DateTime? EnabledTime { get; }
public DateTimeOffset? EnabledTime { get; }
[Ps1Xml(Label = "SourceAccount", Target = ViewControl.Table, Position = 4)]
public string SourceAccount { get; set; }
[Ps1Xml(Label = "DestinationAccount", Target = ViewControl.Table, Position = 5)]
Expand All @@ -46,43 +48,31 @@ public class PSObjectReplicationPolicy
public PSObjectReplicationPolicy()
{ }

public PSObjectReplicationPolicy(ObjectReplicationPolicy policy, string ResourceGroupName, string StorageAccountName)
public PSObjectReplicationPolicy(Track2.ObjectReplicationPolicyResource policy, string ResourceGroupName, string StorageAccountName)
{
this.ResourceGroupName = ResourceGroupName;
this.StorageAccountName = StorageAccountName;
this.ResourceId = policy.Id;
this.Name = policy.Name;
this.Type = policy.Type;
this.PolicyId = policy.PolicyId;
this.EnabledTime = policy.EnabledTime;
this.SourceAccount = policy.SourceAccount;
this.DestinationAccount = policy.DestinationAccount;
this.Rules = PSObjectReplicationPolicyRule.GetPSObjectReplicationPolicyRules(policy.Rules);
this.Name = policy.Data.Name;
this.Type = policy.Data.ResourceType;
this.PolicyId = policy.Data.PolicyId;
this.EnabledTime = policy.Data.EnabledOn;
this.SourceAccount = policy.Data.SourceAccount;
this.DestinationAccount = policy.Data.DestinationAccount;
this.Rules = PSObjectReplicationPolicyRule.GetPSObjectReplicationPolicyRules(policy.Data.Rules);
}

public ObjectReplicationPolicy ParseObjectReplicationPolicy()
public Track2.ObjectReplicationPolicyData ParseObjectReplicationPolicy()
{
ObjectReplicationPolicy policy = new ObjectReplicationPolicy()
{
SourceAccount = this.SourceAccount,
DestinationAccount = this.DestinationAccount,
Rules = PSObjectReplicationPolicyRule.ParseObjectReplicationPolicyRules(this.Rules)
};
return policy;
}
Track2.ObjectReplicationPolicyData data = new Track2.ObjectReplicationPolicyData();
data.SourceAccount = this.SourceAccount;
data.DestinationAccount = this.DestinationAccount;

public static PSObjectReplicationPolicy[] GetPSObjectReplicationPolicies(IEnumerable<ObjectReplicationPolicy> policies, string ResourceGroupName, string StorageAccountName)
{
if (policies == null)
{
return null;
}
List<PSObjectReplicationPolicy> pspolicies = new List<PSObjectReplicationPolicy>();
foreach (ObjectReplicationPolicy policy in policies)
foreach(PSObjectReplicationPolicyRule rule in this.Rules)
{
pspolicies.Add(new PSObjectReplicationPolicy(policy, ResourceGroupName, StorageAccountName));
data.Rules.Add(rule.ParseObjectReplicationPolicyRule());
}
return pspolicies.ToArray();
return data;
}
}

Expand All @@ -104,51 +94,35 @@ public PSObjectReplicationPolicyRule()
{
}

public PSObjectReplicationPolicyRule(ObjectReplicationPolicyRule rule)
public PSObjectReplicationPolicyRule(Track2Models.ObjectReplicationPolicyRule rule)
{
this.RuleId = rule.RuleId;
this.SourceContainer = rule.SourceContainer;
this.DestinationContainer = rule.DestinationContainer;
this.Filters = rule.Filters is null ? null : new PSObjectReplicationPolicyFilter(rule.Filters);
}

public ObjectReplicationPolicyRule ParseObjectReplicationPolicyRule()
public Track2Models.ObjectReplicationPolicyRule ParseObjectReplicationPolicyRule()
{
ObjectReplicationPolicyRule rule = new ObjectReplicationPolicyRule();
Track2Models.ObjectReplicationPolicyRule rule = new Track2Models.ObjectReplicationPolicyRule(this.SourceContainer, this.DestinationContainer);
rule.RuleId = this.RuleId;
rule.SourceContainer = this.SourceContainer;
rule.DestinationContainer = this.DestinationContainer;
rule.Filters = this.Filters is null ? null : this.Filters.ParseObjectReplicationPolicyFilter();
rule.Filters = this.Filters?.ParseObjectReplicationPolicyFilter();
return rule;
}

public static PSObjectReplicationPolicyRule[] GetPSObjectReplicationPolicyRules(IList<ObjectReplicationPolicyRule> rules)
public static PSObjectReplicationPolicyRule[] GetPSObjectReplicationPolicyRules(IList<Track2Models.ObjectReplicationPolicyRule> rules)
{
if (rules == null)
{
return null;
}
List<PSObjectReplicationPolicyRule> psrules = new List<PSObjectReplicationPolicyRule>();
foreach (ObjectReplicationPolicyRule rule in rules)
foreach (Track2Models.ObjectReplicationPolicyRule rule in rules)
{
psrules.Add(new PSObjectReplicationPolicyRule(rule));
}
return psrules.ToArray();
}

public static List<ObjectReplicationPolicyRule> ParseObjectReplicationPolicyRules(PSObjectReplicationPolicyRule[] psrules)
{
if (psrules == null)
{
return null;
}
List<ObjectReplicationPolicyRule> rules = new List<ObjectReplicationPolicyRule>();
foreach (PSObjectReplicationPolicyRule psrule in psrules)
{
rules.Add(psrule.ParseObjectReplicationPolicyRule());
}
return rules;
}
}

/// <summary>
Expand All @@ -157,39 +131,44 @@ public static List<ObjectReplicationPolicyRule> ParseObjectReplicationPolicyRule
public class PSObjectReplicationPolicyFilter
{
public string[] PrefixMatch { get; set; }
public DateTime? MinCreationTime;
public DateTimeOffset? MinCreationTime;

public PSObjectReplicationPolicyFilter()
{
}

public PSObjectReplicationPolicyFilter(ObjectReplicationPolicyFilter filter)
public PSObjectReplicationPolicyFilter(Track2Models.ObjectReplicationPolicyFilter filter)
{
if (filter != null)
{
this.PrefixMatch = filter.PrefixMatch is null ? null : new List<string>(filter.PrefixMatch).ToArray();
if (string.IsNullOrEmpty(filter.MinCreationTime))
{
this.MinCreationTime = null;
}
else
if (filter.MinCreationTime != null)
{
if (filter.MinCreationTime.ToUpper()[filter.MinCreationTime.Length - 1] != 'Z')
{
filter.MinCreationTime = filter.MinCreationTime + "Z";
filter.MinCreationTime += "Z";
}
this.MinCreationTime = Convert.ToDateTime(filter.MinCreationTime);
}
}
}
public ObjectReplicationPolicyFilter ParseObjectReplicationPolicyFilter()

public Track2Models.ObjectReplicationPolicyFilter ParseObjectReplicationPolicyFilter()
{
return new ObjectReplicationPolicyFilter()
Track2Models.ObjectReplicationPolicyFilter filter = new Track2Models.ObjectReplicationPolicyFilter();

if (this.PrefixMatch != null)
{
PrefixMatch = this.PrefixMatch is null ? null : new List<string>(this.PrefixMatch),
//must be in format: 2020-02-19T16:05:00Z
MinCreationTime = this.MinCreationTime is null ? null : this.MinCreationTime.Value.ToUniversalTime().ToString("s") + "Z"
};
foreach (string itm in this.PrefixMatch)
{
filter.PrefixMatch.Add(itm);
}
}
if (this.MinCreationTime != null)
{
filter.MinCreationTime = this.MinCreationTime.Value.ToUniversalTime().ToString("s") + "Z";
}
return filter;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Azure;
using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using System.Collections.Generic;
using System.Management.Automation;
using Track2 = Azure.ResourceManager.Storage;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down Expand Up @@ -82,19 +82,26 @@ public override void ExecuteCmdlet()
}
if (!string.IsNullOrEmpty(PolicyId))
{
ObjectReplicationPolicy policy = this.StorageClient.ObjectReplicationPolicies.Get(
this.ResourceGroupName,
this.StorageAccountName,
PolicyId);
Track2.ObjectReplicationPolicyResource policy = this.StorageClientTrack2
.GetObjectReplicationPolicyResource(this.ResourceGroupName, this.StorageAccountName, this.PolicyId)
.Get();

WriteObject(new PSObjectReplicationPolicy(policy, this.ResourceGroupName, this.StorageAccountName));
}
else
{
IEnumerable<ObjectReplicationPolicy> policies = this.StorageClient.ObjectReplicationPolicies.List(
this.ResourceGroupName,
this.StorageAccountName);
WriteObject(PSObjectReplicationPolicy.GetPSObjectReplicationPolicies(policies, this.ResourceGroupName, this.StorageAccountName), true);
Pageable<Track2.ObjectReplicationPolicyResource> policies = this.StorageClientTrack2
.GetStorageAccount(this.ResourceGroupName, this.StorageAccountName)
.GetObjectReplicationPolicies()
.GetAll();

List<PSObjectReplicationPolicy> pspolicies = new List<PSObjectReplicationPolicy>();

foreach (Track2.ObjectReplicationPolicyResource policy in policies)
{
pspolicies.Add(new PSObjectReplicationPolicy(policy, this.ResourceGroupName, this.StorageAccountName));
}
WriteObject(pspolicies.ToArray(), true);
}
}
}
Expand Down
Loading