Skip to content

Commit d858c2d

Browse files
authored
Merge pull request #14 from yifanz0/yifan/track2.1
Migrate Restore-AzStorageBlobRange and ManagementPolicy related cmdlets
2 parents 85aba87 + 0fc210d commit d858c2d

12 files changed

+342
-195
lines changed

src/Storage/Storage.Management/Models/PSBlobRestore.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public PSBlobRestoreRange(Track2Models.BlobRestoreRange range)
5757
foreach (PSBlobRestoreRange range in ranges)
5858
{
5959
re.Add(
60-
new Track2Models.BlobRestoreRange(range.EndRange, range.StartRange));
60+
new Track2Models.BlobRestoreRange(range.StartRange, range.EndRange));
6161
}
6262
}
6363
return re;
@@ -112,6 +112,14 @@ public PSBlobRestoreStatus(Track2Models.BlobRestoreStatus status)
112112
this.Parameters = status.Parameters is null ? null : new PSBlobRestoreParameters(status.Parameters);
113113
}
114114
}
115+
116+
public PSBlobRestoreStatus(string status, string failureReason, string restoreId, PSBlobRestoreParameters parameters)
117+
{
118+
Status = status;
119+
FailureReason = failureReason;
120+
RestoreId = restoreId;
121+
Parameters = parameters;
122+
}
115123
}
116124

117125
/// <summary>

src/Storage/Storage.Management/Models/PSDataPolicy.cs

Lines changed: 117 additions & 95 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
<ItemGroup>
1717
<Compile Remove="StorageAccount\AddAzureStorageAccountNetworkRule.cs" />
1818
<Compile Remove="StorageAccount\GetAzureStorageAccountNetworkRuleSet.cs" />
19-
<Compile Remove="StorageAccount\InvokeAzureStorageAccountFailover.cs" />
2019
<Compile Remove="StorageAccount\InvokeAzureStorageAccountHierarchicalNamespaceUpgrade.cs" />
2120
<Compile Remove="StorageAccount\RemoveAzureStorageAccountNetworkRule.cs" />
22-
<Compile Remove="StorageAccount\RestoreAzStorageBlobRange.cs" />
2321
<Compile Remove="StorageAccount\UpdateAzureStorageAccountNetworkRuleSet.cs" />
22+
<Compile Remove="StorageAccount\InvokeAzureStorageAccountFailover.cs" />
2423
</ItemGroup>
2524
<ItemGroup>
2625
<!-- <PackageReference Include="Azure.Core" Version="1.24.0" /> -->

src/Storage/Storage.Management/StorageAccount/AddAzureStorageAccountManagementPolicyAction.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
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.Globalization;
2119
using System.Management.Automation;

src/Storage/Storage.Management/StorageAccount/GetAzureStorageAccountManagementPolicy.cs

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

15+
using Azure.ResourceManager.Storage;
1516
using Microsoft.Azure.Commands.Management.Storage.Models;
1617
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1718
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18-
using Microsoft.Azure.Management.Storage;
19-
using Microsoft.Azure.Management.Storage.Models;
2019
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
2120
using System.Management.Automation;
2221

@@ -95,11 +94,11 @@ public override void ExecuteCmdlet()
9594
break;
9695
}
9796

98-
ManagementPolicy managementPolicy = this.StorageClient.ManagementPolicies.Get(
99-
this.ResourceGroupName,
100-
this.StorageAccountName);
97+
ManagementPolicyResource policy = this.StorageClientTrack2.GetManagementPolicyResource(
98+
this.ResourceGroupName, this.StorageAccountName, "default").Get();
10199

102-
WriteObject(new PSManagementPolicy(managementPolicy, this.ResourceGroupName, this.StorageAccountName), true);
100+
var result = new PSManagementPolicy(policy, this.ResourceGroupName, this.StorageAccountName);
101+
WriteObject(result, true);
103102
}
104103
}
105104
}

src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyFilter.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
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 System.Management.Automation;
2018

2119
namespace Microsoft.Azure.Commands.Management.Storage

src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyRule.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
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.Management.Automation;
2119

src/Storage/Storage.Management/StorageAccount/RemoveAzureStorageAccountManagementPolicy.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Azure;
1516
using Microsoft.Azure.Commands.Management.Storage.Models;
1617
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1718
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18-
using Microsoft.Azure.Management.Storage;
19-
using Microsoft.Azure.Management.Storage.Models;
2019
using System.Management.Automation;
2120

