Skip to content

Commit cc4a708

Browse files
authored
Merge pull request #17 from yifanz0/yifan/track2.1
Migrate BlobInventoryPolicy related cmdelts and Invoke-AzStorageAccountFailover to Track2 SDK
2 parents 5ef71ae + 7e8fe7b commit cc4a708

7 files changed

+120
-112
lines changed

src/Storage/Storage.Management/Models/PSBlobInventoryPolicy.cs

Lines changed: 83 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Track2 = Azure.ResourceManager.Storage;
16+
using Track2Models = Azure.ResourceManager.Storage.Models;
1517
using Microsoft.Azure.Management.Storage.Models;
1618
using Microsoft.WindowsAzure.Commands.Common.Attributes;
1719
using System;
@@ -27,23 +29,22 @@ public class PSBlobInventoryPolicy
2729
public PSBlobInventoryPolicy()
2830
{ }
2931

30-
public PSBlobInventoryPolicy(BlobInventoryPolicy policy, string ResourceGroupName, string StorageAccountName)
32+
public PSBlobInventoryPolicy(Track2.BlobInventoryPolicyResource policy, string resourceGroupName, string storageAccountName)
3133
{
32-
this.ResourceGroupName = ResourceGroupName;
33-
this.StorageAccountName = StorageAccountName;
34+
this.ResourceGroupName = resourceGroupName;
35+
this.StorageAccountName = storageAccountName;
3436
this.Id = policy.Id;
35-
this.Name = policy.Name;
36-
this.Type = policy.Type;
37-
this.LastModifiedTime = policy.LastModifiedTime;
38-
this.SystemData = policy.SystemData is null ? null : new PSSystemData(policy.SystemData);
39-
40-
this.Enabled = policy.Policy.Enabled;
41-
this.Destination = policy.Policy.Destination;
42-
43-
if (policy.Policy.Rules != null)
37+
this.Name = policy.Data.Name;
38+
this.Type = policy.Data.ResourceType;
39+
this.LastModifiedTime = policy.Data.LastModifiedOn;
40+
this.SystemData = policy.Data.SystemData is null ? null : new PSSystemData(policy.Data.SystemData);
41+
this.Enabled = policy.Data.Policy.Enabled;
42+
// TODO: Destination is currently not supported yet. Will add later.
43+
44+
if (policy.Data.Policy.Rules != null)
4445
{
4546
List<PSBlobInventoryPolicyRule> psRules = new List<PSBlobInventoryPolicyRule>();
46-
foreach (BlobInventoryPolicyRule rule in policy.Policy.Rules)
47+
foreach (Track2Models.BlobInventoryPolicyRule rule in policy.Data.Policy.Rules)
4748
{
4849
psRules.Add(new PSBlobInventoryPolicyRule(rule));
4950
}
@@ -55,32 +56,29 @@ public PSBlobInventoryPolicy(BlobInventoryPolicy policy, string ResourceGroupNam
5556
}
5657
}
5758

58-
public BlobInventoryPolicy ParseBlobInventoryPolicy()
59+
60+
public Track2.BlobInventoryPolicyData ParseBlobInventoryPolicy()
5961
{
60-
List<BlobInventoryPolicyRule> invRules = ParseBlobInventoryPolicyRules(this.Rules);
62+
List<Track2Models.BlobInventoryPolicyRule> invRules = ParseBlobInventoryPolicyRules(this.Rules);
6163

62-
BlobInventoryPolicySchema policySchema = new BlobInventoryPolicySchema()
64+
Track2Models.BlobInventoryPolicySchema policySchema = new Track2Models.BlobInventoryPolicySchema(
65+
this.Enabled,
66+
Track2Models.InventoryRuleType.Inventory,
67+
invRules);
68+
69+
Track2.BlobInventoryPolicyData data = new Track2.BlobInventoryPolicyData
6370
{
64-
Enabled = this.Enabled,
65-
//Destination = this.Destination,
66-
Rules = invRules
71+
Policy = policySchema,
6772
};
68-
return new BlobInventoryPolicy(
69-
policySchema,
70-
this.Id,
71-
this.Name,
72-
this.Type,
73-
this.LastModifiedTime,
74-
this.SystemData is null ? null : this.SystemData.ParseSystemData()
75-
);
73+
return data;
7674
}
7775

78-
public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlobInventoryPolicyRule[] rules)
76+
public static List<Track2Models.BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlobInventoryPolicyRule[] rules)
7977
{
80-
List<BlobInventoryPolicyRule> invRules = null;
78+
List<Track2Models.BlobInventoryPolicyRule> invRules = null;
8179
if (rules != null)
8280
{
83-
invRules = new List<BlobInventoryPolicyRule>();
81+
invRules = new List<Track2Models.BlobInventoryPolicyRule>();
8482
foreach (PSBlobInventoryPolicyRule rule in rules)
8583
{
8684
invRules.Add(rule.ParseBlobInventoryPolicyRule());
@@ -99,7 +97,7 @@ public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlob
9997
[Ps1Xml(Label = "Type", Target = ViewControl.List, Position = 2)]
10098
public string Type { get; set; }
10199
[Ps1Xml(Label = "LastModifiedTime", Target = ViewControl.List, Position = 4)]
102-
public DateTime? LastModifiedTime { get; set; }
100+
public DateTimeOffset? LastModifiedTime { get; set; }
103101

104102
//public PSBlobInventoryPolicySchema Policy { get; set; }
105103

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

114111
/// <summary>
@@ -117,23 +114,25 @@ public static List<BlobInventoryPolicyRule> ParseBlobInventoryPolicyRules(PSBlob
117114
public class PSBlobInventoryPolicyRule
118115
{
119116
public PSBlobInventoryPolicyRule() { }
120-
public PSBlobInventoryPolicyRule(BlobInventoryPolicyRule rule)
117+
public PSBlobInventoryPolicyRule(Track2Models.BlobInventoryPolicyRule rule)
121118
{
122119
this.Enabled = rule.Enabled;
123120
this.Name = rule.Name;
124121
this.Destination = rule.Destination;
125122
this.Definition = rule.Definition is null ? null : new PSBlobInventoryPolicyDefinition(rule.Definition);
126123
}
127124

128-
public BlobInventoryPolicyRule ParseBlobInventoryPolicyRule()
125+
public Track2Models.BlobInventoryPolicyRule ParseBlobInventoryPolicyRule()
129126
{
130-
return new BlobInventoryPolicyRule()
131-
{
132-
Enabled = this.Enabled,
133-
Name = this.Name,
134-
Destination = this.Destination,
135-
Definition = this.Definition is null ? null : this.Definition.parseBlobInventoryPolicyDefinition()
136-
};
127+
Track2Models.BlobInventoryPolicyDefinition policyDefinition = this.Definition?.parseBlobInventoryPolicyDefinition();
128+
129+
Track2Models.BlobInventoryPolicyRule policyRule = new Track2Models.BlobInventoryPolicyRule(
130+
this.Enabled,
131+
this.Name,
132+
this.Destination,
133+
policyDefinition);
134+
135+
return policyRule;
137136
}
138137

139138
public bool Enabled { get; set; }
@@ -152,20 +151,29 @@ public class PSBlobInventoryPolicyDefinition
152151
{
153152
public PSBlobInventoryPolicyDefinition() { }
154153

155-
public PSBlobInventoryPolicyDefinition(BlobInventoryPolicyDefinition Definition)
154+
public PSBlobInventoryPolicyDefinition(Track2Models.BlobInventoryPolicyDefinition Definition)
156155
{
157156
this.Filters = Definition.Filters is null ? null : new PSBlobInventoryPolicyFilter(Definition.Filters);
158-
this.Format = Definition.Format;
159-
this.Schedule = Definition.Schedule;
160-
this.ObjectType = Definition.ObjectType;
157+
this.Format = Definition.Format.ToString();
158+
this.Schedule = Definition.Schedule.ToString();
159+
this.ObjectType = Definition.ObjectType.ToString();
161160
this.SchemaFields = Definition.SchemaFields is null ? null : ((List<string>)Definition.SchemaFields).ToArray();
162161
}
163162

164-
public BlobInventoryPolicyDefinition parseBlobInventoryPolicyDefinition()
163+
public Track2Models.BlobInventoryPolicyDefinition parseBlobInventoryPolicyDefinition()
165164
{
166-
return new BlobInventoryPolicyDefinition(this.Format, this.Schedule, this.ObjectType,
167-
this.SchemaFields is null? null: new List<string>(this.SchemaFields),
168-
this.Filters is null? null : this.Filters.ParseBlobInventoryPolicyFilter());
165+
Track2Models.BlobInventoryPolicyDefinition policyDefinition = new Track2Models.BlobInventoryPolicyDefinition(
166+
new Track2Models.Format(this.Format),
167+
new Track2Models.Schedule(this.Schedule),
168+
new Track2Models.ObjectType(this.ObjectType),
169+
this.SchemaFields is null ? null : new List<string>(this.SchemaFields)
170+
);
171+
172+
if (this.Filters != null)
173+
{
174+
policyDefinition.Filters = this.Filters.ParseBlobInventoryPolicyFilter();
175+
}
176+
return policyDefinition;
169177
}
170178

171179
public PSBlobInventoryPolicyFilter Filters { get; set; }
@@ -213,23 +221,37 @@ public class PSBlobInventoryPolicyFilter
213221
{
214222
public PSBlobInventoryPolicyFilter() { }
215223

216-
public PSBlobInventoryPolicyFilter(BlobInventoryPolicyFilter filters)
224+
public PSBlobInventoryPolicyFilter(Track2Models.BlobInventoryPolicyFilter filters)
217225
{
218226
this.PrefixMatch = PSManagementPolicyRuleFilter.StringListToArray(filters.PrefixMatch);
219227
this.BlobTypes = PSManagementPolicyRuleFilter.StringListToArray(filters.BlobTypes);
220228
this.IncludeBlobVersions = filters.IncludeBlobVersions;
221229
this.IncludeSnapshots = filters.IncludeSnapshots;
222230
}
223231

224-
public BlobInventoryPolicyFilter ParseBlobInventoryPolicyFilter()
232+
public Track2Models.BlobInventoryPolicyFilter ParseBlobInventoryPolicyFilter()
225233
{
226-
return new BlobInventoryPolicyFilter()
234+
Track2Models.BlobInventoryPolicyFilter policyFilter = new Track2Models.BlobInventoryPolicyFilter()
227235
{
228-
PrefixMatch = PSManagementPolicyRuleFilter.StringArrayToList(this.PrefixMatch),
229-
BlobTypes = PSManagementPolicyRuleFilter.StringArrayToList(this.BlobTypes),
230236
IncludeSnapshots = this.IncludeSnapshots,
231237
IncludeBlobVersions = this.IncludeBlobVersions
232238
};
239+
240+
if (this.PrefixMatch != null)
241+
{
242+
foreach (string prefixMatch in this.PrefixMatch)
243+
{
244+
policyFilter.PrefixMatch.Add(prefixMatch);
245+
}
246+
}
247+
if (this.BlobTypes != null)
248+
{
249+
foreach (string blobType in this.BlobTypes)
250+
{
251+
policyFilter.BlobTypes.Add(blobType);
252+
}
253+
}
254+
return policyFilter;
233255
}
234256

235257
public string[] PrefixMatch { get; set; }
@@ -245,26 +267,21 @@ public class PSSystemData
245267
{
246268
public PSSystemData() { }
247269

248-
public PSSystemData(SystemData SystemData)
270+
public PSSystemData(global::Azure.ResourceManager.Models.SystemData SystemData)
249271
{
250272
this.CreatedBy = SystemData.CreatedBy;
251-
this.CreatedByType = SystemData.CreatedByType;
252-
this.CreatedAt = SystemData.CreatedAt;
273+
this.CreatedByType = SystemData.CreatedByType.ToString();
274+
this.CreatedAt = SystemData.CreatedOn;
253275
this.LastModifiedBy = SystemData.LastModifiedBy;
254-
this.LastModifiedByType = SystemData.LastModifiedByType;
255-
this.LastModifiedAt = SystemData.LastModifiedAt;
256-
}
257-
258-
public SystemData ParseSystemData()
259-
{
260-
return new SystemData(this.CreatedBy, this.CreatedByType, this.CreatedAt, this.LastModifiedBy, this.LastModifiedByType, this.LastModifiedAt);
276+
this.LastModifiedByType = SystemData.LastModifiedByType.ToString();
277+
this.LastModifiedAt = SystemData.LastModifiedOn;
261278
}
262279

263280
public string CreatedBy { get; set; }
264281
public string CreatedByType { get; set; }
265-
public DateTime? CreatedAt { get; set; }
282+
public DateTimeOffset? CreatedAt { get; set; }
266283
public string LastModifiedBy { get; set; }
267284
public string LastModifiedByType { get; set; }
268-
public DateTime? LastModifiedAt { get; set; }
285+
public DateTimeOffset? LastModifiedAt { get; set; }
269286
}
270287
}

src/Storage/Storage.Management/Storage.Management.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
<Compile Remove="File\UpdateAzAzStorageFileServiceProperty.cs" />
2020
<Compile Remove="Models\PSFileServiceProperties.cs" />
2121
<Compile Remove="StorageAccount\InvokeAzureStorageAccountHierarchicalNamespaceUpgrade.cs" />
22-
<Compile Remove="StorageAccount\InvokeAzureStorageAccountFailover.cs" />
2322
</ItemGroup>
2423
<ItemGroup>
2524
<!-- <PackageReference Include="Azure.Core" Version="1.24.0" /> -->

src/Storage/Storage.Management/StorageAccount/GetAzureStorageBlobInventoryPolicy.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
using Microsoft.Azure.Commands.Management.Storage.Models;
1616
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1717
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18-
using Microsoft.Azure.Management.Storage;
19-
using Microsoft.Azure.Management.Storage.Models;
2018
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2119
using System.Management.Automation;
20+
using Track2 = Azure.ResourceManager.Storage;
2221

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

42+
/// <summary>
43+
/// Default policy name
44+
/// </summary>
45+
private const string DefaultPolicyName = "default";
46+
4347
[Parameter(
4448
Position = 0,
4549
Mandatory = true,
@@ -95,9 +99,8 @@ public override void ExecuteCmdlet()
9599
break;
96100
}
97101

98-
BlobInventoryPolicy policy = this.StorageClient.BlobInventoryPolicies.Get(
99-
this.ResourceGroupName,
100-
this.StorageAccountName);
102+
Track2.BlobInventoryPolicyResource policy =
103+
this.StorageClientTrack2.GetBlobInventoryPolicyResource(this.ResourceGroupName, this.StorageAccountName, DefaultPolicyName).Get();
101104

102105
WriteObject(new PSBlobInventoryPolicy(policy, this.ResourceGroupName, this.StorageAccountName), true);
103106
}

src/Storage/Storage.Management/StorageAccount/InvokeAzureStorageAccountFailover.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using System.Collections;
16-
using System.Collections.Generic;
17-
using System.Management.Automation;
18-
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
19-
using Microsoft.Azure.Management.Storage;
20-
using Microsoft.Azure.Management.Storage.Models;
21-
using StorageModels = Microsoft.Azure.Management.Storage.Models;
2215
using Microsoft.Azure.Commands.Management.Storage.Models;
2316
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
17+
using Microsoft.Azure.Commands.ResourceManager.Common.Tags;
2418
using System;
19+
using System.Collections;
20+
using System.Collections.Generic;
21+
using System.Management.Automation;
2522
using System.Text;
2623

2724
namespace Microsoft.Azure.Commands.Management.Storage
@@ -89,7 +86,6 @@ public override void ExecuteCmdlet()
8986
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).");
9087
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/");
9188

92-
9389
if (this.force || ShouldContinue(shouldContinuePrompt.ToString(), ""))
9490
{
9591
if (ParameterSetName == AccountObjectParameterSet)
@@ -98,11 +94,8 @@ public override void ExecuteCmdlet()
9894
this.Name = InputObject.StorageAccountName;
9995
}
10096

101-
this.StorageClient.StorageAccounts.Failover(
102-
this.ResourceGroupName,
103-
this.Name);
104-
105-
var storageAccount = this.StorageClient.StorageAccounts.GetProperties(this.ResourceGroupName, this.Name);
97+
this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.Name).Failover(global::Azure.WaitUntil.Completed);
98+
var storageAccount = this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.Name).Get();
10699

107100
WriteStorageAccount(storageAccount);
108101
}

src/Storage/Storage.Management/StorageAccount/NewAzureStorageBlobInventoryPolicyRule.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@
1414

1515
using Microsoft.Azure.Commands.Management.Storage.Models;
1616
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
17-
using Microsoft.Azure.Management.Storage;
18-
using Microsoft.Azure.Management.Storage.Models;
1917
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2018
using System.Collections.Generic;
2119
using System.Management.Automation;
2220
using System.Reflection;
23-
using StorageModels = Microsoft.Azure.Management.Storage.Models;
2421

2522
namespace Microsoft.Azure.Commands.Management.Storage
2623
{

src/Storage/Storage.Management/StorageAccount/RemoveAzureStorageBlobInventoryPolicy.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
using Microsoft.Azure.Commands.Management.Storage.Models;
1616
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1717
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18-
using Microsoft.Azure.Management.Storage;
19-
using Microsoft.Azure.Management.Storage.Models;
2018
using System.Management.Automation;
19+
using Azure;
2120

2221
namespace Microsoft.Azure.Commands.Management.Storage
2322
{
@@ -115,10 +114,7 @@ public override void ExecuteCmdlet()
115114
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
116115
break;
117116
}
118-
119-
this.StorageClient.BlobInventoryPolicies.Delete(
120-
this.ResourceGroupName,
121-
this.StorageAccountName);
117+
this.StorageClientTrack2.GetBlobInventoryPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default").Delete(WaitUntil.Completed);
122118

123119
if (PassThru.IsPresent)
124120
{

0 commit comments

Comments
 (0)