Skip to content

Commit 9798cb4

Browse files
committed
Optimised recoveryPointConversion
1 parent 209f6a8 commit 9798cb4

File tree

1 file changed

+143
-205
lines changed

1 file changed

+143
-205
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Helpers/Conversions/RecoveryPointConversions.cs

Lines changed: 143 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -49,110 +49,17 @@ public static List<RecoveryPointBase> GetPSAzureRecoveryPoints(
4949
{
5050
if (rp.Properties.GetType() == typeof(ServiceClientModel.IaasVMRecoveryPoint))
5151
{
52-
ServiceClientModel.IaasVMRecoveryPoint recPoint =
53-
rp.Properties as ServiceClientModel.IaasVMRecoveryPoint;
54-
55-
DateTime recPointTime = DateTime.MinValue;
56-
if (recPoint.RecoveryPointTime.HasValue)
57-
{
58-
recPointTime = (DateTime)recPoint.RecoveryPointTime;
59-
}
60-
else
61-
{
62-
throw new ArgumentNullException("RecoveryPointTime is null");
63-
}
64-
65-
bool isInstantILRSessionActive =
66-
recPoint.IsInstantIlrSessionActive.HasValue ?
67-
(bool)recPoint.IsInstantIlrSessionActive : false;
68-
69-
AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint()
70-
{
71-
RecoveryPointId = rp.Name,
72-
BackupManagementType = item.BackupManagementType,
73-
ItemName = protectedItemName,
74-
ContainerName = containerName,
75-
ContainerType = item.ContainerType,
76-
RecoveryPointTime = recPointTime,
77-
RecoveryPointType = recPoint.RecoveryPointType,
78-
Id = rp.Id,
79-
WorkloadType = item.WorkloadType,
80-
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
81-
SourceVMStorageType = recPoint.SourceVMStorageType,
82-
SourceResourceId = item.SourceResourceId,
83-
EncryptionEnabled = recPoint.IsSourceVMEncrypted.HasValue ?
84-
recPoint.IsSourceVMEncrypted.Value : false,
85-
IlrSessionActive = isInstantILRSessionActive,
86-
IsManagedVirtualMachine = recPoint.IsManagedVirtualMachine.HasValue ?
87-
recPoint.IsManagedVirtualMachine.Value : false,
88-
OriginalSAEnabled = recPoint.OriginalStorageAccountOption.HasValue ?
89-
recPoint.OriginalStorageAccountOption.Value : false,
90-
};
91-
result.Add(rpBase);
52+
result.Add(GetPSAzureVMRecoveryPoint(rp, item));
9253
}
9354

9455
if (rp.Properties.GetType() == typeof(ServiceClientModel.AzureFileShareRecoveryPoint))
9556
{
96-
ServiceClientModel.AzureFileShareRecoveryPoint recPoint =
97-
rp.Properties as ServiceClientModel.AzureFileShareRecoveryPoint;
98-
99-
DateTime recPointTime = DateTime.MinValue;
100-
if (recPoint.RecoveryPointTime.HasValue)
101-
{
102-
recPointTime = (DateTime)recPoint.RecoveryPointTime;
103-
}
104-
else
105-
{
106-
throw new ArgumentNullException("RecoveryPointTime is null");
107-
}
108-
109-
AzureFileShareRecoveryPoint rpBase = new AzureFileShareRecoveryPoint()
110-
{
111-
RecoveryPointId = rp.Name,
112-
BackupManagementType = item.BackupManagementType,
113-
ItemName = protectedItemName,
114-
ContainerName = containerName,
115-
ContainerType = item.ContainerType,
116-
RecoveryPointTime = recPointTime,
117-
RecoveryPointType = recPoint.RecoveryPointType,
118-
Id = rp.Id,
119-
WorkloadType = item.WorkloadType,
120-
FileShareSnapshotUri = recPoint.FileShareSnapshotUri,
121-
};
122-
result.Add(rpBase);
57+
result.Add(GetPSAzureFileRecoveryPoint(rp, item));
12358
}
12459

12560
if (rp.Properties.GetType() == typeof(ServiceClientModel.GenericRecoveryPoint))
12661
{
127-
ServiceClientModel.GenericRecoveryPoint recPoint =
128-
rp.Properties as ServiceClientModel.GenericRecoveryPoint;
129-
130-
DateTime recPointTime = DateTime.MinValue;
131-
if (recPoint.RecoveryPointTime.HasValue)
132-
{
133-
recPointTime = (DateTime)recPoint.RecoveryPointTime;
134-
}
135-
else
136-
{
137-
throw new ArgumentNullException("RecoveryPointTime is null");
138-
}
139-
140-
AzureSqlRecoveryPoint rpBase = new AzureSqlRecoveryPoint()
141-
{
142-
RecoveryPointId = rp.Name,
143-
BackupManagementType = item.BackupManagementType,
144-
ItemName = protectedItemName,
145-
ContainerName = containerUri,
146-
ContainerType = item.ContainerType,
147-
RecoveryPointTime = recPointTime,
148-
RecoveryPointType = recPoint.RecoveryPointType,
149-
Id = rp.Id,
150-
WorkloadType = item.WorkloadType,
151-
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
152-
FriendlyName = recPoint.FriendlyName,
153-
};
154-
155-
result.Add(rpBase);
62+
result.Add(GetPSAzureGenericRecoveryPoint(rp, item));
15663
}
15764
}
15865

@@ -173,136 +80,167 @@ public static RecoveryPointBase GetPSAzureRecoveryPoints(
17380

17481
RecoveryPointBase result = null;
17582

176-
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id);
177-
string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id);
178-
string containerName = IdUtils.GetNameFromUri(containerUri);
179-
string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id);
180-
string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri);
181-
18283
if (rpResponse.Properties.GetType() ==
18384
typeof(ServiceClientModel.IaasVMRecoveryPoint))
18485
{
185-
ServiceClientModel.IaasVMRecoveryPoint recPoint =
186-
rpResponse.Properties as ServiceClientModel.IaasVMRecoveryPoint;
86+
result = GetPSAzureVMRecoveryPoint(rpResponse, item);
87+
}
18788