2221
namespace Microsoft.Azure.Commands.Management.Storage
@@ -116,9 +115,8 @@ public override void ExecuteCmdlet()
116115
break;
117116
}
118117

119-
this.StorageClient.ManagementPolicies.Delete(
120-
this.ResourceGroupName,
121-
this.StorageAccountName);
118+
this.StorageClientTrack2.GetManagementPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default")
119+
.Delete(WaitUntil.Completed);
122120

123121
if (PassThru.IsPresent)
124122
{

src/Storage/Storage.Management/StorageAccount/RestoreAzStorageBlobRange.cs

Lines changed: 101 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Azure;
1516
using Microsoft.Azure.Commands.Management.Storage.Models;
1617
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1718
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
18-
using Microsoft.Azure.Management.Storage;
19-
using Microsoft.Azure.Management.Storage.Models;
2019
using Microsoft.Rest.Azure;
20+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
2121
using System;
22+
using System.Collections.Generic;
2223
using System.Management.Automation;
2324
using System.Threading.Tasks;
25+
using Track2 = Azure.ResourceManager.Storage;
26+
using Track2Models = Azure.ResourceManager.Storage.Models;
2427

2528
namespace Microsoft.Azure.Commands.Management.Storage
2629
{
@@ -79,7 +82,7 @@ public class RestoreAzureStorageBlobRangeCommand : StorageAccountBaseCmdlet
7982

8083
[Parameter(Mandatory = true, HelpMessage = "The Time to Restore Blob.")]
8184
[ValidateNotNull]
82-
public DateTime TimeToRestore { get; set; }
85+
public DateTimeOffset TimeToRestore { get; set; }
8386

8487
[Parameter(Mandatory = false, HelpMessage = "The blob range to Restore.")]
8588
[ValidateNotNull]
@@ -118,50 +121,116 @@ public override void ExecuteCmdlet()
118121

119122
if (ShouldProcess(this.StorageAccountName, "Restore Blob Range"))
120123
{
121-
if (waitForComplete)
122-
{
123-
Task<AzureOperationResponse<BlobRestoreStatus>> beginTask = this.StorageClient.StorageAccounts.BeginRestoreBlobRangesWithHttpMessagesAsync(
124-
this.ResourceGroupName,
125-
this.StorageAccountName,
126-
this.TimeToRestore,
127-
PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange));
128-
129-
beginTask.Wait();
130-
131-
AzureOperationResponse<BlobRestoreStatus> response = beginTask.Result;
124+
Track2.StorageAccountResource account = this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.StorageAccountName);
125+
var restoreLro = account.RestoreBlobRanges(
126+
WaitUntil.Started,
127+
new Track2Models.BlobRestoreContent(
128+
this.TimeToRestore.ToUniversalTime(),
129+
PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange)));
130+
131+
// This is a temporary workaround of SDK issue https://github.com/Azure/azure-sdk-for-net/issues/29060
132+
// The workaround is to get the raw response and parse it into the output desired
133+
// The Blob restore status should be got from SDK directly once the issue is fixed
134+
Dictionary<string, object> temp = restoreLro.GetRawResponse().Content.ToObjectFromJson() as Dictionary<string, object>;
132135

133-
WriteWarning(string.Format("Restore blob ranges with Id '{0}' started. Restore blob ranges time to complete is dependent on the size of the restore.", response.Body is null ? "" : response.Body.RestoreId));
136+
if (waitForComplete)
137+
{
138+
if (temp == null)
139+
{
140+
throw new InvalidJobStateException("Could not fetch the Blob restore response.");
141+
}
142+
PSBlobRestoreStatus blobRestoreStatus = ParseRestoreRawResponse(temp);
143+
if (blobRestoreStatus.RestoreId != null)
144+
{
145+
WriteWarning(string.Format("Restore blob ranges with Id '{0}' started. Restore blob ranges time to complete is dependent on the size of the restore.", blobRestoreStatus.RestoreId));
146+
}
147+
else
148+
{
149+
WriteWarning(string.Format("Could not fetch the Restore Id."));
150+
}
134151

135-
Task<AzureOperationResponse<BlobRestoreStatus>> waitTask = ((StorageManagementClient)this.StorageClient).GetPostOrDeleteOperationResultAsync(response, null, new System.Threading.CancellationToken());
136152
try
137153
{
138-
waitTask.Wait();
154+
var result = restoreLro.WaitForCompletion().Value;
155+
WriteObject(new PSBlobRestoreStatus(result));
139156
}
140157
catch (System.AggregateException ex) when (ex.InnerException is CloudException)
141158
{
142-
throw new InvalidJobStateException(string.Format("Blob ranges restore failed with information: '{0}'.", ((CloudException)ex.InnerException).Response.Content));
159+
throw new InvalidJobStateException(string.Format("Blob ranges restore failed with information: '{0}'.", ex.ToString()));
143160
}
144-
145-
AzureOperationResponse<BlobRestoreStatus> result = waitTask.Result;
146-
147-
WriteObject(new PSBlobRestoreStatus(result.Body));
148-
149161
}
150162
else
151163
{
152-
BlobRestoreStatus status = this.StorageClient.StorageAccounts.BeginRestoreBlobRanges(
153-
this.ResourceGroupName,
154-
this.StorageAccountName,
155-
this.TimeToRestore,
156-
PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange));
157-
158-
WriteObject(new PSBlobRestoreStatus(status));
159-
if (status != null && status.Status == BlobRestoreProgressStatus.Failed)
164+
if (temp == null)
165+
{
166+
throw new InvalidJobStateException("Could not fetch the Blob restore response.");
167+
}
168+
169+
PSBlobRestoreStatus blobRestoreStatus = ParseRestoreRawResponse(temp);
170+
171+
if (blobRestoreStatus.Status != null)
172+
{
173+
if (blobRestoreStatus.Status == Track2Models.BlobRestoreProgressStatus.Failed.ToString())
174+
{
175+
throw new InvalidJobStateException("Blob ranges restore failed.");
176+
}
177+
}
178+
else
160179
{
161-
throw new InvalidJobStateException("Blob ranges restore failed.");
180+
WriteWarning(string.Format("Could not fetch the status."));
162181
}
182+
WriteObject(blobRestoreStatus);
163183
}
164184
}
165185
}
186+
187+
private PSBlobRestoreStatus ParseRestoreRawResponse(Dictionary<string, object> response)
188+
{
189+
response.TryGetValue("restoreId", out object restoreId);
190+
response.TryGetValue("status", out object jobStatus);
191+
response.TryGetValue("parameters", out object parameters);
192+
193+
PSBlobRestoreParameters blobRestoreParameters;
194+
Dictionary<string, object> paramMap = parameters as Dictionary<string, object>;
195+
196+
if (paramMap == null)
197+
{
198+
blobRestoreParameters = null;
199+
}
200+
else
201+
{
202+
blobRestoreParameters = new PSBlobRestoreParameters();
203+
paramMap.TryGetValue("timetoRestore", out object timeToRestore);
204+
DateTimeOffset.TryParse(timeToRestore?.ToString(), out DateTimeOffset parseDate);
205+
blobRestoreParameters.TimeToRestore = parseDate;
206+
207+
paramMap.TryGetValue("blobRanges", out object ranges);
208+
List<PSBlobRestoreRange> blobRestoreRanges = new List<PSBlobRestoreRange>();
209+
210+
object[] rangesList = ranges as object[];
211+
foreach (object range in rangesList)
212+
{
213+
Dictionary<string, object> rangeMap = range as Dictionary<string, object>;
214+
215+
rangeMap.TryGetValue("startRange", out object startRange);
216+
rangeMap.TryGetValue("endRange", out object endRange);
217+
218+
PSBlobRestoreRange blobRestoreRange = new PSBlobRestoreRange
219+
{
220+
StartRange = startRange?.ToString(),
221+
EndRange = endRange?.ToString()
222+
};
223+
224+
blobRestoreRanges.Add(blobRestoreRange);
225+
}
226+
blobRestoreParameters.BlobRanges = blobRestoreRanges.ToArray();
227+
}
228+
229+
return new PSBlobRestoreStatus(
230+
status: jobStatus?.ToString(),
231+
failureReason: null,
232+
restoreId: restoreId?.ToString(),
233+
parameters: blobRestoreParameters);
234+
}
166235
}
167236
}

