Skip to content

Migrate BlobInventoryPolicy related cmdelts and Invoke-AzStorageAccountFailover to Track2 SDK #17

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 3 commits into from
Jun 29, 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
149 changes: 83 additions & 66 deletions src/Storage/Storage.Management/Models/PSBlobInventoryPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Track2 = Azure.ResourceManager.Storage;
using Track2Models = Azure.ResourceManager.Storage.Models;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.WindowsAzure.Commands.Common.Attributes;
using System;
Expand All @@ -27,23 +29,22 @@ public class PSBlobInventoryPolicy
public PSBlobInventoryPolicy()
{ }

public PSBlobInventoryPolicy(BlobInventoryPolicy policy, string ResourceGroupName, string StorageAccountName)
public PSBlobInventoryPolicy(Track2.BlobInventoryPolicyResource policy, string resourceGroupName, string storageAccountName)
{
this.ResourceGroupName = ResourceGroupName;
this.StorageAccountName = StorageAccountName;
this.ResourceGroupName = resourceGroupName;
this.StorageAccountName = storageAccountName;
this.Id = policy.Id;
this.Name = policy.Name;
this.Type = policy.Type;
this.LastModifiedTime = policy.LastModifiedTime;
this.SystemData = policy.SystemData is null ? null : new PSSystemData(policy.SystemData);

this.Enabled = policy.Policy.Enabled;
this.Destination = policy.Policy.Destination;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might need to add a comments to add the destination which is on a later API version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


if (policy.Policy.Rules != null)
this.Name = policy.Data.Name;
this.Type = policy.Data.ResourceType;
this.LastModifiedTime = policy.Data.LastModifiedOn;
this.SystemData = policy.Data.SystemData is null ? null : new PSSystemData(policy.Data.SystemData);
this.Enabled = policy.Data.Policy.Enabled;
// TODO: Destination is currently not supported yet. Will add later.

if (policy.Data.Policy.Rules != null)
{
List<PSBlobInventoryPolicyRule> psRules = new List<PSBlobInventoryPolicyRule>();
foreach (BlobInventoryPolicyRule rule in policy.Policy.Rules)
foreach (Track2Models.BlobInventoryPolicyRule rule in policy.Data.Policy.Rules)
{
psRules.Add(new PSBlobInventoryPolicyRule(rule));
}
Expand All @@ -55,32 +56,29 @@ public PSBlobInventoryPolicy(BlobInventoryPolicy policy, string ResourceGroupNam
}
}

public BlobInventoryPolicy ParseBlobInventoryPolicy()

public Track2.BlobInventoryPolicyData ParseBlobInventoryPolicy()
{
List<BlobInventoryPolicyRule> invRules = ParseBlobInventoryPolicyRules(this.Rules);
List<Track2Models.BlobInventoryPolicyRule> invRules = ParseBlobInventoryPolicyRules(this.Rules);

BlobInventoryPolicySchema policySchema = new BlobInventoryPolicySchema()
Track2Models.BlobInventoryPolicySchema policySchema = new Track2Models.BlobInventoryPolicySchema(
this.Enabled,
Track2Models.InventoryRuleType.Inventory,
invRules);

Track2.BlobInventoryPolicyData data = new Track2.BlobInventoryPolicyData
{
Enabled = this.Enabled,
//Destination = this.Destination,
Rules = invRules
Policy = policySchema,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks still still missing ID, Name, LastModified...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we cannot assign those values, including id, name, and lastModifiedOn, as the constructor is internal. I'll record this issue.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might not be an issue, since this function is only used in set-AzstorageStorageBlobInventoryPolicy, to parse the input the policy (PSH object) to the policy (SDK object) to set.

And I can understand SDK not allow to set them, since they should only be read from server, but not set by customer.

In a word, if PSH code works with it, I believe this will not be an issue.

};
return new BlobInventoryPolicy(
policySchema,
this.Id,
this.Name,
this.Type,
this.LastModifiedTime,
this.SystemData is null ? null : this.SystemData.ParseSystemData()
);
return data;
}

public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlobInventoryPolicyRule[] rules)
public static List<Track2Models.BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlobInventoryPolicyRule[] rules)
{
List<BlobInventoryPolicyRule> invRules = null;
List<Track2Models.BlobInventoryPolicyRule> invRules = null;
if (rules != null)
{
invRules = new List<BlobInventoryPolicyRule>();
invRules = new List<Track2Models.BlobInventoryPolicyRule>();
foreach (PSBlobInventoryPolicyRule rule in rules)
{
invRules.Add(rule.ParseBlobInventoryPolicyRule());
Expand All @@ -99,7 +97,7 @@ public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlob
[Ps1Xml(Label = "Type", Target = ViewControl.List, Position = 2)]
public string Type { get; set; }
[Ps1Xml(Label = "LastModifiedTime", Target = ViewControl.List, Position = 4)]
public DateTime? LastModifiedTime { get; set; }
public DateTimeOffset? LastModifiedTime { get; set; }

//public PSBlobInventoryPolicySchema Policy { get; set; }

Expand All @@ -108,7 +106,6 @@ public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlob
[Ps1Xml(Label = "Rules", Target = ViewControl.List, Position = 7)]
public PSBlobInventoryPolicyRule[] Rules { get; set; }
public PSSystemData SystemData { get; set; }
public string Destination { get; private set; }
}

/// <summary>
Expand All @@ -117,23 +114,25 @@ public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlob
public class PSBlobInventoryPolicyRule
{
public PSBlobInventoryPolicyRule() { }
public PSBlobInventoryPolicyRule(BlobInventoryPolicyRule rule)
public PSBlobInventoryPolicyRule(Track2Models.BlobInventoryPolicyRule rule)
{
this.Enabled = rule.Enabled;
this.Name = rule.Name;
this.Destination = rule.Destination;
this.Definition = rule.Definition is null ? null : new PSBlobInventoryPolicyDefinition(rule.Definition);
}

public BlobInventoryPolicyRule ParseBlobInventoryPolicyRule()
public Track2Models.BlobInventoryPolicyRule ParseBlobInventoryPolicyRule()
{
return new BlobInventoryPolicyRule()
{
Enabled = this.Enabled,
Name = this.Name,
Destination = this.Destination,
Definition = this.Definition is null ? null : this.Definition.parseBlobInventoryPolicyDefinition()
};
Track2Models.BlobInventoryPolicyDefinition policyDefinition = this.Definition?.parseBlobInventoryPolicyDefinition();

Track2Models.BlobInventoryPolicyRule policyRule = new Track2Models.BlobInventoryPolicyRule(
this.Enabled,
this.Name,
this.Destination,
policyDefinition);

return policyRule;
}

public bool Enabled { get; set; }
Expand All @@ -152,20 +151,29 @@ public class PSBlobInventoryPolicyDefinition
{
public PSBlobInventoryPolicyDefinition() { }

public PSBlobInventoryPolicyDefinition(BlobInventoryPolicyDefinition Definition)
public PSBlobInventoryPolicyDefinition(Track2Models.BlobInventoryPolicyDefinition Definition)
{
this.Filters = Definition.Filters is null ? null : new PSBlobInventoryPolicyFilter(Definition.Filters);
this.Format = Definition.Format;
this.Schedule = Definition.Schedule;
this.ObjectType = Definition.ObjectType;
this.Format = Definition.Format.ToString();
this.Schedule = Definition.Schedule.ToString();
this.ObjectType = Definition.ObjectType.ToString();
this.SchemaFields = Definition.SchemaFields is null ? null : ((List<string>)Definition.SchemaFields).ToArray();
}

public BlobInventoryPolicyDefinition parseBlobInventoryPolicyDefinition()
public Track2Models.BlobInventoryPolicyDefinition parseBlobInventoryPolicyDefinition()
{
return new BlobInventoryPolicyDefinition(this.Format, this.Schedule, this.ObjectType,
this.SchemaFields is null? null: new List<string>(this.SchemaFields),
this.Filters is null? null : this.Filters.ParseBlobInventoryPolicyFilter());
Track2Models.BlobInventoryPolicyDefinition policyDefinition = new Track2Models.BlobInventoryPolicyDefinition(
new Track2Models.Format(this.Format),
new Track2Models.Schedule(this.Schedule),
new Track2Models.ObjectType(this.ObjectType),
this.SchemaFields is null ? null : new List<string>(this.SchemaFields)
);

if (this.Filters != null)
{
policyDefinition.Filters = this.Filters.ParseBlobInventoryPolicyFilter();
}
return policyDefinition;
}

public PSBlobInventoryPolicyFilter Filters { get; set; }
Expand Down Expand Up @@ -213,23 +221,37 @@ public class PSBlobInventoryPolicyFilter
{
public PSBlobInventoryPolicyFilter() { }

public PSBlobInventoryPolicyFilter(BlobInventoryPolicyFilter filters)
public PSBlobInventoryPolicyFilter(Track2Models.BlobInventoryPolicyFilter filters)
{
this.PrefixMatch = PSManagementPolicyRuleFilter.StringListToArray(filters.PrefixMatch);
this.BlobTypes = PSManagementPolicyRuleFilter.StringListToArray(filters.BlobTypes);
this.IncludeBlobVersions = filters.IncludeBlobVersions;
this.IncludeSnapshots = filters.IncludeSnapshots;
}

public BlobInventoryPolicyFilter ParseBlobInventoryPolicyFilter()
public Track2Models.BlobInventoryPolicyFilter ParseBlobInventoryPolicyFilter()
{
return new BlobInventoryPolicyFilter()
Track2Models.BlobInventoryPolicyFilter policyFilter = new Track2Models.BlobInventoryPolicyFilter()
{
PrefixMatch = PSManagementPolicyRuleFilter.StringArrayToList(this.PrefixMatch),
BlobTypes = PSManagementPolicyRuleFilter.StringArrayToList(this.BlobTypes),
IncludeSnapshots = this.IncludeSnapshots,
IncludeBlobVersions = this.IncludeBlobVersions
};

if (this.PrefixMatch != null)
{
foreach (string prefixMatch in this.PrefixMatch)
{
policyFilter.PrefixMatch.Add(prefixMatch);
}
}
if (this.BlobTypes != null)
{
foreach (string blobType in this.BlobTypes)
{
policyFilter.BlobTypes.Add(blobType);
}
}
return policyFilter;
}

public string[] PrefixMatch { get; set; }
Expand All @@ -245,26 +267,21 @@ public class PSSystemData
{
public PSSystemData() { }

public PSSystemData(SystemData SystemData)
public PSSystemData(global::Azure.ResourceManager.Models.SystemData SystemData)
{
this.CreatedBy = SystemData.CreatedBy;
this.CreatedByType = SystemData.CreatedByType;
this.CreatedAt = SystemData.CreatedAt;
this.CreatedByType = SystemData.CreatedByType.ToString();
this.CreatedAt = SystemData.CreatedOn;
this.LastModifiedBy = SystemData.LastModifiedBy;
this.LastModifiedByType = SystemData.LastModifiedByType;
this.LastModifiedAt = SystemData.LastModifiedAt;
}

public SystemData ParseSystemData()
{
return new SystemData(this.CreatedBy, this.CreatedByType, this.CreatedAt, this.LastModifiedBy, this.LastModifiedByType, this.LastModifiedAt);
this.LastModifiedByType = SystemData.LastModifiedByType.ToString();
this.LastModifiedAt = SystemData.LastModifiedOn;
}

public string CreatedBy { get; set; }
public string CreatedByType { get; set; }
public DateTime? CreatedAt { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public string LastModifiedBy { get; set; }
public string LastModifiedByType { get; set; }
public DateTime? LastModifiedAt { get; set; }
public DateTimeOffset? LastModifiedAt { get; set; }
}
}
1 change: 0 additions & 1 deletion src/Storage/Storage.Management/Storage.Management.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
<Compile Remove="File\UpdateAzAzStorageFileServiceProperty.cs" />
<Compile Remove="Models\PSFileServiceProperties.cs" />
<Compile Remove="StorageAccount\InvokeAzureStorageAccountHierarchicalNamespaceUpgrade.cs" />
<Compile Remove="StorageAccount\InvokeAzureStorageAccountFailover.cs" />
</ItemGroup>
<ItemGroup>
<!-- <PackageReference Include="Azure.Core" Version="1.24.0" /> -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using System.Management.Automation;
using Track2 = Azure.ResourceManager.Storage;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand All @@ -40,6 +39,11 @@ public class GetAzureStorageBlobInventoryPolicyCommand : StorageAccountBaseCmdle
/// </summary>
private const string AccountResourceIdParameterSet = "AccountResourceId";

/// <summary>
/// Default policy name
/// </summary>
private const string DefaultPolicyName = "default";

[Parameter(
Position = 0,
Mandatory = true,
Expand Down Expand Up @@ -95,9 +99,8 @@ public override void ExecuteCmdlet()
break;
}

BlobInventoryPolicy policy = this.StorageClient.BlobInventoryPolicies.Get(
this.ResourceGroupName,
this.StorageAccountName);
Track2.BlobInventoryPolicyResource policy =
this.StorageClientTrack2.GetBlobInventoryPolicyResource(this.ResourceGroupName, this.StorageAccountName, DefaultPolicyName).Get();

WriteObject(new PSBlobInventoryPolicy(policy, this.ResourceGroupName, this.StorageAccountName), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

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

namespace Microsoft.Azure.Commands.Management.Storage
Expand Down Expand Up @@ -89,7 +86,6 @@ public override void ExecuteCmdlet()
shouldContinuePrompt.AppendLine(" 2. After the failover, your storage account type will be converted to locally redundant storage (LRS). You can convert your account to use geo-redundant storage (GRS).");
shouldContinuePrompt.AppendLine(" 3. Once you re-enable GRS for your storage account, Microsoft will replicate data to your new secondary region. Replication time is dependent on the amount of data to replicate. Please note that there are bandwidth charges for the bootstrap. Please refer to doc: https://azure.microsoft.com/en-us/pricing/details/bandwidth/");


if (this.force || ShouldContinue(shouldContinuePrompt.ToString(), ""))
{
if (ParameterSetName == AccountObjectParameterSet)
Expand All @@ -98,11 +94,8 @@ public override void ExecuteCmdlet()
this.Name = InputObject.StorageAccountName;
}

this.StorageClient.StorageAccounts.Failover(
this.ResourceGroupName,
this.Name);

var storageAccount = this.StorageClient.StorageAccounts.GetProperties(this.ResourceGroupName, this.Name);
this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.Name).Failover(global::Azure.WaitUntil.Completed);
var storageAccount = this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.Name).Get();

WriteStorageAccount(storageAccount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@

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 Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
using System.Collections.Generic;
using System.Management.Automation;
using System.Reflection;
using StorageModels = Microsoft.Azure.Management.Storage.Models;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
using Microsoft.Azure.Commands.Management.Storage.Models;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
using Microsoft.Azure.Management.Storage;
using Microsoft.Azure.Management.Storage.Models;
using System.Management.Automation;
using Azure;

namespace Microsoft.Azure.Commands.Management.Storage
{
Expand Down Expand Up @@ -115,10 +114,7 @@ public override void ExecuteCmdlet()
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
break;
}

this.StorageClient.BlobInventoryPolicies.Delete(
this.ResourceGroupName,
this.StorageAccountName);
this.StorageClientTrack2.GetBlobInventoryPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default").Delete(WaitUntil.Completed);

if (PassThru.IsPresent)
{
Expand Down
Loading