188-
DateTime recPointTime = DateTime.MinValue;
189-
if (recPoint.RecoveryPointTime.HasValue)
190-
{
191-
recPointTime = (DateTime)recPoint.RecoveryPointTime;
192-
}
193-
else
194-
{
195-
throw new ArgumentNullException("RecoveryPointTime is null");
196-
}
89+
if (rpResponse.Properties.GetType() ==
90+
typeof(ServiceClientModel.AzureFileShareRecoveryPoint))
91+
{
92+
result = GetPSAzureFileRecoveryPoint(rpResponse, item);
93+
}
19794

198-
bool isInstantILRSessionActive =
199-
recPoint.IsInstantIlrSessionActive.HasValue ?
200-
(bool)recPoint.IsInstantIlrSessionActive : false;
201-
AzureVmRecoveryPoint vmResult = new AzureVmRecoveryPoint()
202-
{
203-
RecoveryPointId = rpResponse.Name,
204-
BackupManagementType = item.BackupManagementType,
205-
ItemName = protectedItemName,
206-
ContainerName = containerName,
207-
ContainerType = item.ContainerType,
208-
RecoveryPointTime = recPointTime,
209-
RecoveryPointType = recPoint.RecoveryPointType,
210-
Id = rpResponse.Id,
211-
WorkloadType = item.WorkloadType,
212-
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
213-
EncryptionEnabled = recPoint.IsSourceVMEncrypted.HasValue ?
214-
recPoint.IsSourceVMEncrypted.Value : false,
215-
IlrSessionActive = isInstantILRSessionActive,
216-
SourceResourceId = item.SourceResourceId,
217-
SourceVMStorageType = recPoint.SourceVMStorageType,
218-
OriginalSAEnabled = recPoint.OriginalStorageAccountOption.HasValue ?
219-
recPoint.OriginalStorageAccountOption.Value : false,
220-
};
95+
if (rpResponse.Properties.GetType() ==
96+
typeof(ServiceClientModel.GenericRecoveryPoint))
97+
{
98+
result = GetPSAzureGenericRecoveryPoint(rpResponse, item);
99+
}
100+
return result;
101+
}
221102

222-
if (vmResult.EncryptionEnabled && recPoint.KeyAndSecret != null)
223-
{
224-
vmResult.KeyAndSecretDetails = new KeyAndSecretDetails()
225-
{
226-
SecretUrl = recPoint.KeyAndSecret.BekDetails.SecretUrl,
227-
KeyUrl = recPoint.KeyAndSecret.KekDetails.KeyUrl,
228-
SecretData = recPoint.KeyAndSecret.BekDetails.SecretData,
229-
KeyBackupData = recPoint.KeyAndSecret.KekDetails.KeyBackupData,
230-
KeyVaultId = recPoint.KeyAndSecret.KekDetails.KeyVaultId,
231-
SecretVaultId = recPoint.KeyAndSecret.BekDetails.SecretVaultId,
232-
};
233-
}
103+
public static RecoveryPointBase GetPSAzureVMRecoveryPoint(
104+
ServiceClientModel.RecoveryPointResource rp, ItemBase item)
105+
{
106+
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id);
107+
string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id);
108+
string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id);
234109