src/Storage/Storage.Management/StorageAccount/SetAzureStorageAccountManagementPolicy.cs

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

15+
using Azure.ResourceManager.Storage;
1516
using Microsoft.Azure.Commands.Management.Storage.Models;
1617
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
17-
using Microsoft.Azure.Management.Storage;
18-
using Microsoft.Azure.Management.Storage.Models;
19-
using System.Management.Automation;
20-
using Newtonsoft.Json.Linq;
21-
using System.Globalization;
22-
using System.Collections.Generic;
2318
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
2419
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
20+
using Newtonsoft.Json.Linq;
21+
using System.Collections.Generic;
22+
using System.Globalization;
23+
using System.Management.Automation;
24+
using Track2 = Azure.ResourceManager.Storage;
2525

2626
namespace Microsoft.Azure.Commands.Management.Storage
2727
{
@@ -62,7 +62,7 @@ public class SetAzureStorageAccountManagementPolicyCommand : StorageAccountBaseC
6262
Position = 0,
6363
Mandatory = true,
6464
HelpMessage = "Resource Group Name.",
65-
ParameterSetName = AccountNamePolicyRuleParameterSet)]
65+
ParameterSetName = AccountNamePolicyRuleParameterSet)]
6666
[Parameter(
6767
Position = 0,
6868
Mandatory = true,
@@ -155,7 +155,7 @@ public override void ExecuteCmdlet()
155155
base.ExecuteCmdlet();
156156
if (ShouldProcess(this.StorageAccountName, "Set Storage Account Management Policy"))
157157
{
158-
if ((this.ParameterSetName == AccountObjectPolicyRuleParameterSet)
158+
if ((this.ParameterSetName == AccountObjectPolicyRuleParameterSet)
159159
|| (this.ParameterSetName == AccountObjectPolicyObjectParameterSet))
160160
{
161161
this.ResourceGroupName = StorageAccount.ResourceGroupName;
@@ -168,35 +168,43 @@ public override void ExecuteCmdlet()
168168
this.ResourceGroupName = accountResource.ResourceGroupName;
169169
this.StorageAccountName = accountResource.ResourceName;
170170
}
171-
ManagementPolicy managementPolicy;
171+
172+
ManagementPolicyResource managementPolicyResource;
173+
Track2.ManagementPolicyData data;
172174

173175
switch (this.ParameterSetName)
174176
{
175177
case AccountObjectPolicyRuleParameterSet:
176178
case AccountNamePolicyRuleParameterSet:
177179
case AccountResourceIdPolicyRuleParameterSet:
178-
managementPolicy = this.StorageClient.ManagementPolicies.CreateOrUpdate(
179-
this.ResourceGroupName,
180-
this.StorageAccountName,
181-
new ManagementPolicySchema(
182-
//this.version,
183-
PSManagementPolicyRule.ParseManagementPolicyRules(this.Rule)));
180+
181+
data = new Track2.ManagementPolicyData
182+
{
183+
Rules = PSManagementPolicyRule.ParseManagementPolicyRules(this.Rule)
184+
};
185+
186+
managementPolicyResource = this.StorageClientTrack2
187+
.GetManagementPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default")
188+
.CreateOrUpdate(global::Azure.WaitUntil.Completed, data).Value;
184189
break;
185190
case AccountObjectPolicyObjectParameterSet:
186191
case AccountNamePolicyObjectParameterSet:
187192
case AccountResourceIdPolicyObjectParameterSet:
188-
managementPolicy = this.StorageClient.ManagementPolicies.CreateOrUpdate(
189-
this.ResourceGroupName,
190-
this.StorageAccountName,
191-
new ManagementPolicySchema(
192-
//this.Policy.Version,
193-
PSManagementPolicyRule.ParseManagementPolicyRules(this.Policy.Rules)));
193+
194+
data = new Track2.ManagementPolicyData()
195+
{
196+
Rules = PSManagementPolicyRule.ParseManagementPolicyRules(this.Policy.Rules)
197+
};
198+
199+
managementPolicyResource = this.StorageClientTrack2
200+
.GetManagementPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default")
201+
.CreateOrUpdate(global::Azure.WaitUntil.Completed, data).Value;
194202
break;
195203
default:
196204
throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ParameterSet: {0}", this.ParameterSetName));
197205
}
198206

199-
WriteObject(new PSManagementPolicy(managementPolicy, this.ResourceGroupName, this.StorageAccountName), true);
207+
WriteObject(new PSManagementPolicy(managementPolicyResource, this.ResourceGroupName, this.StorageAccountName), true);
200208
}
201209
}
202210
}

0 commit comments

Comments
 (0)