Skip to content

Commit 43db5f3

Browse files
committed
put parser into helper
1 parent fcfd28c commit 43db5f3

File tree

1 file changed

+60
-41
lines changed

1 file changed

+60
-41
lines changed

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

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,15 @@ public override void ExecuteCmdlet()
135135
// The workaround is to get the raw response and parse it into the output desired
136136
// The Blob restore status should be got from SDK directly once the issue is fixed
137137
Dictionary<string, object> temp = restoreLro.GetRawResponse().Content.ToObjectFromJson() as Dictionary<string, object>;
138-
object restoreId;
139138

140-
if (temp != null && temp.TryGetValue("restoreId", out restoreId))
139+
if (temp == null)
140+
{
141+
throw new InvalidJobStateException("Could not fetch the Blob restore response.");
142+
}
143+
PSBlobRestoreStatus blobRestoreStatus = ParseRestoreRawResponse(temp);
144+
if (blobRestoreStatus.RestoreId != null)
141145
{
142-
WriteWarning(string.Format("Restore blob ranges with Id '{0}' started. Restore blob ranges time to complete is dependent on the size of the restore.", restoreId));
146+
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));
143147
}
144148
else
145149
{
@@ -165,62 +169,77 @@ public override void ExecuteCmdlet()
165169
this.TimeToRestore.ToUniversalTime(),
166170
PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange)));
167171
Dictionary<string, object> temp = restoreLro.GetRawResponse().Content.ToObjectFromJson() as Dictionary<string, object>;
168-
object jobStatus = null;
169172

170-
if (temp != null && temp.TryGetValue("status", out jobStatus))
173+
if (temp == null)
174+
{
175+
throw new InvalidJobStateException("Could not fetch the Blob restore response.");
176+
}
177+
178+
PSBlobRestoreStatus blobRestoreStatus = ParseRestoreRawResponse(temp);
179+
180+
if (blobRestoreStatus.Status != null)
171181
{
172-
if (jobStatus != null && jobStatus.ToString() == Track2Models.BlobRestoreProgressStatus.Failed.ToString())
182+
if (blobRestoreStatus.Status == Track2Models.BlobRestoreProgressStatus.Failed.ToString())
173183
{
174184
throw new InvalidJobStateException("Blob ranges restore failed.");
175185
}
176-
} else
186+
}
187+
else
177188
{
178189
WriteWarning(string.Format("Could not fetch the status."));
179190
}
191+
WriteObject(blobRestoreStatus);
192+
}
193+
}
194+
}
180195

181-
if (temp == null)
182-
{
183-
throw new InvalidJobStateException("Could not fetch the Blob restore response.");
184-
}
196+
private PSBlobRestoreStatus ParseRestoreRawResponse(Dictionary<string, object> response)
197+
{
198+
response.TryGetValue("restoreId", out object restoreId);
199+
response.TryGetValue("status", out object jobStatus);
200+
response.TryGetValue("parameters", out object parameters);
185201

186-
temp.TryGetValue("restoreId", out object restoreId);
187-
temp.TryGetValue("parameters", out object parameters);
202+
PSBlobRestoreParameters blobRestoreParameters;
203+
Dictionary<string, object> paramMap = parameters as Dictionary<string, object>;
188204

189-
PSBlobRestoreParameters blobRestoreParameters = new PSBlobRestoreParameters();
190-
Dictionary<string, object> paramMap = parameters as Dictionary<string, object>;
205+
if (paramMap == null)
206+
{
207+
blobRestoreParameters = null;
208+
}
209+
else
210+
{
211+
blobRestoreParameters = new PSBlobRestoreParameters();
212+
paramMap.TryGetValue("timetoRestore", out object timeToRestore);
213+
DateTimeOffset.TryParse(timeToRestore?.ToString(), out DateTimeOffset parseDate);
214+
blobRestoreParameters.TimeToRestore = parseDate;
191215

192-
paramMap.TryGetValue("timetoRestore", out object timeToRestore);
193-
DateTimeOffset.TryParse(timeToRestore.ToString(), out DateTimeOffset parseDate);
194-
blobRestoreParameters.TimeToRestore = parseDate;
216+
paramMap.TryGetValue("blobRanges", out object ranges);
217+
List<PSBlobRestoreRange> blobRestoreRanges = new List<PSBlobRestoreRange>();
195218

196-
paramMap.TryGetValue("blobRanges", out object ranges);
197-
List<PSBlobRestoreRange> blobRestoreRanges = new List<PSBlobRestoreRange>();
198-
199-
object[] rangesList = ranges as object[];
200-
foreach(object range in rangesList)
201-
{
202-
Dictionary<string, object> rangeMap = range as Dictionary<string, object>;
203-
204-
rangeMap.TryGetValue("startRange", out object startRange);
205-
rangeMap.TryGetValue("endRange", out object endRange);
219+
object[] rangesList = ranges as object[];
220+
foreach (object range in rangesList)
221+
{
222+
Dictionary<string, object> rangeMap = range as Dictionary<string, object>;
206223

207-
PSBlobRestoreRange blobRestoreRange = new PSBlobRestoreRange
208-
{
209-
StartRange = startRange?.ToString(),
210-
EndRange = endRange?.ToString()
211-
};
224+
rangeMap.TryGetValue("startRange", out object startRange);
225+
rangeMap.TryGetValue("endRange", out object endRange);
212226

213-
blobRestoreRanges.Add(blobRestoreRange);
214-
}
215-
blobRestoreParameters.BlobRanges = blobRestoreRanges.ToArray();
227+
PSBlobRestoreRange blobRestoreRange = new PSBlobRestoreRange
228+
{
229+
StartRange = startRange?.ToString(),
230+
EndRange = endRange?.ToString()
231+
};
216232

217-
WriteObject(new PSBlobRestoreStatus(
218-
status: jobStatus?.ToString(),
219-
failureReason: null,
220-
restoreId: restoreId?.ToString(),
221-
blobRestoreParameters));
233+
blobRestoreRanges.Add(blobRestoreRange);
222234
}
235+
blobRestoreParameters.BlobRanges = blobRestoreRanges.ToArray();
223236
}
237+
238+
return new PSBlobRestoreStatus(
239+
status: jobStatus?.ToString(),
240+
failureReason: null,
241+
restoreId: restoreId?.ToString(),
242+
parameters: blobRestoreParameters);
224243
}
225244
}
226245
}

0 commit comments

Comments
 (0)