235-
result = vmResult;
236-
}
110+
string containerName = IdUtils.GetNameFromUri(containerUri);
111+
string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri);
237112

238-
if (rpResponse.Properties.GetType() ==
239-
typeof(ServiceClientModel.AzureFileShareRecoveryPoint))
113+
ServiceClientModel.IaasVMRecoveryPoint recoveryPoint =
114+
rp.Properties as ServiceClientModel.IaasVMRecoveryPoint;
115+
116+
DateTime recoveryPointTime = DateTime.MinValue;
117+
if (recoveryPoint.RecoveryPointTime.HasValue)
240118
{
241-
ServiceClientModel.AzureFileShareRecoveryPoint recPoint =
242-
rpResponse.Properties as ServiceClientModel.AzureFileShareRecoveryPoint;
119+
recoveryPointTime = (DateTime)recoveryPoint.RecoveryPointTime;
120+
}
121+
else
122+
{
123+
throw new ArgumentNullException("RecoveryPointTime is null");
124+
}
243125

244-
DateTime recPointTime = DateTime.MinValue;
245-
if (recPoint.RecoveryPointTime.HasValue)
246-
{
247-
recPointTime = (DateTime)recPoint.RecoveryPointTime;
248-
}
249-
else
250-
{
251-
throw new ArgumentNullException("RecoveryPointTime is null");
252-
}
126+
bool isInstantILRSessionActive =
127+
recoveryPoint.IsInstantIlrSessionActive.HasValue ?
128+
(bool)recoveryPoint.IsInstantIlrSessionActive : false;
253129

254-
AzureFileShareRecoveryPoint fileResult = new AzureFileShareRecoveryPoint()
130+
AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint()
131+
{
132+
RecoveryPointId = rp.Name,
133+
BackupManagementType = item.BackupManagementType,
134+
ItemName = protectedItemName,
135+
ContainerName = containerName,
136+
ContainerType = item.ContainerType,
137+
RecoveryPointTime = recoveryPointTime,
138+
RecoveryPointType = recoveryPoint.RecoveryPointType,
139+
Id = rp.Id,
140+
WorkloadType = item.WorkloadType,
141+
RecoveryPointAdditionalInfo = recoveryPoint.RecoveryPointAdditionalInfo,
142+
SourceVMStorageType = recoveryPoint.SourceVMStorageType,
143+
SourceResourceId = item.SourceResourceId,
144+
EncryptionEnabled = recoveryPoint.IsSourceVMEncrypted.HasValue ?
145+
recoveryPoint.IsSourceVMEncrypted.Value : false,
146+
IlrSessionActive = isInstantILRSessionActive,
147+
IsManagedVirtualMachine = recoveryPoint.IsManagedVirtualMachine.HasValue ?
148+
recoveryPoint.IsManagedVirtualMachine.Value : false,
149+
OriginalSAEnabled = recoveryPoint.OriginalStorageAccountOption.HasValue ?
150+
recoveryPoint.OriginalStorageAccountOption.Value : false,
151+
};
152+
153+
if (rpBase.EncryptionEnabled && recoveryPoint.KeyAndSecret != null)
154+
{
155+
rpBase.KeyAndSecretDetails = new KeyAndSecretDetails()
255156
{
256-
RecoveryPointId = rpResponse.Name,
257-
BackupManagementType = item.BackupManagementType,
258-
ItemName = protectedItemName,
259-
ContainerName = containerName,
260-
ContainerType = item.ContainerType,
261-
RecoveryPointTime = recPointTime,
262-
RecoveryPointType = recPoint.RecoveryPointType,
263-
Id = rpResponse.Id,
264-
WorkloadType = item.WorkloadType,
265-
FileShareSnapshotUri = recPoint.FileShareSnapshotUri
157+
SecretUrl = recoveryPoint.KeyAndSecret.BekDetails.SecretUrl,
158+
KeyUrl = recoveryPoint.KeyAndSecret.KekDetails.KeyUrl,
159+
SecretData = recoveryPoint.KeyAndSecret.BekDetails.SecretData,
160+
KeyBackupData = recoveryPoint.KeyAndSecret.KekDetails.KeyBackupData,
161+
KeyVaultId = recoveryPoint.KeyAndSecret.KekDetails.KeyVaultId,
162+
SecretVaultId = recoveryPoint.KeyAndSecret.BekDetails.SecretVaultId,
266163
};
164+
}
165+
return rpBase;
166+
}
267167

268-
result = fileResult;
168+
public static RecoveryPointBase GetPSAzureFileRecoveryPoint(
169+
ServiceClientModel.RecoveryPointResource rp, ItemBase item)
170+
{
171+
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id);
172+
string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id);
173+
string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id);
174+
175+
string containerName = IdUtils.GetNameFromUri(containerUri);
176+
string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri);
177+
ServiceClientModel.AzureFileShareRecoveryPoint recoveryPoint =
178+
rp.Properties as ServiceClientModel.AzureFileShareRecoveryPoint;
179+
180+
DateTime recoveryPointTime = DateTime.MinValue;
181+
if (recoveryPoint.RecoveryPointTime.HasValue)
182+
{
183+
recoveryPointTime = (DateTime)recoveryPoint.RecoveryPointTime;
184+
}
185+
else
186+
{
187+
throw new ArgumentNullException("RecoveryPointTime is null");
269188
}
270189

271-
if (rpResponse.Properties.GetType() ==
272-
typeof(ServiceClientModel.GenericRecoveryPoint))
190+
AzureFileShareRecoveryPoint rpBase = new AzureFileShareRecoveryPoint()
273191
{
274-
ServiceClientModel.GenericRecoveryPoint recPoint =
275-
rpResponse.Properties as ServiceClientModel.GenericRecoveryPoint;
192+
RecoveryPointId = rp.Name,
193+
BackupManagementType = item.BackupManagementType,
194+
ItemName = protectedItemName,
195+
ContainerName = containerName,
196+
ContainerType = item.ContainerType,
197+
RecoveryPointTime = recoveryPointTime,
198+
RecoveryPointType = recoveryPoint.RecoveryPointType,
199+
Id = rp.Id,
200+
WorkloadType = item.WorkloadType,
201+
FileShareSnapshotUri = recoveryPoint.FileShareSnapshotUri,
202+
};
203+
return rpBase;
204+
}
276205

277-
DateTime recPointTime = DateTime.MinValue;
278-
if (recPoint.RecoveryPointTime.HasValue)
279-
{
280-
recPointTime = (DateTime)recPoint.RecoveryPointTime;
281-
}
282-
else
283-
{
284-
throw new ArgumentNullException("RecoveryPointTime is null");
285-
}
206+
public static RecoveryPointBase GetPSAzureGenericRecoveryPoint(
207+
ServiceClientModel.RecoveryPointResource rp, ItemBase item)
208+
{
209+
Dictionary<UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id);
210+
string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id);
211+
string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id);
286212

287-
AzureSqlRecoveryPoint sqlResult = new AzureSqlRecoveryPoint()
288-
{
289-
RecoveryPointId = rpResponse.Name,
290-
BackupManagementType = item.BackupManagementType,
291-
ItemName = protectedItemName,
292-
ContainerName = containerName,
293-
ContainerType = item.ContainerType,
294-
RecoveryPointTime = recPointTime,
295-
RecoveryPointType = recPoint.RecoveryPointType,
296-
Id = rpResponse.Id,
297-
WorkloadType = item.WorkloadType,
298-
RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo,
299-
SourceResourceId = item.SourceResourceId,
300-
FriendlyName = recPoint.FriendlyName
301-
};
213+
string containerName = IdUtils.GetNameFromUri(containerUri);
214+
string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri);
302215

303-
result = sqlResult;
216+
ServiceClientModel.GenericRecoveryPoint recoveryPoint =
217+
rp.Properties as ServiceClientModel.GenericRecoveryPoint;
218+
219+
DateTime recoveryPointTime = DateTime.MinValue;
220+
if (recoveryPoint.RecoveryPointTime.HasValue)
221+
{
222+
recoveryPointTime = (DateTime)recoveryPoint.RecoveryPointTime;
304223
}
305-
return result;
224+
else
225+
{
226+
throw new ArgumentNullException("RecoveryPointTime is null");
227+
}
228+
229+
AzureSqlRecoveryPoint rpBase = new AzureSqlRecoveryPoint()
230+
{
231+
RecoveryPointId = rp.Name,
232+
BackupManagementType = item.BackupManagementType,
233+
ItemName = protectedItemName,
234+
ContainerName = containerUri,
235+
ContainerType = item.ContainerType,
236+
RecoveryPointTime = recoveryPointTime,
237+
RecoveryPointType = recoveryPoint.RecoveryPointType,
238+
Id = rp.Id,
239+
WorkloadType = item.WorkloadType,
240+
RecoveryPointAdditionalInfo = recoveryPoint.RecoveryPointAdditionalInfo,
241+
FriendlyName = recoveryPoint.FriendlyName,
242+
};
243+
return rpBase;
306244
}
307245
}
308246
}

0 commit comments

Comments
 (0)